Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Read Groq key from HF Secrets
|
| 6 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 7 |
+
if not groq_api_key:
|
| 8 |
+
raise ValueError("GROQ_API_KEY not found. Add it in Space → Settings → Secrets.")
|
| 9 |
+
|
| 10 |
+
client = Groq(api_key=groq_api_key)
|
| 11 |
+
|
| 12 |
+
def translate_to_urdu(english_text):
|
| 13 |
+
if not english_text or english_text.strip() == "":
|
| 14 |
+
return "Please enter some text to translate."
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
chat_completion = client.chat.completions.create(
|
| 18 |
+
messages=[
|
| 19 |
+
{
|
| 20 |
+
"role": "system",
|
| 21 |
+
"content": "You are a professional translator. Translate the following English text to Urdu. "
|
| 22 |
+
"Provide only the Urdu translation without any explanations, transliterations, or comments. "
|
| 23 |
+
"Ensure the translation is natural and culturally appropriate for Pakistani Urdu speakers."
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"role": "user",
|
| 27 |
+
"content": english_text
|
| 28 |
+
}
|
| 29 |
+
],
|
| 30 |
+
model="llama-3.3-70b-versatile",
|
| 31 |
+
temperature=0.3,
|
| 32 |
+
max_tokens=1024,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return chat_completion.choices[0].message.content
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error: {str(e)}"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ---------- UI ----------
|
| 42 |
+
custom_css = """
|
| 43 |
+
.container { max-width: 800px; margin: 0 auto; }
|
| 44 |
+
.title { text-align: center; color: #1a73e8; font-size: 2.5em; }
|
| 45 |
+
.description { text-align: center; color: #666; margin-bottom: 2em; }
|
| 46 |
+
.input-box { border: 2px solid #1a73e8 !important; border-radius: 8px !important; }
|
| 47 |
+
.output-box { background:#f8f9fa; border:2px solid #34a853 !important; border-radius:8px !important; }
|
| 48 |
+
.urdu-text { direction: rtl; text-align: right; font-family: 'Noto Nastaliq Urdu','Jameel Noori Nastaleeq',serif; }
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 52 |
+
gr.HTML("""
|
| 53 |
+
<div class="container">
|
| 54 |
+
<h1 class="title">🌐 English to Urdu Translator</h1>
|
| 55 |
+
<p class="description">
|
| 56 |
+
Powered by Groq LLaMA 3.3 | Fast & Accurate
|
| 57 |
+
</p>
|
| 58 |
+
</div>
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column():
|
| 63 |
+
input_text = gr.Textbox(
|
| 64 |
+
label="English Text",
|
| 65 |
+
placeholder="Enter your English text here...",
|
| 66 |
+
lines=4,
|
| 67 |
+
elem_classes=["input-box"]
|
| 68 |
+
)
|
| 69 |
+
translate_btn = gr.Button("Translate to Urdu ➜", variant="primary", size="lg")
|
| 70 |
+
|
| 71 |
+
with gr.Column():
|
| 72 |
+
output_text = gr.Textbox(
|
| 73 |
+
label="Urdu Translation (اردو ترجمہ)",
|
| 74 |
+
placeholder="Translation will appear here...",
|
| 75 |
+
lines=4,
|
| 76 |
+
elem_classes=["output-box", "urdu-text"],
|
| 77 |
+
interactive=False
|
| 78 |
+
)
|
| 79 |
+
copy_btn = gr.Button("📋 Copy Translation")
|
| 80 |
+
|
| 81 |
+
gr.Examples(
|
| 82 |
+
examples=[
|
| 83 |
+
["Hello, how are you today?"],
|
| 84 |
+
["The weather is beautiful today."],
|
| 85 |
+
["Pakistan is a beautiful country with rich culture and history."],
|
| 86 |
+
["Please help me translate this document."],
|
| 87 |
+
],
|
| 88 |
+
inputs=input_text,
|
| 89 |
+
label="Try these examples"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
translate_btn.click(translate_to_urdu, input_text, output_text)
|
| 93 |
+
input_text.submit(translate_to_urdu, input_text, output_text)
|
| 94 |
+
|
| 95 |
+
copy_btn.click(
|
| 96 |
+
fn=None,
|
| 97 |
+
inputs=output_text,
|
| 98 |
+
outputs=None,
|
| 99 |
+
js="""
|
| 100 |
+
async (text) => {
|
| 101 |
+
if (!text) return;
|
| 102 |
+
await navigator.clipboard.writeText(text);
|
| 103 |
+
}
|
| 104 |
+
"""
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
gr.Markdown("""
|
| 108 |
+
<div style="text-align:center;margin-top:20px;color:#666;">
|
| 109 |
+
💡 Tip: Use clear English for best results.
|
| 110 |
+
</div>
|
| 111 |
+
""")
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|