Himel09 commited on
Commit
faffbb6
·
verified ·
1 Parent(s): b196f1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -15,7 +15,8 @@ def translation_to_French(api_key, text):
15
 
16
  try:
17
  genai.configure(api_key=api_key)
18
- model = genai.GenerativeModel("gemini-2.5-flash")
 
19
 
20
  # Translation prompt
21
  translation_prompt = f"Translate the following English text to French:\n\n{text}"
@@ -27,9 +28,11 @@ def translation_to_French(api_key, text):
27
  )
28
  )
29
 
30
- # ✅ Check if candidates exist
31
- if not response_translation or not response_translation.candidates:
32
- return "Error: No translation returned from Gemini API.", None
 
 
33
 
34
  translated = response_translation.text.strip()
35
 
@@ -48,8 +51,9 @@ def translation_to_French(api_key, text):
48
  )
49
  )
50
 
51
- if not response_pronunciation or not response_pronunciation.candidates:
52
- return translated, "Error: No pronunciation returned from Gemini API."
 
53
 
54
  pronunciation = response_pronunciation.text.strip()
55
 
@@ -89,6 +93,7 @@ def process_translation(api_key, english_text):
89
 
90
  translated_text, pronunciation = translation_to_French(api_key, english_text)
91
  if not pronunciation:
 
92
  return translated_text, "", "", None, None
93
 
94
  cleaned_pronunciation = clean_pronunciation(pronunciation)
@@ -99,14 +104,15 @@ def process_translation(api_key, english_text):
99
  f"French Translation: {translated_text}\n"
100
  f"Pronunciation: {cleaned_pronunciation}"
101
  )
102
- result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
103
- with open(result_file.name, "w", encoding="utf-8") as f:
104
- f.write(result_text)
 
105
 
106
- # Generate pronunciation audio
107
- audio_path = generate_audio_from_text(cleaned_pronunciation)
108
 
109
- return "Translation Successful!", translated_text, cleaned_pronunciation, result_file.name, audio_path
110
 
111
  # -----------------------
112
  # 5️⃣ Gradio Interface
@@ -115,12 +121,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
115
  gr.Markdown("# English → French Translator with Pronunciation")
116
  gr.Markdown("Translate English text into **French**, get **pronunciation**, and listen to it!")
117
 
118
- api_key = gr.Textbox(label="🔑 OpenAI API Key", type="password", placeholder="Enter your OpenAI API key")
 
119
  english_text = gr.Textbox(label="📝 Enter English Text", lines=4, placeholder="Type something in English...")
120
 
121
  translate_button = gr.Button("Translate to French 🚀")
122
 
123
- # ✅ Proper Gradio Components
124
  status_label = gr.Label(label="Status")
125
  translation_output = gr.Textbox(label="📘 French Translation")
126
  pronunciation_output = gr.Textbox(label="🔤 Pronunciation (IPA)")
@@ -133,4 +139,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
133
  outputs=[status_label, translation_output, pronunciation_output, file_output, audio_output]
134
  )
135
 
136
- app.launch()
 
15
 
16
  try:
17
  genai.configure(api_key=api_key)
18
+ # ⭐ CORRECTED: Use the valid model name
19
+ model = genai.GenerativeModel("gemini-1.5-flash")
20
 
21
  # Translation prompt
22
  translation_prompt = f"Translate the following English text to French:\n\n{text}"
 
28
  )
29
  )
30
 
31
+ # ✅ Check for a valid response before accessing .text
32
+ if not response_translation.parts:
33
+ # You can inspect the full response to see the finish_reason
34
+ print(f"Translation failed: {response_translation}")
35
+ return f"Error: Translation failed. Reason: {response_translation.candidates[0].finish_reason.name}", None
36
 
37
  translated = response_translation.text.strip()
38
 
 
51
  )
52
  )
53
 
54
+ if not response_pronunciation.parts:
55
+ print(f"Pronunciation generation failed: {response_pronunciation}")
56
+ return translated, "Error: Could not generate pronunciation."
57
 
58
  pronunciation = response_pronunciation.text.strip()
59
 
 
93
 
94
  translated_text, pronunciation = translation_to_French(api_key, english_text)
95
  if not pronunciation:
96
+ # If pronunciation failed, still return the translated text
97
  return translated_text, "", "", None, None
98
 
99
  cleaned_pronunciation = clean_pronunciation(pronunciation)
 
104
  f"French Translation: {translated_text}\n"
105
  f"Pronunciation: {cleaned_pronunciation}"
106
  )
107
+ # IMPROVEMENT: Robust file handling
108
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt", encoding="utf-8") as temp_file:
109
+ temp_file.write(result_text)
110
+ result_file_name = temp_file.name
111
 
112
+ # ⭐ IMPROVEMENT: Generate audio from the actual French text
113
+ audio_path = generate_audio_from_text(translated_text)
114
 
115
+ return "Translation Successful!", translated_text, cleaned_pronunciation, result_file_name, audio_path
116
 
117
  # -----------------------
118
  # 5️⃣ Gradio Interface
 
121
  gr.Markdown("# English → French Translator with Pronunciation")
122
  gr.Markdown("Translate English text into **French**, get **pronunciation**, and listen to it!")
123
 
124
+ # IMPROVEMENT: Corrected the label for clarity
125
+ api_key = gr.Textbox(label="🔑 Gemini API Key", type="password", placeholder="Enter your Gemini API key")
126
  english_text = gr.Textbox(label="📝 Enter English Text", lines=4, placeholder="Type something in English...")
127
 
128
  translate_button = gr.Button("Translate to French 🚀")
129
 
 
130
  status_label = gr.Label(label="Status")
131
  translation_output = gr.Textbox(label="📘 French Translation")
132
  pronunciation_output = gr.Textbox(label="🔤 Pronunciation (IPA)")
 
139
  outputs=[status_label, translation_output, pronunciation_output, file_output, audio_output]
140
  )
141
 
142
+ app.launch()