Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -184,7 +184,7 @@ def delete_documents(selected_docs):
|
|
184 |
|
185 |
return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
|
186 |
|
187 |
-
|
188 |
print(f"Starting generate_chunked_response with model: {model}, num_calls: {num_calls}")
|
189 |
full_response = ""
|
190 |
messages = [{"role": "user", "content": prompt}]
|
@@ -199,27 +199,26 @@ async def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3
|
|
199 |
for i in range(num_calls):
|
200 |
print(f"Starting Cloudflare API call {i+1}")
|
201 |
try:
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
continue
|
223 |
print(f"Cloudflare API call {i+1} completed")
|
224 |
except Exception as e:
|
225 |
print(f"Error in generating response from Cloudflare: {str(e)}")
|
@@ -232,10 +231,11 @@ async def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3
|
|
232 |
for i in range(num_calls):
|
233 |
print(f"Starting Hugging Face API call {i+1}")
|
234 |
try:
|
235 |
-
|
236 |
messages=messages,
|
237 |
max_tokens=max_tokens,
|
238 |
temperature=temperature,
|
|
|
239 |
):
|
240 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
241 |
chunk = message.choices[0].delta.content
|
@@ -308,60 +308,43 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
|
308 |
|
309 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
310 |
|
311 |
-
def respond(message, history, model, temperature, num_calls,
|
312 |
logging.info(f"User Query: {message}")
|
313 |
logging.info(f"Model Used: {model}")
|
314 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
315 |
-
|
316 |
logging.info(f"Selected Documents: {selected_docs}")
|
317 |
|
318 |
try:
|
319 |
if use_web_search:
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
|
|
|
|
325 |
else:
|
326 |
embed = get_embeddings()
|
327 |
if os.path.exists("faiss_database"):
|
328 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
329 |
retriever = database.as_retriever()
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
context_str = "\n".join([doc.page_content for doc in relevant_docs])
|
340 |
-
else:
|
341 |
-
context_str = "No documents available."
|
342 |
-
yield "No documents available. Please upload PDF documents to answer questions."
|
343 |
-
return
|
344 |
-
|
345 |
-
if model == "@cf/meta/llama-3.1-8b-instruct":
|
346 |
-
# Use Cloudflare API
|
347 |
-
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
348 |
-
first_line = partial_response.split('\n')[0] if partial_response else ''
|
349 |
-
# logging.info(f"Generated Response (first line): {first_line}")
|
350 |
-
yield partial_response
|
351 |
else:
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
except Exception as e:
|
358 |
logging.error(f"Error with {model}: {str(e)}")
|
359 |
-
|
360 |
-
logging.info("Falling back to Mistral model due to Phi-3 error")
|
361 |
-
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
362 |
-
yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search, selected_docs)
|
363 |
-
else:
|
364 |
-
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
365 |
|
366 |
logging.basicConfig(level=logging.DEBUG)
|
367 |
|
@@ -575,8 +558,7 @@ use_web_search = gr.Checkbox(label="Use Web Search", value=True)
|
|
575 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
576 |
|
577 |
demo = gr.ChatInterface(
|
578 |
-
|
579 |
-
async_mode=True,
|
580 |
additional_inputs=[
|
581 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
582 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
|
|
184 |
|
185 |
return f"Deleted documents: {', '.join(deleted_docs)}", display_documents()
|
186 |
|
187 |
+
def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temperature=0.2):
|
188 |
print(f"Starting generate_chunked_response with model: {model}, num_calls: {num_calls}")
|
189 |
full_response = ""
|
190 |
messages = [{"role": "user", "content": prompt}]
|
|
|
199 |
for i in range(num_calls):
|
200 |
print(f"Starting Cloudflare API call {i+1}")
|
201 |
try:
|
202 |
+
response = requests.post(
|
203 |
+
f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
|
204 |
+
json={
|
205 |
+
"messages": messages,
|
206 |
+
"stream": True,
|
207 |
+
"max_tokens": max_tokens,
|
208 |
+
"temperature": temperature
|
209 |
+
},
|
210 |
+
headers=headers,
|
211 |
+
stream=True
|
212 |
+
)
|
213 |
+
for line in response.iter_lines():
|
214 |
+
if line:
|
215 |
+
try:
|
216 |
+
json_data = json.loads(line.decode('utf-8').split('data: ')[1])
|
217 |
+
chunk = json_data.get('response', '')
|
218 |
+
full_response += chunk
|
219 |
+
yield full_response
|
220 |
+
except (json.JSONDecodeError, IndexError):
|
221 |
+
continue
|
|
|
222 |
print(f"Cloudflare API call {i+1} completed")
|
223 |
except Exception as e:
|
224 |
print(f"Error in generating response from Cloudflare: {str(e)}")
|
|
|
231 |
for i in range(num_calls):
|
232 |
print(f"Starting Hugging Face API call {i+1}")
|
233 |
try:
|
234 |
+
for message in client.chat_completion(
|
235 |
messages=messages,
|
236 |
max_tokens=max_tokens,
|
237 |
temperature=temperature,
|
238 |
+
stream=True,
|
239 |
):
|
240 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
241 |
chunk = message.choices[0].delta.content
|
|
|
308 |
|
309 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
310 |
|
311 |
+
def respond(message, history, use_web_search, model, temperature, num_calls, selected_docs):
|
312 |
logging.info(f"User Query: {message}")
|
313 |
logging.info(f"Model Used: {model}")
|
314 |
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
|
|
315 |
logging.info(f"Selected Documents: {selected_docs}")
|
316 |
|
317 |
try:
|
318 |
if use_web_search:
|
319 |
+
search_results = duckduckgo_search(message)
|
320 |
+
context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
|
321 |
+
for result in search_results if 'body' in result)
|
322 |
+
prompt = f"""Using the following context:
|
323 |
+
{context}
|
324 |
+
Write a detailed and complete research document that fulfills the following user request: '{message}'
|
325 |
+
After writing the document, please provide a list of sources used in your response."""
|
326 |
else:
|
327 |
embed = get_embeddings()
|
328 |
if os.path.exists("faiss_database"):
|
329 |
database = FAISS.load_local("faiss_database", embed, allow_dangerous_deserialization=True)
|
330 |
retriever = database.as_retriever()
|
331 |
+
relevant_docs = retriever.get_relevant_documents(message)
|
332 |
+
filtered_docs = [doc for doc in relevant_docs if doc.metadata["source"] in selected_docs]
|
333 |
+
if not filtered_docs:
|
334 |
+
return "No relevant information found in the selected documents. Please try selecting different documents or rephrasing your query."
|
335 |
+
context_str = "\n".join([doc.page_content for doc in filtered_docs])
|
336 |
+
prompt = f"""Using the following context from the PDF documents:
|
337 |
+
{context_str}
|
338 |
+
Write a detailed and complete response that answers the following user question: '{message}'"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
else:
|
340 |
+
return "No documents available. Please upload PDF documents to answer questions."
|
341 |
+
|
342 |
+
for response in generate_chunked_response(prompt, model, num_calls=num_calls, temperature=temperature):
|
343 |
+
yield response
|
344 |
+
|
345 |
except Exception as e:
|
346 |
logging.error(f"Error with {model}: {str(e)}")
|
347 |
+
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
|
|
|
|
|
|
|
|
|
|
348 |
|
349 |
logging.basicConfig(level=logging.DEBUG)
|
350 |
|
|
|
558 |
custom_placeholder = "Ask a question (Note: You can toggle between Web Search and PDF Chat in Additional Inputs below)"
|
559 |
|
560 |
demo = gr.ChatInterface(
|
561 |
+
respond,
|
|
|
562 |
additional_inputs=[
|
563 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
|
564 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|