Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
|
| 5 |
+
# Load Groq API key securely
|
| 6 |
+
GROQ_API_KEY = os.getenv("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")
|
| 7 |
+
|
| 8 |
+
if not GROQ_API_KEY:
|
| 9 |
+
raise ValueError("β Please set your GROQ_API_KEY in environment variables.")
|
| 10 |
+
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
|
| 13 |
+
def translate_english_to_urdu(text):
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return "β οΈ Please enter English text to translate."
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
completion = client.chat.completions.create(
|
| 19 |
+
model="llama3-70b-8192",
|
| 20 |
+
messages=[
|
| 21 |
+
{"role": "system", "content": "You are a professional translator. Translate English into pure, natural, and grammatically correct Urdu."},
|
| 22 |
+
{"role": "user", "content": text}
|
| 23 |
+
],
|
| 24 |
+
temperature=0.2,
|
| 25 |
+
max_tokens=1024
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
return completion.choices[0].message.content.strip()
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"β Error: {str(e)}"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
with gr.Blocks(title="π English to Urdu Translator (Powered by Groq)") as demo:
|
| 35 |
+
gr.Markdown("""
|
| 36 |
+
# π English β Urdu Translator
|
| 37 |
+
**Powered by Groq AI β’ Deployed on Hugging Face**
|
| 38 |
+
Translate English text into accurate, natural, and fluent Urdu instantly.
|
| 39 |
+
""")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
english_input = gr.Textbox(label="βοΈ Enter English Text", placeholder="Type your English text here...", lines=6)
|
| 43 |
+
urdu_output = gr.Textbox(label="π Urdu Translation", lines=6)
|
| 44 |
+
|
| 45 |
+
translate_btn = gr.Button("π Translate")
|
| 46 |
+
|
| 47 |
+
translate_btn.click(fn=translate_english_to_urdu, inputs=english_input, outputs=urdu_output)
|
| 48 |
+
|
| 49 |
+
gr.Markdown("""
|
| 50 |
+
---
|
| 51 |
+
### β¨ Features
|
| 52 |
+
βοΈ High-accuracy translation
|
| 53 |
+
βοΈ Fast responses using Groq
|
| 54 |
+
βοΈ Clean & modern UI
|
| 55 |
+
βοΈ Ready for Hugging Face deployment
|
| 56 |
+
βοΈ Production-grade code
|
| 57 |
+
|
| 58 |
+
π¨βπ» Built with β€οΈ by Muhammad Raza
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
demo.launch()
|