HAL1993 commited on
Commit
f6f8a42
·
verified ·
1 Parent(s): ad1c371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -38,16 +38,22 @@ 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):
@@ -124,13 +130,18 @@ def create_demo():
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ërrastësishëm", value=True)
133
- generate_btn = gr.Button("🎨 Gjenero")
 
 
 
 
 
134
  latency = gr.Textbox(label="Koha", interactive=False)
135
 
136
  prompt_albanian.change(
@@ -149,5 +160,6 @@ def create_demo():
149
  return app
150
 
151
  if __name__ == "__main__":
 
152
  app = create_demo()
153
  app.launch(share=True)
 
38
  """Translate Albanian text to English using LibreTranslate public instance."""
39
  if not text.strip():
40
  return ""
41
+ for attempt in range(2): # Retry once
42
+ try:
43
+ response = requests.post(
44
+ "https://libretranslate.de/api/translate",
45
+ json={"q": text, "source": "sq", "target": "en"},
46
+ timeout=5 # 5-second timeout
47
+ )
48
+ response.raise_for_status()
49
+ translated = response.json().get("translatedText", "")
50
+ if translated:
51
+ return translated
52
+ except Exception as e:
53
+ print(f"Translation error (attempt {attempt + 1}): {e}")
54
+ if attempt == 1: # Last attempt
55
+ return f"Gabim në përkthim: {str(e)}. Duke përdorur tekstin origjinal: {text}"
56
+ return text # Fallback to original text
57
 
58
  @spaces.GPU
59
  def generate_image(prompt: str, seed: int = 42, aspect_ratio: str = "16:9", randomize_seed: bool = False):
 
130
 
131
  with gr.Row():
132
  with gr.Column(scale=2):
133
+ output_image = gr.Image(label="Imazhi i Gjeneruar", interactive=False)
134
  with gr.Column(scale=1):
135
+ prompt_albanian = gr.Textbox(label="Përshkrimi në Shqip", placeholder="Shkruani përshkrimin në shqip këtu", lines=3)
136
+ prompt_english = gr.Textbox(
137
+ label="Përshkrimi Anglisht",
138
+ placeholder="Përkthimi doshfaqet këtu",
139
+ interactive=False,
140
+ lines=3
141
+ )
142
+ aspect_ratio = gr.Radio(label="Raporti i Imazhit", choices=list(ASPECT_RATIOS.keys()), value="1:1")
143
+ randomize_seed = gr.Checkbox(label="Përdor numër të rastësishëm", value=False)
144
+ generate_btn = gr.Button("Gjenero")
145
  latency = gr.Textbox(label="Koha", interactive=False)
146
 
147
  prompt_albanian.change(
 
160
  return app
161
 
162
  if __name__ == "__main__":
163
+ print(f"Gradio version: {gr.__version__}")
164
  app = create_demo()
165
  app.launch(share=True)