Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,26 @@
|
|
1 |
-
# Debugging Step: Check if langchain_community is installed properly
|
2 |
-
try:
|
3 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
-
print("β
LangChain Community Module is Installed Correctly!")
|
5 |
-
except ModuleNotFoundError as e:
|
6 |
-
print(f"β Import Failed: {str(e)}")
|
7 |
-
print("πΉ Try running: pip install --upgrade langchain-community")
|
8 |
-
exit(1) # Stop execution if the import fails
|
9 |
import gradio as gr
|
10 |
import openai
|
11 |
import os
|
12 |
-
import
|
13 |
-
import shutil
|
14 |
-
import numpy as np
|
15 |
-
import torch
|
16 |
-
from datasets import load_dataset
|
17 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings # β
Correct
|
18 |
-
from langchain_huggingface import HuggingFaceEmbeddings
|
19 |
from langchain_community.vectorstores import Chroma
|
20 |
from langchain.schema import Document
|
21 |
from sentence_transformers import SentenceTransformer
|
22 |
-
from
|
23 |
-
|
24 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
25 |
-
|
26 |
-
|
27 |
-
# β
Load Pretrained Model
|
28 |
-
model_name = "bert-base-uncased"
|
29 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
30 |
-
#embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
31 |
-
embedding_model = HuggingFaceEmbeddings(model_name="models/bert-base-uncased")
|
32 |
|
33 |
-
|
|
|
|
|
34 |
|
35 |
-
# β
Set OpenAI API Key
|
36 |
-
openai.api_key = os.getenv("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
|
|
|
37 |
|
38 |
-
# β
Download NLTK
|
39 |
nltk.download('punkt')
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
# β
Load RunGalileo Datasets
|
44 |
-
ragbench = {}
|
45 |
-
for dataset in ['covidqa', 'cuad', 'delucionqa', 'emanual', 'expertqa', 'finqa', 'hagrid', 'hotpotqa', 'msmarco', 'pubmedqa', 'tatqa', 'techqa']:
|
46 |
-
ragbench[dataset] = load_dataset("rungalileo/ragbench", dataset)
|
47 |
-
print("Datasets Loaded β
")
|
48 |
-
|
49 |
-
# β
Function to Chunk Documents
|
50 |
-
def chunk_documents_semantic(documents, max_chunk_size=500):
|
51 |
chunks = []
|
52 |
for doc in documents:
|
53 |
sentences = nltk.sent_tokenize(doc)
|
@@ -62,21 +35,15 @@ def chunk_documents_semantic(documents, max_chunk_size=500):
|
|
62 |
chunks.append(current_chunk.strip())
|
63 |
return chunks
|
64 |
|
65 |
-
# β
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
original_documents_full = ragbench[dataset_name][split]['documents']
|
70 |
-
chunked_documents_full = chunk_documents_semantic(original_documents_full)
|
71 |
-
chunked_ragbench[split] = chunked_documents_full
|
72 |
-
print("Chunking Completed β
")
|
73 |
|
74 |
-
# β
Setup ChromaDB
|
75 |
persist_directory = "chroma_db_directory"
|
76 |
-
|
77 |
-
shutil.rmtree(persist_directory)
|
78 |
|
79 |
-
|
80 |
vectordb = Chroma.from_documents(
|
81 |
documents=documents,
|
82 |
embedding=embedding_model,
|
@@ -84,14 +51,14 @@ vectordb = Chroma.from_documents(
|
|
84 |
)
|
85 |
vectordb.persist()
|
86 |
|
87 |
-
# β
Retrieve Documents
|
88 |
def retrieve_documents(question, k=5):
|
89 |
docs = vectordb.similarity_search(question, k=k)
|
90 |
if not docs:
|
91 |
return ["β οΈ No relevant documents found. Try a different query."]
|
92 |
return [doc.page_content for doc in docs]
|
93 |
|
94 |
-
# β
Generate AI Response
|
95 |
def generate_response(question, context):
|
96 |
if not context or "No relevant documents found." in context:
|
97 |
return "No relevant context available. Try a different query."
|
@@ -99,8 +66,7 @@ def generate_response(question, context):
|
|
99 |
full_prompt = f"Context: {context}\n\nQuestion: {question}"
|
100 |
|
101 |
try:
|
102 |
-
|
103 |
-
response = client.chat.completions.create(
|
104 |
model="gpt-4",
|
105 |
messages=[
|
106 |
{"role": "system", "content": "You are an AI assistant that answers user queries based on the given context."},
|
@@ -109,53 +75,16 @@ def generate_response(question, context):
|
|
109 |
max_tokens=300,
|
110 |
temperature=0.7
|
111 |
)
|
112 |
-
return response
|
113 |
except Exception as e:
|
114 |
return f"Error generating response: {str(e)}"
|
115 |
|
116 |
-
# β
Compute Context Relevance, Utilization, Completeness, Adherence
|
117 |
-
def compute_cosine_similarity(text1, text2):
|
118 |
-
vectorizer = TfidfVectorizer()
|
119 |
-
vectors = vectorizer.fit_transform([text1, text2])
|
120 |
-
return cosine_similarity(vectors[0], vectors[1])[0][0]
|
121 |
-
|
122 |
-
def context_relevance(question, relevant_documents):
|
123 |
-
combined_docs = " ".join(relevant_documents)
|
124 |
-
return compute_cosine_similarity(question, combined_docs)
|
125 |
-
|
126 |
-
def context_utilization(response, relevant_documents):
|
127 |
-
combined_docs = " ".join(relevant_documents)
|
128 |
-
return compute_cosine_similarity(response, combined_docs)
|
129 |
-
|
130 |
-
def completeness(response, ground_truth_answer):
|
131 |
-
return compute_cosine_similarity(response, ground_truth_answer)
|
132 |
-
|
133 |
-
def adherence(response, relevant_documents):
|
134 |
-
combined_docs = " ".join(relevant_documents)
|
135 |
-
response_tokens = set(response.split())
|
136 |
-
relevant_tokens = set(combined_docs.split())
|
137 |
-
supported_tokens = response_tokens.intersection(relevant_tokens)
|
138 |
-
return len(supported_tokens) / len(response_tokens)
|
139 |
-
|
140 |
-
def compute_rmse(predicted_values, ground_truth_values):
|
141 |
-
return np.sqrt(mean_squared_error(ground_truth_values, predicted_values))
|
142 |
-
|
143 |
# β
Full RAG Pipeline
|
144 |
def rag_pipeline(question):
|
145 |
retrieved_docs = retrieve_documents(question, k=5)
|
146 |
context = " ".join(retrieved_docs)
|
147 |
response = generate_response(question, context)
|
148 |
-
|
149 |
-
# Compute Evaluation Metrics
|
150 |
-
ground_truth_answer = "Sample ground truth answer from dataset"
|
151 |
-
predicted_metrics = {
|
152 |
-
"context_relevance": context_relevance(question, retrieved_docs),
|
153 |
-
"context_utilization": context_utilization(response, retrieved_docs),
|
154 |
-
"completeness": completeness(response, ground_truth_answer),
|
155 |
-
"adherence": adherence(response, retrieved_docs)
|
156 |
-
}
|
157 |
-
|
158 |
-
return response, "\n\n".join(retrieved_docs), predicted_metrics
|
159 |
|
160 |
# β
Gradio UI Interface
|
161 |
iface = gr.Interface(
|
@@ -163,11 +92,10 @@ iface = gr.Interface(
|
|
163 |
inputs=gr.Textbox(label="Enter your question"),
|
164 |
outputs=[
|
165 |
gr.Textbox(label="Generated Response"),
|
166 |
-
gr.Textbox(label="Retrieved Documents")
|
167 |
-
gr.JSON(label="Evaluation Metrics")
|
168 |
],
|
169 |
-
title="RAG-Based
|
170 |
-
description="Enter a question and retrieve relevant documents with AI-generated response
|
171 |
)
|
172 |
|
173 |
# β
Launch the Gradio App
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from langchain_community.vectorstores import Chroma
|
6 |
from langchain.schema import Document
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
+
from datasets import load_dataset
|
9 |
+
import nltk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# β
Load the Sentence Transformer Embedding Model
|
12 |
+
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
13 |
+
embedding_model = HuggingFaceEmbeddings(model_name=model_name)
|
14 |
|
15 |
+
# β
Set OpenAI API Key
|
16 |
+
openai.api_key = os.getenv("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
|
17 |
+
")
|
18 |
|
19 |
+
# β
Download NLTK Tokenizer
|
20 |
nltk.download('punkt')
|
21 |
|
22 |
+
# β
Load and Chunk Dataset
|
23 |
+
def chunk_documents(documents, max_chunk_size=500):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
chunks = []
|
25 |
for doc in documents:
|
26 |
sentences = nltk.sent_tokenize(doc)
|
|
|
35 |
chunks.append(current_chunk.strip())
|
36 |
return chunks
|
37 |
|
38 |
+
# β
Load Dataset and Prepare ChromaDB
|
39 |
+
dataset = load_dataset("rungalileo/ragbench", "techqa") # Example dataset
|
40 |
+
original_documents = dataset['train']['documents']
|
41 |
+
chunked_documents = chunk_documents(original_documents)
|
|
|
|
|
|
|
|
|
42 |
|
|
|
43 |
persist_directory = "chroma_db_directory"
|
44 |
+
documents = [Document(page_content=chunk) for chunk in chunked_documents]
|
|
|
45 |
|
46 |
+
# β
Initialize ChromaDB
|
47 |
vectordb = Chroma.from_documents(
|
48 |
documents=documents,
|
49 |
embedding=embedding_model,
|
|
|
51 |
)
|
52 |
vectordb.persist()
|
53 |
|
54 |
+
# β
Function to Retrieve Relevant Documents
|
55 |
def retrieve_documents(question, k=5):
|
56 |
docs = vectordb.similarity_search(question, k=k)
|
57 |
if not docs:
|
58 |
return ["β οΈ No relevant documents found. Try a different query."]
|
59 |
return [doc.page_content for doc in docs]
|
60 |
|
61 |
+
# β
Function to Generate AI Response
|
62 |
def generate_response(question, context):
|
63 |
if not context or "No relevant documents found." in context:
|
64 |
return "No relevant context available. Try a different query."
|
|
|
66 |
full_prompt = f"Context: {context}\n\nQuestion: {question}"
|
67 |
|
68 |
try:
|
69 |
+
response = openai.ChatCompletion.create(
|
|
|
70 |
model="gpt-4",
|
71 |
messages=[
|
72 |
{"role": "system", "content": "You are an AI assistant that answers user queries based on the given context."},
|
|
|
75 |
max_tokens=300,
|
76 |
temperature=0.7
|
77 |
)
|
78 |
+
return response['choices'][0]['message']['content'].strip()
|
79 |
except Exception as e:
|
80 |
return f"Error generating response: {str(e)}"
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
# β
Full RAG Pipeline
|
83 |
def rag_pipeline(question):
|
84 |
retrieved_docs = retrieve_documents(question, k=5)
|
85 |
context = " ".join(retrieved_docs)
|
86 |
response = generate_response(question, context)
|
87 |
+
return response, "\n\n".join(retrieved_docs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# β
Gradio UI Interface
|
90 |
iface = gr.Interface(
|
|
|
92 |
inputs=gr.Textbox(label="Enter your question"),
|
93 |
outputs=[
|
94 |
gr.Textbox(label="Generated Response"),
|
95 |
+
gr.Textbox(label="Retrieved Documents")
|
|
|
96 |
],
|
97 |
+
title="RAG-Based Question Answering System",
|
98 |
+
description="Enter a question and retrieve relevant documents with AI-generated response."
|
99 |
)
|
100 |
|
101 |
# β
Launch the Gradio App
|