Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,13 @@ from sentence_transformers import SentenceTransformer
|
|
5 |
import faiss
|
6 |
import numpy as np
|
7 |
import PyPDF2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Grog API key (Use environment variable or replace it with your actual API key)
|
10 |
grog_api_key = "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"
|
@@ -13,7 +20,10 @@ grog_api_key = "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"
|
|
13 |
client = Groq(api_key=grog_api_key)
|
14 |
|
15 |
# Path to the already uploaded book
|
16 |
-
book_path = 'Generative_AI_Foundations_in_Python_Discover_key_techniques_and.pdf'
|
|
|
|
|
|
|
17 |
|
18 |
# Check if the file exists
|
19 |
if os.path.exists(book_path):
|
@@ -67,8 +77,58 @@ else:
|
|
67 |
def generate_query_embedding(query, sentence_transformer_model):
|
68 |
return sentence_transformer_model.encode([query])
|
69 |
|
70 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
def generate_answer_with_grog(query, vector_index, sentences, sentence_transformer_model):
|
|
|
|
|
|
|
|
|
|
|
72 |
try:
|
73 |
# Get the query embedding using the sentence transformer
|
74 |
query_embedding = generate_query_embedding(query, sentence_transformer_model)
|
@@ -76,26 +136,41 @@ def generate_answer_with_grog(query, vector_index, sentences, sentence_transform
|
|
76 |
# Perform similarity search on the vector store (vector index)
|
77 |
D, I = vector_index.search(np.array(query_embedding), k=5) # Find top 5 similar sentences
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# Retrieve the most relevant sentences
|
80 |
relevant_sentences = [sentences[i] for i in I[0]]
|
81 |
|
82 |
# Combine the relevant sentences for the final query
|
83 |
combined_text = " ".join(relevant_sentences)
|
84 |
|
85 |
-
#
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
)
|
93 |
-
|
94 |
-
#
|
95 |
-
|
96 |
-
return
|
|
|
97 |
except Exception as e:
|
98 |
-
|
|
|
99 |
|
100 |
# Gradio app function
|
101 |
def gradio_interface(query):
|
@@ -107,17 +182,21 @@ def gradio_interface(query):
|
|
107 |
if vector_index is None or sentences is None:
|
108 |
return "Vector index or sentences not initialized properly."
|
109 |
|
110 |
-
# Generate the answer using the
|
111 |
answer = generate_answer_with_grog(query, vector_index, sentences, sentence_transformer_model)
|
112 |
-
|
|
|
|
|
|
|
|
|
113 |
|
114 |
# Create the Gradio interface
|
115 |
iface = gr.Interface(
|
116 |
fn=gradio_interface,
|
117 |
-
inputs="
|
118 |
-
outputs="
|
119 |
title="Generative_AI_Foundations_in_Python PDF-based Query Answering",
|
120 |
-
description="Ask any question about the content in the uploaded PDF and receive
|
121 |
)
|
122 |
|
123 |
# Launch the Gradio app
|
|
|
5 |
import faiss
|
6 |
import numpy as np
|
7 |
import PyPDF2
|
8 |
+
import re
|
9 |
+
from collections import Counter
|
10 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
11 |
+
import logging
|
12 |
+
|
13 |
+
# Setup logging
|
14 |
+
logging.basicConfig(filename='query_logs.log', level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
|
15 |
|
16 |
# Grog API key (Use environment variable or replace it with your actual API key)
|
17 |
grog_api_key = "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"
|
|
|
20 |
client = Groq(api_key=grog_api_key)
|
21 |
|
22 |
# Path to the already uploaded book
|
23 |
+
book_path = '/content/Generative_AI_Foundations_in_Python_Discover_key_techniques_and.pdf'
|
24 |
+
|
25 |
+
# Cache system to store previous responses
|
26 |
+
cache = {}
|
27 |
|
28 |
# Check if the file exists
|
29 |
if os.path.exists(book_path):
|
|
|
77 |
def generate_query_embedding(query, sentence_transformer_model):
|
78 |
return sentence_transformer_model.encode([query])
|
79 |
|
80 |
+
# Function to check relevancy and handle out-of-bounds queries
|
81 |
+
def check_relevancy(D, threshold=0.4):
|
82 |
+
if D[0][0] > threshold:
|
83 |
+
return False
|
84 |
+
return True
|
85 |
+
|
86 |
+
# Function to generate diverse responses from the LLM with varied parameters
|
87 |
+
def generate_diverse_responses(client, prompt, n=3):
|
88 |
+
responses = []
|
89 |
+
for i in range(n):
|
90 |
+
temperature = 0.5 + (i * 0.2) # Vary temperature from 0.5 to 0.9
|
91 |
+
top_p = 0.9 - (i * 0.2) # Vary top-p from 0.9 to 0.7
|
92 |
+
try:
|
93 |
+
chat_completion = client.chat.completions.create(
|
94 |
+
messages=[{
|
95 |
+
"role": "user",
|
96 |
+
"content": prompt,
|
97 |
+
}],
|
98 |
+
model="llama3-8b-8192",
|
99 |
+
temperature=temperature,
|
100 |
+
top_p=top_p
|
101 |
+
)
|
102 |
+
responses.append(chat_completion.choices[0].message.content)
|
103 |
+
except Exception as e:
|
104 |
+
logging.error(f"Error generating response: {str(e)}")
|
105 |
+
responses.append("Sorry, an error occurred while generating this response.")
|
106 |
+
return responses
|
107 |
+
|
108 |
+
# Function to aggregate responses based on similarity and voting mechanism
|
109 |
+
def aggregate_responses(responses):
|
110 |
+
# Use a simple voting mechanism to select the most common response
|
111 |
+
response_counter = Counter(responses)
|
112 |
+
most_common_response = response_counter.most_common(1)[0][0]
|
113 |
+
|
114 |
+
# Rank responses by semantic similarity to the first response
|
115 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
116 |
+
embeddings = model.encode(responses)
|
117 |
+
first_embedding = embeddings[0].reshape(1, -1)
|
118 |
+
|
119 |
+
similarities = cosine_similarity(first_embedding, embeddings)[0]
|
120 |
+
top_response_index = np.argmax(similarities)
|
121 |
+
|
122 |
+
# Return the most similar response to the first response
|
123 |
+
return responses[top_response_index]
|
124 |
+
|
125 |
+
# Function to generate answers using the groq API with Llama model
|
126 |
def generate_answer_with_grog(query, vector_index, sentences, sentence_transformer_model):
|
127 |
+
# Check cache for previous queries
|
128 |
+
if query in cache:
|
129 |
+
logging.info(f"Cache hit for query: {query}")
|
130 |
+
return cache[query]
|
131 |
+
|
132 |
try:
|
133 |
# Get the query embedding using the sentence transformer
|
134 |
query_embedding = generate_query_embedding(query, sentence_transformer_model)
|
|
|
136 |
# Perform similarity search on the vector store (vector index)
|
137 |
D, I = vector_index.search(np.array(query_embedding), k=5) # Find top 5 similar sentences
|
138 |
|
139 |
+
# If no relevant content found, generate a fallback response
|
140 |
+
if len(I[0]) == 0 or D[0][0] > 1.0:
|
141 |
+
fallback_response = f"I couldn't find anything relevant in the document, but here's a general answer to your query: {query}"
|
142 |
+
chat_completion = client.chat.completions.create(
|
143 |
+
messages=[{
|
144 |
+
"role": "user",
|
145 |
+
"content": fallback_response,
|
146 |
+
}],
|
147 |
+
model="llama3-8b-8192",
|
148 |
+
)
|
149 |
+
cache[query] = chat_completion.choices[0].message.content
|
150 |
+
return cache[query]
|
151 |
+
|
152 |
# Retrieve the most relevant sentences
|
153 |
relevant_sentences = [sentences[i] for i in I[0]]
|
154 |
|
155 |
# Combine the relevant sentences for the final query
|
156 |
combined_text = " ".join(relevant_sentences)
|
157 |
|
158 |
+
# Create a prompt with the relevant content
|
159 |
+
final_prompt = f"**Relevant Information:**\n\n '{combined_text}'\n\n **Answer:** {query}"
|
160 |
+
|
161 |
+
# Generate diverse responses using the groq API
|
162 |
+
responses = generate_diverse_responses(client, final_prompt)
|
163 |
+
|
164 |
+
# Aggregate the responses to ensure stability and variety
|
165 |
+
final_response = aggregate_responses(responses)
|
166 |
+
|
167 |
+
# Cache the response for future queries
|
168 |
+
cache[query] = final_response
|
169 |
+
return final_response
|
170 |
+
|
171 |
except Exception as e:
|
172 |
+
logging.error(f"Error during answer generation with groq API: {str(e)}")
|
173 |
+
return f"Error during answer generation: {str(e)}"
|
174 |
|
175 |
# Gradio app function
|
176 |
def gradio_interface(query):
|
|
|
182 |
if vector_index is None or sentences is None:
|
183 |
return "Vector index or sentences not initialized properly."
|
184 |
|
185 |
+
# Generate the answer using the groq API and Llama model with varied responses
|
186 |
answer = generate_answer_with_grog(query, vector_index, sentences, sentence_transformer_model)
|
187 |
+
|
188 |
+
# Log the query and answer for monitoring
|
189 |
+
logging.info(f"Query: {query}, Answer: {answer}")
|
190 |
+
|
191 |
+
return f"### Here's your response:\n\n{answer}"
|
192 |
|
193 |
# Create the Gradio interface
|
194 |
iface = gr.Interface(
|
195 |
fn=gradio_interface,
|
196 |
+
inputs=gr.Textbox(label="Enter your query"),
|
197 |
+
outputs="markdown", # Use markdown output for better formatting
|
198 |
title="Generative_AI_Foundations_in_Python PDF-based Query Answering",
|
199 |
+
description="Ask any question about the content in the uploaded PDF and receive diverse, reliable answers."
|
200 |
)
|
201 |
|
202 |
# Launch the Gradio app
|