sreejang commited on
Commit
cd7c2af
·
verified ·
1 Parent(s): ac1debb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from groq import Groq
4
+
5
+ # Get API key from environment (will be set in Hugging Face Space secrets)
6
+ api_key = os.environ.get("GROQ_API_KEY")
7
+ if not api_key:
8
+ raise ValueError("GROQ_API environment variable not set. Please add it to Space Secrets.")
9
+
10
+ client = Groq(api_key=api_key)
11
+
12
+ def translate_english_to_nepali(text):
13
+ if not text or not text.strip():
14
+ return "Please enter some English text to translate."
15
+
16
+ try:
17
+ response = client.chat.completions.create(
18
+ model="llama-3.3-70b-versatile",
19
+ messages=[
20
+ {
21
+ "role": "system",
22
+ "content": "You are a professional translator. Translate the given English text to Nepali (नेपाली) accurately. Provide only the translation without explanations, transliterations, or additional text."
23
+ },
24
+ {
25
+ "role": "user",
26
+ "content": text
27
+ }
28
+ ],
29
+ temperature=0.1, # Lower temperature for more consistent translations
30
+ max_tokens=1024
31
+ )
32
+
33
+ translation = response.choices[0].message.content.strip()
34
+ return translation
35
+
36
+ except Exception as e:
37
+ return f"Error: {str(e)}"
38
+
39
+ # Custom CSS for better appearance
40
+ css = """
41
+ .container {max-width: 800px; margin: 0 auto;}
42
+ .input-text {font-size: 16px;}
43
+ .output-text {font-size: 18px; font-weight: 500;}
44
+ """
45
+
46
+ with gr.Blocks(css=css, title="English to Nepali Translator") as demo:
47
+ gr.Markdown(
48
+ """
49
+ # 🇬🇧 ➡️ 🇳🇵 English to Nepali Translator
50
+ Powered by Groq (Llama 3.1) | High-speed AI Translation
51
+ """
52
+ )
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ input_text = gr.Textbox(
57
+ label="English Text",
58
+ placeholder="Enter English text here...",
59
+ lines=6,
60
+ elem_classes="input-text"
61
+ )
62
+ translate_btn = gr.Button("🔄 Translate", variant="primary", size="lg")
63
+ clear_btn = gr.Button("Clear", size="sm")
64
+
65
+ with gr.Column():
66
+ output_text = gr.Textbox(
67
+ label="Nepali Translation (नेपाली अनुवाद)",
68
+ lines=6,
69
+ elem_classes="output-text",
70
+ interactive=False
71
+ )
72
+
73
+ gr.Markdown(
74
+ """
75
+ ### Tips:
76
+ - Enter natural English sentences for best results
77
+ - The model handles formal and informal Nepali contexts
78
+ - Supports long paragraphs (up to 1024 tokens)
79
+ """
80
+ )
81
+
82
+ # Event handlers
83
+ translate_btn.click(
84
+ fn=translate_english_to_nepali,
85
+ inputs=input_text,
86
+ outputs=output_text
87
+ )
88
+
89
+ clear_btn.click(
90
+ fn=lambda: ("", ""),
91
+ inputs=None,
92
+ outputs=[input_text, output_text]
93
+ )
94
+
95
+ # Allow Enter key to submit
96
+ input_text.submit(
97
+ fn=translate_english_to_nepali,
98
+ inputs=input_text,
99
+ outputs=output_text
100
+ )
101
+
102
+ # Launch configuration
103
+ if __name__ == "__main__":
104
+ demo.launch()