Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from sentence_transformers import CrossEncoder
|
|
7 |
from backend.semantic_search import table, retriever
|
8 |
import numpy as np
|
9 |
from time import perf_counter
|
|
|
10 |
|
11 |
# Set up logging
|
12 |
logging.basicConfig(level=logging.INFO)
|
@@ -21,6 +22,68 @@ if not api_key:
|
|
21 |
else:
|
22 |
os.environ["GROQ_API_KEY"] = api_key
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Initialize PhiData Agent
|
25 |
agent = Agent(
|
26 |
name="Science Education Assistant",
|
@@ -99,25 +162,231 @@ def simple_chat_function(message, history, cross_encoder_choice):
|
|
99 |
|
100 |
return "", history
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
cross_encoder = gr.Radio(
|
106 |
choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
|
107 |
value='(ACCURATE) BGE reranker',
|
108 |
label="Embeddings Model",
|
109 |
info="Select the model for document ranking"
|
110 |
)
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
msg.submit(
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
if __name__ == "__main__":
|
120 |
-
demo.launch()# import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
# from phi.agent import Agent
|
122 |
# from phi.model.groq import Groq
|
123 |
# import os
|
|
|
7 |
from backend.semantic_search import table, retriever
|
8 |
import numpy as np
|
9 |
from time import perf_counter
|
10 |
+
import requests
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
|
|
22 |
else:
|
23 |
os.environ["GROQ_API_KEY"] = api_key
|
24 |
|
25 |
+
# Bhashini API setup
|
26 |
+
bhashini_api_key = os.getenv("API_KEY")
|
27 |
+
bhashini_user_id = os.getenv("USER_ID")
|
28 |
+
|
29 |
+
def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
|
30 |
+
"""Translates text from source language to target language using the Bhashini API."""
|
31 |
+
if not text.strip():
|
32 |
+
print('Input text is empty. Please provide valid text for translation.')
|
33 |
+
return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
|
34 |
+
else:
|
35 |
+
print('Input text - ', text)
|
36 |
+
print(f'Starting translation process from {from_code} to {to_code}...')
|
37 |
+
gr.Warning(f'Translating to {to_code}...')
|
38 |
+
|
39 |
+
url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
|
40 |
+
headers = {
|
41 |
+
"Content-Type": "application/json",
|
42 |
+
"userID": bhashini_user_id,
|
43 |
+
"ulcaApiKey": bhashini_api_key
|
44 |
+
}
|
45 |
+
payload = {
|
46 |
+
"pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}],
|
47 |
+
"pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"}
|
48 |
+
}
|
49 |
+
|
50 |
+
print('Sending initial request to get the pipeline...')
|
51 |
+
response = requests.post(url, json=payload, headers=headers)
|
52 |
+
|
53 |
+
if response.status_code != 200:
|
54 |
+
print(f'Error in initial request: {response.status_code}')
|
55 |
+
return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None}
|
56 |
+
|
57 |
+
print('Initial request successful, processing response...')
|
58 |
+
response_data = response.json()
|
59 |
+
service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"]
|
60 |
+
callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"]
|
61 |
+
|
62 |
+
print(f'Service ID: {service_id}, Callback URL: {callback_url}')
|
63 |
+
|
64 |
+
headers2 = {
|
65 |
+
"Content-Type": "application/json",
|
66 |
+
response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"]
|
67 |
+
}
|
68 |
+
compute_payload = {
|
69 |
+
"pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}],
|
70 |
+
"inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]}
|
71 |
+
}
|
72 |
+
|
73 |
+
print(f'Sending translation request with text: "{text}"')
|
74 |
+
compute_response = requests.post(callback_url, json=compute_payload, headers=headers2)
|
75 |
+
|
76 |
+
if compute_response.status_code != 200:
|
77 |
+
print(f'Error in translation request: {compute_response.status_code}')
|
78 |
+
return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None}
|
79 |
+
|
80 |
+
print('Translation request successful, processing translation...')
|
81 |
+
compute_response_data = compute_response.json()
|
82 |
+
translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"]
|
83 |
+
|
84 |
+
print(f'Translation successful. Translated content: "{translated_content}"')
|
85 |
+
return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content}
|
86 |
+
|
87 |
# Initialize PhiData Agent
|
88 |
agent = Agent(
|
89 |
name="Science Education Assistant",
|
|
|
162 |
|
163 |
return "", history
|
164 |
|
165 |
+
def translate_text(selected_language, history):
|
166 |
+
"""Translate the last response in history to the selected language."""
|
167 |
+
iso_language_codes = {
|
168 |
+
"Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur",
|
169 |
+
"Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr",
|
170 |
+
"Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni",
|
171 |
+
"Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or"
|
172 |
+
}
|
173 |
+
|
174 |
+
to_code = iso_language_codes[selected_language]
|
175 |
+
response_text = history[-1][1] if history and history[-1][1] else ''
|
176 |
+
print('response_text for translation', response_text)
|
177 |
+
translation = bhashini_translate(response_text, to_code=to_code)
|
178 |
+
return translation.get('translated_content', 'Translation failed.')
|
179 |
+
|
180 |
+
# Gradio Interface with layout template
|
181 |
+
with gr.Blocks(title="Science Chatbot", theme='gradio/soft') as demo:
|
182 |
+
# Header section
|
183 |
+
with gr.Row():
|
184 |
+
with gr.Column(scale=10):
|
185 |
+
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>""")
|
186 |
+
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>""")
|
187 |
+
gr.HTML(value=f"""<p style="font-family: Arial, sans-serif; font-size: 14px;"> Suggestions may be sent to <a href="mailto:[email protected]" style="color: #00008B; font-style: italic;">[email protected]</a>.</p>""")
|
188 |
+
with gr.Column(scale=3):
|
189 |
+
try:
|
190 |
+
gr.Image(value='logo.png', height=200, width=200)
|
191 |
+
except:
|
192 |
+
gr.HTML("<div style='height: 200px; width: 200px; background-color: #f0f0f0; display: flex; align-items: center; justify-content: center;'>Logo</div>")
|
193 |
+
|
194 |
+
# Chat and input components
|
195 |
+
chatbot = gr.Chatbot(
|
196 |
+
[],
|
197 |
+
elem_id="chatbot",
|
198 |
+
avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg',
|
199 |
+
'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'),
|
200 |
+
bubble_full_width=False,
|
201 |
+
show_copy_button=True,
|
202 |
+
show_share_button=True,
|
203 |
+
)
|
204 |
+
|
205 |
+
with gr.Row():
|
206 |
+
msg = gr.Textbox(
|
207 |
+
scale=3,
|
208 |
+
show_label=False,
|
209 |
+
placeholder="Enter text and press enter",
|
210 |
+
container=False,
|
211 |
+
)
|
212 |
+
submit_btn = gr.Button(value="Submit text", scale=1, variant="primary")
|
213 |
+
|
214 |
+
# Additional controls
|
215 |
cross_encoder = gr.Radio(
|
216 |
choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
|
217 |
value='(ACCURATE) BGE reranker',
|
218 |
label="Embeddings Model",
|
219 |
info="Select the model for document ranking"
|
220 |
)
|
221 |
+
language_dropdown = gr.Dropdown(
|
222 |
+
choices=[
|
223 |
+
"Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi",
|
224 |
+
"Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali",
|
225 |
+
"Gujarati", "Odia"
|
226 |
+
],
|
227 |
+
value="Hindi",
|
228 |
+
label="Select Language for Translation"
|
229 |
+
)
|
230 |
+
translated_textbox = gr.Textbox(label="Translated Response")
|
231 |
+
|
232 |
+
# Event handlers
|
233 |
+
def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
|
234 |
+
if not message.strip():
|
235 |
+
return "", history, ""
|
236 |
+
|
237 |
+
# Generate response
|
238 |
+
response = retrieve_and_generate_response(message, cross_encoder_choice, history)
|
239 |
+
history.append([message, response])
|
240 |
+
|
241 |
+
# Translate response
|
242 |
+
translated_text = translate_text(selected_language, history)
|
243 |
+
|
244 |
+
return "", history, translated_text
|
245 |
|
246 |
+
msg.submit(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
|
247 |
+
submit_btn.click(update_chat_and_translate, [msg, chatbot, cross_encoder, language_dropdown], [msg, chatbot, translated_textbox])
|
248 |
+
|
249 |
+
clear = gr.Button("Clear Conversation")
|
250 |
+
clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, translated_textbox])
|
251 |
+
|
252 |
+
# Example questions
|
253 |
+
gr.Examples(
|
254 |
+
examples=[
|
255 |
+
'What is the difference between metals and non-metals?',
|
256 |
+
'What is an ionic bond?',
|
257 |
+
'Explain asexual reproduction',
|
258 |
+
'What is photosynthesis?',
|
259 |
+
'Explain Newton\'s laws of motion'
|
260 |
+
],
|
261 |
+
inputs=msg,
|
262 |
+
label="Try these example questions:"
|
263 |
+
)
|
264 |
|
265 |
if __name__ == "__main__":
|
266 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)# import gradio as gr
|
267 |
+
# from phi.agent import Agent
|
268 |
+
# from phi.model.groq import Groq
|
269 |
+
# import os
|
270 |
+
# import logging
|
271 |
+
# from sentence_transformers import CrossEncoder
|
272 |
+
# from backend.semantic_search import table, retriever
|
273 |
+
# import numpy as np
|
274 |
+
# from time import perf_counter
|
275 |
+
|
276 |
+
# # Set up logging
|
277 |
+
# logging.basicConfig(level=logging.INFO)
|
278 |
+
# logger = logging.getLogger(__name__)
|
279 |
+
|
280 |
+
# # API Key setup
|
281 |
+
# api_key = os.getenv("GROQ_API_KEY")
|
282 |
+
# if not api_key:
|
283 |
+
# gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
|
284 |
+
# logger.error("GROQ_API_KEY not found.")
|
285 |
+
# api_key = "" # Fallback to empty string, but this will fail without a key
|
286 |
+
# else:
|
287 |
+
# os.environ["GROQ_API_KEY"] = api_key
|
288 |
+
|
289 |
+
# # Initialize PhiData Agent
|
290 |
+
# agent = Agent(
|
291 |
+
# name="Science Education Assistant",
|
292 |
+
# role="You are a helpful science tutor for 10th-grade students",
|
293 |
+
# instructions=[
|
294 |
+
# "You are an expert science teacher specializing in 10th-grade curriculum.",
|
295 |
+
# "Provide clear, accurate, and age-appropriate explanations.",
|
296 |
+
# "Use simple language and examples that students can understand.",
|
297 |
+
# "Focus on concepts from physics, chemistry, and biology.",
|
298 |
+
# "Structure responses with headings and bullet points when helpful.",
|
299 |
+
# "Encourage learning and curiosity."
|
300 |
+
# ],
|
301 |
+
# model=Groq(id="llama3-70b-8192", api_key=api_key),
|
302 |
+
# markdown=True
|
303 |
+
# )
|
304 |
+
|
305 |
+
# # Response Generation Function
|
306 |
+
# def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
307 |
+
# """Generate response using semantic search and LLM"""
|
308 |
+
# top_rerank = 25
|
309 |
+
# top_k_rank = 20
|
310 |
+
|
311 |
+
# if not query.strip():
|
312 |
+
# return "Please provide a valid question."
|
313 |
+
|
314 |
+
# try:
|
315 |
+
# start_time = perf_counter()
|
316 |
+
|
317 |
+
# # Encode query and search documents
|
318 |
+
# query_vec = retriever.encode(query)
|
319 |
+
# documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
|
320 |
+
# documents = [doc["text"] for doc in documents]
|
321 |
+
|
322 |
+
# # Re-rank documents using cross-encoder
|
323 |
+
# 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')
|
324 |
+
# query_doc_pair = [[query, doc] for doc in documents]
|
325 |
+
# cross_scores = cross_encoder_model.predict(query_doc_pair)
|
326 |
+
# sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
327 |
+
# documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
|
328 |
+
|
329 |
+
# # Create context from top documents
|
330 |
+
# context = "\n\n".join(documents[:10]) if documents else ""
|
331 |
+
# context = f"Context information from educational materials:\n{context}\n\n"
|
332 |
+
|
333 |
+
# # Add conversation history for context
|
334 |
+
# history_context = ""
|
335 |
+
# if history and len(history) > 0:
|
336 |
+
# for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
|
337 |
+
# if user_msg and bot_msg:
|
338 |
+
# history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
|
339 |
+
|
340 |
+
# # Create full prompt
|
341 |
+
# 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."
|
342 |
+
|
343 |
+
# # Generate response
|
344 |
+
# response = agent.run(full_prompt)
|
345 |
+
# response_text = response.content if hasattr(response, 'content') else str(response)
|
346 |
+
|
347 |
+
# logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
|
348 |
+
# return response_text
|
349 |
+
|
350 |
+
# except Exception as e:
|
351 |
+
# logger.error(f"Error in response generation: {e}")
|
352 |
+
# return f"Error generating response: {str(e)}"
|
353 |
+
|
354 |
+
# def simple_chat_function(message, history, cross_encoder_choice):
|
355 |
+
# """Chat function with semantic search and retriever integration"""
|
356 |
+
# if not message.strip():
|
357 |
+
# return "", history
|
358 |
+
|
359 |
+
# # Generate response using the semantic search function
|
360 |
+
# response = retrieve_and_generate_response(message, cross_encoder_choice, history)
|
361 |
+
|
362 |
+
# # Add to history
|
363 |
+
# history.append([message, response])
|
364 |
+
|
365 |
+
# return "", history
|
366 |
+
|
367 |
+
# # Minimal working interface
|
368 |
+
# with gr.Blocks(title="Science Chatbot") as demo:
|
369 |
+
# # Cross-encoder selection
|
370 |
+
# cross_encoder = gr.Radio(
|
371 |
+
# choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
|
372 |
+
# value='(ACCURATE) BGE reranker',
|
373 |
+
# label="Embeddings Model",
|
374 |
+
# info="Select the model for document ranking"
|
375 |
+
# )
|
376 |
+
|
377 |
+
# chatbot = gr.Chatbot(label="Science Tutor Conversation")
|
378 |
+
# msg = gr.Textbox(placeholder="Type your message here...")
|
379 |
+
# clear = gr.Button("Clear")
|
380 |
+
|
381 |
+
# msg.submit(simple_chat_function, [msg, chatbot, cross_encoder], [msg, chatbot])
|
382 |
+
# clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
383 |
+
|
384 |
+
# if __name__ == "__main__":
|
385 |
+
# demo.launch()# import gradio as gr
|
386 |
+
|
387 |
+
|
388 |
+
|
389 |
+
|
390 |
# from phi.agent import Agent
|
391 |
# from phi.model.groq import Groq
|
392 |
# import os
|