Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,8 @@ from gtts import gTTS
|
|
| 4 |
import tempfile
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# -----------------------
|
| 10 |
def translation_to_French(api_key, text):
|
| 11 |
if not api_key:
|
| 12 |
return "Error: API Key is missing.", None
|
|
@@ -28,7 +27,7 @@ def translation_to_French(api_key, text):
|
|
| 28 |
)
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 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}")
|
|
@@ -63,9 +62,9 @@ def translation_to_French(api_key, text):
|
|
| 63 |
print(f"Gemini API Error: {e}")
|
| 64 |
return f"Error: {str(e)}", None
|
| 65 |
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
def clean_pronunciation(pronunciation_text):
|
| 70 |
pronunciation_cleaned = re.sub(r"(?i)(the pronunciation.*?is[::]*|\n|\"|\')", "", pronunciation_text).strip()
|
| 71 |
match = re.search(r"\[.*?\]", pronunciation_cleaned)
|
|
@@ -73,18 +72,17 @@ def clean_pronunciation(pronunciation_text):
|
|
| 73 |
pronunciation_cleaned = match.group(0)
|
| 74 |
return pronunciation_cleaned
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
# -----------------------
|
| 79 |
def generate_audio_from_text(text):
|
| 80 |
tts = gTTS(text, lang="fr")
|
| 81 |
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 82 |
tts.save(temp_audio_file.name)
|
| 83 |
return temp_audio_file.name
|
| 84 |
|
| 85 |
-
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
def process_translation(api_key, english_text):
|
| 89 |
if not api_key:
|
| 90 |
return "Missing API Key", "", "", None, None
|
|
@@ -93,7 +91,7 @@ def process_translation(api_key, english_text):
|
|
| 93 |
|
| 94 |
translated_text, pronunciation = translation_to_French(api_key, english_text)
|
| 95 |
if not pronunciation:
|
| 96 |
-
|
| 97 |
return translated_text, "", "", None, None
|
| 98 |
|
| 99 |
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
|
@@ -104,39 +102,98 @@ def process_translation(api_key, english_text):
|
|
| 104 |
f"French Translation: {translated_text}\n"
|
| 105 |
f"Pronunciation: {cleaned_pronunciation}"
|
| 106 |
)
|
| 107 |
-
|
| 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 |
-
|
| 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
|
| 119 |
-
# -----------------------
|
| 120 |
-
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 121 |
-
gr.Markdown("# English → French Translator with Pronunciation")
|
| 122 |
-
gr.Markdown("Translate English text into **French**, get **pronunciation**, and listen to it!")
|
| 123 |
|
| 124 |
-
|
| 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 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
translate_button.click(
|
| 137 |
fn=process_translation,
|
| 138 |
inputs=[api_key, english_text],
|
| 139 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
|
|
|
|
|
|
| 142 |
app.launch()
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
#Translation Function
|
| 8 |
+
|
|
|
|
| 9 |
def translation_to_French(api_key, text):
|
| 10 |
if not api_key:
|
| 11 |
return "Error: API Key is missing.", None
|
|
|
|
| 27 |
)
|
| 28 |
)
|
| 29 |
|
| 30 |
+
|
| 31 |
if not response_translation.parts:
|
| 32 |
# You can inspect the full response to see the finish_reason
|
| 33 |
print(f"Translation failed: {response_translation}")
|
|
|
|
| 62 |
print(f"Gemini API Error: {e}")
|
| 63 |
return f"Error: {str(e)}", None
|
| 64 |
|
| 65 |
+
|
| 66 |
+
#Clean Pronunciation
|
| 67 |
+
|
| 68 |
def clean_pronunciation(pronunciation_text):
|
| 69 |
pronunciation_cleaned = re.sub(r"(?i)(the pronunciation.*?is[::]*|\n|\"|\')", "", pronunciation_text).strip()
|
| 70 |
match = re.search(r"\[.*?\]", pronunciation_cleaned)
|
|
|
|
| 72 |
pronunciation_cleaned = match.group(0)
|
| 73 |
return pronunciation_cleaned
|
| 74 |
|
| 75 |
+
# Generate Audio
|
| 76 |
+
|
|
|
|
| 77 |
def generate_audio_from_text(text):
|
| 78 |
tts = gTTS(text, lang="fr")
|
| 79 |
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 80 |
tts.save(temp_audio_file.name)
|
| 81 |
return temp_audio_file.name
|
| 82 |
|
| 83 |
+
|
| 84 |
+
# Process Translation for Gradio
|
| 85 |
+
|
| 86 |
def process_translation(api_key, english_text):
|
| 87 |
if not api_key:
|
| 88 |
return "Missing API Key", "", "", None, None
|
|
|
|
| 91 |
|
| 92 |
translated_text, pronunciation = translation_to_French(api_key, english_text)
|
| 93 |
if not pronunciation:
|
| 94 |
+
|
| 95 |
return translated_text, "", "", None, None
|
| 96 |
|
| 97 |
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
|
|
|
| 102 |
f"French Translation: {translated_text}\n"
|
| 103 |
f"Pronunciation: {cleaned_pronunciation}"
|
| 104 |
)
|
| 105 |
+
|
| 106 |
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt", encoding="utf-8") as temp_file:
|
| 107 |
temp_file.write(result_text)
|
| 108 |
result_file_name = temp_file.name
|
| 109 |
|
| 110 |
+
|
| 111 |
audio_path = generate_audio_from_text(translated_text)
|
| 112 |
|
| 113 |
return "Translation Successful!", translated_text, cleaned_pronunciation, result_file_name, audio_path
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
# Gradio Interface
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
import gradio as gr
|
| 119 |
|
| 120 |
+
with gr.Blocks(
|
| 121 |
+
theme=gr.themes.Soft(primary_hue="violet", secondary_hue="blue"),
|
| 122 |
+
title="🌍 English → French Translator",
|
| 123 |
+
) as app:
|
| 124 |
+
|
| 125 |
+
# ✨ Header
|
| 126 |
+
gr.Markdown(
|
| 127 |
+
"""
|
| 128 |
+
<div style="text-align:center; padding: 20px 0;">
|
| 129 |
+
<h1 style="color:#5A4FCF;">🌍 English → French Translator</h1>
|
| 130 |
+
<p style="font-size:18px; color:#444;">
|
| 131 |
+
Translate English text into <b>French</b>, get <b>pronunciation</b>, and <b>listen</b> to it instantly!
|
| 132 |
+
</p>
|
| 133 |
+
</div>
|
| 134 |
+
<hr>
|
| 135 |
+
"""
|
| 136 |
+
)
|
| 137 |
|
| 138 |
+
# 🔹 Layout Section
|
| 139 |
+
with gr.Row():
|
| 140 |
+
# Left side - Input
|
| 141 |
+
with gr.Column(scale=1, min_width=400):
|
| 142 |
+
gr.Markdown("### ✏️ Input Section")
|
| 143 |
+
api_key = gr.Textbox(
|
| 144 |
+
label="🔑 Gemini API Key",
|
| 145 |
+
type="password",
|
| 146 |
+
placeholder="Enter your Gemini API key here..."
|
| 147 |
+
)
|
| 148 |
+
english_text = gr.Textbox(
|
| 149 |
+
label="📝 English Text",
|
| 150 |
+
lines=5,
|
| 151 |
+
placeholder="Type something in English to translate..."
|
| 152 |
+
)
|
| 153 |
+
translate_button = gr.Button("🚀 Translate Now", variant="primary")
|
| 154 |
+
|
| 155 |
+
# Right side - Output
|
| 156 |
+
with gr.Column(scale=1, min_width=400):
|
| 157 |
+
gr.Markdown("### 📘 Output Section")
|
| 158 |
+
status_label = gr.Label(label="Status")
|
| 159 |
+
translation_output = gr.Textbox(
|
| 160 |
+
label="🇫🇷 French Translation",
|
| 161 |
+
placeholder="Your translated text will appear here..."
|
| 162 |
+
)
|
| 163 |
+
pronunciation_output = gr.Textbox(
|
| 164 |
+
label="🔤 Pronunciation (IPA)",
|
| 165 |
+
placeholder="Phonetic pronunciation here..."
|
| 166 |
+
)
|
| 167 |
+
audio_output = gr.Audio(
|
| 168 |
+
label="🔊 Listen to Pronunciation",
|
| 169 |
+
type="filepath"
|
| 170 |
+
)
|
| 171 |
+
file_output = gr.File(label="⬇️ Download Translation Result")
|
| 172 |
+
|
| 173 |
+
# 🔄 Button Click Event
|
| 174 |
translate_button.click(
|
| 175 |
fn=process_translation,
|
| 176 |
inputs=[api_key, english_text],
|
| 177 |
+
outputs=[
|
| 178 |
+
status_label,
|
| 179 |
+
translation_output,
|
| 180 |
+
pronunciation_output,
|
| 181 |
+
file_output,
|
| 182 |
+
audio_output,
|
| 183 |
+
],
|
| 184 |
+
show_progress="full"
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# 💬 Footer
|
| 188 |
+
gr.Markdown(
|
| 189 |
+
"""
|
| 190 |
+
<hr>
|
| 191 |
+
<div style="text-align:center; color:gray; font-size:14px; padding:10px;">
|
| 192 |
+
Made with ❤️ using <b>Gemini API</b> + <b>Gradio</b> | Designed by <b>Himel</b>
|
| 193 |
+
</div>
|
| 194 |
+
"""
|
| 195 |
)
|
| 196 |
|
| 197 |
+
app.launch()
|
| 198 |
+
|
| 199 |
app.launch()
|