lyimo commited on
Commit
47725da
·
verified ·
1 Parent(s): c70227e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -28
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import os
3
  from groq import Groq
4
 
5
- # Initialize Groq client
6
  def initialize_groq_client():
7
  api_key = os.getenv("GROQ_API_KEY")
8
  if not api_key:
@@ -10,21 +9,14 @@ def initialize_groq_client():
10
  return Groq(api_key=api_key)
11
 
12
  def translate_to_swahili(text, temperature=0.3):
13
- """
14
- Translate English text to Swahili using Groq's Llama 4
15
- """
16
  if not text.strip():
17
  return "Please enter some text to translate."
18
 
19
  try:
20
  client = initialize_groq_client()
21
-
22
- # Construct the prompt for translation
23
  prompt = f"""You are a professional translator. Translate the following English text to Swahili.
24
  Provide only the Swahili translation, nothing else.
25
-
26
  English text: {text}
27
-
28
  Swahili translation:"""
29
 
30
  completion = client.chat.completions.create(
@@ -48,21 +40,15 @@ Swahili translation:"""
48
  return f"Error during translation: {str(e)}"
49
 
50
  def translate_with_streaming(text, temperature=0.3):
51
- """
52
- Translate with streaming response for better UX
53
- """
54
  if not text.strip():
55
  yield "Please enter some text to translate."
56
  return
57
 
58
  try:
59
  client = initialize_groq_client()
60
-
61
  prompt = f"""You are a professional translator. Translate the following English text to Swahili.
62
  Provide only the Swahili translation, nothing else.
63
-
64
  English text: {text}
65
-
66
  Swahili translation:"""
67
 
68
  completion = client.chat.completions.create(
@@ -89,7 +75,6 @@ Swahili translation:"""
89
  except Exception as e:
90
  yield f"Error during translation: {str(e)}"
91
 
92
- # Create Gradio interface
93
  def create_interface():
94
  with gr.Blocks(title="English to Swahili Translator", theme=gr.themes.Soft()) as iface:
95
  gr.Markdown("# 🌍 English to Swahili Translator")
@@ -125,7 +110,6 @@ def create_interface():
125
  interactive=False
126
  )
127
 
128
- # Examples
129
  gr.Examples(
130
  examples=[
131
  ["Hello, how are you?"],
@@ -138,7 +122,6 @@ def create_interface():
138
  label="Try these examples:"
139
  )
140
 
141
- # Event handlers
142
  translate_btn.click(
143
  fn=translate_with_streaming,
144
  inputs=[input_text, temperature],
@@ -151,7 +134,6 @@ def create_interface():
151
  outputs=[input_text, output_text]
152
  )
153
 
154
- # Allow Enter key to trigger translation
155
  input_text.submit(
156
  fn=translate_with_streaming,
157
  inputs=[input_text, temperature],
@@ -161,23 +143,15 @@ def create_interface():
161
 
162
  return iface
163
 
164
- # For local testing
165
  if __name__ == "__main__":
166
- # For local development, you can set the API key here (NOT recommended for production)
167
- # os.environ["GROQ_API_KEY"] = "your_api_key_here"
168
-
169
  iface = create_interface()
170
  iface.launch(
171
  debug=True,
172
- share=False, # Set to True if you want a public link
173
  server_name="0.0.0.0",
174
  server_port=7860
175
  )
176
 
177
- # For Hugging Face Spaces deployment
178
  def launch_hf_space():
179
  iface = create_interface()
180
- iface.launch()
181
-
182
- # Uncomment the line below for Hugging Face deployment
183
- # launch_hf_space()
 
2
  import os
3
  from groq import Groq
4
 
 
5
  def initialize_groq_client():
6
  api_key = os.getenv("GROQ_API_KEY")
7
  if not api_key:
 
9
  return Groq(api_key=api_key)
10
 
11
  def translate_to_swahili(text, temperature=0.3):
 
 
 
12
  if not text.strip():
13
  return "Please enter some text to translate."
14
 
15
  try:
16
  client = initialize_groq_client()
 
 
17
  prompt = f"""You are a professional translator. Translate the following English text to Swahili.
18
  Provide only the Swahili translation, nothing else.
 
19
  English text: {text}
 
20
  Swahili translation:"""
21
 
22
  completion = client.chat.completions.create(
 
40
  return f"Error during translation: {str(e)}"
41
 
42
  def translate_with_streaming(text, temperature=0.3):
 
 
 
43
  if not text.strip():
44
  yield "Please enter some text to translate."
45
  return
46
 
47
  try:
48
  client = initialize_groq_client()
 
49
  prompt = f"""You are a professional translator. Translate the following English text to Swahili.
50
  Provide only the Swahili translation, nothing else.
 
51
  English text: {text}
 
52
  Swahili translation:"""
53
 
54
  completion = client.chat.completions.create(
 
75
  except Exception as e:
76
  yield f"Error during translation: {str(e)}"
77
 
 
78
  def create_interface():
79
  with gr.Blocks(title="English to Swahili Translator", theme=gr.themes.Soft()) as iface:
80
  gr.Markdown("# 🌍 English to Swahili Translator")
 
110
  interactive=False
111
  )
112
 
 
113
  gr.Examples(
114
  examples=[
115
  ["Hello, how are you?"],
 
122
  label="Try these examples:"
123
  )
124
 
 
125
  translate_btn.click(
126
  fn=translate_with_streaming,
127
  inputs=[input_text, temperature],
 
134
  outputs=[input_text, output_text]
135
  )
136
 
 
137
  input_text.submit(
138
  fn=translate_with_streaming,
139
  inputs=[input_text, temperature],
 
143
 
144
  return iface
145
 
 
146
  if __name__ == "__main__":
 
 
 
147
  iface = create_interface()
148
  iface.launch(
149
  debug=True,
150
+ share=False,
151
  server_name="0.0.0.0",
152
  server_port=7860
153
  )
154
 
 
155
  def launch_hf_space():
156
  iface = create_interface()
157
+ iface.launch()