AmirAziz1221 commited on
Commit
ddd80b9
·
verified ·
1 Parent(s): fd73bc8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+
5
+ # Load API key securely
6
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
7
+
8
+ SYSTEM_PROMPT = """
9
+ You are a professional English to Urdu translator.
10
+ Rules:
11
+ - Translate accurately and naturally
12
+ - Use proper Urdu grammar
13
+ - Do not add explanations
14
+ - Output ONLY Urdu text
15
+ """
16
+
17
+ def translate_to_urdu(english_text):
18
+ if not english_text.strip():
19
+ return ""
20
+
21
+ response = client.chat.completions.create(
22
+ model="llama3-8b-8192",
23
+ messages=[
24
+ {"role": "system", "content": SYSTEM_PROMPT},
25
+ {"role": "user", "content": english_text}
26
+ ],
27
+ temperature=0.2,
28
+ max_tokens=500
29
+ )
30
+
31
+ return response.choices[0].message.content.strip()
32
+
33
+ # Gradio UI
34
+ with gr.Blocks(theme=gr.themes.Soft(), title="English → Urdu Translator") as demo:
35
+ gr.Markdown(
36
+ """
37
+ # 🌍 English → Urdu Translator Chatbot
38
+ Powered by **Groq LLM**
39
+ """
40
+ )
41
+
42
+ with gr.Row():
43
+ input_text = gr.Textbox(
44
+ label="English Text",
45
+ placeholder="Enter English sentence here...",
46
+ lines=5
47
+ )
48
+ output_text = gr.Textbox(
49
+ label="Urdu Translation",
50
+ lines=5
51
+ )
52
+
53
+ translate_btn = gr.Button("Translate 🇵🇰")
54
+ translate_btn.click(translate_to_urdu, input_text, output_text)
55
+
56
+ demo.launch()