Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,124 @@
|
|
| 1 |
-
# Step 1: Import Libraries
|
| 2 |
-
# No changes needed here, these will be installed from your requirements.txt
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from sentence_transformers import SentenceTransformer
|
| 5 |
-
import faiss
|
| 6 |
-
import numpy as np
|
| 7 |
-
import google.generativeai as genai
|
| 8 |
-
import pickle
|
| 9 |
-
import os
|
| 10 |
-
|
| 11 |
-
# Step 2: Configure API Key from Hugging Face Secrets
|
| 12 |
-
# This section is modified to securely access the API key from your Space's secrets.
|
| 13 |
-
try:
|
| 14 |
-
# Make sure to set 'GOOGLE_API_KEY' in your Hugging Face Space's settings/secrets
|
| 15 |
-
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 16 |
-
if GOOGLE_API_KEY is None:
|
| 17 |
-
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
|
| 18 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
| 19 |
-
print("API Key configured successfully.")
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"ERROR: Could not configure API key. Please ensure 'GOOGLE_API_KEY' is set in your Hugging Face Space secrets. Details: {e}")
|
| 22 |
-
|
| 23 |
-
# Step 3: Define Data Path
|
| 24 |
-
# This is updated to point to the 'rag_chatbot_data' folder you uploaded to the Space.
|
| 25 |
-
# It no longer uses Google Drive.
|
| 26 |
-
DATA_PATH = "
|
| 27 |
-
vector_store_file = os.path.join(DATA_PATH, "vector_store.index")
|
| 28 |
-
data_file = os.path.join(DATA_PATH, "data.pkl")
|
| 29 |
-
|
| 30 |
-
# Step 4: Load Models and Pre-processed Data
|
| 31 |
-
# This section is slightly restructured to load models only if the data is found.
|
| 32 |
-
vector_store_data = None
|
| 33 |
-
if os.path.exists(vector_store_file) and os.path.exists(data_file):
|
| 34 |
-
try:
|
| 35 |
-
print("Loading pre-processed data from the repository...")
|
| 36 |
-
index = faiss.read_index(vector_store_file)
|
| 37 |
-
with open(data_file, "rb") as f:
|
| 38 |
-
stored_data = pickle.load(f)
|
| 39 |
-
texts = stored_data["texts"]
|
| 40 |
-
sources = stored_data["sources"]
|
| 41 |
-
print("β
Data loaded successfully.")
|
| 42 |
-
|
| 43 |
-
# Store everything in a state object for Gradio
|
| 44 |
-
vector_store_data = (index, texts, sources)
|
| 45 |
-
|
| 46 |
-
# Load the embedding and LLM models
|
| 47 |
-
print("Loading AI and embedding models...")
|
| 48 |
-
embedding_model = SentenceTransformer('BAAI/bge-large-en-v1.5')
|
| 49 |
-
llm = genai.GenerativeModel('gemini-pro') # Using gemini-pro as it's a robust choice
|
| 50 |
-
print("β
Models loaded successfully.")
|
| 51 |
-
|
| 52 |
-
except Exception as e:
|
| 53 |
-
print(f"β ERROR: An error occurred during data or model loading: {e}")
|
| 54 |
-
vector_store_data = None
|
| 55 |
-
|
| 56 |
-
else:
|
| 57 |
-
print("β ERROR: Pre-processed data not found in the repository.")
|
| 58 |
-
print(f"Please make sure '{vector_store_file}' and '{data_file}' exist in the '{DATA_PATH}' folder.")
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Step 5: RAG and Chat Functions (No changes needed here)
|
| 62 |
-
def get_relevant_context(query, index, top_k=5):
|
| 63 |
-
query_embedding = embedding_model.encode([query])
|
| 64 |
-
distances, indices = index.search(query_embedding, top_k)
|
| 65 |
-
context = []
|
| 66 |
-
# Ensure that the indices are within the bounds of the texts list
|
| 67 |
-
for i in indices[0]:
|
| 68 |
-
if i < len(texts):
|
| 69 |
-
context.append({"text": texts[i], "source": sources[i]})
|
| 70 |
-
return context
|
| 71 |
-
|
| 72 |
-
def chat_with_rag(message, history, vector_store_data):
|
| 73 |
-
index, texts, sources = vector_store_data
|
| 74 |
-
relevant_context = get_relevant_context(message, index)
|
| 75 |
-
context_str = "\n\n".join([f"Source: {c['source']}\nContent: {c['text']}" for c in relevant_context])
|
| 76 |
-
|
| 77 |
-
prompt = f"""
|
| 78 |
-
You are an Expert scientist in the Halassa Lab at MIT, an expert in computational neuroscience.
|
| 79 |
-
Your primary goal is to answer questions as thoroughly and accurately as possible. You may intelligently synthesize information from the provided context, which consists of key papers from the lab.
|
| 80 |
-
|
| 81 |
-
Follow these rules strictly if you are using the provided context:
|
| 82 |
-
1. Do not simply copy-paste from the context. Read the relevant passages and formulate a comprehensive, well-written answer in your own words.
|
| 83 |
-
2. When your answer uses information directly from a provided paper, you MUST cite the source at the end of the output in a list. Use the format [filename - Page X].
|
| 84 |
-
3. If the provided papers offer relevant concepts but do not contain the full answer, use your broader knowledge of computational neuroscience to provide a more complete explanation.
|
| 85 |
-
|
| 86 |
-
Context from the Halassa Lab's papers:
|
| 87 |
-
---
|
| 88 |
-
{context_str}
|
| 89 |
-
---
|
| 90 |
-
|
| 91 |
-
User Question: {message}
|
| 92 |
-
|
| 93 |
-
Expert Answer:
|
| 94 |
-
"""
|
| 95 |
-
try:
|
| 96 |
-
response = llm.generate_content(prompt)
|
| 97 |
-
return response.text
|
| 98 |
-
except Exception as e:
|
| 99 |
-
return f"An error occurred with the AI model: {str(e)}"
|
| 100 |
-
|
| 101 |
-
# Step 6: Gradio User Interface
|
| 102 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="Halassa Literature Chatbot") as demo:
|
| 103 |
-
gr.Markdown("# Halassa Lab Onboarder")
|
| 104 |
-
if vector_store_data is None:
|
| 105 |
-
gr.Markdown("## β οΈ Error: Could not load data or models. Please check the logs in the Hugging Face Space for details.")
|
| 106 |
-
else:
|
| 107 |
-
gr.Markdown("The documents have been pre-loaded. Ask your questions below.")
|
| 108 |
-
chatbot_ui = gr.Chatbot(label="Chat History", height=600, layout="panel")
|
| 109 |
-
message_box = gr.Textbox(label="Ask your question...", lines=3)
|
| 110 |
-
clear_button = gr.ClearButton(components=[chatbot_ui, message_box])
|
| 111 |
-
|
| 112 |
-
def respond(message, history):
|
| 113 |
-
# Pass the loaded vector_store_data to the chat function
|
| 114 |
-
response_text = chat_with_rag(message, history, vector_store_data)
|
| 115 |
-
history.append((message, response_text))
|
| 116 |
-
return "", history
|
| 117 |
-
|
| 118 |
-
message_box.submit(respond, inputs=[message_box, chatbot_ui], outputs=[message_box, chatbot_ui])
|
| 119 |
-
|
| 120 |
-
# Step 7: Launch the app
|
| 121 |
-
# On Hugging Face, demo.launch() is all you need.
|
| 122 |
-
# It will run the app and make it accessible.
|
| 123 |
-
if vector_store_data:
|
| 124 |
demo.launch()
|
|
|
|
| 1 |
+
# Step 1: Import Libraries
|
| 2 |
+
# No changes needed here, these will be installed from your requirements.txt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
import pickle
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Step 2: Configure API Key from Hugging Face Secrets
|
| 12 |
+
# This section is modified to securely access the API key from your Space's secrets.
|
| 13 |
+
try:
|
| 14 |
+
# Make sure to set 'GOOGLE_API_KEY' in your Hugging Face Space's settings/secrets
|
| 15 |
+
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 16 |
+
if GOOGLE_API_KEY is None:
|
| 17 |
+
raise ValueError("GOOGLE_API_KEY not found in environment variables.")
|
| 18 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 19 |
+
print("API Key configured successfully.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"ERROR: Could not configure API key. Please ensure 'GOOGLE_API_KEY' is set in your Hugging Face Space secrets. Details: {e}")
|
| 22 |
+
|
| 23 |
+
# Step 3: Define Data Path
|
| 24 |
+
# This is updated to point to the 'rag_chatbot_data' folder you uploaded to the Space.
|
| 25 |
+
# It no longer uses Google Drive.
|
| 26 |
+
DATA_PATH = "data"
|
| 27 |
+
vector_store_file = os.path.join(DATA_PATH, "vector_store.index")
|
| 28 |
+
data_file = os.path.join(DATA_PATH, "data.pkl")
|
| 29 |
+
|
| 30 |
+
# Step 4: Load Models and Pre-processed Data
|
| 31 |
+
# This section is slightly restructured to load models only if the data is found.
|
| 32 |
+
vector_store_data = None
|
| 33 |
+
if os.path.exists(vector_store_file) and os.path.exists(data_file):
|
| 34 |
+
try:
|
| 35 |
+
print("Loading pre-processed data from the repository...")
|
| 36 |
+
index = faiss.read_index(vector_store_file)
|
| 37 |
+
with open(data_file, "rb") as f:
|
| 38 |
+
stored_data = pickle.load(f)
|
| 39 |
+
texts = stored_data["texts"]
|
| 40 |
+
sources = stored_data["sources"]
|
| 41 |
+
print("β
Data loaded successfully.")
|
| 42 |
+
|
| 43 |
+
# Store everything in a state object for Gradio
|
| 44 |
+
vector_store_data = (index, texts, sources)
|
| 45 |
+
|
| 46 |
+
# Load the embedding and LLM models
|
| 47 |
+
print("Loading AI and embedding models...")
|
| 48 |
+
embedding_model = SentenceTransformer('BAAI/bge-large-en-v1.5')
|
| 49 |
+
llm = genai.GenerativeModel('gemini-pro') # Using gemini-pro as it's a robust choice
|
| 50 |
+
print("β
Models loaded successfully.")
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"β ERROR: An error occurred during data or model loading: {e}")
|
| 54 |
+
vector_store_data = None
|
| 55 |
+
|
| 56 |
+
else:
|
| 57 |
+
print("β ERROR: Pre-processed data not found in the repository.")
|
| 58 |
+
print(f"Please make sure '{vector_store_file}' and '{data_file}' exist in the '{DATA_PATH}' folder.")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# Step 5: RAG and Chat Functions (No changes needed here)
|
| 62 |
+
def get_relevant_context(query, index, top_k=5):
|
| 63 |
+
query_embedding = embedding_model.encode([query])
|
| 64 |
+
distances, indices = index.search(query_embedding, top_k)
|
| 65 |
+
context = []
|
| 66 |
+
# Ensure that the indices are within the bounds of the texts list
|
| 67 |
+
for i in indices[0]:
|
| 68 |
+
if i < len(texts):
|
| 69 |
+
context.append({"text": texts[i], "source": sources[i]})
|
| 70 |
+
return context
|
| 71 |
+
|
| 72 |
+
def chat_with_rag(message, history, vector_store_data):
|
| 73 |
+
index, texts, sources = vector_store_data
|
| 74 |
+
relevant_context = get_relevant_context(message, index)
|
| 75 |
+
context_str = "\n\n".join([f"Source: {c['source']}\nContent: {c['text']}" for c in relevant_context])
|
| 76 |
+
|
| 77 |
+
prompt = f"""
|
| 78 |
+
You are an Expert scientist in the Halassa Lab at MIT, an expert in computational neuroscience.
|
| 79 |
+
Your primary goal is to answer questions as thoroughly and accurately as possible. You may intelligently synthesize information from the provided context, which consists of key papers from the lab.
|
| 80 |
+
|
| 81 |
+
Follow these rules strictly if you are using the provided context:
|
| 82 |
+
1. Do not simply copy-paste from the context. Read the relevant passages and formulate a comprehensive, well-written answer in your own words.
|
| 83 |
+
2. When your answer uses information directly from a provided paper, you MUST cite the source at the end of the output in a list. Use the format [filename - Page X].
|
| 84 |
+
3. If the provided papers offer relevant concepts but do not contain the full answer, use your broader knowledge of computational neuroscience to provide a more complete explanation.
|
| 85 |
+
|
| 86 |
+
Context from the Halassa Lab's papers:
|
| 87 |
+
---
|
| 88 |
+
{context_str}
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
User Question: {message}
|
| 92 |
+
|
| 93 |
+
Expert Answer:
|
| 94 |
+
"""
|
| 95 |
+
try:
|
| 96 |
+
response = llm.generate_content(prompt)
|
| 97 |
+
return response.text
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return f"An error occurred with the AI model: {str(e)}"
|
| 100 |
+
|
| 101 |
+
# Step 6: Gradio User Interface
|
| 102 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Halassa Literature Chatbot") as demo:
|
| 103 |
+
gr.Markdown("# Halassa Lab Onboarder")
|
| 104 |
+
if vector_store_data is None:
|
| 105 |
+
gr.Markdown("## β οΈ Error: Could not load data or models. Please check the logs in the Hugging Face Space for details.")
|
| 106 |
+
else:
|
| 107 |
+
gr.Markdown("The documents have been pre-loaded. Ask your questions below.")
|
| 108 |
+
chatbot_ui = gr.Chatbot(label="Chat History", height=600, layout="panel")
|
| 109 |
+
message_box = gr.Textbox(label="Ask your question...", lines=3)
|
| 110 |
+
clear_button = gr.ClearButton(components=[chatbot_ui, message_box])
|
| 111 |
+
|
| 112 |
+
def respond(message, history):
|
| 113 |
+
# Pass the loaded vector_store_data to the chat function
|
| 114 |
+
response_text = chat_with_rag(message, history, vector_store_data)
|
| 115 |
+
history.append((message, response_text))
|
| 116 |
+
return "", history
|
| 117 |
+
|
| 118 |
+
message_box.submit(respond, inputs=[message_box, chatbot_ui], outputs=[message_box, chatbot_ui])
|
| 119 |
+
|
| 120 |
+
# Step 7: Launch the app
|
| 121 |
+
# On Hugging Face, demo.launch() is all you need.
|
| 122 |
+
# It will run the app and make it accessible.
|
| 123 |
+
if vector_store_data:
|
| 124 |
demo.launch()
|