AneesTranslator / app.py
aneesqumar's picture
Create app.py
7150449 verified
Raw
History Blame Contribute Delete
2.59 kB
import os
import gradio as gr
from groq import Groq
# -------------------------------------------------
# Groq API Client (HF Secret)
# -------------------------------------------------
api_key = os.environ.get("GROQ_API_KEY")
if not api_key:
raise RuntimeError(
"GROQ_API_KEY is not set. Add it in Hugging Face Spaces β†’ Settings β†’ Secrets."
)
client = Groq(api_key=api_key)
# -------------------------------------------------
# Translation Function
# -------------------------------------------------
def translate_english_to_urdu(text):
if not text or not text.strip():
return ""
messages = [
{
"role": "system",
"content": (
"You are a professional English to Urdu translator. "
"Translate accurately, clearly, and naturally into Urdu. "
"Do NOT add explanations."
)
},
{
"role": "user",
"content": text
}
]
try:
completion = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=messages
)
return completion.choices[0].message.content
except Exception as e:
return f"⚠️ Error: {str(e)}"
# -------------------------------------------------
# Theme & Custom CSS
# -------------------------------------------------
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="gray",
neutral_hue="gray",
font=["Inter", "sans-serif"]
)
custom_css = """
#container {
max-width: 850px;
margin: auto;
}
textarea {
font-size: 16px;
}
footer {display: none;}
"""
# -------------------------------------------------
# UI
# -------------------------------------------------
with gr.Blocks() as demo:
with gr.Column(elem_id="container"):
gr.Markdown(
"""
# 🌍 English β†’ Urdu Translator
**Powered by Groq LLaMA 3.3**
Fast β€’ Accurate β€’ User-Friendly
"""
)
english_input = gr.Textbox(
label="English Text",
placeholder="Enter English text here...",
lines=6
)
translate_btn = gr.Button("Translate", variant="primary")
urdu_output = gr.Textbox(
label="Urdu Translation",
lines=6,
interactive=False
)
translate_btn.click(
fn=translate_english_to_urdu,
inputs=english_input,
outputs=urdu_output
)
demo.launch(theme=theme, css=custom_css)