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