Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,352 +1,146 @@
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import json
|
3 |
import logging
|
4 |
-
import shutil
|
5 |
-
from tempfile import NamedTemporaryFile
|
6 |
-
from typing import List
|
7 |
-
from pydantic import BaseModel, Field
|
8 |
-
from langchain_core.documents import Document
|
9 |
-
from langchain_community.document_loaders import PyPDFLoader
|
10 |
-
from langchain_community.vectorstores import FAISS
|
11 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
12 |
-
from llama_parse import LlamaParse
|
13 |
-
import gradio as gr
|
14 |
|
15 |
-
|
16 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
17 |
|
18 |
# Environment variables and configurations
|
19 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
20 |
-
llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
|
21 |
-
ACCOUNT_ID = os.environ.get("CLOUDFARE_ACCOUNT_ID")
|
22 |
-
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
|
23 |
-
API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/a17f03e0f049ccae0c15cdcf3b9737ce/ai/run/"
|
24 |
-
|
25 |
-
print(f"ACCOUNT_ID: {ACCOUNT_ID}")
|
26 |
-
print(f"CLOUDFLARE_AUTH_TOKEN: {API_TOKEN[:5]}..." if API_TOKEN else "Not set")
|
27 |
-
|
28 |
-
MODELS = [
|
29 |
-
"mistralai/Mistral-7B-Instruct-v0.3",
|
30 |
-
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
31 |
-
"@cf/meta/llama-3.1-8b-instruct",
|
32 |
-
"mistralai/Mistral-Nemo-Instruct-2407"
|
33 |
-
]
|
34 |
-
|
35 |
-
# Initialize LlamaParse
|
36 |
-
llama_parser = LlamaParse(
|
37 |
-
api_key=llama_cloud_api_key,
|
38 |
-
result_type="markdown",
|
39 |
-
num_workers=4,
|
40 |
-
verbose=True,
|
41 |
-
language="en",
|
42 |
-
)
|
43 |
-
|
44 |
-
def load_document(file: NamedTemporaryFile, parser: str = "llamaparse") -> List[Document]:
|
45 |
-
"""Loads and splits the document into pages."""
|
46 |
-
if parser == "pypdf":
|
47 |
-
loader = PyPDFLoader(file.name)
|
48 |
-
return loader.load_and_split()
|
49 |
-
elif parser == "llamaparse":
|
50 |
-
try:
|
51 |
-
documents = llama_parser.load_data(file.name)
|
52 |
-
return [Document(page_content=doc.text, metadata={"source": file.name}) for doc in documents]
|
53 |
-
except Exception as e:
|
54 |
-
print(f"Error using Llama Parse: {str(e)}")
|
55 |
-
print("Falling back to PyPDF parser")
|
56 |
-
loader = PyPDFLoader(file.name)
|
57 |
-
return loader.load_and_split()
|
58 |
-
else:
|
59 |
-
raise ValueError("Invalid parser specified. Use 'pypdf' or 'llamaparse'.")
|
60 |
-
|
61 |
-
def get_embeddings():
|
62 |
-
return HuggingFaceEmbeddings(model_name="avsolatorio/GIST-Embedding-v0")
|
63 |
-
|
64 |
-
DOCUMENTS_FILE = "uploaded_documents.json"
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
return []
|
71 |
|
72 |
-
def
|
73 |
-
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
def
|
80 |
-
global uploaded_documents
|
81 |
-
logging.info(f"Entering update_vectors with {len(files)} files and parser: {parser}")
|
82 |
-
|
83 |
-
if not files:
|
84 |
-
logging.warning("No files provided for update_vectors")
|
85 |
-
return "Please upload at least one PDF file.", display_documents()
|
86 |
-
|
87 |
-
embed = get_embeddings()
|
88 |
-
total_chunks = 0
|
89 |
-
|
90 |
-
all_data = []
|
91 |
-
for file in files:
|
92 |
-
logging.info(f"Processing file: {file.name}")
|
93 |
-
try:
|
94 |
-
data = load_document(file, parser)
|
95 |
-
if not data:
|
96 |
-
logging.warning(f"No chunks loaded from {file.name}")
|
97 |
-
continue
|
98 |
-
logging.info(f"Loaded {len(data)} chunks from {file.name}")
|
99 |
-
all_data.extend(data)
|
100 |
-
total_chunks += len(data)
|
101 |
-
if not any(doc["name"] == file.name for doc in uploaded_documents):
|
102 |
-
uploaded_documents.append({"name": file.name, "selected": True})
|
103 |
-
logging.info(f"Added new document to uploaded_documents: {file.name}")
|
104 |
-
else:
|
105 |
-
logging.info(f"Document already exists in uploaded_documents: {file.name}")
|
106 |
-
except Exception as e:
|
107 |
-
logging.error(f"Error processing file {file.name}: {str(e)}")
|
108 |
-
|
109 |
-
logging.info(f"Total chunks processed: {total_chunks}")
|
110 |
-
|
111 |
-
if not all_data:
|
112 |
-
logging.warning("No valid data extracted from uploaded files")
|
113 |
-
return "No valid data could be extracted from the uploaded files. Please check the file contents and try again.", display_documents()
|
114 |
-
|
115 |
try:
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
else:
|
121 |
-
logging.info("Creating new FAISS database")
|
122 |
-
database = FAISS.from_documents(all_data, embed)
|
123 |
-
|
124 |
-
database.save_local("faiss_database")
|
125 |
-
logging.info("FAISS database saved")
|
126 |
except Exception as e:
|
127 |
-
|
128 |
-
return f"
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
return "No documents selected for deletion.", display_documents()
|
140 |
-
|
141 |
-
embed = get_embeddings()
|
142 |
-
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
143 |
-
|
144 |
-
deleted_docs = []
|
145 |
-
docs_to_keep = []
|
146 |
-
for doc in database.docstore._dict.values():
|
147 |
-
if doc.metadata.get("source") not in selected_docs:
|
148 |
-
docs_to_keep.append(doc)
|
149 |
-
else:
|
150 |
-
deleted_docs.append(doc.metadata.get("source", "Unknown"))
|
151 |
-
|
152 |
-
# Print debugging information
|
153 |
-
logging.info(f"Total documents before deletion: {len(database.docstore._dict)}")
|
154 |
-
logging.info(f"Documents to keep: {len(docs_to_keep)}")
|
155 |
-
logging.info(f"Documents to delete: {len(deleted_docs)}")
|
156 |
-
|
157 |
-
if not docs_to_keep:
|
158 |
-
# If all documents are deleted, remove the FAISS database directory
|
159 |
-
if os.path.exists("faiss_database"):
|
160 |
-
shutil.rmtree("faiss_database")
|
161 |
-
logging.info("All documents deleted. Removed FAISS database directory.")
|
162 |
-
else:
|
163 |
-
# Create new FAISS index with remaining documents
|
164 |
-
new_database = FAISS.from_documents(docs_to_keep, embed)
|
165 |
-
new_database.save_local("faiss_database")
|
166 |
-
logging.info(f"Created new FAISS index with {len(docs_to_keep)} documents.")
|
167 |
-
|
168 |
-
# Update uploaded_documents list
|
169 |
-
uploaded_documents = [doc for doc in uploaded_documents if doc["name"] not in deleted_docs]
|
170 |
-
save_documents(uploaded_documents)
|
171 |
-
|
172 |
-
return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
|
173 |
-
|
174 |
-
def get_response_from_pdf(query, model, selected_docs, num_calls=3, temperature=0.2):
|
175 |
-
logging.info(f"Entering get_response_from_pdf with query: {query}, model: {model}, selected_docs: {selected_docs}")
|
176 |
-
|
177 |
-
embed = get_embeddings()
|
178 |
-
if os.path.exists("faiss_database"):
|
179 |
-
logging.info("Loading FAISS database")
|
180 |
-
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
181 |
-
else:
|
182 |
-
logging.warning("No FAISS database found")
|
183 |
-
yield "No documents available. Please upload PDF documents to answer questions."
|
184 |
-
return
|
185 |
-
|
186 |
-
# Pre-filter the documents
|
187 |
-
filtered_docs = []
|
188 |
-
for doc_id, doc in database.docstore._dict.items():
|
189 |
-
if isinstance(doc, Document) and doc.metadata.get("source") in selected_docs:
|
190 |
-
filtered_docs.append(doc)
|
191 |
-
|
192 |
-
logging.info(f"Number of documents after pre-filtering: {len(filtered_docs)}")
|
193 |
-
|
194 |
-
if not filtered_docs:
|
195 |
-
logging.warning(f"No documents found for the selected sources: {selected_docs}")
|
196 |
-
yield "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
197 |
-
return
|
198 |
-
|
199 |
-
# Create a new FAISS index with only the selected documents
|
200 |
-
filtered_db = FAISS.from_documents(filtered_docs, embed)
|
201 |
-
|
202 |
-
retriever = filtered_db.as_retriever(search_kwargs={"k": 10})
|
203 |
-
logging.info(f"Retrieving relevant documents for query: {query}")
|
204 |
-
relevant_docs = retriever.get_relevant_documents(query)
|
205 |
-
logging.info(f"Number of relevant documents retrieved: {len(relevant_docs)}")
|
206 |
-
|
207 |
-
for doc in relevant_docs:
|
208 |
-
logging.info(f"Document source: {doc.metadata['source']}")
|
209 |
-
logging.info(f"Document content preview: {doc.page_content[:100]}...") # Log first 100 characters of each document
|
210 |
-
|
211 |
-
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
212 |
-
logging.info(f"Total context length: {len(context_str)}")
|
213 |
-
|
214 |
-
if model == "@cf/meta/llama-3.1-8b-instruct":
|
215 |
-
logging.info("Using Cloudflare API")
|
216 |
-
# Use Cloudflare API with the retrieved context
|
217 |
-
for response in get_response_from_cloudflare(prompt="", context=context_str, query=query, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
218 |
-
yield response
|
219 |
-
else:
|
220 |
-
logging.info("Using Hugging Face API")
|
221 |
-
# Use Hugging Face API
|
222 |
-
prompt = f"""Using the following context from the PDF documents:
|
223 |
-
{context_str}
|
224 |
-
Write a detailed and complete response that answers the following user question: '{query}'"""
|
225 |
|
226 |
-
|
227 |
|
228 |
-
|
229 |
-
for i in range(num_calls):
|
230 |
-
logging.info(f"API call {i+1}/{num_calls}")
|
231 |
-
for message in client.chat_completion(
|
232 |
-
messages=[{"role": "user", "content": prompt}],
|
233 |
-
max_tokens=10000,
|
234 |
-
temperature=temperature,
|
235 |
-
stream=True,
|
236 |
-
):
|
237 |
-
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
238 |
-
chunk = message.choices[0].delta.content
|
239 |
-
response += chunk
|
240 |
-
yield response # Yield partial response
|
241 |
|
242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
247 |
|
248 |
-
|
249 |
|
250 |
-
|
251 |
-
if use_web_search:
|
252 |
-
for response in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
|
253 |
-
history[-1] = (message, response)
|
254 |
-
yield history
|
255 |
-
else:
|
256 |
-
for response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
257 |
-
history[-1] = (message, response)
|
258 |
-
yield history
|
259 |
-
except gr.CancelledError:
|
260 |
-
yield history
|
261 |
-
except Exception as e:
|
262 |
-
logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
|
263 |
-
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
264 |
-
yield history
|
265 |
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
last_user_msg = history[-1][0]
|
271 |
-
history = history[:-1] # Remove the last response
|
272 |
-
|
273 |
-
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls, selected_docs)
|
274 |
|
275 |
-
|
276 |
-
return gr.CheckboxGroup(
|
277 |
-
choices=[doc["name"] for doc in uploaded_documents],
|
278 |
-
value=[doc["name"] for doc in uploaded_documents if doc["selected"]],
|
279 |
-
label="Select documents to query or delete"
|
280 |
-
)
|
281 |
|
282 |
-
def
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
"2. Use web search to find information\n"
|
287 |
-
"3. Upload the documents and ask questions about uploaded PDF documents by selecting your respective document\n"
|
288 |
-
"4. For any queries feel free to reach out @[email protected] or discord - shreyas094\n\n"
|
289 |
-
"To get started, upload some PDFs or ask me a question!")
|
290 |
-
]
|
291 |
|
292 |
-
|
293 |
-
|
294 |
-
uploaded_documents = load_documents()
|
295 |
-
return display_documents()
|
296 |
|
297 |
-
|
298 |
-
|
299 |
|
300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
|
302 |
-
|
|
|
|
|
|
|
|
|
303 |
|
|
|
304 |
css = """
|
305 |
-
|
306 |
-
.chatbot-container {
|
307 |
-
height: 600px !important;
|
308 |
-
width: 100% !important;
|
309 |
-
}
|
310 |
-
.chatbot-container > div {
|
311 |
-
height: 100%;
|
312 |
-
width: 100%;
|
313 |
-
}
|
314 |
"""
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
demo = gr.ChatInterface(
|
317 |
-
|
318 |
additional_inputs=[
|
319 |
-
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
320 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
321 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
322 |
-
|
323 |
-
document_selector
|
324 |
],
|
325 |
title="AI-powered Web Search and PDF Chat Assistant",
|
326 |
-
description="
|
327 |
-
theme=
|
328 |
-
primary_hue="orange",
|
329 |
-
secondary_hue="amber",
|
330 |
-
neutral_hue="gray",
|
331 |
-
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
332 |
-
).set(
|
333 |
-
body_background_fill_dark="#0c0505",
|
334 |
-
block_background_fill_dark="#0c0505",
|
335 |
-
block_border_width="1px",
|
336 |
-
block_title_background_fill_dark="#1b0f0f",
|
337 |
-
input_background_fill_dark="#140b0b",
|
338 |
-
button_secondary_background_fill_dark="#140b0b",
|
339 |
-
border_color_accent_dark="#1b0f0f",
|
340 |
-
border_color_primary_dark="#1b0f0f",
|
341 |
-
background_fill_secondary_dark="#0c0505",
|
342 |
-
color_accent_soft_dark="transparent",
|
343 |
-
code_background_fill_dark="#140b0b"
|
344 |
-
),
|
345 |
css=css,
|
346 |
examples=[
|
347 |
-
["
|
348 |
-
["
|
349 |
-
["
|
350 |
],
|
351 |
cache_examples=False,
|
352 |
analytics_enabled=False,
|
@@ -356,50 +150,7 @@ demo = gr.ChatInterface(
|
|
356 |
likeable=True,
|
357 |
layout="bubble",
|
358 |
height=400,
|
359 |
-
value=initial_conversation()
|
360 |
)
|
361 |
)
|
362 |
|
363 |
-
|
364 |
-
with demo:
|
365 |
-
gr.Markdown("## Upload and Manage PDF Documents")
|
366 |
-
|
367 |
-
with gr.Row():
|
368 |
-
file_input = gr.Files(label="Upload your PDF documents", file_types=[".pdf"])
|
369 |
-
parser_dropdown = gr.Dropdown(choices=["pypdf", "llamaparse"], label="Select PDF Parser", value="llamaparse")
|
370 |
-
update_button = gr.Button("Upload Document")
|
371 |
-
refresh_button = gr.Button("Refresh Document List")
|
372 |
-
|
373 |
-
update_output = gr.Textbox(label="Update Status")
|
374 |
-
delete_button = gr.Button("Delete Selected Documents")
|
375 |
-
|
376 |
-
# Update both the output text and the document selector
|
377 |
-
update_button.click(update_vectors,
|
378 |
-
inputs=[file_input, parser_dropdown],
|
379 |
-
outputs=[update_output, document_selector])
|
380 |
-
|
381 |
-
# Add the refresh button functionality
|
382 |
-
refresh_button.click(refresh_documents,
|
383 |
-
inputs=[],
|
384 |
-
outputs=[document_selector])
|
385 |
-
|
386 |
-
# Add the delete button functionality
|
387 |
-
delete_button.click(delete_documents,
|
388 |
-
inputs=[document_selector],
|
389 |
-
outputs=[update_output, document_selector])
|
390 |
-
|
391 |
-
gr.Markdown(
|
392 |
-
"""
|
393 |
-
## How to use
|
394 |
-
1. Upload PDF documents using the file input at the top.
|
395 |
-
2. Select the PDF parser (pypdf or llamaparse) and click "Upload Document" to update the vector store.
|
396 |
-
3. Select the documents you want to query using the checkboxes.
|
397 |
-
4. Ask questions in the chat interface.
|
398 |
-
5. Toggle "Use Web Search" to switch between PDF chat and web search.
|
399 |
-
6. Adjust Temperature and Number of API Calls to fine-tune the response generation.
|
400 |
-
7. Use the provided examples or ask your own questions.
|
401 |
-
"""
|
402 |
-
)
|
403 |
-
|
404 |
-
if __name__ == "__main__":
|
405 |
-
demo.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from duckduckgo_search import DDGS
|
3 |
+
from typing import List, Dict
|
4 |
import os
|
|
|
5 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
|
|
8 |
|
9 |
# Environment variables and configurations
|
10 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
class ConversationManager:
|
13 |
+
def __init__(self):
|
14 |
+
self.history = []
|
15 |
+
self.current_context = None
|
|
|
16 |
|
17 |
+
def add_interaction(self, query, response):
|
18 |
+
self.history.append((query, response))
|
19 |
+
self.current_context = f"Previous query: {query}\nPrevious response summary: {response[:200]}..."
|
20 |
|
21 |
+
def get_context(self):
|
22 |
+
return self.current_context
|
23 |
|
24 |
+
def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
try:
|
26 |
+
results = list(DDGS().text(query, max_results=max_results))
|
27 |
+
if not results:
|
28 |
+
print(f"No results found for query: {query}")
|
29 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
except Exception as e:
|
31 |
+
print(f"An error occurred during web search: {str(e)}")
|
32 |
+
return [{"error": f"An error occurred during web search: {str(e)}"}]
|
33 |
+
|
34 |
+
def rephrase_query(original_query: str, conversation_manager: ConversationManager, model: str) -> str:
|
35 |
+
context = conversation_manager.get_context()
|
36 |
+
if context:
|
37 |
+
prompt = f"""You are a highly intelligent conversational chatbot. Your task is to analyze the given context and new query, then decide whether to rephrase the query with or without incorporating the context. Follow these steps:
|
38 |
+
|
39 |
+
1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.
|
40 |
+
2. If it's a continuation, rephrase the query by incorporating relevant information from the context to make it more specific and contextual.
|
41 |
+
3. If it's a new topic, rephrase the query to make it more appropriate for a web search, focusing on clarity and accuracy without using the previous context.
|
42 |
+
4. Provide ONLY the rephrased query without any additional explanation or reasoning.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
Context: {context}
|
45 |
|
46 |
+
New query: {original_query}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
Rephrased query:"""
|
49 |
+
response = DDGS().chat(prompt, model=model)
|
50 |
+
# Extract only the rephrased query, removing any explanations
|
51 |
+
rephrased_query = response.split('\n')[0].strip()
|
52 |
+
return rephrased_query
|
53 |
+
return original_query
|
54 |
+
|
55 |
+
def summarize_results(query: str, search_results: List[Dict[str, str]], conversation_manager: ConversationManager, model: str) -> str:
|
56 |
+
try:
|
57 |
+
context = conversation_manager.get_context()
|
58 |
+
search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
|
59 |
|
60 |
+
prompt = f"""You are a highly intelligent & expert analyst and your job is to skillfully articulate the web search results about '{query}' and considering the context: {context},
|
61 |
+
You have to create a comprehensive news summary FOCUSING on the context provided to you.
|
62 |
+
Include key facts, relevant statistics, and expert opinions if available.
|
63 |
+
Ensure the article is well-structured with an introduction, main body, and conclusion, IF NECESSARY.
|
64 |
+
Address the query in the context of the ongoing conversation IF APPLICABLE.
|
65 |
+
Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:
|
66 |
|
67 |
+
{search_context}
|
68 |
|
69 |
+
Article:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
summary = DDGS().chat(prompt, model=model)
|
72 |
+
return summary
|
73 |
+
except Exception as e:
|
74 |
+
return f"An error occurred during summarization: {str(e)}"
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
conversation_manager = ConversationManager()
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
def respond(message, chat_history, temperature, num_api_calls, model):
|
79 |
+
final_summary = ""
|
80 |
+
original_query = message
|
81 |
+
rephrased_query = rephrase_query(message, conversation_manager, model)
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
logging.info(f"Original query: {original_query}")
|
84 |
+
logging.info(f"Rephrased query: {rephrased_query}")
|
|
|
|
|
85 |
|
86 |
+
for _ in range(num_api_calls):
|
87 |
+
search_results = get_web_search_results(rephrased_query)
|
88 |
|
89 |
+
if not search_results:
|
90 |
+
final_summary += f"No search results found for the query: {rephrased_query}\n\n"
|
91 |
+
elif "error" in search_results[0]:
|
92 |
+
final_summary += search_results[0]["error"] + "\n\n"
|
93 |
+
else:
|
94 |
+
summary = summarize_results(rephrased_query, search_results, conversation_manager, model)
|
95 |
+
final_summary += summary + "\n\n"
|
96 |
|
97 |
+
if final_summary:
|
98 |
+
conversation_manager.add_interaction(original_query, final_summary)
|
99 |
+
return final_summary
|
100 |
+
else:
|
101 |
+
return "Unable to generate a response. Please try a different query."
|
102 |
|
103 |
+
# The rest of your code (CSS, theme, and Gradio interface setup) remains the same
|
104 |
css = """
|
105 |
+
Your custom CSS here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
"""
|
107 |
|
108 |
+
custom_placeholder = "Ask me anything about web content"
|
109 |
+
|
110 |
+
theme = gr.themes.Soft(
|
111 |
+
primary_hue="orange",
|
112 |
+
secondary_hue="amber",
|
113 |
+
neutral_hue="gray",
|
114 |
+
font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
|
115 |
+
).set(
|
116 |
+
body_background_fill_dark="#0c0505",
|
117 |
+
block_background_fill_dark="#0c0505",
|
118 |
+
block_border_width="1px",
|
119 |
+
block_title_background_fill_dark="#1b0f0f",
|
120 |
+
input_background_fill_dark="#140b0b",
|
121 |
+
button_secondary_background_fill_dark="#140b0b",
|
122 |
+
border_color_accent_dark="#1b0f0f",
|
123 |
+
border_color_primary_dark="#1b0f0f",
|
124 |
+
background_fill_secondary_dark="#0c0505",
|
125 |
+
color_accent_soft_dark="transparent",
|
126 |
+
code_background_fill_dark="#140b0b"
|
127 |
+
)
|
128 |
+
|
129 |
demo = gr.ChatInterface(
|
130 |
+
respond,
|
131 |
additional_inputs=[
|
|
|
132 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
133 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
134 |
+
gr.Dropdown(["gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"], label="Model")
|
|
|
135 |
],
|
136 |
title="AI-powered Web Search and PDF Chat Assistant",
|
137 |
+
description="This AI-powered Web Search and PDF Chat Assistant combines real-time web search capabilities with advanced language processing.",
|
138 |
+
theme=theme,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
css=css,
|
140 |
examples=[
|
141 |
+
["What is AI"],
|
142 |
+
["Any recent news on US Banks"],
|
143 |
+
["Who is Donald Trump"]
|
144 |
],
|
145 |
cache_examples=False,
|
146 |
analytics_enabled=False,
|
|
|
150 |
likeable=True,
|
151 |
layout="bubble",
|
152 |
height=400,
|
|
|
153 |
)
|
154 |
)
|
155 |
|
156 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|