Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import random
|
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
import time
|
|
|
|
| 7 |
from diffusers import DiffusionPipeline, AutoencoderTiny
|
| 8 |
from custom_pipeline import FluxWithCFGPipeline
|
| 9 |
|
|
@@ -32,14 +33,31 @@ pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtyp
|
|
| 32 |
pipe.to(device)
|
| 33 |
print("✅ Flux pipeline loaded.")
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@spaces.GPU
|
| 36 |
def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
|
|
|
|
| 37 |
if pipe is None:
|
| 38 |
raise gr.Error("Pipeline nuk u ngarkua.")
|
| 39 |
|
| 40 |
prompt_clean = prompt.strip()
|
| 41 |
if not prompt_clean:
|
| 42 |
-
return None, seed, "Gabim:
|
| 43 |
|
| 44 |
if randomize_seed:
|
| 45 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -51,7 +69,7 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
|
|
| 51 |
print(f"🎯 Prompt for generation: {prompt_final}")
|
| 52 |
|
| 53 |
try:
|
| 54 |
-
generator = torch.Generator(device=device).manual_seed(
|
| 55 |
start_time = time.time()
|
| 56 |
with torch.inference_mode():
|
| 57 |
images = pipe(
|
|
@@ -73,38 +91,63 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
|
|
| 73 |
raise gr.Error(f"Gabim gjatë gjenerimit: {e}")
|
| 74 |
|
| 75 |
# --- UI Layout ---
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import spaces
|
| 5 |
import torch
|
| 6 |
import time
|
| 7 |
+
import requests
|
| 8 |
from diffusers import DiffusionPipeline, AutoencoderTiny
|
| 9 |
from custom_pipeline import FluxWithCFGPipeline
|
| 10 |
|
|
|
|
| 33 |
pipe.to(device)
|
| 34 |
print("✅ Flux pipeline loaded.")
|
| 35 |
|
| 36 |
+
@spaces.GPU
|
| 37 |
+
def translate_albanian_to_english(text):
|
| 38 |
+
"""Translate Albanian text to English using LibreTranslate public instance."""
|
| 39 |
+
if not text.strip():
|
| 40 |
+
return ""
|
| 41 |
+
try:
|
| 42 |
+
response = requests.post(
|
| 43 |
+
"https://translate.fediverse.gay/api/v1/translate",
|
| 44 |
+
json={"q": text, "source": "sq", "target": "en"}
|
| 45 |
+
)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
return response.json().get("translatedText", "")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Translation error: {e}")
|
| 50 |
+
return ""
|
| 51 |
+
|
| 52 |
@spaces.GPU
|
| 53 |
def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
|
| 54 |
+
"""Generate an image based on the provided English prompt."""
|
| 55 |
if pipe is None:
|
| 56 |
raise gr.Error("Pipeline nuk u ngarkua.")
|
| 57 |
|
| 58 |
prompt_clean = prompt.strip()
|
| 59 |
if not prompt_clean:
|
| 60 |
+
return None, seed, "Gabim: Përshkrimi është bosh."
|
| 61 |
|
| 62 |
if randomize_seed:
|
| 63 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 69 |
print(f"🎯 Prompt for generation: {prompt_final}")
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 73 |
start_time = time.time()
|
| 74 |
with torch.inference_mode():
|
| 75 |
images = pipe(
|
|
|
|
| 91 |
raise gr.Error(f"Gabim gjatë gjenerimit: {e}")
|
| 92 |
|
| 93 |
# --- UI Layout ---
|
| 94 |
+
def create_demo():
|
| 95 |
+
with gr.Blocks() as app:
|
| 96 |
+
gr.HTML("""
|
| 97 |
+
<style>
|
| 98 |
+
body::before {
|
| 99 |
+
content: "";
|
| 100 |
+
display: block;
|
| 101 |
+
height: 320px;
|
| 102 |
+
background-color: var(--body-background-fill);
|
| 103 |
+
}
|
| 104 |
+
button[aria-label="Fullscreen"], button[aria-label="Fullscreen"]:hover {
|
| 105 |
+
display: none !important;
|
| 106 |
+
visibility: hidden !important;
|
| 107 |
+
opacity: 0 !important;
|
| 108 |
+
pointer-events: none !important;
|
| 109 |
+
}
|
| 110 |
+
button[aria-label="Share"], button[aria-label="Share"]:hover {
|
| 111 |
+
display: none !important;
|
| 112 |
+
}
|
| 113 |
+
button[aria-label="Download"] {
|
| 114 |
+
transform: scale(3);
|
| 115 |
+
transform-origin: top right;
|
| 116 |
+
margin: 0 !important;
|
| 117 |
+
padding: 6px !important;
|
| 118 |
+
}
|
| 119 |
+
</style>
|
| 120 |
+
""")
|
| 121 |
+
|
| 122 |
+
gr.Markdown("")
|
| 123 |
+
gr.Markdown("")
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
with gr.Column(scale=2):
|
| 127 |
+
output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False, show_download_button=True)
|
| 128 |
+
with gr.Column(scale=1):
|
| 129 |
+
prompt_albanian = gr.Textbox(label="Përshkrimi në Shqip", placeholder="Shkruani përshkrimin në shqip këtu...", lines=3)
|
| 130 |
+
prompt_english = gr.Textbox(label="Përshkrimi në Anglisht", placeholder="Përkthimi do të shfaqet këtu...", interactive=False, lines=3)
|
| 131 |
+
aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="16:9")
|
| 132 |
+
randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=True)
|
| 133 |
+
generate_btn = gr.Button("🎨 Gjenero")
|
| 134 |
+
latency = gr.Textbox(label="Koha", interactive=False)
|
| 135 |
+
|
| 136 |
+
prompt_albanian.change(
|
| 137 |
+
fn=translate_albanian_to_english,
|
| 138 |
+
inputs=[prompt_albanian],
|
| 139 |
+
outputs=[prompt_english]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
generate_btn.click(
|
| 143 |
+
fn=generate_image,
|
| 144 |
+
inputs=[prompt_english, gr.Number(value=42, visible=False), aspect_ratio, randomize_seed],
|
| 145 |
+
outputs=[output_image, gr.Number(visible=False), latency],
|
| 146 |
+
show_progress="full"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
return app
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
app = create_demo()
|
| 153 |
+
app.launch(share=True)
|