Translator / app.py
shanzaejaz's picture
Update app.py
7cfe646 verified
import os
import gradio as gr
from groq import Groq
# Initialize Groq client (API key comes from HF Secrets)
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def translate_english_to_urdu(text):
if not text or text.strip() == "":
return ""
response = client.chat.completions.create(
model="llama-3.1-8b-instant", # ✅ Active & updated model
messages=[
{
"role": "system",
"content": "You are a professional English to Urdu translator. Translate accurately and naturally."
},
{
"role": "user",
"content": f"Translate the following English text into Urdu:\n{text}"
}
],
temperature=0.2
)
return response.choices[0].message.content.strip()
# Gradio Interface
demo = gr.Interface(
fn=translate_english_to_urdu,
inputs=gr.Textbox(
lines=5,
placeholder="Enter English text here...",
label="English"
),
outputs=gr.Textbox(
lines=5,
label="Urdu Translation"
),
title="English → Urdu Translator",
description="Fast English to Urdu translation using GROQ LLMs",
)
if __name__ == "__main__":
demo.launch()