RAMYASRI-39 commited on
Commit
f3b73c9
·
verified ·
1 Parent(s): 8a9a5d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1015 -588
app.py CHANGED
@@ -10,9 +10,10 @@ 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)
16
  logger = logging.getLogger(__name__)
17
 
18
  # API Key setup
@@ -20,7 +21,7 @@ api_key = os.getenv("GROQ_API_KEY")
20
  if not api_key:
21
  gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
22
  logger.error("GROQ_API_KEY not found.")
23
- api_key = "" # Fallback to empty string, but this will fail without a key
24
  else:
25
  os.environ["GROQ_API_KEY"] = api_key
26
 
@@ -28,15 +29,23 @@ else:
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."""
33
  if not text.strip():
34
- print('Input text is empty. Please provide valid text for translation.')
35
  return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
36
- else:
37
- print('Input text - ', text)
38
- print(f'Starting translation process from {from_code} to {to_code}...')
39
- gr.Warning(f'Translating to {to_code}...')
40
 
41
  url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
42
  headers = {
@@ -44,88 +53,132 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") ->
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"}
55
  }
56
 
57
- print('Sending initial request to get the pipeline...')
58
- response = requests.post(url, json=payload, headers=headers)
59
-
60
- if response.status_code != 200:
61
- print(f'Error in initial request: {response.status_code}, Response: {response.text}')
62
- return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
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}
70
 
71
- service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
72
- callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
73
-
74
- print(f'Service ID: {service_id}, Callback URL: {callback_url}')
75
-
76
- headers2 = {
77
- "Content-Type": "application/json",
78
- response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
79
- }
80
- compute_payload = {
81
- "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
82
- "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
83
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- print(f'Sending translation request with text: "{text}"')
86
- compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
87
-
88
- if compute_response.status_code != 200:
89
- print(f'Error in translation request: {compute_response.status_code}, Response: {compute_response.text}')
90
- return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
91
-
92
- print('Translation request successful, processing translation...')
93
- compute_response_data = compute_response.json()
94
- translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
95
-
96
- print(f'Translation successful. Translated content: "{translated_content}"')
97
- return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
98
-
99
- # Initialize PhiData Agent
100
- agent = Agent(
101
- name="Science Education Assistant",
102
- role="You are a helpful science tutor for 10th-grade students",
103
- instructions=[
104
- "You are an expert science teacher specializing in 10th-grade curriculum.",
105
- "Provide clear, accurate, and age-appropriate explanations.",
106
- "Use simple language and examples that students can understand.",
107
- "Focus on concepts from physics, chemistry, and biology.",
108
- "Structure responses with headings and bullet points when helpful.",
109
- "Encourage learning and curiosity."
110
- ],
111
- model=Groq(id="llama3-70b-8192", api_key=api_key),
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"""
124
  top_rerank = 25
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()
@@ -136,7 +189,8 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
136
  documents = [doc["text"] for doc in documents]
137
 
138
  # Re-rank documents using cross-encoder
139
- 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')
 
140
  query_doc_pair = [[query, doc] for doc in documents]
141
  cross_scores = cross_encoder_model.predict(query_doc_pair)
142
  sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
@@ -149,39 +203,29 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
149
  # Add conversation history for context
150
  history_context = ""
151
  if history and len(history) > 0:
152
- for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
153
  if user_msg and bot_msg:
154
  history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
155
 
156
  # Create full prompt
157
  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."
158
 
159
- # Generate response
160
- response = agent.run(full_prompt)
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."""
@@ -192,103 +236,196 @@ def translate_text(selected_language, history):
192
  "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
193
  }
194
 
195
- to_code = iso_language_codes[selected_language]
196
- response_text = history[-1][1] if history and history[-1][1] else ''
197
- print('response_text for translation', response_text)
198
- translation = bhashini_translate(response_text, to_code=to_code)
 
 
 
 
 
 
199
  return translation.get('translated_content', 'Translation failed.')
200
 
201
- # Gradio Interface with layout template
202
- with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  # Header section
204
  with gr.Row():
205
  with gr.Column(scale=10):
206
- 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>""")
207
- 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 9 std students</p>""")
208
- 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>""")
209
  with gr.Column(scale=3):
210
  try:
211
- gr.Image(value='logo.png', height=200, width=200)
212
  except:
213
- gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
 
 
 
214
 
215
- # Chat and input components
216
  chatbot = gr.Chatbot(
217
  [],
218
  elem_id="chatbot",
219
- avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
220
- 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
 
 
221
  bubble_full_width=False,
222
  show_copy_button=True,
223
  show_share_button=True,
 
224
  )
225
 
 
226
  with gr.Row():
227
  msg = gr.Textbox(
228
- scale=3,
229
  show_label=False,
230
- placeholder="Enter text and press enter",
231
  container=False,
 
232
  )
233
- submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
234
-
235
- # Additional controls
236
- cross_encoder = gr.Radio(
237
- choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
238
- value='(ACCURATE) BGE reranker',
239
- label="Embeddings Model",
240
- info="Select the model for document ranking"
241
- )
242
- language_dropdown = gr.Dropdown(
243
- choices=[
244
- "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
245
- "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
246
- "Gujarati", "Odia"
247
- ],
248
- value="Hindi",
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(
279
  examples=[
280
- 'What is the difference between metals and non-metals?',
281
- 'What is an ionic bond?',
282
- 'Explain asexual reproduction',
283
- 'What is photosynthesis?',
284
- 'Explain Newton\'s laws of motion'
 
285
  ],
286
  inputs=msg,
287
- label="Try these example questions:"
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
@@ -299,6 +436,7 @@ if __name__ == "__main__":
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)
@@ -314,8 +452,8 @@ if __name__ == "__main__":
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."""
@@ -333,6 +471,11 @@ if __name__ == "__main__":
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}}}],
338
  # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
@@ -347,7 +490,7 @@ if __name__ == "__main__":
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}
@@ -383,9 +526,9 @@ if __name__ == "__main__":
383
  # # Initialize PhiData Agent
384
  # agent = Agent(
385
  # name="Science Education Assistant",
386
- # role="You are a helpful science tutor for 9th-grade students",
387
  # instructions=[
388
- # "You are an expert science teacher specializing in 9th-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.",
@@ -395,12 +538,12 @@ if __name__ == "__main__":
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):
@@ -409,7 +552,7 @@ if __name__ == "__main__":
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()
@@ -445,24 +588,27 @@ if __name__ == "__main__":
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."""
@@ -484,8 +630,8 @@ if __name__ == "__main__":
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 9TH 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:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</a>.</p>""")
490
  # with gr.Column(scale=3):
491
  # try:
@@ -530,26 +676,30 @@ if __name__ == "__main__":
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(
@@ -566,315 +716,326 @@ if __name__ == "__main__":
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:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</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",
771
- # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
772
- # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
773
- # bubble_full_width=False,
774
- # show_copy_button=True,
775
- # show_share_button=True,
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}}}],
@@ -885,11 +1046,16 @@ if __name__ == "__main__":
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
 
@@ -908,7 +1074,7 @@ if __name__ == "__main__":
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...')
@@ -918,157 +1084,114 @@ if __name__ == "__main__":
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:ramyasriraman2019@gmail.com" style="color: #00008B; font-style: italic;">ramyadevi1607@yahoo.com</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",
@@ -1080,57 +1203,361 @@ if __name__ == "__main__":
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
 
 
10
  import requests
11
  from jinja2 import Environment, FileSystemLoader
12
  from pathlib import Path
13
+ import time
14
 
15
  # Set up logging
16
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
17
  logger = logging.getLogger(__name__)
18
 
19
  # API Key setup
 
21
  if not api_key:
22
  gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
23
  logger.error("GROQ_API_KEY not found.")
24
+ api_key = ""
25
  else:
26
  os.environ["GROQ_API_KEY"] = api_key
27
 
 
29
  bhashini_api_key = os.getenv("API_KEY", "").strip()
30
  bhashini_user_id = os.getenv("USER_ID", "").strip()
31
 
32
+ # 🎯 MODEL LIST: Ordered by parameter size (largest → smallest)
33
+ # Fallback chain: if rate limit (429) or model error, try next model
34
+ MODEL_CHAIN = [
35
+ {"id": "llama-3.3-70b-versatile", "name": "Llama 3.3 70B", "priority": 1},
36
+ {"id": "qwen/qwen3-32b", "name": "Qwen3 32B", "priority": 2},
37
+ {"id": "meta-llama/llama-4-scout-17b-16e-instruct", "name": "Llama-4 Scout 17B", "priority": 3},
38
+ {"id": "groq/compound", "name": "Groq Compound", "priority": 4},
39
+ {"id": "gemma2-9b-it", "name": "Gemma2 9B", "priority": 5},
40
+ {"id": "llama-3.1-8b-instant", "name": "Llama 3.1 8B Instant", "priority": 6},
41
+ ]
42
+
43
  def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
44
+ """Translates text using Bhashini API"""
45
  if not text.strip():
 
46
  return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
47
+
48
+ print(f'Translating: "{text[:50]}..." from {from_code} to {to_code}')
 
 
49
 
50
  url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
51
  headers = {
 
53
  "userID": bhashini_user_id,
54
  "ulcaApiKey": bhashini_api_key
55
  }
56
+
 
 
 
 
57
  payload = {
58
  "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
59
  "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
60
  }
61
 
62
+ try:
63
+ response = requests.post(url, json=payload, headers=headers, timeout=30)
64
+ if response.status_code != 200:
65
+ logger.error(f"Bhashini initial request failed: {response.status_code}")
66
+ return {"status_code": response.status_code, "message": "Translation request failed", "translated_content": None}
 
67
 
68
+ response_data = response.json()
69
+ if "pipelineInferenceAPIEndPoint" not in response_data:
70
+ return {"status_code": 400, "message": "Unexpected API response", "translated_content": None}
 
 
 
71
 
72
+ service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
73
+ callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
74
+
75
+ headers2 = {
76
+ "Content-Type": "application/json",
77
+ response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]:
78
+ response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
79
+ }
80
+
81
+ compute_payload = {
82
+ "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
83
+ "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
84
+ }
85
+
86
+ compute_response = requests.post(callback_url, json=compute_payload, headers=headers2, timeout=60)
87
+ if compute_response.status_code != 200:
88
+ return {"status_code": compute_response.status_code, "message": "Translation failed", "translated_content": None}
89
+
90
+ compute_response_data = compute_response.json()
91
+ translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
92
+ return {"status_code": 200, "message": "Success", "translated_content": translated_content}
93
+
94
+ except Exception as e:
95
+ logger.error(f"Bhashini translation error: {e}")
96
+ return {"status_code": 500, "message": f"Translation error: {str(e)}", "translated_content": None}
97
+
98
+
99
+ def create_agent_with_model(model_id: str) -> Agent:
100
+ """Create a Phi Agent with specified Groq model"""
101
+ return Agent(
102
+ name="Science Education Assistant",
103
+ role="You are a helpful science tutor for 10th-grade students",
104
+ instructions=[
105
+ "You are an expert science teacher specializing in 10th-grade curriculum.",
106
+ "Provide clear, accurate, and age-appropriate explanations.",
107
+ "Use simple language and examples that students can understand.",
108
+ "Focus on concepts from physics, chemistry, and biology.",
109
+ "Structure responses with headings and bullet points when helpful.",
110
+ "Encourage learning and curiosity.",
111
+ "If uncertain, acknowledge limitations and suggest reliable sources."
112
+ ],
113
+ model=Groq(id=model_id, api_key=api_key),
114
+ markdown=True,
115
+ timeout=60
116
+ )
117
+
118
+
119
+ def generate_with_fallback(prompt: str) -> tuple[str, str]:
120
+ """
121
+ Try models in descending size order.
122
+ Returns: (response_text, model_used)
123
+ """
124
+ last_error = None
125
 
126
+ for model_config in MODEL_CHAIN:
127
+ model_id = model_config["id"]
128
+ model_name = model_config["name"]
129
+ priority = model_config["priority"]
130
+
131
+ logger.info(f"🔄 Trying model #{priority}: {model_name} ({model_id})")
132
+
133
+ try:
134
+ agent = create_agent_with_model(model_id)
135
+ response = agent.run(prompt)
136
+ response_text = response.content if hasattr(response, 'content') else str(response)
137
+
138
+ logger.info(f" Success with {model_name}")
139
+ return response_text.strip(), model_name
140
+
141
+ except Exception as e:
142
+ error_msg = str(e).lower()
143
+ last_error = e
144
+
145
+ # Check for errors that warrant fallback to next model
146
+ should_fallback = any([
147
+ "429" in error_msg,
148
+ "rate limit" in error_msg,
149
+ "too many requests" in error_msg,
150
+ "model_decommissioned" in error_msg,
151
+ "model_not_found" in error_msg,
152
+ "invalid_request_error" in error_msg and "model" in error_msg,
153
+ "timeout" in error_msg and priority < len(MODEL_CHAIN)
154
+ ])
155
+
156
+ if should_fallback:
157
+ logger.warning(f"⚠️ {model_name} failed ({type(e).__name__}), falling back to next model...")
158
+ time.sleep(0.5 * priority)
159
+ continue
160
+ else:
161
+ logger.error(f"❌ {model_name} failed with non-fallback error: {e}")
162
+ break
163
+
164
+ # All models failed
165
+ error_details = f"{type(last_error).__name__}: {str(last_error)}" if last_error else "Unknown error"
166
+ logger.error(f"💥 All models failed. Last error: {error_details}")
167
+
168
+ fallback_response = (
169
+ "⚠️ I'm experiencing high demand right now. Please try again in a moment.\n\n"
170
+ f"*Technical note: Unable to connect to available models ({error_details})*"
171
+ )
172
+ return fallback_response, "fallback"
173
 
 
 
 
 
 
174
 
 
175
  def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
176
+ """Generate response using semantic search + multi-model LLM fallback"""
177
  top_rerank = 25
178
  top_k_rank = 20
179
 
180
  if not query.strip():
181
+ return "Please provide a valid question.", [], "N/A"
182
 
183
  try:
184
  start_time = perf_counter()
 
189
  documents = [doc["text"] for doc in documents]
190
 
191
  # Re-rank documents using cross-encoder
192
+ model_name = 'BAAI/bge-reranker-base' if cross_encoder_choice == '(ACCURATE) BGE reranker' else 'cross-encoder/ms-marco-MiniLM-L-6-v2'
193
+ cross_encoder_model = CrossEncoder(model_name)
194
  query_doc_pair = [[query, doc] for doc in documents]
195
  cross_scores = cross_encoder_model.predict(query_doc_pair)
196
  sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
 
203
  # Add conversation history for context
204
  history_context = ""
205
  if history and len(history) > 0:
206
+ for user_msg, bot_msg in history[-2:]:
207
  if user_msg and bot_msg:
208
  history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
209
 
210
  # Create full prompt
211
  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."
212
 
213
+ # Generate response with model fallback chain
214
+ response_text, model_used = generate_with_fallback(full_prompt)
215
+
216
+ # Add model attribution to response
217
+ if model_used != "fallback":
218
+ response_text = f"*Powered by {model_used}*\n\n{response_text}"
219
+
220
+ elapsed = perf_counter() - start_time
221
+ logger.info(f"⏱️ Response generated in {elapsed:.2f}s using {model_used}")
222
+
223
+ return response_text, documents, model_used
224
 
 
 
 
225
  except Exception as e:
226
+ logger.error(f"Error in response generation: {e}")
227
+ return f"Error: {str(e)}", [], "error"
228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
  def translate_text(selected_language, history):
231
  """Translate the last response in history to the selected language."""
 
236
  "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
237
  }
238
 
239
+ to_code = iso_language_codes.get(selected_language, "hi")
240
+ response_text = history[-1][1] if history and len(history) > 0 and history[-1][1] else ''
241
+
242
+ if not response_text or response_text.startswith("Error:"):
243
+ return "Translation not available for error messages."
244
+
245
+ # Remove model attribution line for cleaner translation
246
+ clean_text = response_text.split("\n\n", 1)[-1] if "*Powered by" in response_text else response_text
247
+
248
+ translation = bhashini_translate(clean_text, to_code=to_code)
249
  return translation.get('translated_content', 'Translation failed.')
250
 
251
+
252
+ # Set up Jinja2 environment
253
+ proj_dir = Path(__file__).parent
254
+ env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
255
+ template = env.get_template('template.j2')
256
+ template_html = env.get_template('template_html.j2')
257
+
258
+
259
+ # Gradio Interface
260
+ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
261
+ .model-badge {
262
+ background: #e0f2fe; color: #0369a1; padding: 2px 8px;
263
+ border-radius: 12px; font-size: 0.8em; font-weight: 500;
264
+ display: inline-block; margin: 4px 0;
265
+ }
266
+ .gradio-container { max-width: 1200px !important; margin: auto; }
267
+ """) as demo:
268
+
269
  # Header section
270
  with gr.Row():
271
  with gr.Column(scale=10):
272
+ gr.HTML(value="""<div style="color: #FF4500;"><h1>🎓 Welcome! I am your Science Friend!</h1><p>Ask me anything about 10th-grade science!</p><h2><span style="color: #008000"> Multi-Model AI with 22-Language Translation</span></h2></div>""")
273
+ gr.HTML(value="<p style='font-family: sans-serif; font-size: 14px; color: #666;'>Developed by K.M. Ramyasri, TGT, GHS Suthukeny Open Source Free for Students</p>")
274
+ gr.HTML(value="<p style='font-family: Arial, sans-serif; font-size: 13px;'>Suggestions: <a href='mailto:ramyasriraman2019@gmail.com' style='color: #0066cc;'>ramyasriraman2019@gmail.com</a></p>")
275
  with gr.Column(scale=3):
276
  try:
277
+ gr.Image(value='logo.png', height=180, width=180, show_label=False)
278
  except:
279
+ gr.HTML("<div style='height: 180px; width: 180px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: 3em;'>🤖</div>")
280
+
281
+ # Model status indicator (Markdown component)
282
+ model_status = gr.Markdown(value="🔄 *Ready to answer with best available model*")
283
 
284
+ # Chatbot
285
  chatbot = gr.Chatbot(
286
  [],
287
  elem_id="chatbot",
288
+ avatar_images=(
289
+ 'https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
290
+ 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'
291
+ ),
292
  bubble_full_width=False,
293
  show_copy_button=True,
294
  show_share_button=True,
295
+ height=400
296
  )
297
 
298
+ # Input row
299
  with gr.Row():
300
  msg = gr.Textbox(
301
+ scale=4,
302
  show_label=False,
303
+ placeholder="Ask a science question (e.g., 'What is photosynthesis?')",
304
  container=False,
305
+ autofocus=True
306
  )
307
+ submit_btn = gr.Button(value="🚀 Ask", scale=1, variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
309
+ # Controls
310
+ with gr.Row():
311
+ cross_encoder = gr.Radio(
312
+ choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
313
+ value='(ACCURATE) BGE reranker',
314
+ label="🔍 Document Ranking",
315
+ info="Balance speed vs accuracy for source retrieval"
316
+ )
317
+ language_dropdown = gr.Dropdown(
318
+ choices=["Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri",
319
+ "Assamese", "Bengali", "Marathi", "Sindhi", "Maithili", "Punjabi", "Malayalam",
320
+ "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", "Gujarati", "Odia"],
321
+ value="Hindi",
322
+ label="🌐 Translate Response To",
323
+ scale=2
324
+ )
325
+
326
+ # Outputs
327
+ with gr.Row():
328
+ translated_textbox = gr.Textbox(label="🌍 Translated Response", lines=3, interactive=False)
329
+
330
+ prompt_html = gr.HTML(label="📚 Source Documents")
331
+
332
+ # Event handler function - NO .update() calls, return values instead
333
  def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
334
+ """Handler that returns all outputs in correct order"""
335
+
336
+ # Handle empty message
337
  if not message.strip():
338
+ return "", history, "", "", "🔄 *Ready to answer with best available model*"
339
+
340
+ # Generate response with fallback chain
341
+ response, documents, model_used = retrieve_and_generate_response(
342
+ message, cross_encoder_choice, history
343
+ )
344
+
345
+ # Prepare markdown status as STRING (return value, not .update())
346
+ status_emoji = "✅" if model_used != "fallback" else "⚠️"
347
+ model_status_text = f"{status_emoji} *Responded using: **{model_used}***"
348
 
349
+ # Add to chat history
 
350
  history.append([message, response])
351
 
352
  # Translate response
353
  translated_text = translate_text(selected_language, history)
354
 
355
+ # Render HTML template
356
+ prompt_html_content = template_html.render(
357
+ documents=documents,
358
+ query=message,
359
+ model=model_used,
360
+ timestamp=time.strftime("%H:%M:%S")
361
+ )
362
 
363
+ # RETURN ALL VALUES IN EXACT ORDER MATCHING outputs=[...]
364
+ # outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
365
+ return (
366
+ "", # 1. msg: clear input textbox
367
+ history, # 2. chatbot: updated conversation history
368
+ translated_text, # 3. translated_textbox: translation result
369
+ prompt_html_content, # 4. prompt_html: rendered source documents
370
+ model_status_text # 5. model_status: markdown status (STRING)
371
+ )
372
 
373
+ # Bind events - outputs order must match return tuple order
374
+ msg.submit(
375
+ update_chat_and_translate,
376
+ inputs=[msg, chatbot, cross_encoder, language_dropdown],
377
+ outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
378
+ )
379
+
380
+ submit_btn.click(
381
+ update_chat_and_translate,
382
+ inputs=[msg, chatbot, cross_encoder, language_dropdown],
383
+ outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
384
+ )
385
 
386
+ # Clear button - return tuple matching its outputs order: [chatbot, msg, translated_textbox, prompt_html, model_status]
387
+ clear = gr.Button("🗑️ Clear Conversation", variant="secondary")
388
+ clear.click(
389
+ lambda: ([], "", "", "", "🔄 *Ready to answer with best available model*"),
390
+ outputs=[chatbot, msg, translated_textbox, prompt_html, model_status]
391
+ )
392
 
393
  # Example questions
394
  gr.Examples(
395
  examples=[
396
+ "What is the difference between metals and non-metals?",
397
+ "Explain ionic bonding with an example",
398
+ "What is photosynthesis and why is it important?",
399
+ "Describe Newton's first law of motion",
400
+ "How does asexual reproduction work in plants?",
401
+ "What are the layers of the Earth?"
402
  ],
403
  inputs=msg,
404
+ label="💡 Try these science questions:"
405
  )
406
+
407
+ # Footer with technical details (collapsible)
408
+ gr.Markdown("""
409
+ <details>
410
+ <summary>🔧 Technical: Model Fallback Chain (Largest → Smallest)</summary>
411
+
412
+ | Priority | Model | Parameters | Use Case |
413
+ |----------|-------|-----------|----------|
414
+ | 1 | Llama 3.3 70B | ~70B | Complex reasoning, highest accuracy |
415
+ | 2 | Qwen3 32B | ~32B | Strong multilingual & STEM performance |
416
+ | 3 | Llama-4 Scout 17B | ~17B | Balanced speed/quality |
417
+ | 4 | Groq Compound | ~12B | Specialized mixture model |
418
+ | 5 | Gemma2 9B | ~9B | Fast responses, good quality |
419
+ | 6 | Llama 3.1 8B Instant | ~8B | ⚡ Fastest fallback, lowest cost |
420
+
421
+ *If a model hits rate limits or errors, the system automatically tries the next model.*
422
+ </details>
423
+ """)
424
 
425
  if __name__ == "__main__":
426
+ logger.info("🚀 Starting Science Chatbot with multi-model fallback...")
427
+ logger.info(f"Model chain: {[m['name'] for m in MODEL_CHAIN]}")
428
+ demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)# import gradio as gr
429
  # from phi.agent import Agent
430
  # from phi.model.groq import Groq
431
  # import os
 
436
  # from time import perf_counter
437
  # import requests
438
  # from jinja2 import Environment, FileSystemLoader
439
+ # from pathlib import Path
440
 
441
  # # Set up logging
442
  # logging.basicConfig(level=logging.INFO)
 
452
  # os.environ["GROQ_API_KEY"] = api_key
453
 
454
  # # Bhashini API setup
455
+ # bhashini_api_key = os.getenv("API_KEY", "").strip()
456
+ # bhashini_user_id = os.getenv("USER_ID", "").strip()
457
 
458
  # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
459
  # """Translates text from source language to target language using the Bhashini API."""
 
471
  # "userID": bhashini_user_id,
472
  # "ulcaApiKey": bhashini_api_key
473
  # }
474
+ # for key, value in headers.items():
475
+ # if not isinstance(value, str) or '\n' in value or '\r' in value:
476
+ # print(f"Invalid header value for {key}: {value}")
477
+ # return {"status_code": 400, "message": f"Invalid header value for {key}", "translated_content": None}
478
+
479
  # payload = {
480
  # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
481
  # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
 
490
 
491
  # print('Initial request successful, processing response...')
492
  # response_data = response.json()
493
+ # print('Full response data:', response_data)
494
  # if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
495
  # print('Unexpected response structure:', response_data)
496
  # return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
 
526
  # # Initialize PhiData Agent
527
  # agent = Agent(
528
  # name="Science Education Assistant",
529
+ # role="You are a helpful science tutor for 10th-grade students",
530
  # instructions=[
531
+ # "You are an expert science teacher specializing in 10th-grade curriculum.",
532
  # "Provide clear, accurate, and age-appropriate explanations.",
533
  # "Use simple language and examples that students can understand.",
534
  # "Focus on concepts from physics, chemistry, and biology.",
 
538
  # model=Groq(id="llama3-70b-8192", api_key=api_key),
539
  # markdown=True
540
  # )
541
+
542
  # # Set up Jinja2 environment
543
  # proj_dir = Path(__file__).parent
544
  # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
545
+ # template = env.get_template('template.j2') # For document context
546
+ # template_html = env.get_template('template_html.j2') # For HTML output
 
547
 
548
  # # Response Generation Function
549
  # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
 
552
  # top_k_rank = 20
553
 
554
  # if not query.strip():
555
+ # return "Please provide a valid question.", []
556
 
557
  # try:
558
  # start_time = perf_counter()
 
588
  # response_text = response.content if hasattr(response, 'content') else str(response)
589
 
590
  # logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
591
+ # return response_text, documents # Return documents for template
592
 
593
  # except Exception as e:
594
  # logger.error(f"Error in response generation: {e}")
595
+ # return f"Error generating response: {str(e)}", []
596
 
597
  # def simple_chat_function(message, history, cross_encoder_choice):
598
  # """Chat function with semantic search and retriever integration"""
599
  # if not message.strip():
600
+ # return "", history, ""
601
 
602
+ # # Generate response and get documents
603
+ # response, documents = retrieve_and_generate_response(message, cross_encoder_choice, history)
604
 
605
  # # Add to history
606
  # history.append([message, response])
607
 
608
+ # # Render template with documents and query
609
+ # prompt_html = template_html.render(documents=documents, query=message)
610
+
611
+ # return "", history, prompt_html
612
 
613
  # def translate_text(selected_language, history):
614
  # """Translate the last response in history to the selected language."""
 
630
  # # Header section
631
  # with gr.Row():
632
  # with gr.Column(scale=10):
633
+ # 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>""")
634
+ # 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 9 std students</p>""")
635
  # 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>""")
636
  # with gr.Column(scale=3):
637
  # try:
 
676
  # label="Select Language for Translation"
677
  # )
678
  # translated_textbox = gr.Textbox(label="Translated Response")
679
+ # prompt_html = gr.HTML() # Add HTML component for the template
680
 
681
  # # Event handlers
682
  # def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
683
  # if not message.strip():
684
+ # return "", history, "", ""
685
 
686
+ # # Generate response and get documents
687
+ # response, documents = retrieve_and_generate_response(message, cross_encoder_choice, history)
688
  # history.append([message, response])
689
 
690
  # # Translate response
691
  # translated_text = translate_text(selected_language, history)
692
 
693
+ # # Render template with documents and query
694
+ # prompt_html_content = template_html.render(documents=documents, query=message)
695
+
696
+ # return "", history, translated_text, prompt_html_content
697
 
698
+ # msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox, prompt_html])
699
+ # submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox, prompt_html])
700
 
701
  # clear = gr.Button("Clear Conversation")
702
+ # clear.click(lambda: ([], "", "", ""), outputs=[chatbot, msg, translated_textbox, prompt_html])
703
 
704
  # # Example questions
705
  # gr.Examples(
 
716
 
717
  # if __name__ == "__main__":
718
  # demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
719
+ # # from phi.agent import Agent
720
+ # # from phi.model.groq import Groq
721
+ # # import os
722
+ # # import logging
723
+ # # from sentence_transformers import CrossEncoder
724
+ # # from backend.semantic_search import table, retriever
725
+ # # import numpy as np
726
+ # # from time import perf_counter
727
+ # # import requests
728
+ # # from jinja2 import Environment, FileSystemLoader
729
 
730
+ # # # Set up logging
731
+ # # logging.basicConfig(level=logging.INFO)
732
+ # # logger = logging.getLogger(__name__)
733
 
734
+ # # # API Key setup
735
+ # # api_key = os.getenv("GROQ_API_KEY")
736
+ # # if not api_key:
737
+ # # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
738
+ # # logger.error("GROQ_API_KEY not found.")
739
+ # # api_key = "" # Fallback to empty string, but this will fail without a key
740
+ # # else:
741
+ # # os.environ["GROQ_API_KEY"] = api_key
742
 
743
+ # # # Bhashini API setup
744
+ # # bhashini_api_key = os.getenv("API_KEY")
745
+ # # bhashini_user_id = os.getenv("USER_ID")
746
 
747
+ # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
748
+ # # """Translates text from source language to target language using the Bhashini API."""
749
+ # # if not text.strip():
750
+ # # print('Input text is empty. Please provide valid text for translation.')
751
+ # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
752
+ # # else:
753
+ # # print('Input text - ', text)
754
+ # # print(f'Starting translation process from {from_code} to {to_code}...')
755
+ # # gr.Warning(f'Translating to {to_code}...')
756
 
757
+ # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
758
+ # # headers = {
759
+ # # "Content-Type": "application/json",
760
+ # # "userID": bhashini_user_id,
761
+ # # "ulcaApiKey": bhashini_api_key
762
+ # # }
763
+ # # payload = {
764
+ # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
765
+ # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
766
+ # # }
767
 
768
+ # # print('Sending initial request to get the pipeline...')
769
+ # # response = requests.post(url, json=payload, headers=headers)
770
 
771
+ # # if response.status_code != 200:
772
+ # # print(f'Error in initial request: {response.status_code}, Response: {response.text}')
773
+ # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
774
 
775
+ # # print('Initial request successful, processing response...')
776
+ # # response_data = response.json()
777
+ # # print('Full response data:', response_data) # Debug the full response
778
+ # # if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
779
+ # # print('Unexpected response structure:', response_data)
780
+ # # return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
781
 
782
+ # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
783
+ # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
784
 
785
+ # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
786
 
787
+ # # headers2 = {
788
+ # # "Content-Type": "application/json",
789
+ # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
790
+ # # }
791
+ # # compute_payload = {
792
+ # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
793
+ # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
794
+ # # }
795
 
796
+ # # print(f'Sending translation request with text: "{text}"')
797
+ # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
798
 
799
+ # # if compute_response.status_code != 200:
800
+ # # print(f'Error in translation request: {compute_response.status_code}, Response: {compute_response.text}')
801
+ # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
802
 
803
+ # # print('Translation request successful, processing translation...')
804
+ # # compute_response_data = compute_response.json()
805
+ # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
806
 
807
+ # # print(f'Translation successful. Translated content: "{translated_content}"')
808
+ # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
809
 
810
+ # # # Initialize PhiData Agent
811
+ # # agent = Agent(
812
+ # # name="Science Education Assistant",
813
+ # # role="You are a helpful science tutor for 9th-grade students",
814
+ # # instructions=[
815
+ # # "You are an expert science teacher specializing in 9th-grade curriculum.",
816
+ # # "Provide clear, accurate, and age-appropriate explanations.",
817
+ # # "Use simple language and examples that students can understand.",
818
+ # # "Focus on concepts from physics, chemistry, and biology.",
819
+ # # "Structure responses with headings and bullet points when helpful.",
820
+ # # "Encourage learning and curiosity."
821
+ # # ],
822
+ # # model=Groq(id="llama3-70b-8192", api_key=api_key),
823
+ # # markdown=True
824
+ # # )
825
+ # # # Set up Jinja2 environment
826
+ # # proj_dir = Path(__file__).parent
827
+ # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
828
 
829
+
830
+ # # template_html = env.get_template('template_html.j2')
831
+
832
+ # # # Response Generation Function
833
+ # # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
834
+ # # """Generate response using semantic search and LLM"""
835
+ # # top_rerank = 25
836
+ # # top_k_rank = 20
837
 
838
+ # # if not query.strip():
839
+ # # return "Please provide a valid question."
840
 
841
+ # # try:
842
+ # # start_time = perf_counter()
843
 
844
+ # # # Encode query and search documents
845
+ # # query_vec = retriever.encode(query)
846
+ # # documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
847
+ # # documents = [doc["text"] for doc in documents]
848
 
849
+ # # # Re-rank documents using cross-encoder
850
+ # # 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')
851
+ # # query_doc_pair = [[query, doc] for doc in documents]
852
+ # # cross_scores = cross_encoder_model.predict(query_doc_pair)
853
+ # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
854
+ # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
855
 
856
+ # # # Create context from top documents
857
+ # # context = "\n\n".join(documents[:10]) if documents else ""
858
+ # # context = f"Context information from educational materials:\n{context}\n\n"
859
 
860
+ # # # Add conversation history for context
861
+ # # history_context = ""
862
+ # # if history and len(history) > 0:
863
+ # # for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
864
+ # # if user_msg and bot_msg:
865
+ # # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
866
 
867
+ # # # Create full prompt
868
+ # # 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."
869
 
870
+ # # # Generate response
871
+ # # response = agent.run(full_prompt)
872
+ # # response_text = response.content if hasattr(response, 'content') else str(response)
873
 
874
+ # # logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
875
+ # # return response_text
876
 
877
+ # # except Exception as e:
878
+ # # logger.error(f"Error in response generation: {e}")
879
+ # # return f"Error generating response: {str(e)}"
880
 
881
+ # # def simple_chat_function(message, history, cross_encoder_choice):
882
+ # # """Chat function with semantic search and retriever integration"""
883
+ # # if not message.strip():
884
+ # # return "", history
885
 
886
+ # # # Generate response using the semantic search function
887
+ # # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
888
 
889
+ # # # Add to history
890
+ # # history.append([message, response])
891
 
892
+ # # return "", history
893
 
894
+ # # def translate_text(selected_language, history):
895
+ # # """Translate the last response in history to the selected language."""
896
+ # # iso_language_codes = {
897
+ # # "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
898
+ # # "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
899
+ # # "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
900
+ # # "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
901
+ # # }
902
 
903
+ # # to_code = iso_language_codes[selected_language]
904
+ # # response_text = history[-1][1] if history and history[-1][1] else ''
905
+ # # print('response_text for translation', response_text)
906
+ # # translation = bhashini_translate(response_text, to_code=to_code)
907
+ # # return translation.get('translated_content', 'Translation failed.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
908
 
909
+ # # # Gradio Interface with layout template
910
+ # # with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
911
+ # # # Header section
912
+ # # with gr.Row():
913
+ # # with gr.Column(scale=10):
914
+ # # 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 9TH SCIENCE WITH TRANSLATION IN 22 LANGUAGES</span></h1></div>""")
915
+ # # 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>""")
916
+ # # 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>""")
917
+ # # with gr.Column(scale=3):
918
+ # # try:
919
+ # # gr.Image(value='logo.png', height=200, width=200)
920
+ # # except:
921
+ # # gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
922
 
923
+ # # # Chat and input components
924
+ # # chatbot = gr.Chatbot(
925
+ # # [],
926
+ # # elem_id="chatbot",
927
+ # # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
928
+ # # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
929
+ # # bubble_full_width=False,
930
+ # # show_copy_button=True,
931
+ # # show_share_button=True,
932
+ # # )
933
 
934
+ # # with gr.Row():
935
+ # # msg = gr.Textbox(
936
+ # # scale=3,
937
+ # # show_label=False,
938
+ # # placeholder="Enter text and press enter",
939
+ # # container=False,
940
+ # # )
941
+ # # submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
942
+
943
+ # # # Additional controls
944
+ # # cross_encoder = gr.Radio(
945
+ # # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
946
+ # # value='(ACCURATE) BGE reranker',
947
+ # # label="Embeddings Model",
948
+ # # info="Select the model for document ranking"
949
+ # # )
950
+ # # language_dropdown = gr.Dropdown(
951
+ # # choices=[
952
+ # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
953
+ # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
954
+ # # "Gujarati", "Odia"
955
+ # # ],
956
+ # # value="Hindi",
957
+ # # label="Select Language for Translation"
958
+ # # )
959
+ # # translated_textbox = gr.Textbox(label="Translated Response")
960
 
961
+ # # # Event handlers
962
+ # # def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
963
+ # # if not message.strip():
964
+ # # return "", history, ""
965
 
966
+ # # # Generate response
967
+ # # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
968
+ # # history.append([message, response])
969
 
970
+ # # # Translate response
971
+ # # translated_text = translate_text(selected_language, history)
972
 
973
+ # # return "", history, translated_text
974
+
975
+ # # msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
976
+ # # submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
977
+
978
+ # # clear = gr.Button("Clear Conversation")
979
+ # # clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
980
+
981
+ # # # Example questions
982
+ # # gr.Examples(
983
+ # # examples=[
984
+ # # 'What is the difference between metals and non-metals?',
985
+ # # 'What is an ionic bond?',
986
+ # # 'Explain asexual reproduction',
987
+ # # 'What is photosynthesis?',
988
+ # # 'Explain Newton\'s laws of motion'
989
+ # # ],
990
+ # # inputs=msg,
991
+ # # label="Try these example questions:"
992
+ # # )
 
 
 
993
 
994
+ # # if __name__ == "__main__":
995
+ # # demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
996
  # # import gradio as gr
997
+ # # from phi.agent import Agent
998
+ # # from phi.model.groq import Groq
999
+ # # import os
1000
  # # import logging
 
 
1001
  # # from sentence_transformers import CrossEncoder
 
 
 
 
 
1002
  # # from backend.semantic_search import table, retriever
1003
+ # # import numpy as np
1004
+ # # from time import perf_counter
1005
+ # # import requests
1006
 
1007
+ # # # Set up logging
1008
+ # # logging.basicConfig(level=logging.INFO)
1009
+ # # logger = logging.getLogger(__name__)
1010
 
1011
+ # # # API Key setup
1012
+ # # api_key = os.getenv("GROQ_API_KEY")
1013
+ # # if not api_key:
1014
+ # # gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
1015
+ # # logger.error("GROQ_API_KEY not found.")
1016
+ # # api_key = "" # Fallback to empty string, but this will fail without a key
1017
+ # # else:
1018
+ # # os.environ["GROQ_API_KEY"] = api_key
1019
+
1020
+ # # # Bhashini API setup
1021
+ # # bhashini_api_key = os.getenv("API_KEY")
1022
+ # # bhashini_user_id = os.getenv("USER_ID")
1023
 
1024
  # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
1025
  # # """Translates text from source language to target language using the Bhashini API."""
 
1026
  # # if not text.strip():
1027
  # # print('Input text is empty. Please provide valid text for translation.')
1028
+ # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
1029
  # # else:
1030
+ # # print('Input text - ', text)
 
1031
  # # print(f'Starting translation process from {from_code} to {to_code}...')
1032
  # # gr.Warning(f'Translating to {to_code}...')
1033
 
1034
  # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
1035
  # # headers = {
1036
  # # "Content-Type": "application/json",
1037
+ # # "userID": bhashini_user_id,
1038
+ # # "ulcaApiKey": bhashini_api_key
1039
  # # }
1040
  # # payload = {
1041
  # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
 
1046
  # # response = requests.post(url, json=payload, headers=headers)
1047
 
1048
  # # if response.status_code != 200:
1049
+ # # print(f'Error in initial request: {response.status_code}, Response: {response.text}')
1050
  # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
1051
 
1052
  # # print('Initial request successful, processing response...')
1053
  # # response_data = response.json()
1054
+ # # print('Full response data:', response_data) # Debug the full response
1055
+ # # if "pipelineInferenceAPIEndPoint" not in response_data or "callbackUrl" not in response_data["pipelineInferenceAPIEndPoint"]:
1056
+ # # print('Unexpected response structure:', response_data)
1057
+ # # return {"status_code": 400, "message": "Unexpected API response structure", "translated_content": None}
1058
+
1059
  # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
1060
  # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
1061
 
 
1074
  # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
1075
 
1076
  # # if compute_response.status_code != 200:
1077
+ # # print(f'Error in translation request: {compute_response.status_code}, Response: {compute_response.text}')
1078
  # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
1079
 
1080
  # # print('Translation request successful, processing translation...')
 
1084
  # # print(f'Translation successful. Translated content: "{translated_content}"')
1085
  # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
1086
 
1087
+ # # # Initialize PhiData Agent
1088
+ # # agent = Agent(
1089
+ # # name="Science Education Assistant",
1090
+ # # role="You are a helpful science tutor for 10th-grade students",
1091
+ # # instructions=[
1092
+ # # "You are an expert science teacher specializing in 10th-grade curriculum.",
1093
+ # # "Provide clear, accurate, and age-appropriate explanations.",
1094
+ # # "Use simple language and examples that students can understand.",
1095
+ # # "Focus on concepts from physics, chemistry, and biology.",
1096
+ # # "Structure responses with headings and bullet points when helpful.",
1097
+ # # "Encourage learning and curiosity."
1098
+ # # ],
1099
+ # # model=Groq(id="llama3-70b-8192", api_key=api_key),
1100
+ # # markdown=True
1101
+ # # )
1102
+
1103
+ # # # Response Generation Function
1104
+ # # def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
1105
+ # # """Generate response using semantic search and LLM"""
 
 
 
1106
  # # top_rerank = 25
1107
  # # top_k_rank = 20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1108
 
1109
+ # # if not query.strip():
1110
+ # # return "Please provide a valid question."
 
 
 
 
 
 
1111
 
1112
+ # # try:
1113
+ # # start_time = perf_counter()
1114
+
1115
+ # # # Encode query and search documents
1116
  # # query_vec = retriever.encode(query)
1117
+ # # documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
1118
+ # # documents = [doc["text"] for doc in documents]
 
 
 
 
 
 
 
 
1119
 
1120
+ # # # Re-rank documents using cross-encoder
1121
+ # # 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')
1122
+ # # query_doc_pair = [[query, doc] for doc in documents]
1123
+ # # cross_scores = cross_encoder_model.predict(query_doc_pair)
1124
  # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
 
1125
  # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1126
 
1127
+ # # # Create context from top documents
1128
+ # # context = "\n\n".join(documents[:10]) if documents else ""
1129
+ # # context = f"Context information from educational materials:\n{context}\n\n"
 
1130
 
1131
+ # # # Add conversation history for context
1132
+ # # history_context = ""
1133
+ # # if history and len(history) > 0:
1134
+ # # for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
1135
+ # # if user_msg and bot_msg:
1136
+ # # history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
1137
+
1138
+ # # # Create full prompt
1139
+ # # 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."
1140
+
1141
+ # # # Generate response
1142
+ # # response = agent.run(full_prompt)
1143
+ # # response_text = response.content if hasattr(response, 'content') else str(response)
1144
+
1145
+ # # logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
1146
+ # # return response_text
1147
+
1148
+ # # except Exception as e:
1149
+ # # logger.error(f"Error in response generation: {e}")
1150
+ # # return f"Error generating response: {str(e)}"
1151
 
1152
+ # # def simple_chat_function(message, history, cross_encoder_choice):
1153
+ # # """Chat function with semantic search and retriever integration"""
1154
+ # # if not message.strip():
1155
+ # # return "", history
1156
+
1157
+ # # # Generate response using the semantic search function
1158
+ # # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
1159
 
1160
+ # # # Add to history
1161
+ # # history.append([message, response])
1162
 
1163
+ # # return "", history
1164
+
1165
+ # # def translate_text(selected_language, history):
1166
+ # # """Translate the last response in history to the selected language."""
1167
  # # iso_language_codes = {
1168
+ # # "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
1169
+ # # "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
1170
+ # # "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
1171
+ # # "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1172
  # # }
1173
 
1174
  # # to_code = iso_language_codes[selected_language]
1175
+ # # response_text = history[-1][1] if history and history[-1][1] else ''
1176
+ # # print('response_text for translation', response_text)
1177
  # # translation = bhashini_translate(response_text, to_code=to_code)
1178
+ # # return translation.get('translated_content', 'Translation failed.')
 
1179
 
1180
+ # # # Gradio Interface with layout template
1181
+ # # with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
1182
+ # # # Header section
1183
  # # with gr.Row():
1184
  # # with gr.Column(scale=10):
1185
+ # # 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>""")
1186
  # # 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>""")
1187
  # # 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>""")
 
1188
  # # with gr.Column(scale=3):
1189
+ # # try:
1190
+ # # gr.Image(value='logo.png', height=200, width=200)
1191
+ # # except:
1192
+ # # gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
1193
 
1194
+ # # # Chat and input components
1195
  # # chatbot = gr.Chatbot(
1196
  # # [],
1197
  # # elem_id="chatbot",
 
1203
  # # )
1204
 
1205
  # # with gr.Row():
1206
+ # # msg = gr.Textbox(
1207
  # # scale=3,
1208
  # # show_label=False,
1209
  # # placeholder="Enter text and press enter",
1210
  # # container=False,
1211
  # # )
1212
+ # # submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
1213
+
1214
+ # # # Additional controls
1215
+ # # cross_encoder = gr.Radio(
1216
+ # # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
1217
+ # # value='(ACCURATE) BGE reranker',
1218
+ # # label="Embeddings Model",
1219
+ # # info="Select the model for document ranking"
1220
+ # # )
1221
  # # language_dropdown = gr.Dropdown(
1222
  # # choices=[
1223
  # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1224
  # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1225
  # # "Gujarati", "Odia"
1226
  # # ],
1227
+ # # value="Hindi",
1228
  # # label="Select Language for Translation"
1229
  # # )
 
 
 
1230
  # # translated_textbox = gr.Textbox(label="Translated Response")
1231
+
1232
+ # # # Event handlers
1233
+ # # def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
1234
+ # # if not message.strip():
1235
+ # # return "", history, ""
1236
+
1237
+ # # # Generate response
1238
+ # # response = retrieve_and_generate_response(message, cross_encoder_choice, history)
1239
+ # # history.append([message, response])
1240
+
1241
+ # # # Translate response
1242
+ # # translated_text = translate_text(selected_language, history)
1243
+
1244
+ # # return "", history, translated_text
1245
+
1246
+ # # msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
1247
+ # # submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
1248
+
1249
+ # # clear = gr.Button("Clear Conversation")
1250
+ # # clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
1251
+
1252
+ # # # Example questions
1253
+ # # gr.Examples(
1254
+ # # examples=[
1255
+ # # 'What is the difference between metals and non-metals?',
1256
+ # # 'What is an ionic bond?',
1257
+ # # 'Explain asexual reproduction',
1258
+ # # 'What is photosynthesis?',
1259
+ # # 'Explain Newton\'s laws of motion'
1260
+ # # ],
1261
+ # # inputs=msg,
1262
+ # # label="Try these example questions:"
1263
+ # # )
1264
+
1265
+ # # if __name__ == "__main__":
1266
+ # # demo.launch(server_name="0.0.0.0", server_port=7860)
1267
+
1268
+ # # 1f# import gradio as gr# import requests
1269
+ # # # import gradio as gr
1270
+ # # # from ragatouille import RAGPretrainedModel
1271
+ # # # import logging
1272
+ # # # from pathlib import Path
1273
+ # # # from time import perf_counter
1274
+ # # # from sentence_transformers import CrossEncoder
1275
+ # # # from huggingface_hub import InferenceClient
1276
+ # # # from jinja2 import Environment, FileSystemLoader
1277
+ # # # import numpy as np
1278
+ # # # from os import getenv
1279
+ # # # from backend.query_llm import generate_hf, generate_qwen
1280
+ # # # from backend.semantic_search import table, retriever
1281
+ # # # from huggingface_hub import InferenceClient
1282
+
1283
+
1284
+ # # # # Bhashini API translation function
1285
+ # # # api_key = getenv('API_KEY')
1286
+ # # # user_id = getenv('USER_ID')
1287
+
1288
+ # # # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
1289
+ # # # """Translates text from source language to target language using the Bhashini API."""
1290
+
1291
+ # # # if not text.strip():
1292
+ # # # print('Input text is empty. Please provide valid text for translation.')
1293
+ # # # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None}
1294
+ # # # else:
1295
+ # # # print('Input text - ',text)
1296
+ # # # print(f'Starting translation process from {from_code} to {to_code}...')
1297
+ # # # print(f'Starting translation process from {from_code} to {to_code}...')
1298
+ # # # gr.Warning(f'Translating to {to_code}...')
1299
+
1300
+ # # # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
1301
+ # # # headers = {
1302
+ # # # "Content-Type": "application/json",
1303
+ # # # "userID": user_id,
1304
+ # # # "ulcaApiKey": api_key
1305
+ # # # }
1306
+ # # # payload = {
1307
+ # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
1308
+ # # # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
1309
+ # # # }
1310
+
1311
+ # # # print('Sending initial request to get the pipeline...')
1312
+ # # # response = requests.post(url, json=payload, headers=headers)
1313
+
1314
+ # # # if response.status_code != 200:
1315
+ # # # print(f'Error in initial request: {response.status_code}')
1316
+ # # # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
1317
+
1318
+ # # # print('Initial request successful, processing response...')
1319
+ # # # response_data = response.json()
1320
+ # # # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
1321
+ # # # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
1322
+
1323
+ # # # print(f'Service ID: {service_id}, Callback URL: {callback_url}')
1324
+
1325
+ # # # headers2 = {
1326
+ # # # "Content-Type": "application/json",
1327
+ # # # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
1328
+ # # # }
1329
+ # # # compute_payload = {
1330
+ # # # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
1331
+ # # # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
1332
+ # # # }
1333
+
1334
+ # # # print(f'Sending translation request with text: "{text}"')
1335
+ # # # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
1336
+
1337
+ # # # if compute_response.status_code != 200:
1338
+ # # # print(f'Error in translation request: {compute_response.status_code}')
1339
+ # # # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
1340
+
1341
+ # # # print('Translation request successful, processing translation...')
1342
+ # # # compute_response_data = compute_response.json()
1343
+ # # # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
1344
+
1345
+ # # # print(f'Translation successful. Translated content: "{translated_content}"')
1346
+ # # # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
1347
+
1348
+
1349
+ # # # # Existing chatbot functions
1350
+ # # # VECTOR_COLUMN_NAME = "vector"
1351
+ # # # TEXT_COLUMN_NAME = "text"
1352
+ # # # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN")
1353
+ # # # proj_dir = Path(__file__).parent
1354
+
1355
+ # # # logging.basicConfig(level=logging.INFO)
1356
+ # # # logger = logging.getLogger(__name__)
1357
+ # # # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN)
1358
+ # # # env = Environment(loader=FileSystemLoader(proj_dir / 'templates'))
1359
+
1360
+ # # # template = env.get_template('template.j2')
1361
+ # # # template_html = env.get_template('template_html.j2')
1362
+
1363
+ # # # # def add_text(history, text):
1364
+ # # # # history = [] if history is None else history
1365
+ # # # # history = history + [(text, None)]
1366
+ # # # # return history, gr.Textbox(value="", interactive=False)
1367
+
1368
+ # # # def bot(history, cross_encoder):
1369
+
1370
+ # # # top_rerank = 25
1371
+ # # # top_k_rank = 20
1372
+ # # # query = history[-1][0] if history else ''
1373
+ # # # print('\nQuery: ',query )
1374
+ # # # print('\nHistory:',history)
1375
+ # # # if not query:
1376
+ # # # gr.Warning("Please submit a non-empty string as a prompt")
1377
+ # # # raise ValueError("Empty string was submitted")
1378
+
1379
+ # # # logger.warning('Retrieving documents...')
1380
+
1381
+ # # # if cross_encoder == '(HIGH ACCURATE) ColBERT':
1382
+ # # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait')
1383
+ # # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0")
1384
+ # # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index')
1385
+ # # # documents_full = RAG_db.search(query, k=top_k_rank)
1386
+
1387
+ # # # documents = [item['content'] for item in documents_full]
1388
+ # # # prompt = template.render(documents=documents, query=query)
1389
+ # # # prompt_html = template_html.render(documents=documents, query=query)
1390
+
1391
+ # # # generate_fn = generate_hf
1392
+
1393
+ # # # history[-1][1] = ""
1394
+ # # # for character in generate_fn(prompt, history[:-1]):
1395
+ # # # history[-1][1] = character
1396
+ # # # yield history, prompt_html
1397
+ # # # else:
1398
+ # # # document_start = perf_counter()
1399
+
1400
+ # # # query_vec = retriever.encode(query)
1401
+ # # # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank)
1402
+
1403
+ # # # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list()
1404
+ # # # documents = [doc[TEXT_COLUMN_NAME] for doc in documents]
1405
+
1406
+ # # # query_doc_pair = [[query, doc] for doc in documents]
1407
+ # # # if cross_encoder == '(FAST) MiniLM-L6v2':
1408
+ # # # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
1409
+ # # # elif cross_encoder == '(ACCURATE) BGE reranker':
1410
+ # # # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base')
1411
+
1412
+ # # # cross_scores = cross_encoder1.predict(query_doc_pair)
1413
+ # # # sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
1414
+
1415
+ # # # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
1416
+
1417
+ # # # document_time = perf_counter() - document_start
1418
+
1419
+ # # # prompt = template.render(documents=documents, query=query)
1420
+ # # # prompt_html = template_html.render(documents=documents, query=query)
1421
+
1422
+ # # # #generate_fn = generate_hf
1423
+ # # # generate_fn=generate_qwen
1424
+ # # # # Create a new history entry instead of modifying the tuple directly
1425
+ # # # new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt
1426
+ # # # output=''
1427
+ # # # # for character in generate_fn(prompt, history[:-1]):
1428
+ # # # # #new_history[-1] = (query, character)
1429
+ # # # # output+=character
1430
+ # # # output=generate_fn(prompt, history[:-1])
1431
+
1432
+ # # # print('Output:',output)
1433
+ # # # new_history[-1] = (prompt, output) #query replaced with prompt
1434
+ # # # print('New History',new_history)
1435
+ # # # #print('prompt html',prompt_html)# Update the last tuple with new text
1436
+
1437
+ # # # history_list = list(history[-1])
1438
+ # # # history_list[1] = output # Assuming `character` is what you want to assign
1439
+ # # # # Update the history with the modified list converted back to a tuple
1440
+ # # # history[-1] = tuple(history_list)
1441
+
1442
+ # # # #history[-1][1] = character
1443
+ # # # # yield new_history, prompt_html
1444
+ # # # yield history, prompt_html
1445
+ # # # # new_history,prompt_html
1446
+ # # # # history[-1][1] = ""
1447
+ # # # # for character in generate_fn(prompt, history[:-1]):
1448
+ # # # # history[-1][1] = character
1449
+ # # # # yield history, prompt_html
1450
+
1451
+ # # # #def translate_text(response_text, selected_language):
1452
+
1453
+ # # # def translate_text(selected_language,history):
1454
+
1455
+ # # # iso_language_codes = {
1456
+ # # # "Hindi": "hi",
1457
+ # # # "Gom": "gom",
1458
+ # # # "Kannada": "kn",
1459
+ # # # "Dogri": "doi",
1460
+ # # # "Bodo": "brx",
1461
+ # # # "Urdu": "ur",
1462
+ # # # "Tamil": "ta",
1463
+ # # # "Kashmiri": "ks",
1464
+ # # # "Assamese": "as",
1465
+ # # # "Bengali": "bn",
1466
+ # # # "Marathi": "mr",
1467
+ # # # "Sindhi": "sd",
1468
+ # # # "Maithili": "mai",
1469
+ # # # "Punjabi": "pa",
1470
+ # # # "Malayalam": "ml",
1471
+ # # # "Manipuri": "mni",
1472
+ # # # "Telugu": "te",
1473
+ # # # "Sanskrit": "sa",
1474
+ # # # "Nepali": "ne",
1475
+ # # # "Santali": "sat",
1476
+ # # # "Gujarati": "gu",
1477
+ # # # "Odia": "or"
1478
+ # # # }
1479
+
1480
+ # # # to_code = iso_language_codes[selected_language]
1481
+ # # # response_text = history[-1][1] if history else ''
1482
+ # # # print('response_text for translation',response_text)
1483
+ # # # translation = bhashini_translate(response_text, to_code=to_code)
1484
+ # # # return translation['translated_content']
1485
+
1486
+
1487
+ # # # # Gradio interface
1488
+ # # # with gr.Blocks(theme='gradio/soft') as CHATBOT:
1489
+ # # # history_state = gr.State([])
1490
+ # # # with gr.Row():
1491
+ # # # with gr.Column(scale=10):
1492
+ # # # 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>""")
1493
+ # # # 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>""")
1494
+ # # # 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>""")
1495
+
1496
+ # # # with gr.Column(scale=3):
1497
+ # # # gr.Image(value='logo.png', height=200, width=200)
1498
+
1499
+ # # # chatbot = gr.Chatbot(
1500
+ # # # [],
1501
+ # # # elem_id="chatbot",
1502
+ # # # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
1503
+ # # # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
1504
+ # # # bubble_full_width=False,
1505
+ # # # show_copy_button=True,
1506
+ # # # show_share_button=True,
1507
+ # # # )
1508
+
1509
+ # # # with gr.Row():
1510
+ # # # txt = gr.Textbox(
1511
+ # # # scale=3,
1512
+ # # # show_label=False,
1513
+ # # # placeholder="Enter text and press enter",
1514
+ # # # container=False,
1515
+ # # # )
1516
+ # # # txt_btn = gr.Button(value="Submit text", scale=1)
1517
+
1518
+ # # # 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)")
1519
+ # # # language_dropdown = gr.Dropdown(
1520
+ # # # choices=[
1521
+ # # # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
1522
+ # # # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
1523
+ # # # "Gujarati", "Odia"
1524
+ # # # ],
1525
+ # # # value="Hindi", # default to Hindi
1526
+ # # # label="Select Language for Translation"
1527
+ # # # )
1528
+
1529
+ # # # prompt_html = gr.HTML()
1530
+
1531
+ # # # translated_textbox = gr.Textbox(label="Translated Response")
1532
+ # # # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown):
1533
+ # # # print('History state',history_state)
1534
+ # # # history = history_state
1535
+ # # # history.append((txt, ""))
1536
+ # # # #history_state.value=(history)
1537
 
1538
+ # # # # Call bot function
1539
+ # # # # bot_output = list(bot(history, cross_encoder))
1540
+ # # # bot_output = next(bot(history, cross_encoder))
1541
+ # # # print('bot_output',bot_output)
1542
+ # # # #history, prompt_html = bot_output[-1]
1543
+ # # # history, prompt_html = bot_output
1544
+ # # # print('History',history)
1545
+ # # # # Update the history state
1546
+ # # # history_state[:] = history
1547
 
1548
+ # # # # Translate text
1549
+ # # # translated_text = translate_text(language_dropdown, history)
1550
+ # # # return history, prompt_html, translated_text
1551
 
1552
+ # # # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1553
+ # # # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox])
1554
 
1555
+ # # # examples = ['WHAT IS DIFFERENCES BETWEEN HOMOGENOUS AND HETEROGENOUS MIXTURE?','WHAT IS COVALENT BOND?',
1556
+ # # # 'EXPLAIN GOLGI APPARATUS']
1557
 
1558
+ # # # gr.Examples(examples, txt)
1559
 
1560
 
1561
+ # # # # Launch the Gradio application
1562
+ # # # CHATBOT.launch(share=True,debug=True)
1563