rameshmoorthy commited on
Commit
be80025
·
verified ·
1 Parent(s): 72f5896

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -1497
app.py CHANGED
@@ -83,7 +83,7 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
83
  logger.error(f"Error in response generation: {e}")
84
  return f"Error: {str(e)}"
85
 
86
- # Gradio Interface (Inspired by Quiz App)
87
  with gr.Blocks(title="Science Chatbot", theme=gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")) as chatbot_app:
88
  gr.HTML("""
89
  <center>
@@ -93,49 +93,52 @@ with gr.Blocks(title="Science Chatbot", theme=gr.themes.Default(primary_hue="cya
93
  </center>
94
  """)
95
 
 
96
  with gr.Row():
97
- query = gr.Textbox(label="Ask a Science Question", placeholder="E.g., What is an ionic bond?")
98
  submit_btn = gr.Button("Submit", variant="primary")
99
 
100
- response_output = gr.Textbox(label="Answer", interactive=False)
101
  cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings")
102
- history_state = gr.State(value=[])
103
 
104
  def handle_query(user_input, history, cross_encoder_choice):
105
  if not user_input.strip():
106
- return history, "Please enter a valid question.", history
 
107
  response = retrieve_and_generate_response(user_input, cross_encoder_choice, history)
108
  history.append([user_input, response])
109
- return history, response, history[-2:] # Limit history to last 2 exchanges for context
110
 
111
  submit_btn.click(
112
  fn=handle_query,
113
- inputs=[query, history_state, cross_encoder],
114
- outputs=[history_state, response_output, history_state]
115
  )
116
 
117
- if __name__ == "__main__":
118
- chatbot_app.queue().launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
119
  # import logging
120
  # from sentence_transformers import CrossEncoder
121
  # from phi.agent import Agent
122
  # from phi.model.groq import Groq
123
  # from backend.semantic_search import table, retriever
124
- # from os import getenv
125
  # import numpy as np
126
  # from time import perf_counter
 
127
 
128
  # # Logging setup
129
  # logging.basicConfig(level=logging.INFO)
130
  # logger = logging.getLogger(__name__)
131
 
132
- # # API Key
133
- # groq_api_key = getenv('GROQ_API_KEY')
 
 
 
 
 
134
 
135
- # # Phi Agent Setup
136
  # def create_phi_agent():
137
- # if not groq_api_key:
138
- # logger.error("GROQ_API_KEY not found.")
139
  # return None
140
  # agent = Agent(
141
  # name="Science Education Assistant",
@@ -148,7 +151,7 @@ if __name__ == "__main__":
148
  # "Structure responses with headings and bullet points when helpful.",
149
  # "Encourage learning and curiosity."
150
  # ],
151
- # model=Groq(id="llama3-70b-8192", api_key=groq_api_key),
152
  # markdown=True
153
  # )
154
  # return agent
@@ -196,1494 +199,36 @@ if __name__ == "__main__":
196
  # logger.error(f"Error in response generation: {e}")
197
  # return f"Error: {str(e)}"
198
 
199
- # # Gradio Interface
200
- # with gr.Blocks(theme='gradio/soft') as chatbot_app:
201
- # gr.Markdown("# Science Chatbot for 10th Grade Students")
202
- # chatbot = gr.Chatbot()
 
 
 
 
 
 
203
  # with gr.Row():
204
- # query = gr.Textbox(placeholder="Ask a science question...", label="Question")
205
- # submit_btn = gr.Button("Submit")
 
 
206
  # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings")
 
207
 
208
  # def handle_query(user_input, history, cross_encoder_choice):
209
  # if not user_input.strip():
210
- # return history
211
- # history = history or []
212
- # history.append([user_input, None])
213
- # response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1])
214
- # history[-1][1] = response
215
- # return history
216
 
217
  # submit_btn.click(
218
  # fn=handle_query,
219
- # inputs=[query, chatbot, cross_encoder],
220
- # outputs=[chatbot]
221
  # )
222
 
223
  # if __name__ == "__main__":
224
- # chatbot_app.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
225
- # # import logging
226
- # # from sentence_transformers import CrossEncoder
227
- # # from phi.agent import Agent
228
- # # from phi.model.groq import Groq
229
- # # from backend.semantic_search import table, retriever
230
- # # from os import getenv
231
- # # import requests
232
- # # import numpy as np
233
- # # from time import perf_counter
234
-
235
- # # # Logging setup
236
- # # logging.basicConfig(level=logging.INFO)
237
- # # logger = logging.getLogger(__name__)
238
-
239
- # # # API Keys
240
- # # groq_api_key = getenv('GROQ_API_KEY')
241
- # # api_key = getenv('API_KEY')
242
- # # user_id = getenv('USER_ID')
243
-
244
- # # # Bhashini Translation
245
- # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
246
- # # if not text.strip():
247
- # # return {"status_code": 400, "message": "Input text is empty", "translated_content": text}
248
-
249
- # # try:
250
- # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
251
- # # headers = {"Content-Type": "application/json", "userID": user_id, "ulcaApiKey": api_key}
252
- # # payload = {
253
- # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
254
- # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
255
- # # }
256
-
257
- # # response = requests.post(url, json=payload, headers=headers)
258
- # # if response.status_code != 200:
259
- # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": text}
260
-
261
- # # response_data = response.json()
262
- # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
263
- # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
264
-
265
- # # headers2 = {
266
- # # "Content-Type": "application/json",
267
- # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
268
- # # }
269
- # # compute_payload = {
270
- # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
271
- # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
272
- # # }
273
-
274
- # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
275
- # # if compute_response.status_code != 200:
276
- # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": text}
277
-
278
- # # translated_content = compute_response.json()["pipelineResponse"][0]["output"][0]["target"]
279
- # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
280
-
281
- # # except Exception as e:
282
- # # return {"status_code": 500, "message": f"Translation error: {e}", "translated_content": text}
283
-
284
- # # # Phi Agent Setup
285
- # # def create_phi_agent():
286
- # # if not groq_api_key:
287
- # # return None
288
- # # agent = Agent(
289
- # # name="Science Education Assistant",
290
- # # role="You are a helpful science tutor for 10th-grade students",
291
- # # instructions=[
292
- # # "You are an expert science teacher specializing in 10th-grade curriculum.",
293
- # # "Provide clear, accurate, and age-appropriate explanations.",
294
- # # "Use simple language and examples that students can understand.",
295
- # # "Focus on concepts from physics, chemistry, and biology.",
296
- # # "Structure responses with headings and bullet points when helpful.",
297
- # # "Encourage learning and curiosity."
298
- # # ],
299
- # # model=Groq(id="llama3 AnnoDomini", api_key=groq_api_key),
300
- # # markdown=True
301
- # # )
302
- # # return agent
303
-
304
- # # phi_agent = create_phi_agent()
305
-
306
- # # # Response Generation
307
- # # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
308
- # # top_rerank = 25
309
- # # top_k_rank = 20
310
-
311
- # # if not query:
312
- # # return "Please provide a valid question."
313
-
314
- # # try:
315
- # # query_vec = retriever.encode(query)
316
- # # documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
317
- # # documents = [doc["text"] for doc in documents]
318
-
319
- # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') if cross_encoder_choice == '(ACCURATE) BGE reranker' else CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
320
- # # query_doc_pair = [[query, doc] for doc in documents]
321
- # # cross_scores = cross_encoder1.predict(query_doc_pair)
322
- # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
323
- # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
324
-
325
- # # context = "\n\n".join(documents[:10]) if documents else ""
326
- # # context = f"Context information from educational materials:\n{context}\n\n"
327
-
328
- # # history_context = ""
329
- # # if history:
330
- # # for user_msg, bot_msg in history[-2:]:
331
- # # if user_msg and bot_msg:
332
- # # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n"
333
-
334
- # # 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."
335
-
336
- # # if not phi_agent:
337
- # # return "Chatbot not configured properly."
338
- # # response = phi_agent.run(full_prompt)
339
- # # return response.content if hasattr(response, 'content') else str(response)
340
-
341
- # # except Exception as e:
342
- # # return f"Error: {str(e)}"
343
-
344
- # # # Gradio Interface
345
- # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
346
- # # gr.Markdown("# Science Chatbot for 10th Grade Students")
347
- # # chatbot = gr.Chatbot()
348
- # # with gr.Row():
349
- # # query = gr.Textbox(placeholder="Ask a science question...", label="Question")
350
- # # submit_btn = gr.Button("Submit")
351
- # # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings")
352
- # # language = gr.Dropdown(choices=["Hindi", "Tamil", "Telugu", "Kannada", "Marathi", "Gujarati", "Bengali", "Odia", "Punjabi", "Malayalam", "Assamese", "Urdu"], value="Hindi", label="Translation Language")
353
- # # translated_output = gr.Textbox(label="Translated Response")
354
-
355
- # # def handle_query(user_input, history, cross_encoder_choice):
356
- # # if not user_input.strip():
357
- # # return history, ""
358
- # # history = history or []
359
- # # history.append([user_input, None])
360
- # # response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1])
361
- # # history[-1][1] = response
362
- # # translated = bhashini_translate(response, to_code={"Hindi": "hi", "Tamil": "ta", "Telugu": "te", "Kannada": "kn", "Marathi": "mr", "Gujarati": "gu", "Bengali": "bn", "Odia": "or", "Punjabi": "pa", "Malayalam": "ml", "Assamese": "as", "Urdu": "ur"}.get(language, "hi"))
363
- # # return history, translated["translated_content"]
364
-
365
- # # submit_btn.click(
366
- # # fn=handle_query,
367
- # # inputs=[query, chatbot, cross_encoder, language],
368
- # # outputs=[chatbot, translated_output]
369
- # # )
370
-
371
- # # if __name__ == "__main__":
372
- # # CHATBOT.launch(server_name="0.0.0.0", server_port=7860)# import requests
373
- # # # import gradio as gr
374
- # # # import logging
375
- # # # from pathlib import Path
376
- # # # from time import perf_counter
377
- # # # from sentence_transformers import CrossEncoder
378
- # # # from jinja2 import Environment, FileSystemLoader
379
- # # # import numpy as np
380
- # # # from os import getenv
381
-
382
- # # # # Phi Agent imports
383
- # # # from phi.agent import Agent, RunResponse
384
- # # # from phi.model.groq import Groq
385
-
386
- # # # from backend.semantic_search import table, retriever
387
-
388
- # # # # API Keys
389
- # # # api_key = getenv('API_KEY')
390
- # # # user_id = getenv('USER_ID')
391
- # # # groq_api_key = getenv('GROQ_API_KEY')
392
-
393
- # # # # Check for required API keys
394
- # # # if not groq_api_key:
395
- # # # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
396
- # # # logging.error("GROQ_API_KEY not found.")
397
-
398
- # # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
399
- # # # """Translates text from source language to target language using the Bhashini API."""
400
-
401
- # # # if not text.strip():
402
- # # # print('Input text is empty. Please provide valid text for translation.')
403
- # # # return {"status_code": 400, "message": "Input text is empty", "translated_content": text}
404
-
405
- # # # print(f'Input text - {text}')
406
- # # # print(f'Starting translation process from {from_code} to {to_code}...')
407
- # # # gr.Warning(f'Translating to {to_code}...')
408
-
409
- # # # try:
410
- # # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
411
- # # # headers = {
412
- # # # "Content-Type": "application/json",
413
- # # # "userID": user_id,
414
- # # # "ulcaApiKey": api_key
415
- # # # }
416
- # # # payload = {
417
- # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
418
- # # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
419
- # # # }
420
-
421
- # # # print('Sending initial request to get the pipeline...')
422
- # # # response = requests.post(url, json=payload, headers=headers)
423
-
424
- # # # if response.status_code != 200:
425
- # # # print(f'Error in initial request: {response.status_code}')
426
- # # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": text}
427
-
428
- # # # print('Initial request successful, processing response...')
429
- # # # response_data = response.json()
430
- # # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
431
- # # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
432
-
433
- # # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
434
-
435
- # # # headers2 = {
436
- # # # "Content-Type": "application/json",
437
- # # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
438
- # # # }
439
- # # # compute_payload = {
440
- # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
441
- # # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
442
- # # # }
443
-
444
- # # # print(f'Sending translation request with text: "{text}"')
445
- # # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
446
-
447
- # # # if compute_response.status_code != 200:
448
- # # # print(f'Error in translation request: {compute_response.status_code}')
449
- # # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": text}
450
-
451
- # # # print('Translation request successful, processing translation...')
452
- # # # compute_response_data = compute_response.json()
453
- # # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
454
-
455
- # # # print(f'Translation successful. Translated content: "{translated_content}"')
456
- # # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
457
-
458
- # # # except Exception as e:
459
- # # # print(f'Translation error: {e}')
460
- # # # return {"status_code": 500, "message": f"Translation error: {e}", "translated_content": text}
461
-
462
- # # # # Initialize Phi Agent
463
- # # # def create_phi_agent():
464
- # # # """Create and return a Phi Agent for science education chatbot"""
465
- # # # if not groq_api_key:
466
- # # # return None
467
-
468
- # # # agent = Agent(
469
- # # # name="Science Education Assistant",
470
- # # # role="You are a helpful science tutor for 10th-grade students",
471
- # # # instructions=[
472
- # # # "You are an expert science teacher specializing in 10th-grade curriculum.",
473
- # # # "Provide clear, accurate, and age-appropriate explanations.",
474
- # # # "Use simple language and examples that students can understand.",
475
- # # # "Focus on concepts from physics, chemistry, and biology.",
476
- # # # "When answering, structure your response with clear headings and bullet points when helpful.",
477
- # # # "Always encourage learning and curiosity.",
478
- # # # "If you're not sure about something, acknowledge it honestly."
479
- # # # ],
480
- # # # model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key),
481
- # # # markdown=True,
482
- # # # show_tool_calls=False,
483
- # # # debug_mode=False
484
- # # # )
485
- # # # return agent
486
-
487
- # # # # Create the agent instance
488
- # # # phi_agent = create_phi_agent()
489
-
490
- # # # def generate_phi_response(prompt: str, history: list = None) -> str:
491
- # # # """Generate response using Phi Agent"""
492
- # # # if not phi_agent:
493
- # # # return "Sorry, the chatbot is not properly configured. Please check the API key settings."
494
-
495
- # # # try:
496
- # # # # Build context from history if available
497
- # # # context = ""
498
- # # # if history:
499
- # # # for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context
500
- # # # if user_msg and bot_msg:
501
- # # # context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n"
502
-
503
- # # # # Combine context with current prompt
504
- # # # full_prompt = f"{context}Current Question: {prompt}"
505
-
506
- # # # # Run the agent
507
- # # # response: RunResponse = phi_agent.run(full_prompt)
508
-
509
- # # # # Extract the content from the response
510
- # # # if hasattr(response, 'content') and response.content:
511
- # # # return response.content
512
- # # # elif hasattr(response, 'messages') and response.messages:
513
- # # # # Get the last message content
514
- # # # last_message = response.messages[-1]
515
- # # # if hasattr(last_message, 'content'):
516
- # # # return last_message.content
517
-
518
- # # # return "I apologize, but I couldn't generate a proper response. Please try again."
519
-
520
- # # # except Exception as e:
521
- # # # print(f"Error in Phi Agent: {e}")
522
- # # # return f"I encountered an error while processing your question: {str(e)}. Please try again."
523
-
524
- # # # # Constants
525
- # # # VECTOR_COLUMN_NAME = "vector"
526
- # # # TEXT_COLUMN_NAME = "text"
527
- # # # proj_dir = Path(__file__).parent
528
-
529
- # # # logging.basicConfig(level=logging.INFO)
530
- # # # logger = logging.getLogger(__name__)
531
-
532
- # # # # Setup Jinja2 templates
533
- # # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
534
- # # # try:
535
- # # # template = env.get_template('template.j2')
536
- # # # template_html = env.get_template('template_html.j2')
537
- # # # except:
538
- # # # # Fallback if templates don't exist
539
- # # # template = None
540
- # # # template_html = None
541
-
542
- # # # def retrieve_and_generate_response(query, cross_encoder, history=None):
543
- # # # """Retrieve documents and generate response"""
544
- # # # top_rerank = 25
545
- # # # top_k_rank = 20
546
-
547
- # # # if not query:
548
- # # # return "Please provide a valid question."
549
-
550
- # # # logger.warning('Retrieving documents...')
551
-
552
- # # # try:
553
- # # # document_start = perf_counter()
554
-
555
- # # # query_vec = retriever.encode(query)
556
- # # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
557
- # # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
558
-
559
- # # # query_doc_pair = [[query, doc] for doc in documents]
560
-
561
- # # # if cross_encoder == '(FAST) MiniLM-L6v2':
562
- # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
563
- # # # elif cross_encoder == '(ACCURATE) BGE reranker':
564
- # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
565
- # # # else:
566
- # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') # Default
567
-
568
- # # # cross_scores = cross_encoder1.predict(query_doc_pair)
569
- # # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
570
-
571
- # # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
572
-
573
- # # # document_time = perf_counter() - document_start
574
- # # # print(f"Document retrieval took {document_time:.2f} seconds")
575
-
576
- # # # # Create context from documents
577
- # # # context = ""
578
- # # # if documents:
579
- # # # context = "\n\n".join(documents[:10]) # Use top 10 documents
580
- # # # context = f"Context information from educational materials:\n{context}\n\n"
581
-
582
- # # # # Build conversation history
583
- # # # history_context = ""
584
- # # # if history:
585
- # # # for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
586
- # # # if user_msg and bot_msg:
587
- # # # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n"
588
-
589
- # # # # Create enhanced prompt
590
- # # # full_prompt = f"""
591
- # # # {history_context}
592
- # # # {context}
593
- # # # Question: {query}
594
-
595
- # # # Please 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.
596
- # # # """
597
-
598
- # # # # Generate response using Phi Agent
599
- # # # output = generate_phi_response(full_prompt, history)
600
-
601
- # # # print('Output:', output)
602
-
603
- # # # return output
604
-
605
- # # # except Exception as e:
606
- # # # print(f"Error in retrieve_and_generate_response: {e}")
607
- # # # return f"Sorry, I encountered an error: {str(e)}"
608
-
609
- # # # def translate_response(selected_language, response_text):
610
- # # # """Translate the response to selected language"""
611
- # # # if not response_text or not selected_language:
612
- # # # return response_text
613
-
614
- # # # iso_language_codes = {
615
- # # # "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
616
- # # # "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
617
- # # # "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
618
- # # # "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
619
- # # # }
620
-
621
- # # # to_code = iso_language_codes.get(selected_language, "hi")
622
- # # # translation = bhashini_translate(response_text, to_code=to_code)
623
- # # # return translation.get('translated_content', response_text)
624
-
625
- # # # # Simplified Gradio interface
626
- # # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
627
- # # # with gr.Row():
628
- # # # with gr.Column(scale=10):
629
- # # # 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 10 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
630
- # # # 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 Phi Agent & Groq LLMs for 10 std students</p>""")
631
- # # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""")
632
-
633
- # # # with gr.Column(scale=3):
634
- # # # try:
635
- # # # gr.Image(value='logo.png', height=200, width=200)
636
- # # # except:
637
- # # # pass # Skip if logo not found
638
-
639
- # # # chatbot = gr.Chatbot(
640
- # # # [],
641
- # # # elem_id="chatbot",
642
- # # # bubble_full_width=False,
643
- # # # show_copy_button=True,
644
- # # # show_share_button=True,
645
- # # # )
646
-
647
- # # # with gr.Row():
648
- # # # txt = gr.Textbox(
649
- # # # scale=3,
650
- # # # show_label=False,
651
- # # # placeholder="Enter text and press enter",
652
- # # # container=False,
653
- # # # )
654
- # # # txt_btn = gr.Button(value="Submit text", scale=1)
655
-
656
- # # # cross_encoder = gr.Radio(
657
- # # # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
658
- # # # value='(ACCURATE) BGE reranker',
659
- # # # label="Embeddings"
660
- # # # )
661
-
662
- # # # language_dropdown = gr.Dropdown(
663
- # # # choices=[
664
- # # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
665
- # # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
666
- # # # "Gujarati", "Odia"
667
- # # # ],
668
- # # # value="Hindi",
669
- # # # label="Select Language for Translation"
670
- # # # )
671
-
672
- # # # translated_textbox = gr.Textbox(label="Translated Response")
673
-
674
- # # # def user_message(user_input, history):
675
- # # # """Handle user input and add to history"""
676
- # # # if not user_input.strip():
677
- # # # return "", history
678
- # # # return "", history + [[user_input, None]]
679
-
680
- # # # def bot_response(history, cross_encoder_choice):
681
- # # # """Generate bot response"""
682
- # # # if not history:
683
- # # # return history
684
-
685
- # # # user_input = history[-1][0]
686
-
687
- # # # # Generate response
688
- # # # response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1])
689
-
690
- # # # # Update history
691
- # # # history[-1][1] = response
692
- # # # return history
693
-
694
- # # # def translate_current_response(history, selected_language):
695
- # # # """Translate the current response"""
696
- # # # if not history or not history[-1][1]:
697
- # # # return ""
698
-
699
- # # # response_text = history[-1][1]
700
- # # # translated_text = translate_response(selected_language, response_text)
701
- # # # return translated_text
702
-
703
- # # # # Event handlers
704
- # # # txt_msg = txt.submit(user_message, [txt, chatbot], [txt, chatbot]).then(
705
- # # # bot_response, [chatbot, cross_encoder], chatbot
706
- # # # ).then(
707
- # # # translate_current_response, [chatbot, language_dropdown], translated_textbox
708
- # # # )
709
-
710
- # # # txt_btn.click(user_message, [txt, chatbot], [txt, chatbot]).then(
711
- # # # bot_response, [chatbot, cross_encoder], chatbot
712
- # # # ).then(
713
- # # # translate_current_response, [chatbot, language_dropdown], translated_textbox
714
- # # # )
715
-
716
- # # # examples = [
717
- # # # 'CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?',
718
- # # # 'WHAT IS IONIC BOND?',
719
- # # # 'EXPLAIN ASEXUAL REPRODUCTION'
720
- # # # ]
721
-
722
- # # # gr.Examples(examples, txt)
723
-
724
- # # # # Launch the Gradio application
725
- # # # if __name__ == "__main__":
726
- # # # CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860)
727
- # # # # import gradio as gr
728
- # # # # # from ragatouille import RAGPretrainedModel # COMMENTED OUT
729
- # # # # import logging
730
- # # # # from pathlib import Path
731
- # # # # from time import perf_counter
732
- # # # # from sentence_transformers import CrossEncoder
733
- # # # # #from huggingface_hub import InferenceClient
734
- # # # # from jinja2 import Environment, FileSystemLoader
735
- # # # # import numpy as np
736
- # # # # from os import getenv
737
-
738
- # # # # # Phi Agent imports
739
- # # # # from phi.agent import Agent, RunResponse
740
- # # # # from phi.model.groq import Groq
741
- # # # # from phi.utils.pprint import pprint_run_response
742
-
743
- # # # # #from backend.query_llm import generate_hf
744
- # # # # from backend.semantic_search import table, retriever
745
-
746
- # # # # # Bhashini API translation function
747
- # # # # api_key = getenv('API_KEY')
748
- # # # # user_id = getenv('USER_ID')
749
- # # # # groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key
750
-
751
- # # # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
752
- # # # # """Translates text from source language to target language using the Bhashini API."""
753
-
754
- # # # # if not text.strip():
755
- # # # # print('Input text is empty. Please provide valid text for translation.')
756
- # # # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
757
- # # # # else:
758
- # # # # print('Input text - ',text)
759
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
760
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
761
- # # # # gr.Warning(f'Translating to {to_code}...')
762
-
763
- # # # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
764
- # # # # headers = {
765
- # # # # "Content-Type": "application/json",
766
- # # # # "userID": user_id,
767
- # # # # "ulcaApiKey": api_key
768
- # # # # }
769
- # # # # payload = {
770
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
771
- # # # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
772
- # # # # }
773
-
774
- # # # # print('Sending initial request to get the pipeline...')
775
- # # # # response = requests.post(url, json=payload, headers=headers)
776
-
777
- # # # # if response.status_code != 200:
778
- # # # # print(f'Error in initial request: {response.status_code}')
779
- # # # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
780
-
781
- # # # # print('Initial request successful, processing response...')
782
- # # # # response_data = response.json()
783
- # # # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
784
- # # # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
785
-
786
- # # # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
787
-
788
- # # # # headers2 = {
789
- # # # # "Content-Type": "application/json",
790
- # # # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
791
- # # # # }
792
- # # # # compute_payload = {
793
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
794
- # # # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
795
- # # # # }
796
-
797
- # # # # print(f'Sending translation request with text: "{text}"')
798
- # # # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
799
-
800
- # # # # if compute_response.status_code != 200:
801
- # # # # print(f'Error in translation request: {compute_response.status_code}')
802
- # # # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
803
-
804
- # # # # print('Translation request successful, processing translation...')
805
- # # # # compute_response_data = compute_response.json()
806
- # # # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
807
-
808
- # # # # print(f'Translation successful. Translated content: "{translated_content}"')
809
- # # # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
810
-
811
- # # # # # Initialize Phi Agent
812
- # # # # def create_phi_agent():
813
- # # # # """Create and return a Phi Agent for science education chatbot"""
814
- # # # # agent = Agent(
815
- # # # # name="Science Education Assistant",
816
- # # # # role="You are a helpful science tutor for 10th-grade students",
817
- # # # # instructions=[
818
- # # # # "You are an expert science teacher specializing in 10th-grade curriculum.",
819
- # # # # "Provide clear, accurate, and age-appropriate explanations.",
820
- # # # # "Use simple language and examples that students can understand.",
821
- # # # # "Focus on concepts from physics, chemistry, and biology.",
822
- # # # # "When answering, structure your response with clear headings and bullet points when helpful.",
823
- # # # # "Always encourage learning and curiosity.",
824
- # # # # "If you're not sure about something, acknowledge it honestly."
825
- # # # # ],
826
- # # # # model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key),
827
- # # # # markdown=True,
828
- # # # # show_tool_calls=False,
829
- # # # # debug_mode=False
830
- # # # # )
831
- # # # # return agent
832
-
833
- # # # # # Create the agent instance
834
- # # # # phi_agent = create_phi_agent()
835
-
836
- # # # # def generate_phi_response(prompt: str, history: list = None) -> str:
837
- # # # # """Generate response using Phi Agent"""
838
- # # # # try:
839
- # # # # # Build context from history if available
840
- # # # # context = ""
841
- # # # # if history:
842
- # # # # for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context
843
- # # # # if user_msg and bot_msg:
844
- # # # # context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n"
845
-
846
- # # # # # Combine context with current prompt
847
- # # # # full_prompt = f"{context}Current Question: {prompt}"
848
-
849
- # # # # # Run the agent
850
- # # # # response: RunResponse = phi_agent.run(full_prompt)
851
-
852
- # # # # # Extract the content from the response
853
- # # # # if hasattr(response, 'content') and response.content:
854
- # # # # return response.content
855
- # # # # elif hasattr(response, 'messages') and response.messages:
856
- # # # # # Get the last message content
857
- # # # # last_message = response.messages[-1]
858
- # # # # if hasattr(last_message, 'content'):
859
- # # # # return last_message.content
860
-
861
- # # # # return "I apologize, but I couldn't generate a proper response. Please try again."
862
-
863
- # # # # except Exception as e:
864
- # # # # print(f"Error in Phi Agent: {e}")
865
- # # # # return f"I encountered an error while processing your question: {str(e)}. Please try again."
866
-
867
- # # # # # Existing chatbot functions
868
- # # # # VECTOR_COLUMN_NAME = "vector"
869
- # # # # TEXT_COLUMN_NAME = "text"
870
- # # # # #HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
871
- # # # # proj_dir = Path(__file__).parent
872
-
873
- # # # # logging.basicConfig(level=logging.INFO)
874
- # # # # logger = logging.getLogger(__name__)
875
- # # # # #client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
876
- # # # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
877
-
878
- # # # # template = env.get_template('template.j2')
879
- # # # # template_html = env.get_template('template_html.j2')
880
-
881
- # # # # def bot(history, cross_encoder):
882
- # # # # top_rerank = 25
883
- # # # # top_k_rank = 20
884
- # # # # query = history[-1][0] if history else ''
885
- # # # # print('\nQuery: ',query )
886
- # # # # print('\nHistory:',history)
887
- # # # # if not query:
888
- # # # # gr.Warning("Please submit a non-empty string as a prompt")
889
- # # # # raise ValueError("Empty string was submitted")
890
-
891
- # # # # logger.warning('Retrieving documents...')
892
-
893
- # # # # # COMMENTED OUT RAGatouille section
894
- # # # # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
895
- # # # # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
896
- # # # # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
897
- # # # # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
898
- # # # # # documents_full = RAG_db.search(query, k=top_k_rank)
899
- # # # # #
900
- # # # # # documents = [item['content'] for item in documents_full]
901
- # # # # # prompt = template.render(documents=documents, query=query)
902
- # # # # # prompt_html = template_html.render(documents=documents, query=query)
903
- # # # # #
904
- # # # # # generate_fn = generate_hf
905
- # # # # #
906
- # # # # # history[-1][1] = ""
907
- # # # # # for character in generate_fn(prompt, history[:-1]):
908
- # # # # # history[-1][1] = character
909
- # # # # # yield history, prompt_html
910
- # # # # # else:
911
-
912
- # # # # # Handle ColBERT case differently for now
913
- # # # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
914
- # # # # gr.Warning('ColBERT is temporarily disabled. Using BGE reranker instead.')
915
- # # # # cross_encoder = '(ACCURATE) BGE reranker'
916
-
917
- # # # # document_start = perf_counter()
918
-
919
- # # # # query_vec = retriever.encode(query)
920
- # # # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
921
-
922
- # # # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
923
- # # # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
924
-
925
- # # # # query_doc_pair = [[query, doc] for doc in documents]
926
- # # # # if cross_encoder == '(FAST) MiniLM-L6v2':
927
- # # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
928
- # # # # elif cross_encoder == '(ACCURATE) BGE reranker':
929
- # # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
930
-
931
- # # # # cross_scores = cross_encoder1.predict(query_doc_pair)
932
- # # # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
933
-
934
- # # # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
935
-
936
- # # # # document_time = perf_counter() - document_start
937
-
938
- # # # # prompt = template.render(documents=documents, query=query)
939
- # # # # prompt_html = template_html.render(documents=documents, query=query)
940
-
941
- # # # # # REPLACED: Use Phi Agent instead of generate_qwen
942
- # # # # print("Using Phi Agent for response generation...")
943
- # # # # output = generate_phi_response(prompt, history[:-1])
944
-
945
- # # # # print('Output:',output)
946
-
947
- # # # # # Update history with the response
948
- # # # # history_list = list(history[-1])
949
- # # # # history_list[1] = output
950
- # # # # history[-1] = tuple(history_list)
951
-
952
- # # # # yield history, prompt_html
953
-
954
- # # # # def translate_text(selected_language, history):
955
- # # # # iso_language_codes = {
956
- # # # # "Hindi": "hi",
957
- # # # # "Gom": "gom",
958
- # # # # "Kannada": "kn",
959
- # # # # "Dogri": "doi",
960
- # # # # "Bodo": "brx",
961
- # # # # "Urdu": "ur",
962
- # # # # "Tamil": "ta",
963
- # # # # "Kashmiri": "ks",
964
- # # # # "Assamese": "as",
965
- # # # # "Bengali": "bn",
966
- # # # # "Marathi": "mr",
967
- # # # # "Sindhi": "sd",
968
- # # # # "Maithili": "mai",
969
- # # # # "Punjabi": "pa",
970
- # # # # "Malayalam": "ml",
971
- # # # # "Manipuri": "mni",
972
- # # # # "Telugu": "te",
973
- # # # # "Sanskrit": "sa",
974
- # # # # "Nepali": "ne",
975
- # # # # "Santali": "sat",
976
- # # # # "Gujarati": "gu",
977
- # # # # "Odia": "or"
978
- # # # # }
979
-
980
- # # # # to_code = iso_language_codes[selected_language]
981
- # # # # response_text = history[-1][1] if history else ''
982
- # # # # print('response_text for translation',response_text)
983
- # # # # translation = bhashini_translate(response_text, to_code=to_code)
984
- # # # # return translation['translated_content']
985
-
986
- # # # # # Gradio interface - SIMPLIFIED to avoid schema issues
987
- # # # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
988
- # # # # history_state = gr.State([])
989
- # # # # with gr.Row():
990
- # # # # with gr.Column(scale=10):
991
- # # # # 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 10 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
992
- # # # # 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 Phi Agent & Groq LLMs for 10 std students</p>""")
993
- # # # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""")
994
-
995
- # # # # with gr.Column(scale=3):
996
- # # # # try:
997
- # # # # gr.Image(value='logo.png', height=200, width=200)
998
- # # # # except:
999
- # # # # pass # Skip if logo not found
1000
-
1001
- # # # # chatbot = gr.Chatbot(
1002
- # # # # [],
1003
- # # # # elem_id="chatbot",
1004
- # # # # bubble_full_width=False,
1005
- # # # # show_copy_button=True,
1006
- # # # # show_share_button=True,
1007
- # # # # )
1008
-
1009
- # # # # with gr.Row():
1010
- # # # # txt = gr.Textbox(
1011
- # # # # scale=3,
1012
- # # # # show_label=False,
1013
- # # # # placeholder="Enter text and press enter",
1014
- # # # # container=False,
1015
- # # # # )
1016
- # # # # txt_btn = gr.Button(value="Submit text", scale=1)
1017
-
1018
- # # # # # SIMPLIFIED: Remove ColBERT option temporarily
1019
- # # # # cross_encoder = gr.Radio(
1020
- # # # # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
1021
- # # # # value='(ACCURATE) BGE reranker',
1022
- # # # # label="Embeddings"
1023
- # # # # )
1024
-
1025
- # # # # language_dropdown = gr.Dropdown(
1026
- # # # # choices=[
1027
- # # # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1028
- # # # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1029
- # # # # "Gujarati", "Odia"
1030
- # # # # ],
1031
- # # # # value="Hindi", # default to Hindi
1032
- # # # # label="Select Language for Translation"
1033
- # # # # )
1034
-
1035
- # # # # prompt_html = gr.HTML()
1036
-
1037
- # # # # translated_textbox = gr.Textbox(label="Translated Response")
1038
-
1039
- # # # # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
1040
- # # # # print('History state',history_state)
1041
- # # # # history = history_state
1042
- # # # # history.append((txt, ""))
1043
-
1044
- # # # # # Call bot function
1045
- # # # # bot_output = next(bot(history, cross_encoder))
1046
- # # # # print('bot_output',bot_output)
1047
- # # # # history, prompt_html = bot_output
1048
- # # # # print('History',history)
1049
-
1050
- # # # # # Update the history state
1051
- # # # # history_state[:] = history
1052
-
1053
- # # # # # Translate text
1054
- # # # # translated_text = translate_text(language_dropdown, history)
1055
- # # # # return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input
1056
-
1057
- # # # # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt])
1058
- # # # # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt])
1059
-
1060
- # # # # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?',
1061
- # # # # 'EXPLAIN ASEXUAL REPRODUCTION']
1062
-
1063
- # # # # gr.Examples(examples, txt)
1064
-
1065
- # # # # # Launch the Gradio application
1066
- # # # # if __name__ == "__main__":
1067
- # # # # CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860)# import requests
1068
- # # # # import gradio as gr
1069
- # # # # from ragatouille import RAGPretrainedModel
1070
- # # # # import logging
1071
- # # # # from pathlib import Path
1072
- # # # # from time import perf_counter
1073
- # # # # from sentence_transformers import CrossEncoder
1074
- # # # # from huggingface_hub import InferenceClient
1075
- # # # # from jinja2 import Environment, FileSystemLoader
1076
- # # # # import numpy as np
1077
- # # # # from os import getenv
1078
-
1079
- # # # # # Phi Agent imports
1080
- # # # # from phi.agent import Agent, RunResponse
1081
- # # # # from phi.model.groq import Groq
1082
- # # # # from phi.utils.pprint import pprint_run_response
1083
-
1084
- # # # # #from backend.query_llm import generate_hf
1085
- # # # # from backend.semantic_search import table, retriever
1086
-
1087
- # # # # # Bhashini API translation function
1088
- # # # # api_key = getenv('API_KEY')
1089
- # # # # user_id = getenv('USER_ID')
1090
- # # # # groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key
1091
-
1092
- # # # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
1093
- # # # # """Translates text from source language to target language using the Bhashini API."""
1094
-
1095
- # # # # if not text.strip():
1096
- # # # # print('Input text is empty. Please provide valid text for translation.')
1097
- # # # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
1098
- # # # # else:
1099
- # # # # print('Input text - ',text)
1100
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
1101
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
1102
- # # # # gr.Warning(f'Translating to {to_code}...')
1103
-
1104
- # # # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
1105
- # # # # headers = {
1106
- # # # # "Content-Type": "application/json",
1107
- # # # # "userID": user_id,
1108
- # # # # "ulcaApiKey": api_key
1109
- # # # # }
1110
- # # # # payload = {
1111
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
1112
- # # # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
1113
- # # # # }
1114
-
1115
- # # # # print('Sending initial request to get the pipeline...')
1116
- # # # # response = requests.post(url, json=payload, headers=headers)
1117
-
1118
- # # # # if response.status_code != 200:
1119
- # # # # print(f'Error in initial request: {response.status_code}')
1120
- # # # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
1121
-
1122
- # # # # print('Initial request successful, processing response...')
1123
- # # # # response_data = response.json()
1124
- # # # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
1125
- # # # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
1126
-
1127
- # # # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
1128
-
1129
- # # # # headers2 = {
1130
- # # # # "Content-Type": "application/json",
1131
- # # # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
1132
- # # # # }
1133
- # # # # compute_payload = {
1134
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
1135
- # # # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
1136
- # # # # }
1137
-
1138
- # # # # print(f'Sending translation request with text: "{text}"')
1139
- # # # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
1140
-
1141
- # # # # if compute_response.status_code != 200:
1142
- # # # # print(f'Error in translation request: {compute_response.status_code}')
1143
- # # # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
1144
-
1145
- # # # # print('Translation request successful, processing translation...')
1146
- # # # # compute_response_data = compute_response.json()
1147
- # # # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
1148
-
1149
- # # # # print(f'Translation successful. Translated content: "{translated_content}"')
1150
- # # # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
1151
-
1152
- # # # # # Initialize Phi Agent
1153
- # # # # def create_phi_agent():
1154
- # # # # """Create and return a Phi Agent for science education chatbot"""
1155
- # # # # agent = Agent(
1156
- # # # # name="Science Education Assistant",
1157
- # # # # role="You are a helpful science tutor for 10th-grade students",
1158
- # # # # instructions=[
1159
- # # # # "You are an expert science teacher specializing in 10th-grade curriculum.",
1160
- # # # # "Provide clear, accurate, and age-appropriate explanations.",
1161
- # # # # "Use simple language and examples that students can understand.",
1162
- # # # # "Focus on concepts from physics, chemistry, and biology.",
1163
- # # # # "When answering, structure your response with clear headings and bullet points when helpful.",
1164
- # # # # "Always encourage learning and curiosity.",
1165
- # # # # "If you're not sure about something, acknowledge it honestly."
1166
- # # # # ],
1167
- # # # # model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key),
1168
- # # # # markdown=True,
1169
- # # # # show_tool_calls=False,
1170
- # # # # debug_mode=False
1171
- # # # # )
1172
- # # # # return agent
1173
-
1174
- # # # # # Create the agent instance
1175
- # # # # phi_agent = create_phi_agent()
1176
-
1177
- # # # # def generate_phi_response(prompt: str, history: list = None) -> str:
1178
- # # # # """Generate response using Phi Agent"""
1179
- # # # # try:
1180
- # # # # # Build context from history if available
1181
- # # # # context = ""
1182
- # # # # if history:
1183
- # # # # for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context
1184
- # # # # if user_msg and bot_msg:
1185
- # # # # context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n"
1186
-
1187
- # # # # # Combine context with current prompt
1188
- # # # # full_prompt = f"{context}Current Question: {prompt}"
1189
-
1190
- # # # # # Run the agent
1191
- # # # # response: RunResponse = phi_agent.run(full_prompt)
1192
-
1193
- # # # # # Extract the content from the response
1194
- # # # # if hasattr(response, 'content') and response.content:
1195
- # # # # return response.content
1196
- # # # # elif hasattr(response, 'messages') and response.messages:
1197
- # # # # # Get the last message content
1198
- # # # # last_message = response.messages[-1]
1199
- # # # # if hasattr(last_message, 'content'):
1200
- # # # # return last_message.content
1201
-
1202
- # # # # return "I apologize, but I couldn't generate a proper response. Please try again."
1203
-
1204
- # # # # except Exception as e:
1205
- # # # # print(f"Error in Phi Agent: {e}")
1206
- # # # # return f"I encountered an error while processing your question: {str(e)}. Please try again."
1207
-
1208
- # # # # # Existing chatbot functions
1209
- # # # # VECTOR_COLUMN_NAME = "vector"
1210
- # # # # TEXT_COLUMN_NAME = "text"
1211
- # # # # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
1212
- # # # # proj_dir = Path(__file__).parent
1213
-
1214
- # # # # logging.basicConfig(level=logging.INFO)
1215
- # # # # logger = logging.getLogger(__name__)
1216
- # # # # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
1217
- # # # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
1218
-
1219
- # # # # template = env.get_template('template.j2')
1220
- # # # # template_html = env.get_template('template_html.j2')
1221
-
1222
- # # # # def bot(history, cross_encoder):
1223
- # # # # top_rerank = 25
1224
- # # # # top_k_rank = 20
1225
- # # # # query = history[-1][0] if history else ''
1226
- # # # # print('\nQuery: ',query )
1227
- # # # # print('\nHistory:',history)
1228
- # # # # if not query:
1229
- # # # # gr.Warning("Please submit a non-empty string as a prompt")
1230
- # # # # raise ValueError("Empty string was submitted")
1231
-
1232
- # # # # logger.warning('Retrieving documents...')
1233
-
1234
- # # # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
1235
- # # # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
1236
- # # # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
1237
- # # # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
1238
- # # # # documents_full = RAG_db.search(query, k=top_k_rank)
1239
-
1240
- # # # # documents = [item['content'] for item in documents_full]
1241
- # # # # prompt = template.render(documents=documents, query=query)
1242
- # # # # prompt_html = template_html.render(documents=documents, query=query)
1243
-
1244
- # # # # generate_fn = generate_hf
1245
-
1246
- # # # # history[-1][1] = ""
1247
- # # # # for character in generate_fn(prompt, history[:-1]):
1248
- # # # # history[-1][1] = character
1249
- # # # # yield history, prompt_html
1250
- # # # # else:
1251
- # # # # document_start = perf_counter()
1252
-
1253
- # # # # query_vec = retriever.encode(query)
1254
- # # # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
1255
-
1256
- # # # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
1257
- # # # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
1258
-
1259
- # # # # query_doc_pair = [[query, doc] for doc in documents]
1260
- # # # # if cross_encoder == '(FAST) MiniLM-L6v2':
1261
- # # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
1262
- # # # # elif cross_encoder == '(ACCURATE) BGE reranker':
1263
- # # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
1264
-
1265
- # # # # cross_scores = cross_encoder1.predict(query_doc_pair)
1266
- # # # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
1267
-
1268
- # # # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
1269
-
1270
- # # # # document_time = perf_counter() - document_start
1271
-
1272
- # # # # prompt = template.render(documents=documents, query=query)
1273
- # # # # prompt_html = template_html.render(documents=documents, query=query)
1274
-
1275
- # # # # # REPLACED: Use Phi Agent instead of generate_qwen
1276
- # # # # print("Using Phi Agent for response generation...")
1277
- # # # # output = generate_phi_response(prompt, history[:-1])
1278
-
1279
- # # # # print('Output:',output)
1280
-
1281
- # # # # # Update history with the response
1282
- # # # # history_list = list(history[-1])
1283
- # # # # history_list[1] = output
1284
- # # # # history[-1] = tuple(history_list)
1285
-
1286
- # # # # yield history, prompt_html
1287
-
1288
- # # # # def translate_text(selected_language, history):
1289
- # # # # iso_language_codes = {
1290
- # # # # "Hindi": "hi",
1291
- # # # # "Gom": "gom",
1292
- # # # # "Kannada": "kn",
1293
- # # # # "Dogri": "doi",
1294
- # # # # "Bodo": "brx",
1295
- # # # # "Urdu": "ur",
1296
- # # # # "Tamil": "ta",
1297
- # # # # "Kashmiri": "ks",
1298
- # # # # "Assamese": "as",
1299
- # # # # "Bengali": "bn",
1300
- # # # # "Marathi": "mr",
1301
- # # # # "Sindhi": "sd",
1302
- # # # # "Maithili": "mai",
1303
- # # # # "Punjabi": "pa",
1304
- # # # # "Malayalam": "ml",
1305
- # # # # "Manipuri": "mni",
1306
- # # # # "Telugu": "te",
1307
- # # # # "Sanskrit": "sa",
1308
- # # # # "Nepali": "ne",
1309
- # # # # "Santali": "sat",
1310
- # # # # "Gujarati": "gu",
1311
- # # # # "Odia": "or"
1312
- # # # # }
1313
-
1314
- # # # # to_code = iso_language_codes[selected_language]
1315
- # # # # response_text = history[-1][1] if history else ''
1316
- # # # # print('response_text for translation',response_text)
1317
- # # # # translation = bhashini_translate(response_text, to_code=to_code)
1318
- # # # # return translation['translated_content']
1319
-
1320
- # # # # # Gradio interface
1321
- # # # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
1322
- # # # # history_state = gr.State([])
1323
- # # # # with gr.Row():
1324
- # # # # with gr.Column(scale=10):
1325
- # # # # 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 10 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
1326
- # # # # 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 Phi Agent & Groq LLMs for 10 std students</p>""")
1327
- # # # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""")
1328
-
1329
- # # # # with gr.Column(scale=3):
1330
- # # # # gr.Image(value='logo.png', height=200, width=200)
1331
-
1332
- # # # # chatbot = gr.Chatbot(
1333
- # # # # [],
1334
- # # # # elem_id="chatbot",
1335
- # # # # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
1336
- # # # # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
1337
- # # # # bubble_full_width=False,
1338
- # # # # show_copy_button=True,
1339
- # # # # show_share_button=True,
1340
- # # # # )
1341
-
1342
- # # # # with gr.Row():
1343
- # # # # txt = gr.Textbox(
1344
- # # # # scale=3,
1345
- # # # # show_label=False,
1346
- # # # # placeholder="Enter text and press enter",
1347
- # # # # container=False,
1348
- # # # # )
1349
- # # # # txt_btn = gr.Button(value="Submit text", scale=1)
1350
-
1351
- # # # # 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)")
1352
- # # # # language_dropdown = gr.Dropdown(
1353
- # # # # choices=[
1354
- # # # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1355
- # # # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1356
- # # # # "Gujarati", "Odia"
1357
- # # # # ],
1358
- # # # # value="Hindi", # default to Hindi
1359
- # # # # label="Select Language for Translation"
1360
- # # # # )
1361
-
1362
- # # # # prompt_html = gr.HTML()
1363
-
1364
- # # # # translated_textbox = gr.Textbox(label="Translated Response")
1365
-
1366
- # # # # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
1367
- # # # # print('History state',history_state)
1368
- # # # # history = history_state
1369
- # # # # history.append((txt, ""))
1370
-
1371
- # # # # # Call bot function
1372
- # # # # bot_output = next(bot(history, cross_encoder))
1373
- # # # # print('bot_output',bot_output)
1374
- # # # # history, prompt_html = bot_output
1375
- # # # # print('History',history)
1376
-
1377
- # # # # # Update the history state
1378
- # # # # history_state[:] = history
1379
-
1380
- # # # # # Translate text
1381
- # # # # translated_text = translate_text(language_dropdown, history)
1382
- # # # # return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input
1383
-
1384
- # # # # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt])
1385
- # # # # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt])
1386
-
1387
- # # # # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?',
1388
- # # # # 'EXPLAIN ASEXUAL REPRODUCTION']
1389
-
1390
- # # # # gr.Examples(examples, txt)
1391
-
1392
- # # # # # Launch the Gradio application
1393
- # # # # if __name__ == "__main__":
1394
- # # # # CHATBOT.launch(share=True, debug=True)# import requests
1395
- # # # # import gradio as gr
1396
- # # # # from ragatouille import RAGPretrainedModel
1397
- # # # # import logging
1398
- # # # # from pathlib import Path
1399
- # # # # from time import perf_counter
1400
- # # # # from sentence_transformers import CrossEncoder
1401
- # # # # from huggingface_hub import InferenceClient
1402
- # # # # from jinja2 import Environment, FileSystemLoader
1403
- # # # # import numpy as np
1404
- # # # # from os import getenv
1405
- # # # # from backend.query_llm import generate_hf, generate_qwen
1406
- # # # # from backend.semantic_search import table, retriever
1407
- # # # # from huggingface_hub import InferenceClient
1408
-
1409
-
1410
- # # # # # Bhashini API translation function
1411
- # # # # api_key = getenv('API_KEY')
1412
- # # # # user_id = getenv('USER_ID')
1413
-
1414
- # # # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
1415
- # # # # """Translates text from source language to target language using the Bhashini API."""
1416
-
1417
- # # # # if not text.strip():
1418
- # # # # print('Input text is empty. Please provide valid text for translation.')
1419
- # # # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
1420
- # # # # else:
1421
- # # # # print('Input text - ',text)
1422
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
1423
- # # # # print(f'Starting translation process from {from_code} to {to_code}...')
1424
- # # # # gr.Warning(f'Translating to {to_code}...')
1425
-
1426
- # # # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
1427
- # # # # headers = {
1428
- # # # # "Content-Type": "application/json",
1429
- # # # # "userID": user_id,
1430
- # # # # "ulcaApiKey": api_key
1431
- # # # # }
1432
- # # # # payload = {
1433
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
1434
- # # # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
1435
- # # # # }
1436
-
1437
- # # # # print('Sending initial request to get the pipeline...')
1438
- # # # # response = requests.post(url, json=payload, headers=headers)
1439
-
1440
- # # # # if response.status_code != 200:
1441
- # # # # print(f'Error in initial request: {response.status_code}')
1442
- # # # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
1443
-
1444
- # # # # print('Initial request successful, processing response...')
1445
- # # # # response_data = response.json()
1446
- # # # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
1447
- # # # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
1448
-
1449
- # # # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
1450
-
1451
- # # # # headers2 = {
1452
- # # # # "Content-Type": "application/json",
1453
- # # # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
1454
- # # # # }
1455
- # # # # compute_payload = {
1456
- # # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
1457
- # # # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
1458
- # # # # }
1459
-
1460
- # # # # print(f'Sending translation request with text: "{text}"')
1461
- # # # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
1462
-
1463
- # # # # if compute_response.status_code != 200:
1464
- # # # # print(f'Error in translation request: {compute_response.status_code}')
1465
- # # # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
1466
-
1467
- # # # # print('Translation request successful, processing translation...')
1468
- # # # # compute_response_data = compute_response.json()
1469
- # # # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
1470
-
1471
- # # # # print(f'Translation successful. Translated content: "{translated_content}"')
1472
- # # # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
1473
-
1474
-
1475
- # # # # # Existing chatbot functions
1476
- # # # # VECTOR_COLUMN_NAME = "vector"
1477
- # # # # TEXT_COLUMN_NAME = "text"
1478
- # # # # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
1479
- # # # # proj_dir = Path(__file__).parent
1480
-
1481
- # # # # logging.basicConfig(level=logging.INFO)
1482
- # # # # logger = logging.getLogger(__name__)
1483
- # # # # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
1484
- # # # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
1485
-
1486
- # # # # template = env.get_template('template.j2')
1487
- # # # # template_html = env.get_template('template_html.j2')
1488
-
1489
- # # # # # def add_text(history, text):
1490
- # # # # # history = [] if history is None else history
1491
- # # # # # history = history + [(text, None)]
1492
- # # # # # return history, gr.Textbox(value="", interactive=False)
1493
-
1494
- # # # # def bot(history, cross_encoder):
1495
-
1496
- # # # # top_rerank = 25
1497
- # # # # top_k_rank = 20
1498
- # # # # query = history[-1][0] if history else ''
1499
- # # # # print('\nQuery: ',query )
1500
- # # # # print('\nHistory:',history)
1501
- # # # # if not query:
1502
- # # # # gr.Warning("Please submit a non-empty string as a prompt")
1503
- # # # # raise ValueError("Empty string was submitted")
1504
-
1505
- # # # # logger.warning('Retrieving documents...')
1506
-
1507
- # # # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
1508
- # # # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
1509
- # # # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
1510
- # # # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
1511
- # # # # documents_full = RAG_db.search(query, k=top_k_rank)
1512
-
1513
- # # # # documents = [item['content'] for item in documents_full]
1514
- # # # # prompt = template.render(documents=documents, query=query)
1515
- # # # # prompt_html = template_html.render(documents=documents, query=query)
1516
-
1517
- # # # # generate_fn = generate_hf
1518
-
1519
- # # # # history[-1][1] = ""
1520
- # # # # for character in generate_fn(prompt, history[:-1]):
1521
- # # # # history[-1][1] = character
1522
- # # # # yield history, prompt_html
1523
- # # # # else:
1524
- # # # # document_start = perf_counter()
1525
-
1526
- # # # # query_vec = retriever.encode(query)
1527
- # # # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
1528
-
1529
- # # # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
1530
- # # # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
1531
-
1532
- # # # # query_doc_pair = [[query, doc] for doc in documents]
1533
- # # # # if cross_encoder == '(FAST) MiniLM-L6v2':
1534
- # # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
1535
- # # # # elif cross_encoder == '(ACCURATE) BGE reranker':
1536
- # # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
1537
-
1538
- # # # # cross_scores = cross_encoder1.predict(query_doc_pair)
1539
- # # # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
1540
-
1541
- # # # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
1542
-
1543
- # # # # document_time = perf_counter() - document_start
1544
-
1545
- # # # # prompt = template.render(documents=documents, query=query)
1546
- # # # # prompt_html = template_html.render(documents=documents, query=query)
1547
-
1548
- # # # # #generate_fn = generate_hf
1549
- # # # # generate_fn=generate_qwen
1550
- # # # # # Create a new history entry instead of modifying the tuple directly
1551
- # # # # new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt
1552
- # # # # output=''
1553
- # # # # # for character in generate_fn(prompt, history[:-1]):
1554
- # # # # # #new_history[-1] = (query, character)
1555
- # # # # # output+=character
1556
- # # # # output=generate_fn(prompt, history[:-1])
1557
-
1558
- # # # # print('Output:',output)
1559
- # # # # new_history[-1] = (prompt, output) #query replaced with prompt
1560
- # # # # print('New History',new_history)
1561
- # # # # #print('prompt html',prompt_html)# Update the last tuple with new text
1562
-
1563
- # # # # history_list = list(history[-1])
1564
- # # # # history_list[1] = output # Assuming `character` is what you want to assign
1565
- # # # # # Update the history with the modified list converted back to a tuple
1566
- # # # # history[-1] = tuple(history_list)
1567
-
1568
- # # # # #history[-1][1] = character
1569
- # # # # # yield new_history, prompt_html
1570
- # # # # yield history, prompt_html
1571
- # # # # # new_history,prompt_html
1572
- # # # # # history[-1][1] = ""
1573
- # # # # # for character in generate_fn(prompt, history[:-1]):
1574
- # # # # # history[-1][1] = character
1575
- # # # # # yield history, prompt_html
1576
-
1577
- # # # # #def translate_text(response_text, selected_language):
1578
-
1579
- # # # # def translate_text(selected_language,history):
1580
-
1581
- # # # # iso_language_codes = {
1582
- # # # # "Hindi": "hi",
1583
- # # # # "Gom": "gom",
1584
- # # # # "Kannada": "kn",
1585
- # # # # "Dogri": "doi",
1586
- # # # # "Bodo": "brx",
1587
- # # # # "Urdu": "ur",
1588
- # # # # "Tamil": "ta",
1589
- # # # # "Kashmiri": "ks",
1590
- # # # # "Assamese": "as",
1591
- # # # # "Bengali": "bn",
1592
- # # # # "Marathi": "mr",
1593
- # # # # "Sindhi": "sd",
1594
- # # # # "Maithili": "mai",
1595
- # # # # "Punjabi": "pa",
1596
- # # # # "Malayalam": "ml",
1597
- # # # # "Manipuri": "mni",
1598
- # # # # "Telugu": "te",
1599
- # # # # "Sanskrit": "sa",
1600
- # # # # "Nepali": "ne",
1601
- # # # # "Santali": "sat",
1602
- # # # # "Gujarati": "gu",
1603
- # # # # "Odia": "or"
1604
- # # # # }
1605
-
1606
- # # # # to_code = iso_language_codes[selected_language]
1607
- # # # # response_text = history[-1][1] if history else ''
1608
- # # # # print('response_text for translation',response_text)
1609
- # # # # translation = bhashini_translate(response_text, to_code=to_code)
1610
- # # # # return translation['translated_content']
1611
-
1612
-
1613
- # # # # # Gradio interface
1614
- # # # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
1615
- # # # # history_state = gr.State([])
1616
- # # # # with gr.Row():
1617
- # # # # with gr.Column(scale=10):
1618
- # # # # 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 10 SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
1619
- # # # # 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>""")
1620
- # # # # gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""")
1621
-
1622
- # # # # with gr.Column(scale=3):
1623
- # # # # gr.Image(value='logo.png', height=200, width=200)
1624
-
1625
- # # # # chatbot = gr.Chatbot(
1626
- # # # # [],
1627
- # # # # elem_id="chatbot",
1628
- # # # # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
1629
- # # # # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
1630
- # # # # bubble_full_width=False,
1631
- # # # # show_copy_button=True,
1632
- # # # # show_share_button=True,
1633
- # # # # )
1634
-
1635
- # # # # with gr.Row():
1636
- # # # # txt = gr.Textbox(
1637
- # # # # scale=3,
1638
- # # # # show_label=False,
1639
- # # # # placeholder="Enter text and press enter",
1640
- # # # # container=False,
1641
- # # # # )
1642
- # # # # txt_btn = gr.Button(value="Submit text", scale=1)
1643
-
1644
- # # # # 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)")
1645
- # # # # language_dropdown = gr.Dropdown(
1646
- # # # # choices=[
1647
- # # # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1648
- # # # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1649
- # # # # "Gujarati", "Odia"
1650
- # # # # ],
1651
- # # # # value="Hindi", # default to Hindi
1652
- # # # # label="Select Language for Translation"
1653
- # # # # )
1654
-
1655
- # # # # prompt_html = gr.HTML()
1656
-
1657
- # # # # translated_textbox = gr.Textbox(label="Translated Response")
1658
- # # # # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
1659
- # # # # print('History state',history_state)
1660
- # # # # history = history_state
1661
- # # # # history.append((txt, ""))
1662
- # # # # #history_state.value=(history)
1663
-
1664
- # # # # # Call bot function
1665
- # # # # # bot_output = list(bot(history, cross_encoder))
1666
- # # # # bot_output = next(bot(history, cross_encoder))
1667
- # # # # print('bot_output',bot_output)
1668
- # # # # #history, prompt_html = bot_output[-1]
1669
- # # # # history, prompt_html = bot_output
1670
- # # # # print('History',history)
1671
- # # # # # Update the history state
1672
- # # # # history_state[:] = history
1673
-
1674
- # # # # # Translate text
1675
- # # # # translated_text = translate_text(language_dropdown, history)
1676
- # # # # return history, prompt_html, translated_text
1677
-
1678
- # # # # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1679
- # # # # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1680
-
1681
- # # # # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?',
1682
- # # # # 'EXPLAIN ASEXUAL REPRODUCTION']
1683
-
1684
- # # # # gr.Examples(examples, txt)
1685
-
1686
-
1687
- # # # # # Launch the Gradio application
1688
- # # # # CHATBOT.launch(share=True,debug=True)
1689
-
 
83
  logger.error(f"Error in response generation: {e}")
84
  return f"Error: {str(e)}"
85
 
86
+ # Gradio Interface with Chatbot
87
  with gr.Blocks(title="Science Chatbot", theme=gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")) as chatbot_app:
88
  gr.HTML("""
89
  <center>
 
93
  </center>
94
  """)
95
 
96
+ chatbot = gr.Chatbot(label="Conversation", bubble_full_width=False)
97
  with gr.Row():
98
+ query = gr.Textbox(label="Ask a Science Question", placeholder="E.g., What is an ionic bond?", show_label=False)
99
  submit_btn = gr.Button("Submit", variant="primary")
100
 
 
101
  cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings")
 
102
 
103
  def handle_query(user_input, history, cross_encoder_choice):
104
  if not user_input.strip():
105
+ return history, ""
106
+ history = history or []
107
  response = retrieve_and_generate_response(user_input, cross_encoder_choice, history)
108
  history.append([user_input, response])
109
+ return history, ""
110
 
111
  submit_btn.click(
112
  fn=handle_query,
113
+ inputs=[query, chatbot, cross_encoder],
114
+ outputs=[chatbot, query]
115
  )
116
 
117
+ if __name__ == "__main__":# import gradio as gr
 
118
  # import logging
119
  # from sentence_transformers import CrossEncoder
120
  # from phi.agent import Agent
121
  # from phi.model.groq import Groq
122
  # from backend.semantic_search import table, retriever
 
123
  # import numpy as np
124
  # from time import perf_counter
125
+ # import os
126
 
127
  # # Logging setup
128
  # logging.basicConfig(level=logging.INFO)
129
  # logger = logging.getLogger(__name__)
130
 
131
+ # # API Key setup
132
+ # api_key = os.getenv("GROQ_API_KEY")
133
+ # if not api_key:
134
+ # logger.error("GROQ_API_KEY not found.")
135
+ # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
136
+ # else:
137
+ # os.environ["GROQ_API_KEY"] = api_key
138
 
139
+ # # Initialize Phi Agent
140
  # def create_phi_agent():
141
+ # if not api_key:
 
142
  # return None
143
  # agent = Agent(
144
  # name="Science Education Assistant",
 
151
  # "Structure responses with headings and bullet points when helpful.",
152
  # "Encourage learning and curiosity."
153
  # ],
154
+ # model=Groq(id="llama3-70b-8192", api_key=api_key),
155
  # markdown=True
156
  # )
157
  # return agent
 
199
  # logger.error(f"Error in response generation: {e}")
200
  # return f"Error: {str(e)}"
201
 
202
+ # # Gradio Interface (Inspired by Quiz App)
203
+ # with gr.Blocks(title="Science Chatbot", theme=gr.themes.Default(primary_hue="cyan", secondary_hue="yellow", neutral_hue="purple")) as chatbot_app:
204
+ # gr.HTML("""
205
+ # <center>
206
+ # <h1><span style="color: purple;">Science Chatbot for 10th Grade Students</span></h1>
207
+ # <h2>AI-powered Science Tutor</h2>
208
+ # <i>⚠️ Ask any question from 10th-grade science (physics, chemistry, biology) and get clear, accurate answers! ⚠️</i>
209
+ # </center>
210
+ # """)
211
+
212
  # with gr.Row():
213
+ # query = gr.Textbox(label="Ask a Science Question", placeholder="E.g., What is an ionic bond?")
214
+ # submit_btn = gr.Button("Submit", variant="primary")
215
+
216
+ # response_output = gr.Textbox(label="Answer", interactive=False)
217
  # cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings")
218
+ # history_state = gr.State(value=[])
219
 
220
  # def handle_query(user_input, history, cross_encoder_choice):
221
  # if not user_input.strip():
222
+ # return history, "Please enter a valid question.", history
223
+ # response = retrieve_and_generate_response(user_input, cross_encoder_choice, history)
224
+ # history.append([user_input, response])
225
+ # return history, response, history[-2:] # Limit history to last 2 exchanges for context
 
 
226
 
227
  # submit_btn.click(
228
  # fn=handle_query,
229
+ # inputs=[query, history_state, cross_encoder],
230
+ # outputs=[history_state, response_output, history_state]
231
  # )
232
 
233
  # if __name__ == "__main__":
234
+ # chatbot_app.queue().launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr