NCTCMumbai commited on
Commit
58c3d9a
·
verified ·
1 Parent(s): 0aeac8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -13
app.py CHANGED
@@ -17,6 +17,7 @@ from huggingface_hub import InferenceClient
17
  # Bhashini API translation function
18
  api_key = getenv('API_KEY')
19
  user_id = getenv('USER_ID')
 
20
  def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
21
  """Translates text from source language to target language using the Bhashini API."""
22
 
@@ -98,9 +99,10 @@ def add_text(history, text):
98
  return history, gr.Textbox(value="", interactive=False)
99
 
100
  def bot(history, cross_encoder):
 
101
  top_rerank = 25
102
  top_k_rank = 20
103
- query = history[-1][0]
104
 
105
  if not query:
106
  gr.Warning("Please submit a non-empty string as a prompt")
@@ -156,7 +158,10 @@ def bot(history, cross_encoder):
156
  history[-1][1] = character
157
  yield history, prompt_html
158
 
159
- def translate_text(response_text, selected_language):
 
 
 
160
  iso_language_codes = {
161
  "Hindi": "hi",
162
  "Gom": "gom",
@@ -183,8 +188,37 @@ def translate_text(response_text, selected_language):
183
  }
184
 
185
  to_code = iso_language_codes[selected_language]
 
186
  translation = bhashini_translate(response_text, to_code=to_code)
187
  return translation['translated_content']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  # Gradio interface
190
  with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
@@ -228,28 +262,25 @@ with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
228
  )
229
 
230
  prompt_html = gr.HTML()
231
- translated_textbox = gr.Textbox(label="Translated Response")
232
 
233
- # Click event handler for submit button
 
234
  txt_btn.click(
235
  add_text, [chatbot, txt], [chatbot, txt], queue=False
236
  ).then(
237
- bot, [chatbot, cross_encoder], [chatbot, prompt_html]
238
  ).then(
239
- lambda history: history[-1][1], [chatbot], translated_textbox # Extract chatbot output
240
- ).then(
241
- translate_text, [txt, language_dropdown], translated_textbox
242
  )
243
 
244
- # Submit event handler for pressing Enter key
245
  txt.submit(
246
  add_text, [chatbot, txt], [chatbot, txt], queue=False
247
  ).then(
248
- bot, [chatbot, cross_encoder], [chatbot, prompt_html]
249
- ).then(
250
- lambda history: history[-1][1], [chatbot], translated_textbox # Extract chatbot output
251
  ).then(
252
- translate_text, [txt, language_dropdown], translated_textbox
 
 
253
  )
254
  # txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
255
  # bot, [chatbot, cross_encoder], [chatbot, prompt_html]).then(
 
17
  # Bhashini API translation function
18
  api_key = getenv('API_KEY')
19
  user_id = getenv('USER_ID')
20
+ global history
21
  def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
22
  """Translates text from source language to target language using the Bhashini API."""
23
 
 
99
  return history, gr.Textbox(value="", interactive=False)
100
 
101
  def bot(history, cross_encoder):
102
+ global history
103
  top_rerank = 25
104
  top_k_rank = 20
105
+ query = history[-1][0] if history else ''
106
 
107
  if not query:
108
  gr.Warning("Please submit a non-empty string as a prompt")
 
158
  history[-1][1] = character
159
  yield history, prompt_html
160
 
161
+ #def translate_text(response_text, selected_language):
162
+
163
+ def translate_text(selected_language):
164
+ global history
165
  iso_language_codes = {
166
  "Hindi": "hi",
167
  "Gom": "gom",
 
188
  }
189
 
190
  to_code = iso_language_codes[selected_language]
191
+ response_text = history[-1][1] if history else ''
192
  translation = bhashini_translate(response_text, to_code=to_code)
193
  return translation['translated_content']
194
+ # iso_language_codes = {
195
+ # "Hindi": "hi",
196
+ # "Gom": "gom",
197
+ # "Kannada": "kn",
198
+ # "Dogri": "doi",
199
+ # "Bodo": "brx",
200
+ # "Urdu": "ur",
201
+ # "Tamil": "ta",
202
+ # "Kashmiri": "ks",
203
+ # "Assamese": "as",
204
+ # "Bengali": "bn",
205
+ # "Marathi": "mr",
206
+ # "Sindhi": "sd",
207
+ # "Maithili": "mai",
208
+ # "Punjabi": "pa",
209
+ # "Malayalam": "ml",
210
+ # "Manipuri": "mni",
211
+ # "Telugu": "te",
212
+ # "Sanskrit": "sa",
213
+ # "Nepali": "ne",
214
+ # "Santali": "sat",
215
+ # "Gujarati": "gu",
216
+ # "Odia": "or"
217
+ # }
218
+
219
+ # to_code = iso_language_codes[selected_language]
220
+ # translation = bhashini_translate(response_text, to_code=to_code)
221
+ # return translation['translated_content']
222
 
223
  # Gradio interface
224
  with gr.Blocks(theme='NoCrypt/miku') as CHATBOT:
 
262
  )
263
 
264
  prompt_html = gr.HTML()
 
265
 
266
+ translated_textbox = gr.Textbox(label="Translated Response")
267
+
268
  txt_btn.click(
269
  add_text, [chatbot, txt], [chatbot, txt], queue=False
270
  ).then(
271
+ bot, [cross_encoder], [chatbot, prompt_html]
272
  ).then(
273
+ translate_text, [language_dropdown], translated_textbox
 
 
274
  )
275
 
 
276
  txt.submit(
277
  add_text, [chatbot, txt], [chatbot, txt], queue=False
278
  ).then(
279
+ bot, [cross_encoder], [chatbot, prompt_html]
 
 
280
  ).then(
281
+ translate_text, [language_dropdown], translated_textbox
282
+ )
283
+
284
  )
285
  # txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
286
  # bot, [chatbot, cross_encoder], [chatbot, prompt_html]).then(