Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,14 @@ from langchain.embeddings import HuggingFaceEmbeddings
|
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain.schema import Document
|
| 7 |
|
| 8 |
-
#
|
| 9 |
model_name = "intfloat/e5-small"
|
| 10 |
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
openai.api_key = os.getenv("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") # Add in Hugging Face Secrets
|
| 14 |
|
| 15 |
-
#
|
| 16 |
persist_directory = "./docs/chroma/"
|
| 17 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 18 |
|
|
@@ -27,10 +27,10 @@ embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
|
| 27 |
# Define the ChromaDB persist directory
|
| 28 |
persist_directory = "./docs/chroma/"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
if vectordb._collection.count() == 0:
|
| 35 |
print("β οΈ No documents found in ChromaDB. Re-indexing dataset...")
|
| 36 |
|
|
@@ -43,15 +43,15 @@ if vectordb._collection.count() == 0:
|
|
| 43 |
Document(page_content="Smart thermostats improve energy efficiency through AI-based control.")
|
| 44 |
]
|
| 45 |
|
| 46 |
-
#
|
| 47 |
vectordb.add_documents(documents)
|
| 48 |
|
| 49 |
-
print("
|
| 50 |
else:
|
| 51 |
-
print(f"
|
| 52 |
|
| 53 |
|
| 54 |
-
#
|
| 55 |
def retrieve_documents(question, k=5):
|
| 56 |
"""Retrieve top K relevant documents from ChromaDB"""
|
| 57 |
docs = vectordb.similarity_search(question, k=k)
|
|
@@ -62,7 +62,7 @@ def retrieve_documents(question, k=5):
|
|
| 62 |
return [doc.page_content for doc in docs]
|
| 63 |
|
| 64 |
|
| 65 |
-
#
|
| 66 |
import openai
|
| 67 |
|
| 68 |
def generate_response(question, context):
|
|
@@ -89,14 +89,14 @@ def generate_response(question, context):
|
|
| 89 |
return f"Error generating response: {str(e)}"
|
| 90 |
|
| 91 |
|
| 92 |
-
#
|
| 93 |
def rag_pipeline(question):
|
| 94 |
retrieved_docs = retrieve_documents(question, k=5)
|
| 95 |
context = " ".join(retrieved_docs)
|
| 96 |
response = generate_response(question, context)
|
| 97 |
return response, "\n\n".join(retrieved_docs)
|
| 98 |
|
| 99 |
-
#
|
| 100 |
iface = gr.Interface(
|
| 101 |
fn=rag_pipeline,
|
| 102 |
inputs=gr.Textbox(label="Enter your question"),
|
|
|
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain.schema import Document
|
| 7 |
|
| 8 |
+
# Load the Sentence Transformer Embedding Model
|
| 9 |
model_name = "intfloat/e5-small"
|
| 10 |
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
| 11 |
|
| 12 |
+
# Set up OpenAI API Key (Replace with your own API key)
|
| 13 |
openai.api_key = os.getenv("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") # Add in Hugging Face Secrets
|
| 14 |
|
| 15 |
+
# Load ChromaDB with RunGalileo Dataset
|
| 16 |
persist_directory = "./docs/chroma/"
|
| 17 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 18 |
|
|
|
|
| 27 |
# Define the ChromaDB persist directory
|
| 28 |
persist_directory = "./docs/chroma/"
|
| 29 |
|
| 30 |
+
# Load ChromaDB (or create if empty)
|
| 31 |
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding_model)
|
| 32 |
|
| 33 |
+
# Check if documents exist
|
| 34 |
if vectordb._collection.count() == 0:
|
| 35 |
print("β οΈ No documents found in ChromaDB. Re-indexing dataset...")
|
| 36 |
|
|
|
|
| 43 |
Document(page_content="Smart thermostats improve energy efficiency through AI-based control.")
|
| 44 |
]
|
| 45 |
|
| 46 |
+
# Insert documents into ChromaDB
|
| 47 |
vectordb.add_documents(documents)
|
| 48 |
|
| 49 |
+
print(" Documents successfully indexed into ChromaDB.")
|
| 50 |
else:
|
| 51 |
+
print(f" ChromaDB contains {vectordb._collection.count()} documents.")
|
| 52 |
|
| 53 |
|
| 54 |
+
# Function to Retrieve Top-K Relevant Documents
|
| 55 |
def retrieve_documents(question, k=5):
|
| 56 |
"""Retrieve top K relevant documents from ChromaDB"""
|
| 57 |
docs = vectordb.similarity_search(question, k=k)
|
|
|
|
| 62 |
return [doc.page_content for doc in docs]
|
| 63 |
|
| 64 |
|
| 65 |
+
# Function to Generate AI Response
|
| 66 |
import openai
|
| 67 |
|
| 68 |
def generate_response(question, context):
|
|
|
|
| 89 |
return f"Error generating response: {str(e)}"
|
| 90 |
|
| 91 |
|
| 92 |
+
# Full RAG Pipeline
|
| 93 |
def rag_pipeline(question):
|
| 94 |
retrieved_docs = retrieve_documents(question, k=5)
|
| 95 |
context = " ".join(retrieved_docs)
|
| 96 |
response = generate_response(question, context)
|
| 97 |
return response, "\n\n".join(retrieved_docs)
|
| 98 |
|
| 99 |
+
# Gradio UI Interface
|
| 100 |
iface = gr.Interface(
|
| 101 |
fn=rag_pipeline,
|
| 102 |
inputs=gr.Textbox(label="Enter your question"),
|