Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,25 +53,29 @@ def translate_albanian_to_english(text):
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Translation error (attempt {attempt + 1}): {e}")
|
| 55 |
if attempt == 1:
|
| 56 |
-
return f"Përkthimi dështoi: {str(e)}
|
| 57 |
-
return f"Përkthimi dështoi
|
| 58 |
|
| 59 |
@spaces.GPU
|
| 60 |
-
def
|
| 61 |
-
"""
|
| 62 |
-
if
|
| 63 |
-
raise gr.Error("Pipeline nuk u ngarkua.")
|
| 64 |
-
|
| 65 |
-
prompt_clean = prompt.strip()
|
| 66 |
-
if not prompt_clean:
|
| 67 |
return None, seed, "Gabim: Përshkrimi është bosh."
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if randomize_seed:
|
| 70 |
seed = random.randint(0, MAX_SEED)
|
| 71 |
|
| 72 |
width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
|
| 73 |
|
| 74 |
-
prompt_final =
|
| 75 |
|
| 76 |
print(f"🎯 Prompt for generation: {prompt_final}")
|
| 77 |
|
|
@@ -95,7 +99,7 @@ def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", rand
|
|
| 95 |
except Exception as e:
|
| 96 |
if torch.cuda.is_available():
|
| 97 |
torch.cuda.empty_cache()
|
| 98 |
-
|
| 99 |
|
| 100 |
# UI Layout
|
| 101 |
def create_demo():
|
|
@@ -134,26 +138,14 @@ def create_demo():
|
|
| 134 |
output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False)
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
prompt_albanian = gr.Textbox(label="Përshkrimi në Shqip", placeholder="Shkruani përshkrimin në shqip këtu", lines=3)
|
| 137 |
-
prompt_english = gr.Textbox(
|
| 138 |
-
label="Përshkrimi në Anglisht",
|
| 139 |
-
placeholder="Përkthimi do të shfaqet këtu",
|
| 140 |
-
interactive=False,
|
| 141 |
-
lines=3
|
| 142 |
-
)
|
| 143 |
aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="1:1")
|
| 144 |
randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=False)
|
| 145 |
generate_btn = gr.Button("Gjenero")
|
| 146 |
latency = gr.Textbox(label="Koha", interactive=False)
|
| 147 |
|
| 148 |
-
prompt_albanian.change(
|
| 149 |
-
fn=translate_albanian_to_english,
|
| 150 |
-
inputs=[prompt_albanian],
|
| 151 |
-
outputs=[prompt_english]
|
| 152 |
-
)
|
| 153 |
-
|
| 154 |
generate_btn.click(
|
| 155 |
-
fn=
|
| 156 |
-
inputs=[
|
| 157 |
outputs=[output_image, gr.Number(visible=False), latency],
|
| 158 |
show_progress="full"
|
| 159 |
)
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Translation error (attempt {attempt + 1}): {e}")
|
| 55 |
if attempt == 1:
|
| 56 |
+
return f"Përkthimi dështoi: {str(e)}"
|
| 57 |
+
return f"Përkthimi dështoi"
|
| 58 |
|
| 59 |
@spaces.GPU
|
| 60 |
+
def generate_image_from_albanian(prompt_albanian: str, seed: int = 42, aspect_ratio: str = "1:1", randomize_seed: bool = False):
|
| 61 |
+
"""Translate Albanian prompt to English and generate image."""
|
| 62 |
+
if not prompt_albanian.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return None, seed, "Gabim: Përshkrimi është bosh."
|
| 64 |
|
| 65 |
+
# Translate stealthily
|
| 66 |
+
prompt_english = translate_albanian_to_english(prompt_albanian)
|
| 67 |
+
if prompt_english.startswith("Përkthimi dështoi"):
|
| 68 |
+
return None, seed, prompt_english
|
| 69 |
+
|
| 70 |
+
if pipe is None:
|
| 71 |
+
return None, seed, "Pipeline nuk u ngarkua."
|
| 72 |
+
|
| 73 |
if randomize_seed:
|
| 74 |
seed = random.randint(0, MAX_SEED)
|
| 75 |
|
| 76 |
width, height = ASPECT_RATIOS.get(aspect_ratio, (DEFAULT_WIDTH, DEFAULT_HEIGHT))
|
| 77 |
|
| 78 |
+
prompt_final = prompt_english + ", ultra realistic, sharp, 8k resolution"
|
| 79 |
|
| 80 |
print(f"🎯 Prompt for generation: {prompt_final}")
|
| 81 |
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
if torch.cuda.is_available():
|
| 101 |
torch.cuda.empty_cache()
|
| 102 |
+
return None, seed, f"Gabim gjatë gjenerimit: {str(e)}"
|
| 103 |
|
| 104 |
# UI Layout
|
| 105 |
def create_demo():
|
|
|
|
| 138 |
output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False)
|
| 139 |
with gr.Column(scale=1):
|
| 140 |
prompt_albanian = gr.Textbox(label="Përshkrimi në Shqip", placeholder="Shkruani përshkrimin në shqip këtu", lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="1:1")
|
| 142 |
randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=False)
|
| 143 |
generate_btn = gr.Button("Gjenero")
|
| 144 |
latency = gr.Textbox(label="Koha", interactive=False)
|
| 145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
generate_btn.click(
|
| 147 |
+
fn=generate_image_from_albanian,
|
| 148 |
+
inputs=[prompt_albanian, gr.Number(value=42, visible=False), aspect_ratio, randomize_seed],
|
| 149 |
outputs=[output_image, gr.Number(visible=False), latency],
|
| 150 |
show_progress="full"
|
| 151 |
)
|