Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from google import genai | |
| from google.genai.types import GenerateContentConfig | |
| # Initialize GenAI Client | |
| API_KEY = os.getenv("Gemini_API_Key") | |
| client = genai.Client(api_key=API_KEY) | |
| MODEL_ID = "gemini-2.5-flash" | |
| def translate_text(user_text, target_language="English"): | |
| try: | |
| # Instruction | |
| instruction = f""" | |
| You are a professional translator. | |
| Translate the following text into {target_language}. | |
| Keep the meaning accurate and preserve the style. | |
| """ | |
| full_prompt = f"{instruction}\n\nText: {user_text}" | |
| # Generate the response | |
| response = client.models.generate_content( | |
| model=MODEL_ID, | |
| contents=full_prompt, | |
| config=GenerateContentConfig() | |
| ) | |
| translated_text = response.text | |
| return translated_text | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Gradio Interface | |
| app = gr.Interface( | |
| fn=translate_text, | |
| inputs=[ | |
| gr.Textbox(lines=3, label="Enter text to translate"), | |
| gr.Textbox(lines=1, label="Target Language", value="English") | |
| ], | |
| outputs=gr.Textbox(label="Translated Text"), | |
| title="Gemini Translator", | |
| description="Enter text and choose the target language. The AI will translate the text accurately." | |
| ) | |
| if __name__ == "__main__": | |
| app.launch(share=True) | |