diff --git "a/app.py" "b/app.py" --- "a/app.py" +++ "b/app.py" @@ -1,1318 +1,1466 @@ -import requests -import gradio as gr -import logging -from pathlib import Path -from time import perf_counter -from sentence_transformers import CrossEncoder -from jinja2 import Environment, FileSystemLoader -import numpy as np -from os import getenv - -# Phi Agent imports -from phi.agent import Agent, RunResponse -from phi.model.groq import Groq - -from backend.semantic_search import table, retriever - -# API Keys -api_key = getenv('API_KEY') -user_id = getenv('USER_ID') -groq_api_key = getenv('GROQ_API_KEY') - -# Check for required API keys -if not groq_api_key: - gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.") - logging.error("GROQ_API_KEY not found.") - -def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: - """Translates text from source language to target language using the Bhashini API.""" - - if not text.strip(): - print('Input text is empty. Please provide valid text for translation.') - return {"status_code": 400, "message": "Input text is empty", "translated_content": text} - - print(f'Input text - {text}') - print(f'Starting translation process from {from_code} to {to_code}...') - gr.Warning(f'Translating to {to_code}...') - - try: - url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' - headers = { - "Content-Type": "application/json", - "userID": user_id, - "ulcaApiKey": api_key - } - payload = { - "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], - "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} - } - - print('Sending initial request to get the pipeline...') - response = requests.post(url, json=payload, headers=headers) - - if response.status_code != 200: - print(f'Error in initial request: {response.status_code}') - return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": text} - - print('Initial request successful, processing response...') - response_data = response.json() - service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] - callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] - - print(f'Service ID: {service_id}, Callback URL: {callback_url}') - - headers2 = { - "Content-Type": "application/json", - response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] - } - compute_payload = { - "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], - "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} - } - - print(f'Sending translation request with text: "{text}"') - compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) - - if compute_response.status_code != 200: - print(f'Error in translation request: {compute_response.status_code}') - return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": text} - - print('Translation request successful, processing translation...') - compute_response_data = compute_response.json() - translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] - - print(f'Translation successful. Translated content: "{translated_content}"') - return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} - - except Exception as e: - print(f'Translation error: {e}') - return {"status_code": 500, "message": f"Translation error: {e}", "translated_content": text} - -# Initialize Phi Agent -def create_phi_agent(): - """Create and return a Phi Agent for science education chatbot""" - if not groq_api_key: - return None - - agent = Agent( - name="Science Education Assistant", - role="You are a helpful science tutor for 10th-grade students", - instructions=[ - "You are an expert science teacher specializing in 10th-grade curriculum.", - "Provide clear, accurate, and age-appropriate explanations.", - "Use simple language and examples that students can understand.", - "Focus on concepts from physics, chemistry, and biology.", - "When answering, structure your response with clear headings and bullet points when helpful.", - "Always encourage learning and curiosity.", - "If you're not sure about something, acknowledge it honestly." - ], - model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), - markdown=True, - show_tool_calls=False, - debug_mode=False - ) - return agent - -# Create the agent instance -phi_agent = create_phi_agent() - -def generate_phi_response(prompt: str, history: list = None) -> str: - """Generate response using Phi Agent""" - if not phi_agent: - return "Sorry, the chatbot is not properly configured. Please check the API key settings." - - try: - # Build context from history if available - context = "" - if history: - for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context - if user_msg and bot_msg: - context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" - - # Combine context with current prompt - full_prompt = f"{context}Current Question: {prompt}" - - # Run the agent - response: RunResponse = phi_agent.run(full_prompt) - - # Extract the content from the response - if hasattr(response, 'content') and response.content: - return response.content - elif hasattr(response, 'messages') and response.messages: - # Get the last message content - last_message = response.messages[-1] - if hasattr(last_message, 'content'): - return last_message.content - - return "I apologize, but I couldn't generate a proper response. Please try again." - - except Exception as e: - print(f"Error in Phi Agent: {e}") - return f"I encountered an error while processing your question: {str(e)}. Please try again." - -# Constants -VECTOR_COLUMN_NAME = "vector" -TEXT_COLUMN_NAME = "text" -proj_dir = Path(__file__).parent - -logging.basicConfig(level=logging.INFO) -logger = logging.getLogger(__name__) - -# Setup Jinja2 templates -env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) -try: - template = env.get_template('template.j2') - template_html = env.get_template('template_html.j2') -except: - # Fallback if templates don't exist - template = None - template_html = None - -def retrieve_and_generate_response(query, cross_encoder, history=None): - """Retrieve documents and generate response""" - top_rerank = 25 - top_k_rank = 20 - - if not query: - return "Please provide a valid question." - - logger.warning('Retrieving documents...') - - try: - document_start = perf_counter() - - query_vec = retriever.encode(query) - documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() - documents = [doc[TEXT_COLUMN_NAME] for doc in documents] - - query_doc_pair = [[query, doc] for doc in documents] - - if cross_encoder == '(FAST) MiniLM-L6v2': - cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') - elif cross_encoder == '(ACCURATE) BGE reranker': - cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') - else: - cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') # Default - - cross_scores = cross_encoder1.predict(query_doc_pair) - sim_scores_argsort = list(reversed(np.argsort(cross_scores))) - - documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] - - document_time = perf_counter() - document_start - print(f"Document retrieval took {document_time:.2f} seconds") - - # Create context from documents - context = "" - if documents: - context = "\n\n".join(documents[:10]) # Use top 10 documents - context = f"Context information from educational materials:\n{context}\n\n" - - # Build conversation history - history_context = "" - if history: - for user_msg, bot_msg in history[-2:]: # Last 2 exchanges - if user_msg and bot_msg: - history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" - - # Create enhanced prompt - full_prompt = f""" - {history_context} - {context} - Question: {query} - - Please answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics. - """ - - # Generate response using Phi Agent - output = generate_phi_response(full_prompt, history) - - print('Output:', output) - - return output - - except Exception as e: - print(f"Error in retrieve_and_generate_response: {e}") - return f"Sorry, I encountered an error: {str(e)}" - -def translate_response(selected_language, response_text): - """Translate the response to selected language""" - if not response_text or not selected_language: - return response_text - - iso_language_codes = { - "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur", - "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr", - "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni", - "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or" - } - - to_code = iso_language_codes.get(selected_language, "hi") - translation = bhashini_translate(response_text, to_code=to_code) - return translation.get('translated_content', response_text) - -# Simplified Gradio interface -with gr.Blocks(theme='gradio/soft') as CHATBOT: - with gr.Row(): - with gr.Column(scale=10): - gr.HTML(value="""
A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") - gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") - - with gr.Column(scale=3): - try: - gr.Image(value='logo.png', height=200, width=200) - except: - pass # Skip if logo not found - - chatbot = gr.Chatbot( - [], - elem_id="chatbot", - bubble_full_width=False, - show_copy_button=True, - show_share_button=True, - ) - - with gr.Row(): - txt = gr.Textbox( - scale=3, - show_label=False, - placeholder="Enter text and press enter", - container=False, - ) - txt_btn = gr.Button(value="Submit text", scale=1) - - cross_encoder = gr.Radio( - choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], - value='(ACCURATE) BGE reranker', - label="Embeddings" - ) - - language_dropdown = gr.Dropdown( - choices=[ - "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", - "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", - "Gujarati", "Odia" - ], - value="Hindi", - label="Select Language for Translation" - ) - - translated_textbox = gr.Textbox(label="Translated Response") - - def user_message(user_input, history): - """Handle user input and add to history""" - if not user_input.strip(): - return "", history - return "", history + [[user_input, None]] - - def bot_response(history, cross_encoder_choice): - """Generate bot response""" - if not history: - return history - - user_input = history[-1][0] - - # Generate response - response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1]) - - # Update history - history[-1][1] = response - return history - - def translate_current_response(history, selected_language): - """Translate the current response""" - if not history or not history[-1][1]: - return "" - - response_text = history[-1][1] - translated_text = translate_response(selected_language, response_text) - return translated_text - - # Event handlers - txt_msg = txt.submit(user_message, [txt, chatbot], [txt, chatbot]).then( - bot_response, [chatbot, cross_encoder], chatbot - ).then( - translate_current_response, [chatbot, language_dropdown], translated_textbox - ) - - txt_btn.click(user_message, [txt, chatbot], [txt, chatbot]).then( - bot_response, [chatbot, cross_encoder], chatbot - ).then( - translate_current_response, [chatbot, language_dropdown], translated_textbox - ) - - examples = [ - 'CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?', - 'WHAT IS IONIC BOND?', - 'EXPLAIN ASEXUAL REPRODUCTION' - ] - - gr.Examples(examples, txt) - -# Launch the Gradio application -if __name__ == "__main__": - CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860) -# import gradio as gr -# # from ragatouille import RAGPretrainedModel # COMMENTED OUT -# import logging -# from pathlib import Path -# from time import perf_counter -# from sentence_transformers import CrossEncoder -# #from huggingface_hub import InferenceClient -# from jinja2 import Environment, FileSystemLoader -# import numpy as np -# from os import getenv - -# # Phi Agent imports -# from phi.agent import Agent, RunResponse -# from phi.model.groq import Groq -# from phi.utils.pprint import pprint_run_response - -# #from backend.query_llm import generate_hf -# from backend.semantic_search import table, retriever - -# # Bhashini API translation function -# api_key = getenv('API_KEY') -# user_id = getenv('USER_ID') -# groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key - -# def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: -# """Translates text from source language to target language using the Bhashini API.""" - -# if not text.strip(): -# print('Input text is empty. Please provide valid text for translation.') -# return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} -# else: -# print('Input text - ',text) -# print(f'Starting translation process from {from_code} to {to_code}...') -# print(f'Starting translation process from {from_code} to {to_code}...') -# gr.Warning(f'Translating to {to_code}...') - -# url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' -# headers = { -# "Content-Type": "application/json", -# "userID": user_id, -# "ulcaApiKey": api_key -# } -# payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], -# "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} -# } - -# print('Sending initial request to get the pipeline...') -# response = requests.post(url, json=payload, headers=headers) - -# if response.status_code != 200: -# print(f'Error in initial request: {response.status_code}') -# return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} - -# print('Initial request successful, processing response...') -# response_data = response.json() -# service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] -# callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] - -# print(f'Service ID: {service_id}, Callback URL: {callback_url}') - -# headers2 = { -# "Content-Type": "application/json", -# response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] -# } -# compute_payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], -# "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} -# } - -# print(f'Sending translation request with text: "{text}"') -# compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) - -# if compute_response.status_code != 200: -# print(f'Error in translation request: {compute_response.status_code}') -# return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} - -# print('Translation request successful, processing translation...') -# compute_response_data = compute_response.json() -# translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] - -# print(f'Translation successful. Translated content: "{translated_content}"') -# return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} - -# # Initialize Phi Agent -# def create_phi_agent(): -# """Create and return a Phi Agent for science education chatbot""" -# agent = Agent( -# name="Science Education Assistant", -# role="You are a helpful science tutor for 10th-grade students", -# instructions=[ -# "You are an expert science teacher specializing in 10th-grade curriculum.", -# "Provide clear, accurate, and age-appropriate explanations.", -# "Use simple language and examples that students can understand.", -# "Focus on concepts from physics, chemistry, and biology.", -# "When answering, structure your response with clear headings and bullet points when helpful.", -# "Always encourage learning and curiosity.", -# "If you're not sure about something, acknowledge it honestly." -# ], -# model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), -# markdown=True, -# show_tool_calls=False, -# debug_mode=False -# ) -# return agent - -# # Create the agent instance -# phi_agent = create_phi_agent() - -# def generate_phi_response(prompt: str, history: list = None) -> str: -# """Generate response using Phi Agent""" -# try: -# # Build context from history if available -# context = "" -# if history: -# for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context -# if user_msg and bot_msg: -# context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" - -# # Combine context with current prompt -# full_prompt = f"{context}Current Question: {prompt}" - -# # Run the agent -# response: RunResponse = phi_agent.run(full_prompt) - -# # Extract the content from the response -# if hasattr(response, 'content') and response.content: -# return response.content -# elif hasattr(response, 'messages') and response.messages: -# # Get the last message content -# last_message = response.messages[-1] -# if hasattr(last_message, 'content'): -# return last_message.content - -# return "I apologize, but I couldn't generate a proper response. Please try again." - -# except Exception as e: -# print(f"Error in Phi Agent: {e}") -# return f"I encountered an error while processing your question: {str(e)}. Please try again." - -# # Existing chatbot functions -# VECTOR_COLUMN_NAME = "vector" -# TEXT_COLUMN_NAME = "text" -# #HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") -# proj_dir = Path(__file__).parent - -# logging.basicConfig(level=logging.INFO) -# logger = logging.getLogger(__name__) -# #client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) -# env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) - -# template = env.get_template('template.j2') -# template_html = env.get_template('template_html.j2') - -# def bot(history, cross_encoder): -# top_rerank = 25 -# top_k_rank = 20 -# query = history[-1][0] if history else '' -# print('\nQuery: ',query ) -# print('\nHistory:',history) -# if not query: -# gr.Warning("Please submit a non-empty string as a prompt") -# raise ValueError("Empty string was submitted") - -# logger.warning('Retrieving documents...') - -# # COMMENTED OUT RAGatouille section -# # if cross_encoder == '(HIGH ACCURATE) ColBERT': -# # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') -# # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") -# # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') -# # documents_full = RAG_db.search(query, k=top_k_rank) -# # -# # documents = [item['content'] for item in documents_full] -# # prompt = template.render(documents=documents, query=query) -# # prompt_html = template_html.render(documents=documents, query=query) -# # -# # generate_fn = generate_hf -# # -# # history[-1][1] = "" -# # for character in generate_fn(prompt, history[:-1]): -# # history[-1][1] = character -# # yield history, prompt_html -# # else: - -# # Handle ColBERT case differently for now -# if cross_encoder == '(HIGH ACCURATE) ColBERT': -# gr.Warning('ColBERT is temporarily disabled. Using BGE reranker instead.') -# cross_encoder = '(ACCURATE) BGE reranker' - -# document_start = perf_counter() - -# query_vec = retriever.encode(query) -# doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) - -# documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() -# documents = [doc[TEXT_COLUMN_NAME] for doc in documents] - -# query_doc_pair = [[query, doc] for doc in documents] -# if cross_encoder == '(FAST) MiniLM-L6v2': -# cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') -# elif cross_encoder == '(ACCURATE) BGE reranker': -# cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') - -# cross_scores = cross_encoder1.predict(query_doc_pair) -# sim_scores_argsort = list(reversed(np.argsort(cross_scores))) - -# documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] - -# document_time = perf_counter() - document_start - -# prompt = template.render(documents=documents, query=query) -# prompt_html = template_html.render(documents=documents, query=query) - -# # REPLACED: Use Phi Agent instead of generate_qwen -# print("Using Phi Agent for response generation...") -# output = generate_phi_response(prompt, history[:-1]) - -# print('Output:',output) - -# # Update history with the response -# history_list = list(history[-1]) -# history_list[1] = output -# history[-1] = tuple(history_list) - -# yield history, prompt_html - -# def translate_text(selected_language, history): -# iso_language_codes = { -# "Hindi": "hi", -# "Gom": "gom", -# "Kannada": "kn", -# "Dogri": "doi", -# "Bodo": "brx", -# "Urdu": "ur", -# "Tamil": "ta", -# "Kashmiri": "ks", -# "Assamese": "as", -# "Bengali": "bn", -# "Marathi": "mr", -# "Sindhi": "sd", -# "Maithili": "mai", -# "Punjabi": "pa", -# "Malayalam": "ml", -# "Manipuri": "mni", -# "Telugu": "te", -# "Sanskrit": "sa", -# "Nepali": "ne", -# "Santali": "sat", -# "Gujarati": "gu", -# "Odia": "or" -# } - -# to_code = iso_language_codes[selected_language] -# response_text = history[-1][1] if history else '' -# print('response_text for translation',response_text) -# translation = bhashini_translate(response_text, to_code=to_code) -# return translation['translated_content'] - -# # Gradio interface - SIMPLIFIED to avoid schema issues -# with gr.Blocks(theme='gradio/soft') as CHATBOT: -# history_state = gr.State([]) -# with gr.Row(): -# with gr.Column(scale=10): -# gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") -# gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") - -# with gr.Column(scale=3): -# try: -# gr.Image(value='logo.png', height=200, width=200) -# except: -# pass # Skip if logo not found - -# chatbot = gr.Chatbot( -# [], -# elem_id="chatbot", -# bubble_full_width=False, -# show_copy_button=True, -# show_share_button=True, -# ) - -# with gr.Row(): -# txt = gr.Textbox( -# scale=3, -# show_label=False, -# placeholder="Enter text and press enter", -# container=False, -# ) -# txt_btn = gr.Button(value="Submit text", scale=1) - -# # SIMPLIFIED: Remove ColBERT option temporarily -# cross_encoder = gr.Radio( -# choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], -# value='(ACCURATE) BGE reranker', -# label="Embeddings" -# ) - -# language_dropdown = gr.Dropdown( -# choices=[ -# "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", -# "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", -# "Gujarati", "Odia" -# ], -# value="Hindi", # default to Hindi -# label="Select Language for Translation" -# ) - -# prompt_html = gr.HTML() - -# translated_textbox = gr.Textbox(label="Translated Response") - -# def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): -# print('History state',history_state) -# history = history_state -# history.append((txt, "")) - -# # Call bot function -# bot_output = next(bot(history, cross_encoder)) -# print('bot_output',bot_output) -# history, prompt_html = bot_output -# print('History',history) - -# # Update the history state -# history_state[:] = history - -# # Translate text -# translated_text = translate_text(language_dropdown, history) -# return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input - -# txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) -# txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) - -# examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', -# 'EXPLAIN ASEXUAL REPRODUCTION'] - -# gr.Examples(examples, txt) - -# # Launch the Gradio application -# if __name__ == "__main__": -# CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860)# import requests -# import gradio as gr -# from ragatouille import RAGPretrainedModel -# import logging -# from pathlib import Path -# from time import perf_counter -# from sentence_transformers import CrossEncoder -# from huggingface_hub import InferenceClient -# from jinja2 import Environment, FileSystemLoader -# import numpy as np -# from os import getenv - -# # Phi Agent imports -# from phi.agent import Agent, RunResponse -# from phi.model.groq import Groq -# from phi.utils.pprint import pprint_run_response - -# #from backend.query_llm import generate_hf -# from backend.semantic_search import table, retriever - -# # Bhashini API translation function -# api_key = getenv('API_KEY') -# user_id = getenv('USER_ID') -# groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key - -# def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: -# """Translates text from source language to target language using the Bhashini API.""" - -# if not text.strip(): -# print('Input text is empty. Please provide valid text for translation.') -# return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} -# else: -# print('Input text - ',text) -# print(f'Starting translation process from {from_code} to {to_code}...') -# print(f'Starting translation process from {from_code} to {to_code}...') -# gr.Warning(f'Translating to {to_code}...') - -# url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' -# headers = { -# "Content-Type": "application/json", -# "userID": user_id, -# "ulcaApiKey": api_key -# } -# payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], -# "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} -# } - -# print('Sending initial request to get the pipeline...') -# response = requests.post(url, json=payload, headers=headers) - -# if response.status_code != 200: -# print(f'Error in initial request: {response.status_code}') -# return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} - -# print('Initial request successful, processing response...') -# response_data = response.json() -# service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] -# callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] - -# print(f'Service ID: {service_id}, Callback URL: {callback_url}') - -# headers2 = { -# "Content-Type": "application/json", -# response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] -# } -# compute_payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], -# "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} -# } - -# print(f'Sending translation request with text: "{text}"') -# compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) - -# if compute_response.status_code != 200: -# print(f'Error in translation request: {compute_response.status_code}') -# return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} - -# print('Translation request successful, processing translation...') -# compute_response_data = compute_response.json() -# translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] - -# print(f'Translation successful. Translated content: "{translated_content}"') -# return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} - -# # Initialize Phi Agent -# def create_phi_agent(): -# """Create and return a Phi Agent for science education chatbot""" -# agent = Agent( -# name="Science Education Assistant", -# role="You are a helpful science tutor for 10th-grade students", -# instructions=[ -# "You are an expert science teacher specializing in 10th-grade curriculum.", -# "Provide clear, accurate, and age-appropriate explanations.", -# "Use simple language and examples that students can understand.", -# "Focus on concepts from physics, chemistry, and biology.", -# "When answering, structure your response with clear headings and bullet points when helpful.", -# "Always encourage learning and curiosity.", -# "If you're not sure about something, acknowledge it honestly." -# ], -# model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), -# markdown=True, -# show_tool_calls=False, -# debug_mode=False -# ) -# return agent - -# # Create the agent instance -# phi_agent = create_phi_agent() - -# def generate_phi_response(prompt: str, history: list = None) -> str: -# """Generate response using Phi Agent""" -# try: -# # Build context from history if available -# context = "" -# if history: -# for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context -# if user_msg and bot_msg: -# context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" - -# # Combine context with current prompt -# full_prompt = f"{context}Current Question: {prompt}" - -# # Run the agent -# response: RunResponse = phi_agent.run(full_prompt) - -# # Extract the content from the response -# if hasattr(response, 'content') and response.content: -# return response.content -# elif hasattr(response, 'messages') and response.messages: -# # Get the last message content -# last_message = response.messages[-1] -# if hasattr(last_message, 'content'): -# return last_message.content - -# return "I apologize, but I couldn't generate a proper response. Please try again." - -# except Exception as e: -# print(f"Error in Phi Agent: {e}") -# return f"I encountered an error while processing your question: {str(e)}. Please try again." - -# # Existing chatbot functions -# VECTOR_COLUMN_NAME = "vector" -# TEXT_COLUMN_NAME = "text" -# HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") -# proj_dir = Path(__file__).parent - -# logging.basicConfig(level=logging.INFO) -# logger = logging.getLogger(__name__) -# client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) -# env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) - -# template = env.get_template('template.j2') -# template_html = env.get_template('template_html.j2') - -# def bot(history, cross_encoder): -# top_rerank = 25 -# top_k_rank = 20 -# query = history[-1][0] if history else '' -# print('\nQuery: ',query ) -# print('\nHistory:',history) -# if not query: -# gr.Warning("Please submit a non-empty string as a prompt") -# raise ValueError("Empty string was submitted") - -# logger.warning('Retrieving documents...') - -# if cross_encoder == '(HIGH ACCURATE) ColBERT': -# gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') -# RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") -# RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') -# documents_full = RAG_db.search(query, k=top_k_rank) - -# documents = [item['content'] for item in documents_full] -# prompt = template.render(documents=documents, query=query) -# prompt_html = template_html.render(documents=documents, query=query) - -# generate_fn = generate_hf - -# history[-1][1] = "" -# for character in generate_fn(prompt, history[:-1]): -# history[-1][1] = character -# yield history, prompt_html -# else: -# document_start = perf_counter() - -# query_vec = retriever.encode(query) -# doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) - -# documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() -# documents = [doc[TEXT_COLUMN_NAME] for doc in documents] - -# query_doc_pair = [[query, doc] for doc in documents] -# if cross_encoder == '(FAST) MiniLM-L6v2': -# cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') -# elif cross_encoder == '(ACCURATE) BGE reranker': -# cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') - -# cross_scores = cross_encoder1.predict(query_doc_pair) -# sim_scores_argsort = list(reversed(np.argsort(cross_scores))) - -# documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] - -# document_time = perf_counter() - document_start - -# prompt = template.render(documents=documents, query=query) -# prompt_html = template_html.render(documents=documents, query=query) - -# # REPLACED: Use Phi Agent instead of generate_qwen -# print("Using Phi Agent for response generation...") -# output = generate_phi_response(prompt, history[:-1]) - -# print('Output:',output) - -# # Update history with the response -# history_list = list(history[-1]) -# history_list[1] = output -# history[-1] = tuple(history_list) - -# yield history, prompt_html - -# def translate_text(selected_language, history): -# iso_language_codes = { -# "Hindi": "hi", -# "Gom": "gom", -# "Kannada": "kn", -# "Dogri": "doi", -# "Bodo": "brx", -# "Urdu": "ur", -# "Tamil": "ta", -# "Kashmiri": "ks", -# "Assamese": "as", -# "Bengali": "bn", -# "Marathi": "mr", -# "Sindhi": "sd", -# "Maithili": "mai", -# "Punjabi": "pa", -# "Malayalam": "ml", -# "Manipuri": "mni", -# "Telugu": "te", -# "Sanskrit": "sa", -# "Nepali": "ne", -# "Santali": "sat", -# "Gujarati": "gu", -# "Odia": "or" -# } - -# to_code = iso_language_codes[selected_language] -# response_text = history[-1][1] if history else '' -# print('response_text for translation',response_text) -# translation = bhashini_translate(response_text, to_code=to_code) -# return translation['translated_content'] - -# # Gradio interface -# with gr.Blocks(theme='gradio/soft') as CHATBOT: -# history_state = gr.State([]) -# with gr.Row(): -# with gr.Column(scale=10): -# gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") -# gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") - -# with gr.Column(scale=3): -# gr.Image(value='logo.png', height=200, width=200) - -# chatbot = gr.Chatbot( -# [], -# elem_id="chatbot", -# avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg', -# 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'), -# bubble_full_width=False, -# show_copy_button=True, -# show_share_button=True, -# ) - -# with gr.Row(): -# txt = gr.Textbox( -# scale=3, -# show_label=False, -# placeholder="Enter text and press enter", -# container=False, -# ) -# txt_btn = gr.Button(value="Submit text", scale=1) - -# 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)") -# language_dropdown = gr.Dropdown( -# choices=[ -# "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", -# "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", -# "Gujarati", "Odia" -# ], -# value="Hindi", # default to Hindi -# label="Select Language for Translation" -# ) - -# prompt_html = gr.HTML() - -# translated_textbox = gr.Textbox(label="Translated Response") - -# def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): -# print('History state',history_state) -# history = history_state -# history.append((txt, "")) - -# # Call bot function -# bot_output = next(bot(history, cross_encoder)) -# print('bot_output',bot_output) -# history, prompt_html = bot_output -# print('History',history) - -# # Update the history state -# history_state[:] = history - -# # Translate text -# translated_text = translate_text(language_dropdown, history) -# return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input - -# txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) -# txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) - -# examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', -# 'EXPLAIN ASEXUAL REPRODUCTION'] - -# gr.Examples(examples, txt) - -# # Launch the Gradio application -# if __name__ == "__main__": -# CHATBOT.launch(share=True, debug=True)# import requests -# import gradio as gr -# from ragatouille import RAGPretrainedModel -# import logging -# from pathlib import Path -# from time import perf_counter -# from sentence_transformers import CrossEncoder -# from huggingface_hub import InferenceClient -# from jinja2 import Environment, FileSystemLoader -# import numpy as np -# from os import getenv -# from backend.query_llm import generate_hf, generate_qwen -# from backend.semantic_search import table, retriever -# from huggingface_hub import InferenceClient - - -# # Bhashini API translation function -# api_key = getenv('API_KEY') -# user_id = getenv('USER_ID') - -# def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: -# """Translates text from source language to target language using the Bhashini API.""" - -# if not text.strip(): -# print('Input text is empty. Please provide valid text for translation.') -# return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} -# else: -# print('Input text - ',text) -# print(f'Starting translation process from {from_code} to {to_code}...') -# print(f'Starting translation process from {from_code} to {to_code}...') -# gr.Warning(f'Translating to {to_code}...') - -# url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' -# headers = { -# "Content-Type": "application/json", -# "userID": user_id, -# "ulcaApiKey": api_key -# } -# payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], -# "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} -# } - -# print('Sending initial request to get the pipeline...') -# response = requests.post(url, json=payload, headers=headers) - -# if response.status_code != 200: -# print(f'Error in initial request: {response.status_code}') -# return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} - -# print('Initial request successful, processing response...') -# response_data = response.json() -# service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] -# callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] - -# print(f'Service ID: {service_id}, Callback URL: {callback_url}') - -# headers2 = { -# "Content-Type": "application/json", -# response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] -# } -# compute_payload = { -# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], -# "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} -# } - -# print(f'Sending translation request with text: "{text}"') -# compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) - -# if compute_response.status_code != 200: -# print(f'Error in translation request: {compute_response.status_code}') -# return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} - -# print('Translation request successful, processing translation...') -# compute_response_data = compute_response.json() -# translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] - -# print(f'Translation successful. Translated content: "{translated_content}"') -# return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} - - -# # Existing chatbot functions -# VECTOR_COLUMN_NAME = "vector" -# TEXT_COLUMN_NAME = "text" -# HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") -# proj_dir = Path(__file__).parent - -# logging.basicConfig(level=logging.INFO) -# logger = logging.getLogger(__name__) -# client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) -# env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) - -# template = env.get_template('template.j2') -# template_html = env.get_template('template_html.j2') - -# # def add_text(history, text): -# # history = [] if history is None else history -# # history = history + [(text, None)] -# # return history, gr.Textbox(value="", interactive=False) - -# def bot(history, cross_encoder): - -# top_rerank = 25 -# top_k_rank = 20 -# query = history[-1][0] if history else '' -# print('\nQuery: ',query ) -# print('\nHistory:',history) -# if not query: -# gr.Warning("Please submit a non-empty string as a prompt") -# raise ValueError("Empty string was submitted") - -# logger.warning('Retrieving documents...') - -# if cross_encoder == '(HIGH ACCURATE) ColBERT': -# gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') -# RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") -# RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') -# documents_full = RAG_db.search(query, k=top_k_rank) - -# documents = [item['content'] for item in documents_full] -# prompt = template.render(documents=documents, query=query) -# prompt_html = template_html.render(documents=documents, query=query) - -# generate_fn = generate_hf - -# history[-1][1] = "" -# for character in generate_fn(prompt, history[:-1]): -# history[-1][1] = character -# yield history, prompt_html -# else: -# document_start = perf_counter() - -# query_vec = retriever.encode(query) -# doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) - -# documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() -# documents = [doc[TEXT_COLUMN_NAME] for doc in documents] - -# query_doc_pair = [[query, doc] for doc in documents] -# if cross_encoder == '(FAST) MiniLM-L6v2': -# cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') -# elif cross_encoder == '(ACCURATE) BGE reranker': -# cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') - -# cross_scores = cross_encoder1.predict(query_doc_pair) -# sim_scores_argsort = list(reversed(np.argsort(cross_scores))) - -# documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] - -# document_time = perf_counter() - document_start - -# prompt = template.render(documents=documents, query=query) -# prompt_html = template_html.render(documents=documents, query=query) - -# #generate_fn = generate_hf -# generate_fn=generate_qwen -# # Create a new history entry instead of modifying the tuple directly -# new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt -# output='' -# # for character in generate_fn(prompt, history[:-1]): -# # #new_history[-1] = (query, character) -# # output+=character -# output=generate_fn(prompt, history[:-1]) - -# print('Output:',output) -# new_history[-1] = (prompt, output) #query replaced with prompt -# print('New History',new_history) -# #print('prompt html',prompt_html)# Update the last tuple with new text - -# history_list = list(history[-1]) -# history_list[1] = output # Assuming `character` is what you want to assign -# # Update the history with the modified list converted back to a tuple -# history[-1] = tuple(history_list) - -# #history[-1][1] = character -# # yield new_history, prompt_html -# yield history, prompt_html -# # new_history,prompt_html -# # history[-1][1] = "" -# # for character in generate_fn(prompt, history[:-1]): -# # history[-1][1] = character -# # yield history, prompt_html - -# #def translate_text(response_text, selected_language): - -# def translate_text(selected_language,history): - -# iso_language_codes = { -# "Hindi": "hi", -# "Gom": "gom", -# "Kannada": "kn", -# "Dogri": "doi", -# "Bodo": "brx", -# "Urdu": "ur", -# "Tamil": "ta", -# "Kashmiri": "ks", -# "Assamese": "as", -# "Bengali": "bn", -# "Marathi": "mr", -# "Sindhi": "sd", -# "Maithili": "mai", -# "Punjabi": "pa", -# "Malayalam": "ml", -# "Manipuri": "mni", -# "Telugu": "te", -# "Sanskrit": "sa", -# "Nepali": "ne", -# "Santali": "sat", -# "Gujarati": "gu", -# "Odia": "or" -# } - -# to_code = iso_language_codes[selected_language] -# response_text = history[-1][1] if history else '' -# print('response_text for translation',response_text) -# translation = bhashini_translate(response_text, to_code=to_code) -# return translation['translated_content'] - - -# # Gradio interface -# with gr.Blocks(theme='gradio/soft') as CHATBOT: -# history_state = gr.State([]) -# with gr.Row(): -# with gr.Column(scale=10): -# gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students
""") -# gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") - -# with gr.Column(scale=3): -# gr.Image(value='logo.png', height=200, width=200) - -# chatbot = gr.Chatbot( -# [], -# elem_id="chatbot", -# avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg', -# 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'), -# bubble_full_width=False, -# show_copy_button=True, -# show_share_button=True, -# ) - -# with gr.Row(): -# txt = gr.Textbox( -# scale=3, -# show_label=False, -# placeholder="Enter text and press enter", -# container=False, -# ) -# txt_btn = gr.Button(value="Submit text", scale=1) - -# 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)") -# language_dropdown = gr.Dropdown( -# choices=[ -# "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", -# "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", -# "Gujarati", "Odia" -# ], -# value="Hindi", # default to Hindi -# label="Select Language for Translation" -# ) - -# prompt_html = gr.HTML() - -# translated_textbox = gr.Textbox(label="Translated Response") -# def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): -# print('History state',history_state) -# history = history_state -# history.append((txt, "")) -# #history_state.value=(history) - -# # Call bot function -# # bot_output = list(bot(history, cross_encoder)) -# bot_output = next(bot(history, cross_encoder)) -# print('bot_output',bot_output) -# #history, prompt_html = bot_output[-1] -# history, prompt_html = bot_output -# print('History',history) -# # Update the history state -# history_state[:] = history - -# # Translate text -# translated_text = translate_text(language_dropdown, history) -# return history, prompt_html, translated_text - -# txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox]) -# txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox]) - -# examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', -# 'EXPLAIN ASEXUAL REPRODUCTION'] - -# gr.Examples(examples, txt) - - -# # Launch the Gradio application -# CHATBOT.launch(share=True,debug=True) - +import gradio as gr +import logging +from sentence_transformers import CrossEncoder +from phi.agent import Agent +from phi.model.groq import Groq +from backend.semantic_search import table, retriever +from os import getenv +import requests +import numpy as np +from time import perf_counter + +# Logging setup +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + +# API Keys +groq_api_key = getenv('GROQ_API_KEY') +api_key = getenv('API_KEY') +user_id = getenv('USER_ID') + +# Bhashini Translation +def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: + if not text.strip(): + return {"status_code": 400, "message": "Input text is empty", "translated_content": text} + + try: + url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' + headers = {"Content-Type": "application/json", "userID": user_id, "ulcaApiKey": api_key} + payload = { + "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], + "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} + } + + response = requests.post(url, json=payload, headers=headers) + if response.status_code != 200: + return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": text} + + response_data = response.json() + service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] + callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] + + headers2 = { + "Content-Type": "application/json", + response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] + } + compute_payload = { + "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], + "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} + } + + compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) + if compute_response.status_code != 200: + return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": text} + + translated_content = compute_response.json()["pipelineResponse"][0]["output"][0]["target"] + return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} + + except Exception as e: + return {"status_code": 500, "message": f"Translation error: {e}", "translated_content": text} + +# Phi Agent Setup +def create_phi_agent(): + if not groq_api_key: + return None + agent = Agent( + name="Science Education Assistant", + role="You are a helpful science tutor for 10th-grade students", + instructions=[ + "You are an expert science teacher specializing in 10th-grade curriculum.", + "Provide clear, accurate, and age-appropriate explanations.", + "Use simple language and examples that students can understand.", + "Focus on concepts from physics, chemistry, and biology.", + "Structure responses with headings and bullet points when helpful.", + "Encourage learning and curiosity." + ], + model=Groq(id="llama3 AnnoDomini", api_key=groq_api_key), + markdown=True + ) + return agent + +phi_agent = create_phi_agent() + +# Response Generation +def retrieve_and_generate_response(query, cross_encoder_choice, history=None): + top_rerank = 25 + top_k_rank = 20 + + if not query: + return "Please provide a valid question." + + try: + query_vec = retriever.encode(query) + documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list() + documents = [doc["text"] for doc in documents] + + cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') if cross_encoder_choice == '(ACCURATE) BGE reranker' else CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') + query_doc_pair = [[query, doc] for doc in documents] + cross_scores = cross_encoder1.predict(query_doc_pair) + sim_scores_argsort = list(reversed(np.argsort(cross_scores))) + documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] + + context = "\n\n".join(documents[:10]) if documents else "" + context = f"Context information from educational materials:\n{context}\n\n" + + history_context = "" + if history: + for user_msg, bot_msg in history[-2:]: + if user_msg and bot_msg: + history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" + + 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." + + if not phi_agent: + return "Chatbot not configured properly." + response = phi_agent.run(full_prompt) + return response.content if hasattr(response, 'content') else str(response) + + except Exception as e: + return f"Error: {str(e)}" + +# Gradio Interface +with gr.Blocks(theme='gradio/soft') as CHATBOT: + gr.Markdown("# Science Chatbot for 10th Grade Students") + chatbot = gr.Chatbot() + with gr.Row(): + query = gr.Textbox(placeholder="Ask a science question...", label="Question") + submit_btn = gr.Button("Submit") + cross_encoder = gr.Radio(choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], value='(ACCURATE) BGE reranker', label="Embeddings") + language = gr.Dropdown(choices=["Hindi", "Tamil", "Telugu", "Kannada", "Marathi", "Gujarati", "Bengali", "Odia", "Punjabi", "Malayalam", "Assamese", "Urdu"], value="Hindi", label="Translation Language") + translated_output = gr.Textbox(label="Translated Response") + + def handle_query(user_input, history, cross_encoder_choice): + if not user_input.strip(): + return history, "" + history = history or [] + history.append([user_input, None]) + response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1]) + history[-1][1] = response + translated = bhashini_translate(response, to_code={"Hindi": "hi", "Tamil": "ta", "Telugu": "te", "Kannada": "kn", "Marathi": "mr", "Gujarati": "gu", "Bengali": "bn", "Odia": "or", "Punjabi": "pa", "Malayalam": "ml", "Assamese": "as", "Urdu": "ur"}.get(language, "hi")) + return history, translated["translated_content"] + + submit_btn.click( + fn=handle_query, + inputs=[query, chatbot, cross_encoder, language], + outputs=[chatbot, translated_output] + ) + +if __name__ == "__main__": + CHATBOT.launch(server_name="0.0.0.0", server_port=7860)# import requests +# import gradio as gr +# import logging +# from pathlib import Path +# from time import perf_counter +# from sentence_transformers import CrossEncoder +# from jinja2 import Environment, FileSystemLoader +# import numpy as np +# from os import getenv + +# # Phi Agent imports +# from phi.agent import Agent, RunResponse +# from phi.model.groq import Groq + +# from backend.semantic_search import table, retriever + +# # API Keys +# api_key = getenv('API_KEY') +# user_id = getenv('USER_ID') +# groq_api_key = getenv('GROQ_API_KEY') + +# # Check for required API keys +# if not groq_api_key: +# gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.") +# logging.error("GROQ_API_KEY not found.") + +# def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: +# """Translates text from source language to target language using the Bhashini API.""" + +# if not text.strip(): +# print('Input text is empty. Please provide valid text for translation.') +# return {"status_code": 400, "message": "Input text is empty", "translated_content": text} + +# print(f'Input text - {text}') +# print(f'Starting translation process from {from_code} to {to_code}...') +# gr.Warning(f'Translating to {to_code}...') + +# try: +# url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' +# headers = { +# "Content-Type": "application/json", +# "userID": user_id, +# "ulcaApiKey": api_key +# } +# payload = { +# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], +# "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} +# } + +# print('Sending initial request to get the pipeline...') +# response = requests.post(url, json=payload, headers=headers) + +# if response.status_code != 200: +# print(f'Error in initial request: {response.status_code}') +# return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": text} + +# print('Initial request successful, processing response...') +# response_data = response.json() +# service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] +# callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] + +# print(f'Service ID: {service_id}, Callback URL: {callback_url}') + +# headers2 = { +# "Content-Type": "application/json", +# response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] +# } +# compute_payload = { +# "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], +# "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} +# } + +# print(f'Sending translation request with text: "{text}"') +# compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) + +# if compute_response.status_code != 200: +# print(f'Error in translation request: {compute_response.status_code}') +# return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": text} + +# print('Translation request successful, processing translation...') +# compute_response_data = compute_response.json() +# translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] + +# print(f'Translation successful. Translated content: "{translated_content}"') +# return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} + +# except Exception as e: +# print(f'Translation error: {e}') +# return {"status_code": 500, "message": f"Translation error: {e}", "translated_content": text} + +# # Initialize Phi Agent +# def create_phi_agent(): +# """Create and return a Phi Agent for science education chatbot""" +# if not groq_api_key: +# return None + +# agent = Agent( +# name="Science Education Assistant", +# role="You are a helpful science tutor for 10th-grade students", +# instructions=[ +# "You are an expert science teacher specializing in 10th-grade curriculum.", +# "Provide clear, accurate, and age-appropriate explanations.", +# "Use simple language and examples that students can understand.", +# "Focus on concepts from physics, chemistry, and biology.", +# "When answering, structure your response with clear headings and bullet points when helpful.", +# "Always encourage learning and curiosity.", +# "If you're not sure about something, acknowledge it honestly." +# ], +# model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), +# markdown=True, +# show_tool_calls=False, +# debug_mode=False +# ) +# return agent + +# # Create the agent instance +# phi_agent = create_phi_agent() + +# def generate_phi_response(prompt: str, history: list = None) -> str: +# """Generate response using Phi Agent""" +# if not phi_agent: +# return "Sorry, the chatbot is not properly configured. Please check the API key settings." + +# try: +# # Build context from history if available +# context = "" +# if history: +# for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context +# if user_msg and bot_msg: +# context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" + +# # Combine context with current prompt +# full_prompt = f"{context}Current Question: {prompt}" + +# # Run the agent +# response: RunResponse = phi_agent.run(full_prompt) + +# # Extract the content from the response +# if hasattr(response, 'content') and response.content: +# return response.content +# elif hasattr(response, 'messages') and response.messages: +# # Get the last message content +# last_message = response.messages[-1] +# if hasattr(last_message, 'content'): +# return last_message.content + +# return "I apologize, but I couldn't generate a proper response. Please try again." + +# except Exception as e: +# print(f"Error in Phi Agent: {e}") +# return f"I encountered an error while processing your question: {str(e)}. Please try again." + +# # Constants +# VECTOR_COLUMN_NAME = "vector" +# TEXT_COLUMN_NAME = "text" +# proj_dir = Path(__file__).parent + +# logging.basicConfig(level=logging.INFO) +# logger = logging.getLogger(__name__) + +# # Setup Jinja2 templates +# env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) +# try: +# template = env.get_template('template.j2') +# template_html = env.get_template('template_html.j2') +# except: +# # Fallback if templates don't exist +# template = None +# template_html = None + +# def retrieve_and_generate_response(query, cross_encoder, history=None): +# """Retrieve documents and generate response""" +# top_rerank = 25 +# top_k_rank = 20 + +# if not query: +# return "Please provide a valid question." + +# logger.warning('Retrieving documents...') + +# try: +# document_start = perf_counter() + +# query_vec = retriever.encode(query) +# documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() +# documents = [doc[TEXT_COLUMN_NAME] for doc in documents] + +# query_doc_pair = [[query, doc] for doc in documents] + +# if cross_encoder == '(FAST) MiniLM-L6v2': +# cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') +# elif cross_encoder == '(ACCURATE) BGE reranker': +# cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') +# else: +# cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') # Default + +# cross_scores = cross_encoder1.predict(query_doc_pair) +# sim_scores_argsort = list(reversed(np.argsort(cross_scores))) + +# documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] + +# document_time = perf_counter() - document_start +# print(f"Document retrieval took {document_time:.2f} seconds") + +# # Create context from documents +# context = "" +# if documents: +# context = "\n\n".join(documents[:10]) # Use top 10 documents +# context = f"Context information from educational materials:\n{context}\n\n" + +# # Build conversation history +# history_context = "" +# if history: +# for user_msg, bot_msg in history[-2:]: # Last 2 exchanges +# if user_msg and bot_msg: +# history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" + +# # Create enhanced prompt +# full_prompt = f""" +# {history_context} +# {context} +# Question: {query} + +# Please answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics. +# """ + +# # Generate response using Phi Agent +# output = generate_phi_response(full_prompt, history) + +# print('Output:', output) + +# return output + +# except Exception as e: +# print(f"Error in retrieve_and_generate_response: {e}") +# return f"Sorry, I encountered an error: {str(e)}" + +# def translate_response(selected_language, response_text): +# """Translate the response to selected language""" +# if not response_text or not selected_language: +# return response_text + +# iso_language_codes = { +# "Hindi": "hi", "Gom": "gom", "Kannada": "kn", "Dogri": "doi", "Bodo": "brx", "Urdu": "ur", +# "Tamil": "ta", "Kashmiri": "ks", "Assamese": "as", "Bengali": "bn", "Marathi": "mr", +# "Sindhi": "sd", "Maithili": "mai", "Punjabi": "pa", "Malayalam": "ml", "Manipuri": "mni", +# "Telugu": "te", "Sanskrit": "sa", "Nepali": "ne", "Santali": "sat", "Gujarati": "gu", "Odia": "or" +# } + +# to_code = iso_language_codes.get(selected_language, "hi") +# translation = bhashini_translate(response_text, to_code=to_code) +# return translation.get('translated_content', response_text) + +# # Simplified Gradio interface +# with gr.Blocks(theme='gradio/soft') as CHATBOT: +# with gr.Row(): +# with gr.Column(scale=10): +# gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") +# gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") + +# with gr.Column(scale=3): +# try: +# gr.Image(value='logo.png', height=200, width=200) +# except: +# pass # Skip if logo not found + +# chatbot = gr.Chatbot( +# [], +# elem_id="chatbot", +# bubble_full_width=False, +# show_copy_button=True, +# show_share_button=True, +# ) + +# with gr.Row(): +# txt = gr.Textbox( +# scale=3, +# show_label=False, +# placeholder="Enter text and press enter", +# container=False, +# ) +# txt_btn = gr.Button(value="Submit text", scale=1) + +# cross_encoder = gr.Radio( +# choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], +# value='(ACCURATE) BGE reranker', +# label="Embeddings" +# ) + +# language_dropdown = gr.Dropdown( +# choices=[ +# "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", +# "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", +# "Gujarati", "Odia" +# ], +# value="Hindi", +# label="Select Language for Translation" +# ) + +# translated_textbox = gr.Textbox(label="Translated Response") + +# def user_message(user_input, history): +# """Handle user input and add to history""" +# if not user_input.strip(): +# return "", history +# return "", history + [[user_input, None]] + +# def bot_response(history, cross_encoder_choice): +# """Generate bot response""" +# if not history: +# return history + +# user_input = history[-1][0] + +# # Generate response +# response = retrieve_and_generate_response(user_input, cross_encoder_choice, history[:-1]) + +# # Update history +# history[-1][1] = response +# return history + +# def translate_current_response(history, selected_language): +# """Translate the current response""" +# if not history or not history[-1][1]: +# return "" + +# response_text = history[-1][1] +# translated_text = translate_response(selected_language, response_text) +# return translated_text + +# # Event handlers +# txt_msg = txt.submit(user_message, [txt, chatbot], [txt, chatbot]).then( +# bot_response, [chatbot, cross_encoder], chatbot +# ).then( +# translate_current_response, [chatbot, language_dropdown], translated_textbox +# ) + +# txt_btn.click(user_message, [txt, chatbot], [txt, chatbot]).then( +# bot_response, [chatbot, cross_encoder], chatbot +# ).then( +# translate_current_response, [chatbot, language_dropdown], translated_textbox +# ) + +# examples = [ +# 'CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?', +# 'WHAT IS IONIC BOND?', +# 'EXPLAIN ASEXUAL REPRODUCTION' +# ] + +# gr.Examples(examples, txt) + +# # Launch the Gradio application +# if __name__ == "__main__": +# CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860) +# # import gradio as gr +# # # from ragatouille import RAGPretrainedModel # COMMENTED OUT +# # import logging +# # from pathlib import Path +# # from time import perf_counter +# # from sentence_transformers import CrossEncoder +# # #from huggingface_hub import InferenceClient +# # from jinja2 import Environment, FileSystemLoader +# # import numpy as np +# # from os import getenv + +# # # Phi Agent imports +# # from phi.agent import Agent, RunResponse +# # from phi.model.groq import Groq +# # from phi.utils.pprint import pprint_run_response + +# # #from backend.query_llm import generate_hf +# # from backend.semantic_search import table, retriever + +# # # Bhashini API translation function +# # api_key = getenv('API_KEY') +# # user_id = getenv('USER_ID') +# # groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key + +# # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: +# # """Translates text from source language to target language using the Bhashini API.""" + +# # if not text.strip(): +# # print('Input text is empty. Please provide valid text for translation.') +# # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} +# # else: +# # print('Input text - ',text) +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # gr.Warning(f'Translating to {to_code}...') + +# # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' +# # headers = { +# # "Content-Type": "application/json", +# # "userID": user_id, +# # "ulcaApiKey": api_key +# # } +# # payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], +# # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} +# # } + +# # print('Sending initial request to get the pipeline...') +# # response = requests.post(url, json=payload, headers=headers) + +# # if response.status_code != 200: +# # print(f'Error in initial request: {response.status_code}') +# # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} + +# # print('Initial request successful, processing response...') +# # response_data = response.json() +# # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] +# # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] + +# # print(f'Service ID: {service_id}, Callback URL: {callback_url}') + +# # headers2 = { +# # "Content-Type": "application/json", +# # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] +# # } +# # compute_payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], +# # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} +# # } + +# # print(f'Sending translation request with text: "{text}"') +# # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) + +# # if compute_response.status_code != 200: +# # print(f'Error in translation request: {compute_response.status_code}') +# # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} + +# # print('Translation request successful, processing translation...') +# # compute_response_data = compute_response.json() +# # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] + +# # print(f'Translation successful. Translated content: "{translated_content}"') +# # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} + +# # # Initialize Phi Agent +# # def create_phi_agent(): +# # """Create and return a Phi Agent for science education chatbot""" +# # agent = Agent( +# # name="Science Education Assistant", +# # role="You are a helpful science tutor for 10th-grade students", +# # instructions=[ +# # "You are an expert science teacher specializing in 10th-grade curriculum.", +# # "Provide clear, accurate, and age-appropriate explanations.", +# # "Use simple language and examples that students can understand.", +# # "Focus on concepts from physics, chemistry, and biology.", +# # "When answering, structure your response with clear headings and bullet points when helpful.", +# # "Always encourage learning and curiosity.", +# # "If you're not sure about something, acknowledge it honestly." +# # ], +# # model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), +# # markdown=True, +# # show_tool_calls=False, +# # debug_mode=False +# # ) +# # return agent + +# # # Create the agent instance +# # phi_agent = create_phi_agent() + +# # def generate_phi_response(prompt: str, history: list = None) -> str: +# # """Generate response using Phi Agent""" +# # try: +# # # Build context from history if available +# # context = "" +# # if history: +# # for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context +# # if user_msg and bot_msg: +# # context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" + +# # # Combine context with current prompt +# # full_prompt = f"{context}Current Question: {prompt}" + +# # # Run the agent +# # response: RunResponse = phi_agent.run(full_prompt) + +# # # Extract the content from the response +# # if hasattr(response, 'content') and response.content: +# # return response.content +# # elif hasattr(response, 'messages') and response.messages: +# # # Get the last message content +# # last_message = response.messages[-1] +# # if hasattr(last_message, 'content'): +# # return last_message.content + +# # return "I apologize, but I couldn't generate a proper response. Please try again." + +# # except Exception as e: +# # print(f"Error in Phi Agent: {e}") +# # return f"I encountered an error while processing your question: {str(e)}. Please try again." + +# # # Existing chatbot functions +# # VECTOR_COLUMN_NAME = "vector" +# # TEXT_COLUMN_NAME = "text" +# # #HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") +# # proj_dir = Path(__file__).parent + +# # logging.basicConfig(level=logging.INFO) +# # logger = logging.getLogger(__name__) +# # #client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) +# # env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) + +# # template = env.get_template('template.j2') +# # template_html = env.get_template('template_html.j2') + +# # def bot(history, cross_encoder): +# # top_rerank = 25 +# # top_k_rank = 20 +# # query = history[-1][0] if history else '' +# # print('\nQuery: ',query ) +# # print('\nHistory:',history) +# # if not query: +# # gr.Warning("Please submit a non-empty string as a prompt") +# # raise ValueError("Empty string was submitted") + +# # logger.warning('Retrieving documents...') + +# # # COMMENTED OUT RAGatouille section +# # # if cross_encoder == '(HIGH ACCURATE) ColBERT': +# # # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') +# # # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") +# # # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') +# # # documents_full = RAG_db.search(query, k=top_k_rank) +# # # +# # # documents = [item['content'] for item in documents_full] +# # # prompt = template.render(documents=documents, query=query) +# # # prompt_html = template_html.render(documents=documents, query=query) +# # # +# # # generate_fn = generate_hf +# # # +# # # history[-1][1] = "" +# # # for character in generate_fn(prompt, history[:-1]): +# # # history[-1][1] = character +# # # yield history, prompt_html +# # # else: + +# # # Handle ColBERT case differently for now +# # if cross_encoder == '(HIGH ACCURATE) ColBERT': +# # gr.Warning('ColBERT is temporarily disabled. Using BGE reranker instead.') +# # cross_encoder = '(ACCURATE) BGE reranker' + +# # document_start = perf_counter() + +# # query_vec = retriever.encode(query) +# # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) + +# # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() +# # documents = [doc[TEXT_COLUMN_NAME] for doc in documents] + +# # query_doc_pair = [[query, doc] for doc in documents] +# # if cross_encoder == '(FAST) MiniLM-L6v2': +# # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') +# # elif cross_encoder == '(ACCURATE) BGE reranker': +# # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') + +# # cross_scores = cross_encoder1.predict(query_doc_pair) +# # sim_scores_argsort = list(reversed(np.argsort(cross_scores))) + +# # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] + +# # document_time = perf_counter() - document_start + +# # prompt = template.render(documents=documents, query=query) +# # prompt_html = template_html.render(documents=documents, query=query) + +# # # REPLACED: Use Phi Agent instead of generate_qwen +# # print("Using Phi Agent for response generation...") +# # output = generate_phi_response(prompt, history[:-1]) + +# # print('Output:',output) + +# # # Update history with the response +# # history_list = list(history[-1]) +# # history_list[1] = output +# # history[-1] = tuple(history_list) + +# # yield history, prompt_html + +# # def translate_text(selected_language, history): +# # iso_language_codes = { +# # "Hindi": "hi", +# # "Gom": "gom", +# # "Kannada": "kn", +# # "Dogri": "doi", +# # "Bodo": "brx", +# # "Urdu": "ur", +# # "Tamil": "ta", +# # "Kashmiri": "ks", +# # "Assamese": "as", +# # "Bengali": "bn", +# # "Marathi": "mr", +# # "Sindhi": "sd", +# # "Maithili": "mai", +# # "Punjabi": "pa", +# # "Malayalam": "ml", +# # "Manipuri": "mni", +# # "Telugu": "te", +# # "Sanskrit": "sa", +# # "Nepali": "ne", +# # "Santali": "sat", +# # "Gujarati": "gu", +# # "Odia": "or" +# # } + +# # to_code = iso_language_codes[selected_language] +# # response_text = history[-1][1] if history else '' +# # print('response_text for translation',response_text) +# # translation = bhashini_translate(response_text, to_code=to_code) +# # return translation['translated_content'] + +# # # Gradio interface - SIMPLIFIED to avoid schema issues +# # with gr.Blocks(theme='gradio/soft') as CHATBOT: +# # history_state = gr.State([]) +# # with gr.Row(): +# # with gr.Column(scale=10): +# # gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") +# # gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") + +# # with gr.Column(scale=3): +# # try: +# # gr.Image(value='logo.png', height=200, width=200) +# # except: +# # pass # Skip if logo not found + +# # chatbot = gr.Chatbot( +# # [], +# # elem_id="chatbot", +# # bubble_full_width=False, +# # show_copy_button=True, +# # show_share_button=True, +# # ) + +# # with gr.Row(): +# # txt = gr.Textbox( +# # scale=3, +# # show_label=False, +# # placeholder="Enter text and press enter", +# # container=False, +# # ) +# # txt_btn = gr.Button(value="Submit text", scale=1) + +# # # SIMPLIFIED: Remove ColBERT option temporarily +# # cross_encoder = gr.Radio( +# # choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'], +# # value='(ACCURATE) BGE reranker', +# # label="Embeddings" +# # ) + +# # language_dropdown = gr.Dropdown( +# # choices=[ +# # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", +# # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", +# # "Gujarati", "Odia" +# # ], +# # value="Hindi", # default to Hindi +# # label="Select Language for Translation" +# # ) + +# # prompt_html = gr.HTML() + +# # translated_textbox = gr.Textbox(label="Translated Response") + +# # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): +# # print('History state',history_state) +# # history = history_state +# # history.append((txt, "")) + +# # # Call bot function +# # bot_output = next(bot(history, cross_encoder)) +# # print('bot_output',bot_output) +# # history, prompt_html = bot_output +# # print('History',history) + +# # # Update the history state +# # history_state[:] = history + +# # # Translate text +# # translated_text = translate_text(language_dropdown, history) +# # return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input + +# # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) +# # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) + +# # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', +# # 'EXPLAIN ASEXUAL REPRODUCTION'] + +# # gr.Examples(examples, txt) + +# # # Launch the Gradio application +# # if __name__ == "__main__": +# # CHATBOT.queue().launch(server_name="0.0.0.0", server_port=7860)# import requests +# # import gradio as gr +# # from ragatouille import RAGPretrainedModel +# # import logging +# # from pathlib import Path +# # from time import perf_counter +# # from sentence_transformers import CrossEncoder +# # from huggingface_hub import InferenceClient +# # from jinja2 import Environment, FileSystemLoader +# # import numpy as np +# # from os import getenv + +# # # Phi Agent imports +# # from phi.agent import Agent, RunResponse +# # from phi.model.groq import Groq +# # from phi.utils.pprint import pprint_run_response + +# # #from backend.query_llm import generate_hf +# # from backend.semantic_search import table, retriever + +# # # Bhashini API translation function +# # api_key = getenv('API_KEY') +# # user_id = getenv('USER_ID') +# # groq_api_key = getenv('GROQ_API_KEY') # Add GROQ API key + +# # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: +# # """Translates text from source language to target language using the Bhashini API.""" + +# # if not text.strip(): +# # print('Input text is empty. Please provide valid text for translation.') +# # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} +# # else: +# # print('Input text - ',text) +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # gr.Warning(f'Translating to {to_code}...') + +# # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' +# # headers = { +# # "Content-Type": "application/json", +# # "userID": user_id, +# # "ulcaApiKey": api_key +# # } +# # payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], +# # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} +# # } + +# # print('Sending initial request to get the pipeline...') +# # response = requests.post(url, json=payload, headers=headers) + +# # if response.status_code != 200: +# # print(f'Error in initial request: {response.status_code}') +# # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} + +# # print('Initial request successful, processing response...') +# # response_data = response.json() +# # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] +# # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] + +# # print(f'Service ID: {service_id}, Callback URL: {callback_url}') + +# # headers2 = { +# # "Content-Type": "application/json", +# # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] +# # } +# # compute_payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], +# # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} +# # } + +# # print(f'Sending translation request with text: "{text}"') +# # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) + +# # if compute_response.status_code != 200: +# # print(f'Error in translation request: {compute_response.status_code}') +# # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} + +# # print('Translation request successful, processing translation...') +# # compute_response_data = compute_response.json() +# # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] + +# # print(f'Translation successful. Translated content: "{translated_content}"') +# # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} + +# # # Initialize Phi Agent +# # def create_phi_agent(): +# # """Create and return a Phi Agent for science education chatbot""" +# # agent = Agent( +# # name="Science Education Assistant", +# # role="You are a helpful science tutor for 10th-grade students", +# # instructions=[ +# # "You are an expert science teacher specializing in 10th-grade curriculum.", +# # "Provide clear, accurate, and age-appropriate explanations.", +# # "Use simple language and examples that students can understand.", +# # "Focus on concepts from physics, chemistry, and biology.", +# # "When answering, structure your response with clear headings and bullet points when helpful.", +# # "Always encourage learning and curiosity.", +# # "If you're not sure about something, acknowledge it honestly." +# # ], +# # model=Groq(id="llama3-groq-70b-8192-tool-use-preview", api_key=groq_api_key), +# # markdown=True, +# # show_tool_calls=False, +# # debug_mode=False +# # ) +# # return agent + +# # # Create the agent instance +# # phi_agent = create_phi_agent() + +# # def generate_phi_response(prompt: str, history: list = None) -> str: +# # """Generate response using Phi Agent""" +# # try: +# # # Build context from history if available +# # context = "" +# # if history: +# # for user_msg, bot_msg in history[-3:]: # Last 3 exchanges for context +# # if user_msg and bot_msg: +# # context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n\n" + +# # # Combine context with current prompt +# # full_prompt = f"{context}Current Question: {prompt}" + +# # # Run the agent +# # response: RunResponse = phi_agent.run(full_prompt) + +# # # Extract the content from the response +# # if hasattr(response, 'content') and response.content: +# # return response.content +# # elif hasattr(response, 'messages') and response.messages: +# # # Get the last message content +# # last_message = response.messages[-1] +# # if hasattr(last_message, 'content'): +# # return last_message.content + +# # return "I apologize, but I couldn't generate a proper response. Please try again." + +# # except Exception as e: +# # print(f"Error in Phi Agent: {e}") +# # return f"I encountered an error while processing your question: {str(e)}. Please try again." + +# # # Existing chatbot functions +# # VECTOR_COLUMN_NAME = "vector" +# # TEXT_COLUMN_NAME = "text" +# # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") +# # proj_dir = Path(__file__).parent + +# # logging.basicConfig(level=logging.INFO) +# # logger = logging.getLogger(__name__) +# # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) +# # env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) + +# # template = env.get_template('template.j2') +# # template_html = env.get_template('template_html.j2') + +# # def bot(history, cross_encoder): +# # top_rerank = 25 +# # top_k_rank = 20 +# # query = history[-1][0] if history else '' +# # print('\nQuery: ',query ) +# # print('\nHistory:',history) +# # if not query: +# # gr.Warning("Please submit a non-empty string as a prompt") +# # raise ValueError("Empty string was submitted") + +# # logger.warning('Retrieving documents...') + +# # if cross_encoder == '(HIGH ACCURATE) ColBERT': +# # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') +# # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") +# # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') +# # documents_full = RAG_db.search(query, k=top_k_rank) + +# # documents = [item['content'] for item in documents_full] +# # prompt = template.render(documents=documents, query=query) +# # prompt_html = template_html.render(documents=documents, query=query) + +# # generate_fn = generate_hf + +# # history[-1][1] = "" +# # for character in generate_fn(prompt, history[:-1]): +# # history[-1][1] = character +# # yield history, prompt_html +# # else: +# # document_start = perf_counter() + +# # query_vec = retriever.encode(query) +# # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) + +# # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() +# # documents = [doc[TEXT_COLUMN_NAME] for doc in documents] + +# # query_doc_pair = [[query, doc] for doc in documents] +# # if cross_encoder == '(FAST) MiniLM-L6v2': +# # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') +# # elif cross_encoder == '(ACCURATE) BGE reranker': +# # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') + +# # cross_scores = cross_encoder1.predict(query_doc_pair) +# # sim_scores_argsort = list(reversed(np.argsort(cross_scores))) + +# # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] + +# # document_time = perf_counter() - document_start + +# # prompt = template.render(documents=documents, query=query) +# # prompt_html = template_html.render(documents=documents, query=query) + +# # # REPLACED: Use Phi Agent instead of generate_qwen +# # print("Using Phi Agent for response generation...") +# # output = generate_phi_response(prompt, history[:-1]) + +# # print('Output:',output) + +# # # Update history with the response +# # history_list = list(history[-1]) +# # history_list[1] = output +# # history[-1] = tuple(history_list) + +# # yield history, prompt_html + +# # def translate_text(selected_language, history): +# # iso_language_codes = { +# # "Hindi": "hi", +# # "Gom": "gom", +# # "Kannada": "kn", +# # "Dogri": "doi", +# # "Bodo": "brx", +# # "Urdu": "ur", +# # "Tamil": "ta", +# # "Kashmiri": "ks", +# # "Assamese": "as", +# # "Bengali": "bn", +# # "Marathi": "mr", +# # "Sindhi": "sd", +# # "Maithili": "mai", +# # "Punjabi": "pa", +# # "Malayalam": "ml", +# # "Manipuri": "mni", +# # "Telugu": "te", +# # "Sanskrit": "sa", +# # "Nepali": "ne", +# # "Santali": "sat", +# # "Gujarati": "gu", +# # "Odia": "or" +# # } + +# # to_code = iso_language_codes[selected_language] +# # response_text = history[-1][1] if history else '' +# # print('response_text for translation',response_text) +# # translation = bhashini_translate(response_text, to_code=to_code) +# # return translation['translated_content'] + +# # # Gradio interface +# # with gr.Blocks(theme='gradio/soft') as CHATBOT: +# # history_state = gr.State([]) +# # with gr.Row(): +# # with gr.Column(scale=10): +# # gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Phi Agent & Groq LLMs for 10 std students
""") +# # gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") + +# # with gr.Column(scale=3): +# # gr.Image(value='logo.png', height=200, width=200) + +# # chatbot = gr.Chatbot( +# # [], +# # elem_id="chatbot", +# # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg', +# # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'), +# # bubble_full_width=False, +# # show_copy_button=True, +# # show_share_button=True, +# # ) + +# # with gr.Row(): +# # txt = gr.Textbox( +# # scale=3, +# # show_label=False, +# # placeholder="Enter text and press enter", +# # container=False, +# # ) +# # txt_btn = gr.Button(value="Submit text", scale=1) + +# # 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)") +# # language_dropdown = gr.Dropdown( +# # choices=[ +# # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", +# # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", +# # "Gujarati", "Odia" +# # ], +# # value="Hindi", # default to Hindi +# # label="Select Language for Translation" +# # ) + +# # prompt_html = gr.HTML() + +# # translated_textbox = gr.Textbox(label="Translated Response") + +# # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): +# # print('History state',history_state) +# # history = history_state +# # history.append((txt, "")) + +# # # Call bot function +# # bot_output = next(bot(history, cross_encoder)) +# # print('bot_output',bot_output) +# # history, prompt_html = bot_output +# # print('History',history) + +# # # Update the history state +# # history_state[:] = history + +# # # Translate text +# # translated_text = translate_text(language_dropdown, history) +# # return history, prompt_html, translated_text, gr.Textbox(value="", interactive=True) # Clear input + +# # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) +# # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox, txt]) + +# # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', +# # 'EXPLAIN ASEXUAL REPRODUCTION'] + +# # gr.Examples(examples, txt) + +# # # Launch the Gradio application +# # if __name__ == "__main__": +# # CHATBOT.launch(share=True, debug=True)# import requests +# # import gradio as gr +# # from ragatouille import RAGPretrainedModel +# # import logging +# # from pathlib import Path +# # from time import perf_counter +# # from sentence_transformers import CrossEncoder +# # from huggingface_hub import InferenceClient +# # from jinja2 import Environment, FileSystemLoader +# # import numpy as np +# # from os import getenv +# # from backend.query_llm import generate_hf, generate_qwen +# # from backend.semantic_search import table, retriever +# # from huggingface_hub import InferenceClient + + +# # # Bhashini API translation function +# # api_key = getenv('API_KEY') +# # user_id = getenv('USER_ID') + +# # def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict: +# # """Translates text from source language to target language using the Bhashini API.""" + +# # if not text.strip(): +# # print('Input text is empty. Please provide valid text for translation.') +# # return {"status_code": 400, "message": "Input text is empty", "translated_content": None, "speech_content": None} +# # else: +# # print('Input text - ',text) +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # print(f'Starting translation process from {from_code} to {to_code}...') +# # gr.Warning(f'Translating to {to_code}...') + +# # url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline' +# # headers = { +# # "Content-Type": "application/json", +# # "userID": user_id, +# # "ulcaApiKey": api_key +# # } +# # payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}}}], +# # "pipelineRequestConfig": {"pipelineId": "64392f96daac500b55c543cd"} +# # } + +# # print('Sending initial request to get the pipeline...') +# # response = requests.post(url, json=payload, headers=headers) + +# # if response.status_code != 200: +# # print(f'Error in initial request: {response.status_code}') +# # return {"status_code": response.status_code, "message": "Error in translation request", "translated_content": None} + +# # print('Initial request successful, processing response...') +# # response_data = response.json() +# # service_id = response_data["pipelineResponseConfig"][0]["config"][0]["serviceId"] +# # callback_url = response_data["pipelineInferenceAPIEndPoint"]["callbackUrl"] + +# # print(f'Service ID: {service_id}, Callback URL: {callback_url}') + +# # headers2 = { +# # "Content-Type": "application/json", +# # response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["name"]: response_data["pipelineInferenceAPIEndPoint"]["inferenceApiKey"]["value"] +# # } +# # compute_payload = { +# # "pipelineTasks": [{"taskType": "translation", "config": {"language": {"sourceLanguage": from_code, "targetLanguage": to_code}, "serviceId": service_id}}], +# # "inputData": {"input": [{"source": text}], "audio": [{"audioContent": None}]} +# # } + +# # print(f'Sending translation request with text: "{text}"') +# # compute_response = requests.post(callback_url, json=compute_payload, headers=headers2) + +# # if compute_response.status_code != 200: +# # print(f'Error in translation request: {compute_response.status_code}') +# # return {"status_code": compute_response.status_code, "message": "Error in translation", "translated_content": None} + +# # print('Translation request successful, processing translation...') +# # compute_response_data = compute_response.json() +# # translated_content = compute_response_data["pipelineResponse"][0]["output"][0]["target"] + +# # print(f'Translation successful. Translated content: "{translated_content}"') +# # return {"status_code": 200, "message": "Translation successful", "translated_content": translated_content} + + +# # # Existing chatbot functions +# # VECTOR_COLUMN_NAME = "vector" +# # TEXT_COLUMN_NAME = "text" +# # HF_TOKEN = getenv("HUGGING_FACE_HUB_TOKEN") +# # proj_dir = Path(__file__).parent + +# # logging.basicConfig(level=logging.INFO) +# # logger = logging.getLogger(__name__) +# # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1", token=HF_TOKEN) +# # env = Environment(loader=FileSystemLoader(proj_dir / 'templates')) + +# # template = env.get_template('template.j2') +# # template_html = env.get_template('template_html.j2') + +# # # def add_text(history, text): +# # # history = [] if history is None else history +# # # history = history + [(text, None)] +# # # return history, gr.Textbox(value="", interactive=False) + +# # def bot(history, cross_encoder): + +# # top_rerank = 25 +# # top_k_rank = 20 +# # query = history[-1][0] if history else '' +# # print('\nQuery: ',query ) +# # print('\nHistory:',history) +# # if not query: +# # gr.Warning("Please submit a non-empty string as a prompt") +# # raise ValueError("Empty string was submitted") + +# # logger.warning('Retrieving documents...') + +# # if cross_encoder == '(HIGH ACCURATE) ColBERT': +# # gr.Warning('Retrieving using ColBERT.. First time query will take a minute for model to load..pls wait') +# # RAG = RAGPretrainedModel.from_pretrained("colbert-ir/colbertv2.0") +# # RAG_db = RAG.from_index('.ragatouille/colbert/indexes/cbseclass10index') +# # documents_full = RAG_db.search(query, k=top_k_rank) + +# # documents = [item['content'] for item in documents_full] +# # prompt = template.render(documents=documents, query=query) +# # prompt_html = template_html.render(documents=documents, query=query) + +# # generate_fn = generate_hf + +# # history[-1][1] = "" +# # for character in generate_fn(prompt, history[:-1]): +# # history[-1][1] = character +# # yield history, prompt_html +# # else: +# # document_start = perf_counter() + +# # query_vec = retriever.encode(query) +# # doc1 = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_k_rank) + +# # documents = table.search(query_vec, vector_column_name=VECTOR_COLUMN_NAME).limit(top_rerank).to_list() +# # documents = [doc[TEXT_COLUMN_NAME] for doc in documents] + +# # query_doc_pair = [[query, doc] for doc in documents] +# # if cross_encoder == '(FAST) MiniLM-L6v2': +# # cross_encoder1 = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2') +# # elif cross_encoder == '(ACCURATE) BGE reranker': +# # cross_encoder1 = CrossEncoder('BAAI/bge-reranker-base') + +# # cross_scores = cross_encoder1.predict(query_doc_pair) +# # sim_scores_argsort = list(reversed(np.argsort(cross_scores))) + +# # documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]] + +# # document_time = perf_counter() - document_start + +# # prompt = template.render(documents=documents, query=query) +# # prompt_html = template_html.render(documents=documents, query=query) + +# # #generate_fn = generate_hf +# # generate_fn=generate_qwen +# # # Create a new history entry instead of modifying the tuple directly +# # new_history = history[:-1] + [ (prompt, "") ] # query replaced prompt +# # output='' +# # # for character in generate_fn(prompt, history[:-1]): +# # # #new_history[-1] = (query, character) +# # # output+=character +# # output=generate_fn(prompt, history[:-1]) + +# # print('Output:',output) +# # new_history[-1] = (prompt, output) #query replaced with prompt +# # print('New History',new_history) +# # #print('prompt html',prompt_html)# Update the last tuple with new text + +# # history_list = list(history[-1]) +# # history_list[1] = output # Assuming `character` is what you want to assign +# # # Update the history with the modified list converted back to a tuple +# # history[-1] = tuple(history_list) + +# # #history[-1][1] = character +# # # yield new_history, prompt_html +# # yield history, prompt_html +# # # new_history,prompt_html +# # # history[-1][1] = "" +# # # for character in generate_fn(prompt, history[:-1]): +# # # history[-1][1] = character +# # # yield history, prompt_html + +# # #def translate_text(response_text, selected_language): + +# # def translate_text(selected_language,history): + +# # iso_language_codes = { +# # "Hindi": "hi", +# # "Gom": "gom", +# # "Kannada": "kn", +# # "Dogri": "doi", +# # "Bodo": "brx", +# # "Urdu": "ur", +# # "Tamil": "ta", +# # "Kashmiri": "ks", +# # "Assamese": "as", +# # "Bengali": "bn", +# # "Marathi": "mr", +# # "Sindhi": "sd", +# # "Maithili": "mai", +# # "Punjabi": "pa", +# # "Malayalam": "ml", +# # "Manipuri": "mni", +# # "Telugu": "te", +# # "Sanskrit": "sa", +# # "Nepali": "ne", +# # "Santali": "sat", +# # "Gujarati": "gu", +# # "Odia": "or" +# # } + +# # to_code = iso_language_codes[selected_language] +# # response_text = history[-1][1] if history else '' +# # print('response_text for translation',response_text) +# # translation = bhashini_translate(response_text, to_code=to_code) +# # return translation['translated_content'] + + +# # # Gradio interface +# # with gr.Blocks(theme='gradio/soft') as CHATBOT: +# # history_state = gr.State([]) +# # with gr.Row(): +# # with gr.Column(scale=10): +# # gr.HTML(value="""A free chat bot developed by K.M.RAMYASRI,TGT,GHS.SUTHUKENY using Open source LLMs for 10 std students
""") +# # gr.HTML(value=f"""Suggestions may be sent to ramyadevi1607@yahoo.com.
""") + +# # with gr.Column(scale=3): +# # gr.Image(value='logo.png', height=200, width=200) + +# # chatbot = gr.Chatbot( +# # [], +# # elem_id="chatbot", +# # avatar_images=('https://aui.atlassian.com/aui/8.8/docs/images/avatar-person.svg', +# # 'https://huggingface.co/datasets/huggingface/brand-assets/resolve/main/hf-logo.svg'), +# # bubble_full_width=False, +# # show_copy_button=True, +# # show_share_button=True, +# # ) + +# # with gr.Row(): +# # txt = gr.Textbox( +# # scale=3, +# # show_label=False, +# # placeholder="Enter text and press enter", +# # container=False, +# # ) +# # txt_btn = gr.Button(value="Submit text", scale=1) + +# # 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)") +# # language_dropdown = gr.Dropdown( +# # choices=[ +# # "Hindi", "Gom", "Kannada", "Dogri", "Bodo", "Urdu", "Tamil", "Kashmiri", "Assamese", "Bengali", "Marathi", +# # "Sindhi", "Maithili", "Punjabi", "Malayalam", "Manipuri", "Telugu", "Sanskrit", "Nepali", "Santali", +# # "Gujarati", "Odia" +# # ], +# # value="Hindi", # default to Hindi +# # label="Select Language for Translation" +# # ) + +# # prompt_html = gr.HTML() + +# # translated_textbox = gr.Textbox(label="Translated Response") +# # def update_history_and_translate(txt, cross_encoder, history_state, language_dropdown): +# # print('History state',history_state) +# # history = history_state +# # history.append((txt, "")) +# # #history_state.value=(history) + +# # # Call bot function +# # # bot_output = list(bot(history, cross_encoder)) +# # bot_output = next(bot(history, cross_encoder)) +# # print('bot_output',bot_output) +# # #history, prompt_html = bot_output[-1] +# # history, prompt_html = bot_output +# # print('History',history) +# # # Update the history state +# # history_state[:] = history + +# # # Translate text +# # translated_text = translate_text(language_dropdown, history) +# # return history, prompt_html, translated_text + +# # txt_msg = txt_btn.click(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox]) +# # txt_msg = txt.submit(update_history_and_translate, [txt, cross_encoder, history_state, language_dropdown], [chatbot, prompt_html, translated_textbox]) + +# # examples = ['CAN U SAY THE DIFFERENCES BETWEEN METALS AND NON METALS?','WHAT IS IONIC BOND?', +# # 'EXPLAIN ASEXUAL REPRODUCTION'] + +# # gr.Examples(examples, txt) + + +# # # Launch the Gradio application +# # CHATBOT.launch(share=True,debug=True) +