ajalisatgi commited on
Commit
4d2d551
Β·
verified Β·
1 Parent(s): a2b4b17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -98
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 nltk
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 sklearn.metrics import mean_squared_error, roc_auc_score
23
- from sklearn.feature_extraction.text import TfidfVectorizer
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
- embedding_model.client.to(device)
 
 
34
 
35
- # βœ… Set OpenAI API Key (Replace with your own)
36
- openai.api_key = os.getenv("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")
 
37
 
38
- # βœ… Download NLTK Dependencies
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
- # βœ… Chunk the Entire Dataset
66
- chunked_ragbench = {}
67
- for dataset_name in ragbench.keys():
68
- for split in ragbench[dataset_name].keys():
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
- if os.path.exists(persist_directory):
77
- shutil.rmtree(persist_directory)
78
 
79
- documents = [Document(page_content=chunk) for chunk in chunked_documents_full]
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
- client = openai.OpenAI()
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.choices[0].message.content.strip()
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 QA System for RunGalileo",
170
- description="Enter a question and retrieve relevant documents with AI-generated response & evaluation metrics."
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