Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import chromadb
|
| 3 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 4 |
|
| 5 |
# --- 1. Load Model (No changes here) ---
|
| 6 |
print("Loading sentence-transformer model...")
|
| 7 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 8 |
print("Model loaded.")
|
| 9 |
|
| 10 |
-
# --- 2. Setup ChromaDB
|
| 11 |
client = chromadb.Client()
|
| 12 |
|
| 13 |
try:
|
| 14 |
collection = client.create_collection("my_documents")
|
| 15 |
print("ChromaDB collection created.")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
embeddings = model.encode(documents)
|
| 26 |
|
|
@@ -38,14 +50,8 @@ except ValueError:
|
|
| 38 |
|
| 39 |
# --- 3. Define the Chatbot Logic (No changes here) ---
|
| 40 |
def chatbot_response(message, history):
|
| 41 |
-
"""
|
| 42 |
-
This function processes the user message and returns a response.
|
| 43 |
-
'message' is the user's input, 'history' is the conversation history.
|
| 44 |
-
"""
|
| 45 |
-
# 1. Create an embedding for the user's message
|
| 46 |
query_embedding = model.encode([message]).tolist()
|
| 47 |
|
| 48 |
-
# 2. Query ChromaDB to find the most relevant documents
|
| 49 |
results = collection.query(
|
| 50 |
query_embeddings=query_embedding,
|
| 51 |
n_results=2
|
|
@@ -53,25 +59,23 @@ def chatbot_response(message, history):
|
|
| 53 |
|
| 54 |
retrieved_documents = results['documents'][0]
|
| 55 |
|
| 56 |
-
if not retrieved_documents:
|
| 57 |
-
return "I'm sorry, I couldn't find any information on that topic. 🤔"
|
| 58 |
|
| 59 |
-
# 4. Formulate the response
|
| 60 |
context = "\n- ".join(retrieved_documents)
|
| 61 |
-
response = f"
|
| 62 |
|
| 63 |
return response
|
| 64 |
|
| 65 |
-
# --- 4. Create the
|
| 66 |
iface = gr.ChatInterface(
|
| 67 |
fn=chatbot_response,
|
| 68 |
-
title="
|
| 69 |
-
description="Ask me anything
|
| 70 |
theme="soft",
|
| 71 |
examples=[
|
| 72 |
-
|
| 73 |
-
"
|
| 74 |
-
"What is the Eiffel Tower made of?"
|
| 75 |
],
|
| 76 |
cache_examples=False
|
| 77 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import chromadb
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import pandas as pd # Make sure pandas is imported
|
| 5 |
|
| 6 |
# --- 1. Load Model (No changes here) ---
|
| 7 |
print("Loading sentence-transformer model...")
|
| 8 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
print("Model loaded.")
|
| 10 |
|
| 11 |
+
# --- 2. Setup ChromaDB ---
|
| 12 |
client = chromadb.Client()
|
| 13 |
|
| 14 |
try:
|
| 15 |
collection = client.create_collection("my_documents")
|
| 16 |
print("ChromaDB collection created.")
|
| 17 |
|
| 18 |
+
# --- THIS IS THE UPDATED SECTION ---
|
| 19 |
+
# Load your data from the CSV file.
|
| 20 |
+
# IMPORTANT: Change 'text' to the actual name of the column in your CSV that contains the text data.
|
| 21 |
+
try:
|
| 22 |
+
print("Loading data from my_data.csv...")
|
| 23 |
+
df = pd.read_csv('my_data.csv')
|
| 24 |
+
# Ensure you change 'text' to your column's name if it's different
|
| 25 |
+
documents = df['text'].tolist()
|
| 26 |
+
print(f"Successfully loaded {len(documents)} documents.")
|
| 27 |
+
except FileNotFoundError:
|
| 28 |
+
print("Error: my_data.csv not found. Please upload the file to your Space.")
|
| 29 |
+
# Create a fallback document to avoid a crash
|
| 30 |
+
documents = ["Error: my_data.csv not found. Please make sure the file is uploaded to the Hugging Face Space."]
|
| 31 |
+
except KeyError:
|
| 32 |
+
print("Error: The CSV must have a column named 'text'. Please rename your column or update the code.")
|
| 33 |
+
documents = ["Error: Could not find a 'text' column in the CSV file. Please check your data."]
|
| 34 |
+
|
| 35 |
+
# --- END OF UPDATED SECTION ---
|
| 36 |
|
| 37 |
embeddings = model.encode(documents)
|
| 38 |
|
|
|
|
| 50 |
|
| 51 |
# --- 3. Define the Chatbot Logic (No changes here) ---
|
| 52 |
def chatbot_response(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
query_embedding = model.encode([message]).tolist()
|
| 54 |
|
|
|
|
| 55 |
results = collection.query(
|
| 56 |
query_embeddings=query_embedding,
|
| 57 |
n_results=2
|
|
|
|
| 59 |
|
| 60 |
retrieved_documents = results['documents'][0]
|
| 61 |
|
| 62 |
+
if not retrieved_documents or "Error:" in retrieved_documents[0]:
|
| 63 |
+
return "I'm sorry, I couldn't find any information on that topic, or there was an error loading my knowledge base. 🤔"
|
| 64 |
|
|
|
|
| 65 |
context = "\n- ".join(retrieved_documents)
|
| 66 |
+
response = f"Based on the document, here's what I found:\n- {context}"
|
| 67 |
|
| 68 |
return response
|
| 69 |
|
| 70 |
+
# --- 4. Create the Gradio Interface (No changes here) ---
|
| 71 |
iface = gr.ChatInterface(
|
| 72 |
fn=chatbot_response,
|
| 73 |
+
title="My Custom Knowledge Bot 📚",
|
| 74 |
+
description="Ask me anything about the contents of my document.",
|
| 75 |
theme="soft",
|
| 76 |
examples=[
|
| 77 |
+
# Update these examples to be relevant to your data
|
| 78 |
+
"What is the main topic of the document?",
|
|
|
|
| 79 |
],
|
| 80 |
cache_examples=False
|
| 81 |
)
|