File size: 1,411 Bytes
35500cf
 
 
55b17db
35500cf
 
55b17db
35500cf
55b17db
35500cf
55b17db
35500cf
55b17db
 
 
 
 
 
 
 
 
8298e95
55b17db
35500cf
 
55b17db
 
35500cf
 
55b17db
 
35500cf
 
55b17db
35500cf
 
 
55b17db
 
 
 
35500cf
55b17db
 
 
35500cf
 
 
55b17db
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
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)