Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
# -----------------------
|
| 9 |
+
# Translation Function
|
| 10 |
+
# -----------------------
|
| 11 |
+
def translation_to_French(api_key, text):
|
| 12 |
+
if not api_key:
|
| 13 |
+
return "Error: API Key is missing.", None
|
| 14 |
+
if not text:
|
| 15 |
+
return "Error: Input Text is empty", None
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
genai.configure(api_key=api_key)
|
| 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}"
|
| 23 |
+
response_translation = model.generate_content(
|
| 24 |
+
translation_prompt,
|
| 25 |
+
generation_config=genai.types.GenerationConfig(
|
| 26 |
+
temperature=0.5,
|
| 27 |
+
max_output_tokens=150
|
| 28 |
+
)
|
| 29 |
+
)
|
| 30 |
+
translated = response_translation.text.strip()
|
| 31 |
+
|
| 32 |
+
# Pronunciation prompt
|
| 33 |
+
pronunciation_prompt = (
|
| 34 |
+
"You are a helpful assistant who provides only the phonetic pronunciation "
|
| 35 |
+
"of French text, enclosed in square brackets. "
|
| 36 |
+
"Provide only the phonetic pronunciation for this French text:\n\n"
|
| 37 |
+
f"{translated}"
|
| 38 |
+
)
|
| 39 |
+
response_pronunciation = model.generate_content(
|
| 40 |
+
pronunciation_prompt,
|
| 41 |
+
generation_config=genai.types.GenerationConfig(
|
| 42 |
+
temperature=0.5,
|
| 43 |
+
max_output_tokens=150
|
| 44 |
+
)
|
| 45 |
+
)
|
| 46 |
+
pronunciation = response_pronunciation.text.strip()
|
| 47 |
+
return translated, pronunciation
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Gemini API Error: {e}")
|
| 51 |
+
return f"Error: {str(e)}", None
|
| 52 |
+
|
| 53 |
+
# -----------------------
|
| 54 |
+
# Clean Pronunciation
|
| 55 |
+
# -----------------------
|
| 56 |
+
def clean_pronunciation(pronunciation_text):
|
| 57 |
+
# Remove extra explanation or surrounding sentences
|
| 58 |
+
pronunciation_cleaned = re.sub(r"(?i)(the pronunciation.*?is[::]*|\n|\"|\')", "", pronunciation_text).strip()
|
| 59 |
+
# Keep only text inside [ ] if present
|
| 60 |
+
match = re.search(r"\[.*?\]", pronunciation_cleaned)
|
| 61 |
+
if match:
|
| 62 |
+
pronunciation_cleaned = match.group(0)
|
| 63 |
+
return pronunciation_cleaned
|
| 64 |
+
|
| 65 |
+
# -----------------------
|
| 66 |
+
# Generate Audio
|
| 67 |
+
# -----------------------
|
| 68 |
+
def generate_audio_from_text(text):
|
| 69 |
+
tts = gTTS(text, lang="fr")
|
| 70 |
+
temp_audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 71 |
+
tts.save(temp_audio_file.name)
|
| 72 |
+
return temp_audio_file.name
|
| 73 |
+
|
| 74 |
+
# -----------------------
|
| 75 |
+
# Process Translation for Gradio
|
| 76 |
+
# -----------------------
|
| 77 |
+
def process_translation(api_key, english_text):
|
| 78 |
+
if not api_key:
|
| 79 |
+
return "Missing API Key", "", "", None, None
|
| 80 |
+
if not english_text:
|
| 81 |
+
return "Please enter English text.", "", "", None, None
|
| 82 |
+
|
| 83 |
+
translated_text, pronunciation = translation_to_French(api_key, english_text)
|
| 84 |
+
if not pronunciation:
|
| 85 |
+
return translated_text, "", "", None, None
|
| 86 |
+
|
| 87 |
+
cleaned_pronunciation = clean_pronunciation(pronunciation)
|
| 88 |
+
|
| 89 |
+
# Create result text file
|
| 90 |
+
result_text = (
|
| 91 |
+
f"English Text: {english_text}\n\n"
|
| 92 |
+
f"French Translation: {translated_text}\n"
|
| 93 |
+
f"Pronunciation: {cleaned_pronunciation}"
|
| 94 |
+
)
|
| 95 |
+
result_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
| 96 |
+
with open(result_file.name, "w", encoding="utf-8") as f:
|
| 97 |
+
f.write(result_text)
|
| 98 |
+
|
| 99 |
+
# Generate pronunciation audio
|
| 100 |
+
audio_path = generate_audio_from_text(cleaned_pronunciation)
|
| 101 |
+
|
| 102 |
+
return "Translation Successful!", translated_text, cleaned_pronunciation, result_file.name, audio_path
|
| 103 |
+
|
| 104 |
+
# -----------------------
|
| 105 |
+
# Gradio Interface
|
| 106 |
+
# -----------------------
|
| 107 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 108 |
+
gr.Markdown("# English → French Translator with Pronunciation")
|
| 109 |
+
gr.Markdown("Translate English text into **French**, get **pronunciation**, and listen to it!")
|
| 110 |
+
|
| 111 |
+
api_key = gr.Textbox(label="🔑 OpenAI API Key", type="password", placeholder="Enter your OpenAI API key")
|
| 112 |
+
english_text = gr.Textbox(label="📝 Enter English Text", lines=4, placeholder="Type something in English...")
|
| 113 |
+
|
| 114 |
+
translate_button = gr.Button("Translate to French 🚀")
|
| 115 |
+
|
| 116 |
+
translation_output = gr.Textbox(label="📘 French Translation")
|
| 117 |
+
pronunciation_output = gr.Textbox(label="🔤 Pronunciation (IPA)")
|
| 118 |
+
audio_output = gr.Audio(label="🔊 Listen to Pronunciation", type="filepath")
|
| 119 |
+
file_output = gr.File(label="⬇️ Download Translation Result")
|
| 120 |
+
|
| 121 |
+
translate_button.click(
|
| 122 |
+
fn=process_translation,
|
| 123 |
+
inputs=[api_key, english_text],
|
| 124 |
+
outputs=["label", translation_output, pronunciation_output, file_output, audio_output]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
app.launch()
|