Spaces:
Sleeping
Sleeping
| 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() |