RAMYASRI-39 commited on
Commit
2b039b0
·
verified ·
1 Parent(s): ef5fa92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +758 -188
app.py CHANGED
@@ -8,6 +8,8 @@ from backend.semantic_search import table, retriever
8
  import numpy as np
9
  from time import perf_counter
10
  import requests
 
 
11
 
12
  # Set up logging
13
  logging.basicConfig(level=logging.INFO)
@@ -23,8 +25,8 @@ else:
23
  os.environ["GROQ_API_KEY"] = api_key
24
 
25
  # Bhashini API setup
26
- bhashini_api_key = os.getenv("API_KEY")
27
- bhashini_user_id = os.getenv("USER_ID")
28
 
29
  def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
30
  """Translates text from source language to target language using the Bhashini API."""
@@ -42,6 +44,11 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") ->
42
  "userID": bhashini_user_id,
43
  "ulcaApiKey": bhashini_api_key
44
  }
 
 
 
 
 
45
  payload = {
46
  "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
47
  "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
@@ -56,7 +63,7 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") ->
56
 
57
  print('Initial request successful, processing response...')
58
  response_data = response.json()
59
- print('Full response data:', response_data) # Debug the full response
60
  if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
61
  print('Unexpected response structure:', response_data)
62
  return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
@@ -105,6 +112,12 @@ agent = Agent(
105
  markdown=True
106
  )
107
 
 
 
 
 
 
 
108
  # Response Generation Function
109
  def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
110
  """Generate response using semantic search and LLM"""
@@ -112,7 +125,7 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
112
  top_k_rank = 20
113
 
114
  if not query.strip():
115
- return "Please provide a valid question."
116
 
117
  try:
118
  start_time = perf_counter()
@@ -148,24 +161,27 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
148
  response_text = response.content if hasattr(response, 'content') else str(response)
149
 
150
  logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
151
- return response_text
152
 
153
  except Exception as e:
154
  logger.error(f"Error in response generation: {e}")
155
- return f"Error generating response: {str(e)}"
156
 
157
  def simple_chat_function(message, history, cross_encoder_choice):
158
  """Chat function with semantic search and retriever integration"""
159
  if not message.strip():
160
- return "", history
161
 
162
- # Generate response using the semantic search function
163
- response = retrieve_and_generate_response(message, cross_encoder_choice, history)
164
 
165
  # Add to history
166
  history.append([message, response])
167
 
168
- return "", history
 
 
 
169
 
170
  def translate_text(selected_language, history):
171
  """Translate the last response in history to the selected language."""
@@ -233,26 +249,30 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
233
  label="Select Language for Translation"
234
  )
235
  translated_textbox = gr.Textbox(label="Translated Response")
 
236
 
237
  # Event handlers
238
  def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
239
  if not message.strip():
240
- return "", history, ""
241
 
242
- # Generate response
243
- response = retrieve_and_generate_response(message, cross_encoder_choice, history)
244
  history.append([message, response])
245
 
246
  # Translate response
247
  translated_text = translate_text(selected_language, history)
248
 
249
- return "", history, translated_text
 
 
 
250
 
251
- msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
252
- submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
253
 
254
  clear = gr.Button("Clear Conversation")
255
- clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
256
 
257
  # Example questions
258
  gr.Examples(
@@ -268,43 +288,50 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
268
  )
269
 
270
  if __name__ == "__main__":
271
- demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr# import requests
272
- # import gradio as gr
273
- # from ragatouille import RAGPretrainedModel
 
274
  # import logging
275
- # from pathlib import Path
276
- # from time import perf_counter
277
  # from sentence_transformers import CrossEncoder
278
- # from huggingface_hub import InferenceClient
279
- # from jinja2 import Environment, FileSystemLoader
280
- # import numpy as np
281
- # from os import getenv
282
- # from backend.query_llm import generate_hf, generate_qwen
283
  # from backend.semantic_search import table, retriever
284
- # from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
285
 
 
 
 
 
 
 
 
 
286
 
287
- # # Bhashini API translation function
288
- # api_key = getenv('API_KEY')
289
- # user_id = getenv('USER_ID')
290
 
291
  # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
292
  # """Translates text from source language to target language using the Bhashini API."""
293
-
294
  # if not text.strip():
295
  # print('Input text is empty. Please provide valid text for translation.')
296
- # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
297
  # else:
298
- # print('Input text - ',text)
299
- # print(f'Starting translation process from {from_code} to {to_code}...')
300
  # print(f'Starting translation process from {from_code} to {to_code}...')
301
  # gr.Warning(f'Translating to {to_code}...')
302
 
303
  # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
304
  # headers = {
305
  # "Content-Type": "application/json",
306
- # "userID": user_id,
307
- # "ulcaApiKey": api_key
308
  # }
309
  # payload = {
310
  # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
@@ -315,11 +342,16 @@ if __name__ == "__main__":
315
  # response = requests.post(url, json=payload, headers=headers)
316
 
317
  # if response.status_code != 200:
318
- # print(f'Error in initial request: {response.status_code}')
319
  # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
320
 
321
  # print('Initial request successful, processing response...')
322
  # response_data = response.json()
 
 
 
 
 
323
  # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
324
  # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
325
 
@@ -338,7 +370,7 @@ if __name__ == "__main__":
338
  # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
339
 
340
  # if compute_response.status_code != 200:
341
- # print(f'Error in translation request: {compute_response.status_code}')
342
  # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
343
 
344
  # print('Translation request successful, processing translation...')
@@ -348,157 +380,391 @@ if __name__ == "__main__":
348
  # print(f'Translation successful. Translated content: "{translated_content}"')
349
  # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
350
 
351
-
352
- # # Existing chatbot functions
353
- # VECTOR_COLUMN_NAME = "vector"
354
- # TEXT_COLUMN_NAME = "text"
355
- # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
 
 
 
 
 
 
 
 
 
 
 
356
  # proj_dir = Path(__file__).parent
357
-
358
- # logging.basicConfig(level=logging.INFO)
359
- # logger = logging.getLogger(__name__)
360
- # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
361
  # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
362
 
363
- # template = env.get_template('template.j2')
364
- # template_html = env.get_template('template_html.j2')
365
 
366
- # # def add_text(history, text):
367
- # # history = [] if history is None else history
368
- # # history = history + [(text, None)]
369
- # # return history, gr.Textbox(value="", interactive=False)
370
-
371
- # def bot(history, cross_encoder):
372
 
 
 
 
373
  # top_rerank = 25
374
  # top_k_rank = 20
375
- # query = history[-1][0] if history else ''
376
- # print('\nQuery: ',query )
377
- # print('\nHistory:',history)
378
- # if not query:
379
- # gr.Warning("Please submit a non-empty string as a prompt")
380
- # raise ValueError("Empty string was submitted")
381
-
382
- # logger.warning('Retrieving documents...')
383
-
384
- # if cross_encoder == '(HIGH ACCURATE) ColBERT':
385
- # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
386
- # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
387
- # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
388
- # documents_full = RAG_db.search(query, k=top_k_rank)
389
-
390
- # documents = [item['content'] for item in documents_full]
391
- # prompt = template.render(documents=documents, query=query)
392
- # prompt_html = template_html.render(documents=documents, query=query)
393
-
394
- # generate_fn = generate_hf
395
-
396
- # history[-1][1] = ""
397
- # for character in generate_fn(prompt, history[:-1]):
398
- # history[-1][1] = character
399
- # yield history, prompt_html
400
- # else:
401
- # document_start = perf_counter()
402
 
403
- # query_vec = retriever.encode(query)
404
- # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
405
 
406
- # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
407
- # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
408
-
409
- # query_doc_pair = [[query, doc] for doc in documents]
410
- # if cross_encoder == '(FAST) MiniLM-L6v2':
411
- # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
412
- # elif cross_encoder == '(ACCURATE) BGE reranker':
413
- # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
414
 
415
- # cross_scores = cross_encoder1.predict(query_doc_pair)
416
- # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
 
 
417
 
 
 
 
 
 
418
  # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
 
420
- # document_time = perf_counter() - document_start
 
 
 
 
 
 
 
 
 
 
421
 
422
- # prompt = template.render(documents=documents, query=query)
423
- # prompt_html = template_html.render(documents=documents, query=query)
424
 
425
- # #generate_fn = generate_hf
426
- # generate_fn=generate_qwen
427
- # # Create a new history entry instead of modifying the tuple directly
428
- # new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt
429
- # output=''
430
- # # for character in generate_fn(prompt, history[:-1]):
431
- # # #new_history[-1] = (query, character)
432
- # # output+=character
433
- # output=generate_fn(prompt, history[:-1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434
 
435
- # print('Output:',output)
436
- # new_history[-1] = (prompt, output) #query replaced with prompt
437
- # print('New History',new_history)
438
- # #print('prompt html',prompt_html)# Update the last tuple with new text
439
 
440
- # history_list = list(history[-1])
441
- # history_list[1] = output # Assuming `character` is what you want to assign
442
- # # Update the history with the modified list converted back to a tuple
443
- # history[-1] = tuple(history_list)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
 
445
- # #history[-1][1] = character
446
- # # yield new_history, prompt_html
447
- # yield history, prompt_html
448
- # # new_history,prompt_html
449
- # # history[-1][1] = ""
450
- # # for character in generate_fn(prompt, history[:-1]):
451
- # # history[-1][1] = character
452
- # # yield history, prompt_html
453
 
454
- # #def translate_text(response_text, selected_language):
 
 
 
 
 
 
 
 
 
 
 
 
455
 
456
- # def translate_text(selected_language,history):
 
457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
  # iso_language_codes = {
459
- # "Hindi": "hi",
460
- # "Gom": "gom",
461
- # "Kannada": "kn",
462
- # "Dogri": "doi",
463
- # "Bodo": "brx",
464
- # "Urdu": "ur",
465
- # "Tamil": "ta",
466
- # "Kashmiri": "ks",
467
- # "Assamese": "as",
468
- # "Bengali": "bn",
469
- # "Marathi": "mr",
470
- # "Sindhi": "sd",
471
- # "Maithili": "mai",
472
- # "Punjabi": "pa",
473
- # "Malayalam": "ml",
474
- # "Manipuri": "mni",
475
- # "Telugu": "te",
476
- # "Sanskrit": "sa",
477
- # "Nepali": "ne",
478
- # "Santali": "sat",
479
- # "Gujarati": "gu",
480
- # "Odia": "or"
481
  # }
482
 
483
  # to_code = iso_language_codes[selected_language]
484
- # response_text = history[-1][1] if history else ''
485
- # print('response_text for translation',response_text)
486
  # translation = bhashini_translate(response_text, to_code=to_code)
487
- # return translation['translated_content']
488
-
489
 
490
- # # Gradio interface
491
- # with gr.Blocks(theme='gradio/soft') as CHATBOT:
492
- # history_state = gr.State([])
493
  # with gr.Row():
494
  # with gr.Column(scale=10):
495
- # gr.HTML(value="""<div style="color: #FF4500;"><h1>Welcome! I am your friend!</h1>Ask me !I will help you<h1><span style="color: #008000">I AM A CHATBOT FOR 9 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
496
  # gr.HTML(value=f"""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students</p>""")
497
  # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:[email protected]" style="color: #00008B; font-style: italic;">[email protected]</a>.</p>""")
498
-
499
  # with gr.Column(scale=3):
500
- # gr.Image(value='logo.png', height=200, width=200)
 
 
 
501
 
 
502
  # chatbot = gr.Chatbot(
503
  # [],
504
  # elem_id="chatbot",
@@ -510,57 +776,361 @@ if __name__ == "__main__":
510
  # )
511
 
512
  # with gr.Row():
513
- # txt = gr.Textbox(
514
  # scale=3,
515
  # show_label=False,
516
  # placeholder="Enter text and press enter",
517
  # container=False,
518
  # )
519
- # txt_btn = gr.Button(value="Submit text", scale=1)
520
-
521
- # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'], value='(ACCURATE) BGE reranker', label="Embeddings", info="Only First query to Colbert may take little time)")
 
 
 
 
 
 
522
  # language_dropdown = gr.Dropdown(
523
  # choices=[
524
  # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
525
  # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
526
  # "Gujarati", "Odia"
527
  # ],
528
- # value="Hindi", # default to Hindi
529
  # label="Select Language for Translation"
530
  # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
 
532
- # prompt_html = gr.HTML()
 
 
 
 
 
 
 
533
 
534
- # translated_textbox = gr.Textbox(label="Translated Response")
535
- # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
536
- # print('History state',history_state)
537
- # history = history_state
538
- # history.append((txt, ""))
539
- # #history_state.value=(history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
 
541
- # # Call bot function
542
- # # bot_output = list(bot(history, cross_encoder))
543
- # bot_output = next(bot(history, cross_encoder))
544
- # print('bot_output',bot_output)
545
- # #history, prompt_html = bot_output[-1]
546
- # history, prompt_html = bot_output
547
- # print('History',history)
548
- # # Update the history state
549
- # history_state[:] = history
550
 
551
- # # Translate text
552
- # translated_text = translate_text(language_dropdown, history)
553
- # return history, prompt_html, translated_text
554
 
555
- # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
556
- # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
557
 
558
- # examples = ['WHAT IS DIFFERENCES BETWEEN HOMOGENOUS AND HETEROGENOUS MIXTURE?','WHAT IS COVALENT BOND?',
559
- # 'EXPLAIN GOLGI APPARATUS']
560
 
561
- # gr.Examples(examples, txt)
562
 
563
 
564
- # # Launch the Gradio application
565
- # CHATBOT.launch(share=True,debug=True)
566
 
 
8
  import numpy as np
9
  from time import perf_counter
10
  import requests
11
+ from jinja2 import Environment, FileSystemLoader
12
+ from pathlib import Path
13
 
14
  # Set up logging
15
  logging.basicConfig(level=logging.INFO)
 
25
  os.environ["GROQ_API_KEY"] = api_key
26
 
27
  # Bhashini API setup
28
+ bhashini_api_key = os.getenv("API_KEY", "").strip()
29
+ bhashini_user_id = os.getenv("USER_ID", "").strip()
30
 
31
  def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
32
  """Translates text from source language to target language using the Bhashini API."""
 
44
  "userID": bhashini_user_id,
45
  "ulcaApiKey": bhashini_api_key
46
  }
47
+ for key, value in headers.items():
48
+ if not isinstance(value, str) or '\n' in value or '\r' in value:
49
+ print(f"Invalid header value for {key}: {value}")
50
+ return {"status_code": 400, "message": f"Invalid header value for {key}", "translated_content": None}
51
+
52
  payload = {
53
  "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
54
  "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
 
63
 
64
  print('Initial request successful, processing response...')
65
  response_data = response.json()
66
+ print('Full response data:', response_data)
67
  if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
68
  print('Unexpected response structure:', response_data)
69
  return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
 
112
  markdown=True
113
  )
114
 
115
+ # Set up Jinja2 environment
116
+ proj_dir = Path(__file__).parent
117
+ env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
118
+ template = env.get_template('template.j2') # For document context
119
+ template_html = env.get_template('template_html.j2') # For HTML output
120
+
121
  # Response Generation Function
122
  def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
123
  """Generate response using semantic search and LLM"""
 
125
  top_k_rank = 20
126
 
127
  if not query.strip():
128
+ return "Please provide a valid question.", []
129
 
130
  try:
131
  start_time = perf_counter()
 
161
  response_text = response.content if hasattr(response, 'content') else str(response)
162
 
163
  logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
164
+ return response_text, documents # Return documents for template
165
 
166
  except Exception as e:
167
  logger.error(f"Error in response generation: {e}")
168
+ return f"Error generating response: {str(e)}", []
169
 
170
  def simple_chat_function(message, history, cross_encoder_choice):
171
  """Chat function with semantic search and retriever integration"""
172
  if not message.strip():
173
+ return "", history, ""
174
 
175
+ # Generate response and get documents
176
+ response, documents = retrieve_and_generate_response(message, cross_encoder_choice, history)
177
 
178
  # Add to history
179
  history.append([message, response])
180
 
181
+ # Render template with documents and query
182
+ prompt_html = template_html.render(documents=documents, query=message)
183
+
184
+ return "", history, prompt_html
185
 
186
  def translate_text(selected_language, history):
187
  """Translate the last response in history to the selected language."""
 
249
  label="Select Language for Translation"
250
  )
251
  translated_textbox = gr.Textbox(label="Translated Response")
252
+ prompt_html = gr.HTML() # Add HTML component for the template
253
 
254
  # Event handlers
255
  def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
256
  if not message.strip():
257
+ return "", history, "", ""
258
 
259
+ # Generate response and get documents
260
+ response, documents = retrieve_and_generate_response(message, cross_encoder_choice, history)
261
  history.append([message, response])
262
 
263
  # Translate response
264
  translated_text = translate_text(selected_language, history)
265
 
266
+ # Render template with documents and query
267
+ prompt_html_content = template_html.render(documents=documents, query=message)
268
+
269
+ return "", history, translated_text, prompt_html_content
270
 
271
+ msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox, prompt_html])
272
+ submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox, prompt_html])
273
 
274
  clear = gr.Button("Clear Conversation")
275
+ clear.click(lambda: ([], "", "", ""), outputs=[chatbot, msg, translated_textbox, prompt_html])
276
 
277
  # Example questions
278
  gr.Examples(
 
288
  )
289
 
290
  if __name__ == "__main__":
291
+ demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
292
+ # from phi.agent import Agent
293
+ # from phi.model.groq import Groq
294
+ # import os
295
  # import logging
 
 
296
  # from sentence_transformers import CrossEncoder
 
 
 
 
 
297
  # from backend.semantic_search import table, retriever
298
+ # import numpy as np
299
+ # from time import perf_counter
300
+ # import requests
301
+ # from jinja2 import Environment, FileSystemLoader
302
+
303
+ # # Set up logging
304
+ # logging.basicConfig(level=logging.INFO)
305
+ # logger = logging.getLogger(__name__)
306
 
307
+ # # API Key setup
308
+ # api_key = os.getenv("GROQ_API_KEY")
309
+ # if not api_key:
310
+ # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
311
+ # logger.error("GROQ_API_KEY not found.")
312
+ # api_key = "" # Fallback to empty string, but this will fail without a key
313
+ # else:
314
+ # os.environ["GROQ_API_KEY"] = api_key
315
 
316
+ # # Bhashini API setup
317
+ # bhashini_api_key = os.getenv("API_KEY")
318
+ # bhashini_user_id = os.getenv("USER_ID")
319
 
320
  # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
321
  # """Translates text from source language to target language using the Bhashini API."""
 
322
  # if not text.strip():
323
  # print('Input text is empty. Please provide valid text for translation.')
324
+ # return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
325
  # else:
326
+ # print('Input text - ', text)
 
327
  # print(f'Starting translation process from {from_code} to {to_code}...')
328
  # gr.Warning(f'Translating to {to_code}...')
329
 
330
  # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
331
  # headers = {
332
  # "Content-Type": "application/json",
333
+ # "userID": bhashini_user_id,
334
+ # "ulcaApiKey": bhashini_api_key
335
  # }
336
  # payload = {
337
  # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
 
342
  # response = requests.post(url, json=payload, headers=headers)
343
 
344
  # if response.status_code != 200:
345
+ # print(f'Error in initial request: {response.status_code}, Response: {response.text}')
346
  # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
347
 
348
  # print('Initial request successful, processing response...')
349
  # response_data = response.json()
350
+ # print('Full response data:', response_data) # Debug the full response
351
+ # if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
352
+ # print('Unexpected response structure:', response_data)
353
+ # return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
354
+
355
  # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
356
  # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
357
 
 
370
  # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
371
 
372
  # if compute_response.status_code != 200:
373
+ # print(f'Error in translation request: {compute_response.status_code}, Response: {compute_response.text}')
374
  # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
375
 
376
  # print('Translation request successful, processing translation...')
 
380
  # print(f'Translation successful. Translated content: "{translated_content}"')
381
  # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
382
 
383
+ # # Initialize PhiData Agent
384
+ # agent = Agent(
385
+ # name="Science Education Assistant",
386
+ # role="You are a helpful science tutor for 10th-grade students",
387
+ # instructions=[
388
+ # "You are an expert science teacher specializing in 10th-grade curriculum.",
389
+ # "Provide clear, accurate, and age-appropriate explanations.",
390
+ # "Use simple language and examples that students can understand.",
391
+ # "Focus on concepts from physics, chemistry, and biology.",
392
+ # "Structure responses with headings and bullet points when helpful.",
393
+ # "Encourage learning and curiosity."
394
+ # ],
395
+ # model=Groq(id="llama3-70b-8192", api_key=api_key),
396
+ # markdown=True
397
+ # )
398
+ # # Set up Jinja2 environment
399
  # proj_dir = Path(__file__).parent
 
 
 
 
400
  # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
401
 
 
 
402
 
403
+ # template_html = env.get_template('template_html.j2')
 
 
 
 
 
404
 
405
+ # # Response Generation Function
406
+ # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
407
+ # """Generate response using semantic search and LLM"""
408
  # top_rerank = 25
409
  # top_k_rank = 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
 
411
+ # if not query.strip():
412
+ # return "Please provide a valid question."
413
 
414
+ # try:
415
+ # start_time = perf_counter()
 
 
 
 
 
 
416
 
417
+ # # Encode query and search documents
418
+ # query_vec = retriever.encode(query)
419
+ # documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
420
+ # documents = [doc["text"] for doc in documents]
421
 
422
+ # # Re-rank documents using cross-encoder
423
+ # cross_encoder_model = CrossEncoder('BAAI/bge-reranker-base') if cross_encoder_choice == '(ACCURATE) BGE reranker' else CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
424
+ # query_doc_pair = [[query, doc] for doc in documents]
425
+ # cross_scores = cross_encoder_model.predict(query_doc_pair)
426
+ # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
427
  # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
428
+
429
+ # # Create context from top documents
430
+ # context = "\n\n".join(documents[:10]) if documents else ""
431
+ # context = f"Context information from educational materials:\n{context}\n\n"
432
+
433
+ # # Add conversation history for context
434
+ # history_context = ""
435
+ # if history and len(history) > 0:
436
+ # for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
437
+ # if user_msg and bot_msg:
438
+ # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
439
+
440
+ # # Create full prompt
441
+ # full_prompt = f"{history_context}{context}Question: {query}\n\nPlease answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics."
442
+
443
+ # # Generate response
444
+ # response = agent.run(full_prompt)
445
+ # response_text = response.content if hasattr(response, 'content') else str(response)
446
+
447
+ # logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
448
+ # return response_text
449
 
450
+ # except Exception as e:
451
+ # logger.error(f"Error in response generation: {e}")
452
+ # return f"Error generating response: {str(e)}"
453
+
454
+ # def simple_chat_function(message, history, cross_encoder_choice):
455
+ # """Chat function with semantic search and retriever integration"""
456
+ # if not message.strip():
457
+ # return "", history
458
+
459
+ # # Generate response using the semantic search function
460
+ # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
461
 
462
+ # # Add to history
463
+ # history.append([message, response])
464
 
465
+ # return "", history
466
+
467
+ # def translate_text(selected_language, history):
468
+ # """Translate the last response in history to the selected language."""
469
+ # iso_language_codes = {
470
+ # "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
471
+ # "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
472
+ # "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
473
+ # "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
474
+ # }
475
+
476
+ # to_code = iso_language_codes[selected_language]
477
+ # response_text = history[-1][1] if history and history[-1][1] else ''
478
+ # print('response_text for translation', response_text)
479
+ # translation = bhashini_translate(response_text, to_code=to_code)
480
+ # return translation.get('translated_content', 'Translation failed.')
481
+
482
+ # # Gradio Interface with layout template
483
+ # with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
484
+ # # Header section
485
+ # with gr.Row():
486
+ # with gr.Column(scale=10):
487
+ # gr.HTML(value="""<div style="color: #FF4500;"><h1>Welcome! I am your friend!</h1>Ask me !I will help you<h1><span style="color: #008000">I AM A CHATBOT FOR 10TH SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
488
+ # gr.HTML(value=f"""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students</p>""")
489
+ # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:[email protected]" style="color: #00008B; font-style: italic;">[email protected]</a>.</p>""")
490
+ # with gr.Column(scale=3):
491
+ # try:
492
+ # gr.Image(value='logo.png', height=200, width=200)
493
+ # except:
494
+ # gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
495
+
496
+ # # Chat and input components
497
+ # chatbot = gr.Chatbot(
498
+ # [],
499
+ # elem_id="chatbot",
500
+ # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
501
+ # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
502
+ # bubble_full_width=False,
503
+ # show_copy_button=True,
504
+ # show_share_button=True,
505
+ # )
506
+
507
+ # with gr.Row():
508
+ # msg = gr.Textbox(
509
+ # scale=3,
510
+ # show_label=False,
511
+ # placeholder="Enter text and press enter",
512
+ # container=False,
513
+ # )
514
+ # submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
515
+
516
+ # # Additional controls
517
+ # cross_encoder = gr.Radio(
518
+ # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
519
+ # value='(ACCURATE) BGE reranker',
520
+ # label="Embeddings Model",
521
+ # info="Select the model for document ranking"
522
+ # )
523
+ # language_dropdown = gr.Dropdown(
524
+ # choices=[
525
+ # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
526
+ # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
527
+ # "Gujarati", "Odia"
528
+ # ],
529
+ # value="Hindi",
530
+ # label="Select Language for Translation"
531
+ # )
532
+ # translated_textbox = gr.Textbox(label="Translated Response")
533
+
534
+ # # Event handlers
535
+ # def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
536
+ # if not message.strip():
537
+ # return "", history, ""
538
+
539
+ # # Generate response
540
+ # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
541
+ # history.append([message, response])
542
 
543
+ # # Translate response
544
+ # translated_text = translate_text(selected_language, history)
 
 
545
 
546
+ # return "", history, translated_text
547
+
548
+ # msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
549
+ # submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
550
+
551
+ # clear = gr.Button("Clear Conversation")
552
+ # clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
553
+
554
+ # # Example questions
555
+ # gr.Examples(
556
+ # examples=[
557
+ # 'What is the difference between metals and non-metals?',
558
+ # 'What is an ionic bond?',
559
+ # 'Explain asexual reproduction',
560
+ # 'What is photosynthesis?',
561
+ # 'Explain Newton\'s laws of motion'
562
+ # ],
563
+ # inputs=msg,
564
+ # label="Try these example questions:"
565
+ # )
566
+
567
+ # if __name__ == "__main__":
568
+ # demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
569
+ # import gradio as gr
570
+ # from phi.agent import Agent
571
+ # from phi.model.groq import Groq
572
+ # import os
573
+ # import logging
574
+ # from sentence_transformers import CrossEncoder
575
+ # from backend.semantic_search import table, retriever
576
+ # import numpy as np
577
+ # from time import perf_counter
578
+ # import requests
579
+
580
+ # # Set up logging
581
+ # logging.basicConfig(level=logging.INFO)
582
+ # logger = logging.getLogger(__name__)
583
+
584
+ # # API Key setup
585
+ # api_key = os.getenv("GROQ_API_KEY")
586
+ # if not api_key:
587
+ # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
588
+ # logger.error("GROQ_API_KEY not found.")
589
+ # api_key = "" # Fallback to empty string, but this will fail without a key
590
+ # else:
591
+ # os.environ["GROQ_API_KEY"] = api_key
592
+
593
+ # # Bhashini API setup
594
+ # bhashini_api_key = os.getenv("API_KEY")
595
+ # bhashini_user_id = os.getenv("USER_ID")
596
+
597
+ # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
598
+ # """Translates text from source language to target language using the Bhashini API."""
599
+ # if not text.strip():
600
+ # print('Input text is empty. Please provide valid text for translation.')
601
+ # return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
602
+ # else:
603
+ # print('Input text - ', text)
604
+ # print(f'Starting translation process from {from_code} to {to_code}...')
605
+ # gr.Warning(f'Translating to {to_code}...')
606
+
607
+ # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
608
+ # headers = {
609
+ # "Content-Type": "application/json",
610
+ # "userID": bhashini_user_id,
611
+ # "ulcaApiKey": bhashini_api_key
612
+ # }
613
+ # payload = {
614
+ # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
615
+ # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
616
+ # }
617
+
618
+ # print('Sending initial request to get the pipeline...')
619
+ # response = requests.post(url, json=payload, headers=headers)
620
+
621
+ # if response.status_code != 200:
622
+ # print(f'Error in initial request: {response.status_code}, Response: {response.text}')
623
+ # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
624
 
625
+ # print('Initial request successful, processing response...')
626
+ # response_data = response.json()
627
+ # print('Full response data:', response_data) # Debug the full response
628
+ # if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
629
+ # print('Unexpected response structure:', response_data)
630
+ # return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
 
 
631
 
632
+ # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
633
+ # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
634
+
635
+ # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
636
+
637
+ # headers2 = {
638
+ # "Content-Type": "application/json",
639
+ # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
640
+ # }
641
+ # compute_payload = {
642
+ # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
643
+ # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
644
+ # }
645
 
646
+ # print(f'Sending translation request with text: "{text}"')
647
+ # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
648
 
649
+ # if compute_response.status_code != 200:
650
+ # print(f'Error in translation request: {compute_response.status_code}, Response: {compute_response.text}')
651
+ # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
652
+
653
+ # print('Translation request successful, processing translation...')
654
+ # compute_response_data = compute_response.json()
655
+ # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
656
+
657
+ # print(f'Translation successful. Translated content: "{translated_content}"')
658
+ # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
659
+
660
+ # # Initialize PhiData Agent
661
+ # agent = Agent(
662
+ # name="Science Education Assistant",
663
+ # role="You are a helpful science tutor for 10th-grade students",
664
+ # instructions=[
665
+ # "You are an expert science teacher specializing in 10th-grade curriculum.",
666
+ # "Provide clear, accurate, and age-appropriate explanations.",
667
+ # "Use simple language and examples that students can understand.",
668
+ # "Focus on concepts from physics, chemistry, and biology.",
669
+ # "Structure responses with headings and bullet points when helpful.",
670
+ # "Encourage learning and curiosity."
671
+ # ],
672
+ # model=Groq(id="llama3-70b-8192", api_key=api_key),
673
+ # markdown=True
674
+ # )
675
+
676
+ # # Response Generation Function
677
+ # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
678
+ # """Generate response using semantic search and LLM"""
679
+ # top_rerank = 25
680
+ # top_k_rank = 20
681
+
682
+ # if not query.strip():
683
+ # return "Please provide a valid question."
684
+
685
+ # try:
686
+ # start_time = perf_counter()
687
+
688
+ # # Encode query and search documents
689
+ # query_vec = retriever.encode(query)
690
+ # documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
691
+ # documents = [doc["text"] for doc in documents]
692
+
693
+ # # Re-rank documents using cross-encoder
694
+ # cross_encoder_model = CrossEncoder('BAAI/bge-reranker-base') if cross_encoder_choice == '(ACCURATE) BGE reranker' else CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
695
+ # query_doc_pair = [[query, doc] for doc in documents]
696
+ # cross_scores = cross_encoder_model.predict(query_doc_pair)
697
+ # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
698
+ # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
699
+
700
+ # # Create context from top documents
701
+ # context = "\n\n".join(documents[:10]) if documents else ""
702
+ # context = f"Context information from educational materials:\n{context}\n\n"
703
+
704
+ # # Add conversation history for context
705
+ # history_context = ""
706
+ # if history and len(history) > 0:
707
+ # for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
708
+ # if user_msg and bot_msg:
709
+ # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
710
+
711
+ # # Create full prompt
712
+ # full_prompt = f"{history_context}{context}Question: {query}\n\nPlease answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics."
713
+
714
+ # # Generate response
715
+ # response = agent.run(full_prompt)
716
+ # response_text = response.content if hasattr(response, 'content') else str(response)
717
+
718
+ # logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
719
+ # return response_text
720
+
721
+ # except Exception as e:
722
+ # logger.error(f"Error in response generation: {e}")
723
+ # return f"Error generating response: {str(e)}"
724
+
725
+ # def simple_chat_function(message, history, cross_encoder_choice):
726
+ # """Chat function with semantic search and retriever integration"""
727
+ # if not message.strip():
728
+ # return "", history
729
+
730
+ # # Generate response using the semantic search function
731
+ # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
732
+
733
+ # # Add to history
734
+ # history.append([message, response])
735
+
736
+ # return "", history
737
+
738
+ # def translate_text(selected_language, history):
739
+ # """Translate the last response in history to the selected language."""
740
  # iso_language_codes = {
741
+ # "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
742
+ # "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
743
+ # "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
744
+ # "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
  # }
746
 
747
  # to_code = iso_language_codes[selected_language]
748
+ # response_text = history[-1][1] if history and history[-1][1] else ''
749
+ # print('response_text for translation', response_text)
750
  # translation = bhashini_translate(response_text, to_code=to_code)
751
+ # return translation.get('translated_content', 'Translation failed.')
 
752
 
753
+ # # Gradio Interface with layout template
754
+ # with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
755
+ # # Header section
756
  # with gr.Row():
757
  # with gr.Column(scale=10):
758
+ # gr.HTML(value="""<div style="color: #FF4500;"><h1>Welcome! I am your friend!</h1>Ask me !I will help you<h1><span style="color: #008000">I AM A CHATBOT FOR 10TH SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
759
  # gr.HTML(value=f"""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students</p>""")
760
  # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:[email protected]" style="color: #00008B; font-style: italic;">[email protected]</a>.</p>""")
 
761
  # with gr.Column(scale=3):
762
+ # try:
763
+ # gr.Image(value='logo.png', height=200, width=200)
764
+ # except:
765
+ # gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
766
 
767
+ # # Chat and input components
768
  # chatbot = gr.Chatbot(
769
  # [],
770
  # elem_id="chatbot",
 
776
  # )
777
 
778
  # with gr.Row():
779
+ # msg = gr.Textbox(
780
  # scale=3,
781
  # show_label=False,
782
  # placeholder="Enter text and press enter",
783
  # container=False,
784
  # )
785
+ # submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
786
+
787
+ # # Additional controls
788
+ # cross_encoder = gr.Radio(
789
+ # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
790
+ # value='(ACCURATE) BGE reranker',
791
+ # label="Embeddings Model",
792
+ # info="Select the model for document ranking"
793
+ # )
794
  # language_dropdown = gr.Dropdown(
795
  # choices=[
796
  # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
797
  # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
798
  # "Gujarati", "Odia"
799
  # ],
800
+ # value="Hindi",
801
  # label="Select Language for Translation"
802
  # )
803
+ # translated_textbox = gr.Textbox(label="Translated Response")
804
+
805
+ # # Event handlers
806
+ # def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
807
+ # if not message.strip():
808
+ # return "", history, ""
809
+
810
+ # # Generate response
811
+ # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
812
+ # history.append([message, response])
813
+
814
+ # # Translate response
815
+ # translated_text = translate_text(selected_language, history)
816
+
817
+ # return "", history, translated_text
818
+
819
+ # msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
820
+ # submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
821
+
822
+ # clear = gr.Button("Clear Conversation")
823
+ # clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
824
+
825
+ # # Example questions
826
+ # gr.Examples(
827
+ # examples=[
828
+ # 'What is the difference between metals and non-metals?',
829
+ # 'What is an ionic bond?',
830
+ # 'Explain asexual reproduction',
831
+ # 'What is photosynthesis?',
832
+ # 'Explain Newton\'s laws of motion'
833
+ # ],
834
+ # inputs=msg,
835
+ # label="Try these example questions:"
836
+ # )
837
+
838
+ # if __name__ == "__main__":
839
+ # demo.launch(server_name="0.0.0.0", server_port=7860)
840
+
841
+ # 1f# import gradio as gr# import requests
842
+ # # import gradio as gr
843
+ # # from ragatouille import RAGPretrainedModel
844
+ # # import logging
845
+ # # from pathlib import Path
846
+ # # from time import perf_counter
847
+ # # from sentence_transformers import CrossEncoder
848
+ # # from huggingface_hub import InferenceClient
849
+ # # from jinja2 import Environment, FileSystemLoader
850
+ # # import numpy as np
851
+ # # from os import getenv
852
+ # # from backend.query_llm import generate_hf, generate_qwen
853
+ # # from backend.semantic_search import table, retriever
854
+ # # from huggingface_hub import InferenceClient
855
+
856
+
857
+ # # # Bhashini API translation function
858
+ # # api_key = getenv('API_KEY')
859
+ # # user_id = getenv('USER_ID')
860
+
861
+ # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
862
+ # # """Translates text from source language to target language using the Bhashini API."""
863
 
864
+ # # if not text.strip():
865
+ # # print('Input text is empty. Please provide valid text for translation.')
866
+ # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
867
+ # # else:
868
+ # # print('Input text - ',text)
869
+ # # print(f'Starting translation process from {from_code} to {to_code}...')
870
+ # # print(f'Starting translation process from {from_code} to {to_code}...')
871
+ # # gr.Warning(f'Translating to {to_code}...')
872
 
873
+ # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
874
+ # # headers = {
875
+ # # "Content-Type": "application/json",
876
+ # # "userID": user_id,
877
+ # # "ulcaApiKey": api_key
878
+ # # }
879
+ # # payload = {
880
+ # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
881
+ # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
882
+ # # }
883
+
884
+ # # print('Sending initial request to get the pipeline...')
885
+ # # response = requests.post(url, json=payload, headers=headers)
886
+
887
+ # # if response.status_code != 200:
888
+ # # print(f'Error in initial request: {response.status_code}')
889
+ # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
890
+
891
+ # # print('Initial request successful, processing response...')
892
+ # # response_data = response.json()
893
+ # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
894
+ # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
895
+
896
+ # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
897
+
898
+ # # headers2 = {
899
+ # # "Content-Type": "application/json",
900
+ # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
901
+ # # }
902
+ # # compute_payload = {
903
+ # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
904
+ # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
905
+ # # }
906
+
907
+ # # print(f'Sending translation request with text: "{text}"')
908
+ # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
909
+
910
+ # # if compute_response.status_code != 200:
911
+ # # print(f'Error in translation request: {compute_response.status_code}')
912
+ # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
913
+
914
+ # # print('Translation request successful, processing translation...')
915
+ # # compute_response_data = compute_response.json()
916
+ # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
917
+
918
+ # # print(f'Translation successful. Translated content: "{translated_content}"')
919
+ # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
920
+
921
+
922
+ # # # Existing chatbot functions
923
+ # # VECTOR_COLUMN_NAME = "vector"
924
+ # # TEXT_COLUMN_NAME = "text"
925
+ # # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
926
+ # # proj_dir = Path(__file__).parent
927
+
928
+ # # logging.basicConfig(level=logging.INFO)
929
+ # # logger = logging.getLogger(__name__)
930
+ # # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
931
+ # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
932
+
933
+ # # template = env.get_template('template.j2')
934
+ # # template_html = env.get_template('template_html.j2')
935
+
936
+ # # # def add_text(history, text):
937
+ # # # history = [] if history is None else history
938
+ # # # history = history + [(text, None)]
939
+ # # # return history, gr.Textbox(value="", interactive=False)
940
+
941
+ # # def bot(history, cross_encoder):
942
+
943
+ # # top_rerank = 25
944
+ # # top_k_rank = 20
945
+ # # query = history[-1][0] if history else ''
946
+ # # print('\nQuery: ',query )
947
+ # # print('\nHistory:',history)
948
+ # # if not query:
949
+ # # gr.Warning("Please submit a non-empty string as a prompt")
950
+ # # raise ValueError("Empty string was submitted")
951
+
952
+ # # logger.warning('Retrieving documents...')
953
+
954
+ # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
955
+ # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
956
+ # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
957
+ # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
958
+ # # documents_full = RAG_db.search(query, k=top_k_rank)
959
+
960
+ # # documents = [item['content'] for item in documents_full]
961
+ # # prompt = template.render(documents=documents, query=query)
962
+ # # prompt_html = template_html.render(documents=documents, query=query)
963
+
964
+ # # generate_fn = generate_hf
965
+
966
+ # # history[-1][1] = ""
967
+ # # for character in generate_fn(prompt, history[:-1]):
968
+ # # history[-1][1] = character
969
+ # # yield history, prompt_html
970
+ # # else:
971
+ # # document_start = perf_counter()
972
+
973
+ # # query_vec = retriever.encode(query)
974
+ # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
975
+
976
+ # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
977
+ # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
978
+
979
+ # # query_doc_pair = [[query, doc] for doc in documents]
980
+ # # if cross_encoder == '(FAST) MiniLM-L6v2':
981
+ # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
982
+ # # elif cross_encoder == '(ACCURATE) BGE reranker':
983
+ # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
984
+
985
+ # # cross_scores = cross_encoder1.predict(query_doc_pair)
986
+ # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
987
+
988
+ # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
989
+
990
+ # # document_time = perf_counter() - document_start
991
+
992
+ # # prompt = template.render(documents=documents, query=query)
993
+ # # prompt_html = template_html.render(documents=documents, query=query)
994
+
995
+ # # #generate_fn = generate_hf
996
+ # # generate_fn=generate_qwen
997
+ # # # Create a new history entry instead of modifying the tuple directly
998
+ # # new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt
999
+ # # output=''
1000
+ # # # for character in generate_fn(prompt, history[:-1]):
1001
+ # # # #new_history[-1] = (query, character)
1002
+ # # # output+=character
1003
+ # # output=generate_fn(prompt, history[:-1])
1004
+
1005
+ # # print('Output:',output)
1006
+ # # new_history[-1] = (prompt, output) #query replaced with prompt
1007
+ # # print('New History',new_history)
1008
+ # # #print('prompt html',prompt_html)# Update the last tuple with new text
1009
+
1010
+ # # history_list = list(history[-1])
1011
+ # # history_list[1] = output # Assuming `character` is what you want to assign
1012
+ # # # Update the history with the modified list converted back to a tuple
1013
+ # # history[-1] = tuple(history_list)
1014
+
1015
+ # # #history[-1][1] = character
1016
+ # # # yield new_history, prompt_html
1017
+ # # yield history, prompt_html
1018
+ # # # new_history,prompt_html
1019
+ # # # history[-1][1] = ""
1020
+ # # # for character in generate_fn(prompt, history[:-1]):
1021
+ # # # history[-1][1] = character
1022
+ # # # yield history, prompt_html
1023
+
1024
+ # # #def translate_text(response_text, selected_language):
1025
+
1026
+ # # def translate_text(selected_language,history):
1027
+
1028
+ # # iso_language_codes = {
1029
+ # # "Hindi": "hi",
1030
+ # # "Gom": "gom",
1031
+ # # "Kannada": "kn",
1032
+ # # "Dogri": "doi",
1033
+ # # "Bodo": "brx",
1034
+ # # "Urdu": "ur",
1035
+ # # "Tamil": "ta",
1036
+ # # "Kashmiri": "ks",
1037
+ # # "Assamese": "as",
1038
+ # # "Bengali": "bn",
1039
+ # # "Marathi": "mr",
1040
+ # # "Sindhi": "sd",
1041
+ # # "Maithili": "mai",
1042
+ # # "Punjabi": "pa",
1043
+ # # "Malayalam": "ml",
1044
+ # # "Manipuri": "mni",
1045
+ # # "Telugu": "te",
1046
+ # # "Sanskrit": "sa",
1047
+ # # "Nepali": "ne",
1048
+ # # "Santali": "sat",
1049
+ # # "Gujarati": "gu",
1050
+ # # "Odia": "or"
1051
+ # # }
1052
+
1053
+ # # to_code = iso_language_codes[selected_language]
1054
+ # # response_text = history[-1][1] if history else ''
1055
+ # # print('response_text for translation',response_text)
1056
+ # # translation = bhashini_translate(response_text, to_code=to_code)
1057
+ # # return translation['translated_content']
1058
+
1059
+
1060
+ # # # Gradio interface
1061
+ # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
1062
+ # # history_state = gr.State([])
1063
+ # # with gr.Row():
1064
+ # # with gr.Column(scale=10):
1065
+ # # gr.HTML(value="""<div style="color: #FF4500;"><h1>Welcome! I am your friend!</h1>Ask me !I will help you<h1><span style="color: #008000">I AM A CHATBOT FOR 9 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
1066
+ # # gr.HTML(value=f"""<p style="font-family: sans-serif; font-size: 16px;">A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students</p>""")
1067
+ # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:[email protected]" style="color: #00008B; font-style: italic;">[email protected]</a>.</p>""")
1068
+
1069
+ # # with gr.Column(scale=3):
1070
+ # # gr.Image(value='logo.png', height=200, width=200)
1071
+
1072
+ # # chatbot = gr.Chatbot(
1073
+ # # [],
1074
+ # # elem_id="chatbot",
1075
+ # # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
1076
+ # # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
1077
+ # # bubble_full_width=False,
1078
+ # # show_copy_button=True,
1079
+ # # show_share_button=True,
1080
+ # # )
1081
+
1082
+ # # with gr.Row():
1083
+ # # txt = gr.Textbox(
1084
+ # # scale=3,
1085
+ # # show_label=False,
1086
+ # # placeholder="Enter text and press enter",
1087
+ # # container=False,
1088
+ # # )
1089
+ # # txt_btn = gr.Button(value="Submit text", scale=1)
1090
+
1091
+ # # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker', '(HIGH ACCURATE) ColBERT'], value='(ACCURATE) BGE reranker', label="Embeddings", info="Only First query to Colbert may take little time)")
1092
+ # # language_dropdown = gr.Dropdown(
1093
+ # # choices=[
1094
+ # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1095
+ # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1096
+ # # "Gujarati", "Odia"
1097
+ # # ],
1098
+ # # value="Hindi", # default to Hindi
1099
+ # # label="Select Language for Translation"
1100
+ # # )
1101
+
1102
+ # # prompt_html = gr.HTML()
1103
+
1104
+ # # translated_textbox = gr.Textbox(label="Translated Response")
1105
+ # # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
1106
+ # # print('History state',history_state)
1107
+ # # history = history_state
1108
+ # # history.append((txt, ""))
1109
+ # # #history_state.value=(history)
1110
 
1111
+ # # # Call bot function
1112
+ # # # bot_output = list(bot(history, cross_encoder))
1113
+ # # bot_output = next(bot(history, cross_encoder))
1114
+ # # print('bot_output',bot_output)
1115
+ # # #history, prompt_html = bot_output[-1]
1116
+ # # history, prompt_html = bot_output
1117
+ # # print('History',history)
1118
+ # # # Update the history state
1119
+ # # history_state[:] = history
1120
 
1121
+ # # # Translate text
1122
+ # # translated_text = translate_text(language_dropdown, history)
1123
+ # # return history, prompt_html, translated_text
1124
 
1125
+ # # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1126
+ # # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1127
 
1128
+ # # examples = ['WHAT IS DIFFERENCES BETWEEN HOMOGENOUS AND HETEROGENOUS MIXTURE?','WHAT IS COVALENT BOND?',
1129
+ # # 'EXPLAIN GOLGI APPARATUS']
1130
 
1131
+ # # gr.Examples(examples, txt)
1132
 
1133
 
1134
+ # # # Launch the Gradio application
1135
+ # # CHATBOT.launch(share=True,debug=True)
1136