Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ from phi.agent import Agent
|
|
| 3 |
from phi.model.groq import Groq
|
| 4 |
import os
|
| 5 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Set up logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -19,58 +23,172 @@ else:
|
|
| 19 |
|
| 20 |
# Initialize PhiData Agent
|
| 21 |
agent = Agent(
|
| 22 |
-
|
|
|
|
| 23 |
instructions=[
|
| 24 |
-
"You are
|
| 25 |
-
"Provide
|
| 26 |
-
"
|
|
|
|
|
|
|
|
|
|
| 27 |
],
|
|
|
|
| 28 |
markdown=True
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Generate response using PhiData agent
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
-
logger.error(f"
|
| 43 |
-
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Add to history
|
| 46 |
-
history.append([message,
|
| 47 |
|
| 48 |
return "", history
|
| 49 |
|
| 50 |
# Minimal working interface
|
| 51 |
-
with gr.Blocks() as demo:
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
msg = gr.Textbox(placeholder="Type your message here...")
|
| 54 |
clear = gr.Button("Clear")
|
| 55 |
|
| 56 |
-
msg.submit(simple_chat_function, [msg, chatbot], [msg, chatbot])
|
| 57 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
demo.launch()
|
| 61 |
-
#
|
| 62 |
-
# import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# def simple_chat_function(message, history):
|
| 65 |
-
# """
|
| 66 |
# if not message.strip():
|
| 67 |
# return "", history
|
| 68 |
|
| 69 |
-
# #
|
| 70 |
-
#
|
|
|
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
# # Add to history
|
| 73 |
-
# history.append([message,
|
| 74 |
|
| 75 |
# return "", history
|
| 76 |
|
|
|
|
| 3 |
from phi.model.groq import Groq
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
+
from sentence_transformers import CrossEncoder
|
| 7 |
+
from backend.semantic_search import table, retriever
|
| 8 |
+
import numpy as np
|
| 9 |
+
from time import perf_counter
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 23 |
|
| 24 |
# Initialize PhiData Agent
|
| 25 |
agent = Agent(
|
| 26 |
+
name="Science Education Assistant",
|
| 27 |
+
role="You are a helpful science tutor for 10th-grade students",
|
| 28 |
instructions=[
|
| 29 |
+
"You are an expert science teacher specializing in 10th-grade curriculum.",
|
| 30 |
+
"Provide clear, accurate, and age-appropriate explanations.",
|
| 31 |
+
"Use simple language and examples that students can understand.",
|
| 32 |
+
"Focus on concepts from physics, chemistry, and biology.",
|
| 33 |
+
"Structure responses with headings and bullet points when helpful.",
|
| 34 |
+
"Encourage learning and curiosity."
|
| 35 |
],
|
| 36 |
+
model=Groq(id="llama3-70b-8192", api_key=api_key),
|
| 37 |
markdown=True
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Response Generation Function
|
| 41 |
+
def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
| 42 |
+
"""Generate response using semantic search and LLM"""
|
| 43 |
+
top_rerank = 25
|
| 44 |
+
top_k_rank = 20
|
| 45 |
+
|
| 46 |
+
if not query.strip():
|
| 47 |
+
return "Please provide a valid question."
|
| 48 |
|
|
|
|
| 49 |
try:
|
| 50 |
+
start_time = perf_counter()
|
| 51 |
+
|
| 52 |
+
# Encode query and search documents
|
| 53 |
+
query_vec = retriever.encode(query)
|
| 54 |
+
documents = table.search(query_vec, vector_column_name="vector").limit(top_rerank).to_list()
|
| 55 |
+
documents = [doc["text"] for doc in documents]
|
| 56 |
+
|
| 57 |
+
# Re-rank documents using cross-encoder
|
| 58 |
+
cross_encoder_model = CrossEncoder('BAAI/bge-reranker-base') if cross_encoder_choice == '(ACCURATE) BGE reranker' else CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
| 59 |
+
query_doc_pair = [[query, doc] for doc in documents]
|
| 60 |
+
cross_scores = cross_encoder_model.predict(query_doc_pair)
|
| 61 |
+
sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
| 62 |
+
documents = [documents[idx] for idx in sim_scores_argsort[:top_k_rank]]
|
| 63 |
+
|
| 64 |
+
# Create context from top documents
|
| 65 |
+
context = "\n\n".join(documents[:10]) if documents else ""
|
| 66 |
+
context = f"Context information from educational materials:\n{context}\n\n"
|
| 67 |
+
|
| 68 |
+
# Add conversation history for context
|
| 69 |
+
history_context = ""
|
| 70 |
+
if history and len(history) > 0:
|
| 71 |
+
for user_msg, bot_msg in history[-2:]: # Last 2 exchanges
|
| 72 |
+
if user_msg and bot_msg:
|
| 73 |
+
history_context += f"Previous Q: {user_msg}\nPrevious A: {bot_msg}\n"
|
| 74 |
+
|
| 75 |
+
# Create full prompt
|
| 76 |
+
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."
|
| 77 |
+
|
| 78 |
+
# Generate response
|
| 79 |
+
response = agent.run(full_prompt)
|
| 80 |
+
response_text = response.content if hasattr(response, 'content') else str(response)
|
| 81 |
+
|
| 82 |
+
logger.info(f"Response generation took {perf_counter() - start_time:.2f} seconds")
|
| 83 |
+
return response_text
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
+
logger.error(f"Error in response generation: {e}")
|
| 87 |
+
return f"Error generating response: {str(e)}"
|
| 88 |
|
| 89 |
+
def simple_chat_function(message, history, cross_encoder_choice):
|
| 90 |
+
"""Chat function with semantic search and retriever integration"""
|
| 91 |
+
if not message.strip():
|
| 92 |
+
return "", history
|
| 93 |
+
|
| 94 |
+
# Generate response using the semantic search function
|
| 95 |
+
response = retrieve_and_generate_response(message, cross_encoder_choice, history)
|
| 96 |
+
|
| 97 |
# Add to history
|
| 98 |
+
history.append([message, response])
|
| 99 |
|
| 100 |
return "", history
|
| 101 |
|
| 102 |
# Minimal working interface
|
| 103 |
+
with gr.Blocks(title="Science Chatbot") as demo:
|
| 104 |
+
# Cross-encoder selection
|
| 105 |
+
cross_encoder = gr.Radio(
|
| 106 |
+
choices=['(FAST) MiniLM-L6v2', '(ACCURATE) BGE reranker'],
|
| 107 |
+
value='(ACCURATE) BGE reranker',
|
| 108 |
+
label="Embeddings Model",
|
| 109 |
+
info="Select the model for document ranking"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
chatbot = gr.Chatbot(label="Science Tutor Conversation")
|
| 113 |
msg = gr.Textbox(placeholder="Type your message here...")
|
| 114 |
clear = gr.Button("Clear")
|
| 115 |
|
| 116 |
+
msg.submit(simple_chat_function, [msg, chatbot, cross_encoder], [msg, chatbot])
|
| 117 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
+
demo.launch()# import gradio as gr
|
| 121 |
+
# from phi.agent import Agent
|
| 122 |
+
# from phi.model.groq import Groq
|
| 123 |
+
# import os
|
| 124 |
+
# import logging
|
| 125 |
+
# from sentence_transformers import SentenceTransformer
|
| 126 |
+
# from typing import List
|
| 127 |
+
|
| 128 |
+
# # Set up logging
|
| 129 |
+
# logging.basicConfig(level=logging.INFO)
|
| 130 |
+
# logger = logging.getLogger(__name__)
|
| 131 |
+
|
| 132 |
+
# # API Key setup
|
| 133 |
+
# api_key = os.getenv("GROQ_API_KEY")
|
| 134 |
+
# if not api_key:
|
| 135 |
+
# gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
|
| 136 |
+
# logger.error("GROQ_API_KEY not found.")
|
| 137 |
+
# api_key = "" # Fallback to empty string, but this will fail without a key
|
| 138 |
+
# else:
|
| 139 |
+
# os.environ["GROQ_API_KEY"] = api_key
|
| 140 |
+
|
| 141 |
+
# # Initialize PhiData Agent
|
| 142 |
+
# agent = Agent(
|
| 143 |
+
# model=Groq(model="llama3-70b-8192", api_key=api_key),
|
| 144 |
+
# instructions=[
|
| 145 |
+
# "You are a helpful assistant designed to answer questions on various topics.",
|
| 146 |
+
# "Use the provided context from retrieved documents to answer questions.",
|
| 147 |
+
# "If you don't have enough information, say 'I don’t have enough information to answer that.'"
|
| 148 |
+
# ],
|
| 149 |
+
# markdown=True
|
| 150 |
+
# )
|
| 151 |
+
|
| 152 |
+
# # Load a simple embedding model
|
| 153 |
+
# embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 154 |
+
|
| 155 |
+
# # Simulated document corpus
|
| 156 |
+
# documents = [
|
| 157 |
+
# "The capital of France is Paris.",
|
| 158 |
+
# "Python is a popular programming language.",
|
| 159 |
+
# "Semantic search uses embeddings to find relevant documents.",
|
| 160 |
+
# "The Eiffel Tower is located in Paris."
|
| 161 |
+
# ]
|
| 162 |
+
|
| 163 |
+
# # Convert documents to embeddings and store them
|
| 164 |
+
# document_embeddings = embedding_model.encode(documents, convert_to_tensor=True)
|
| 165 |
+
# import numpy as np
|
| 166 |
+
# def retrieve_documents(query: str, k: int = 2) -> List[str]:
|
| 167 |
+
# """Simple retriever using cosine similarity."""
|
| 168 |
+
# query_embedding = embedding_model.encode(query, convert_to_tensor=True)
|
| 169 |
+
# similarities = np.dot(document_embeddings, query_embedding.T).cpu().numpy()
|
| 170 |
+
# top_k_indices = similarities.argsort()[-k:][::-1]
|
| 171 |
+
# return [documents[i] for i in top_k_indices]
|
| 172 |
|
| 173 |
# def simple_chat_function(message, history):
|
| 174 |
+
# """Chat function with semantic search and retriever integration"""
|
| 175 |
# if not message.strip():
|
| 176 |
# return "", history
|
| 177 |
|
| 178 |
+
# # Retrieve relevant documents
|
| 179 |
+
# context = retrieve_documents(message)
|
| 180 |
+
# context_text = "\n".join(context) if context else "No relevant context found."
|
| 181 |
|
| 182 |
+
# # Generate response using PhiData agent with context
|
| 183 |
+
# try:
|
| 184 |
+
# response = agent.run(f"Context: {context_text}\n\nQuestion: {message}")
|
| 185 |
+
# response_text = response.content if hasattr(response, 'content') else "Error generating response."
|
| 186 |
+
# except Exception as e:
|
| 187 |
+
# logger.error(f"Agent error: {e}")
|
| 188 |
+
# response_text = "Sorry, there was an error processing your request."
|
| 189 |
+
|
| 190 |
# # Add to history
|
| 191 |
+
# history.append([message, response_text])
|
| 192 |
|
| 193 |
# return "", history
|
| 194 |
|