File size: 2,641 Bytes
e6cf507
c746a50
d031b35
e6cf507
d031b35
aa816ac
e6cf507
d031b35
 
 
 
 
aa816ac
d031b35
 
 
 
c16d378
 
d031b35
 
e6cf507
d031b35
a364322
5a14ea9
d031b35
e6cf507
 
 
 
 
 
aa816ac
acb71d1
d031b35
c746a50
d031b35
 
 
88af9f8
acb71d1
 
 
 
 
 
 
 
 
883c61c
 
 
c746a50
d031b35
 
c746a50
 
883c61c
c746a50
d031b35
 
 
c16d378
883c61c
 
a14c8e3
883c61c
6cffbdf
d031b35
acb71d1
88af9f8
d031b35
6cffbdf
d031b35
acb71d1
 
 
d9ffbb3
d031b35
a14c8e3
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
import os
from openai import OpenAI
import gradio as gr

# πŸ” Load API key securely
api_key = os.getenv("GROQ_API_KEY")

# 🧠 Initialize Groq-compatible OpenAI client
client = OpenAI(
    api_key=api_key,
    base_url="https://api.groq.com/openai/v1"
)

# 🌍 Language options
language_options = [
    "Urdu", "Spanish", "French", "German", "Arabic", "Chinese", "Hindi", "Russian", "Japanese", "Turkish",
    "Korean", "Portuguese", "Italian", "Bengali", "Polish"
]

# 🧠 Translation logic
def translate_text(text, target_lang, history):
    if not text.strip() or not target_lang.strip():
        return "⚠️ Please enter both English text and the target language.", history

    prompt = f"Translate the following English text to {target_lang}:\n\n{text}"

    try:
        response = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.5
        )
        translated = response.choices[0].message.content.strip()
        history.append(f"πŸ”€ **English:** {text}\n🌐 **{target_lang}:** {translated}")
        return translated, history
    except Exception as e:
        return f"❌ Error: {str(e)}", history

# 🎨 Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as app:
    gr.Markdown(
        """
        # 🌍 English Language Translator
        **Features**:
        - Translate English text to 15+ languages
        - Clean modern interface
        - View translation history
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            text_input = gr.Textbox(
                label="✏️ English Text",
                placeholder="e.g., The building was damaged during the flood.",
                lines=4
            )
        with gr.Column(scale=1):
            lang_input = gr.Dropdown(
                label="🌐 Select Target Language",
                choices=language_options,
                value="Urdu"
            )

    with gr.Row():
        translate_btn = gr.Button("🌐 Translate")
        clear_btn = gr.Button("🧹 Clear")

    output_box = gr.Textbox(label="πŸ“ Translated Text", lines=6)
    history_display = gr.Markdown(label="πŸ“œ Translation History")

    state = gr.State([])

    translate_btn.click(fn=translate_text, inputs=[text_input, lang_input, state], outputs=[output_box, state])
    clear_btn.click(lambda: ("", "Urdu", "", []), outputs=[text_input, lang_input, output_box, state])

    state.change(fn=lambda hist: "\n\n---\n\n".join(hist) if hist else "No translations yet.", inputs=state, outputs=history_display)

# πŸš€ Launch
app.launch()