muhammadrazapathan commited on
Commit
97ba343
Β·
verified Β·
1 Parent(s): 48e551a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
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()