Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -221,17 +221,12 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
|
|
221 |
|
222 |
try:
|
223 |
if use_web_search:
|
224 |
-
for main_content, sources
|
225 |
-
|
226 |
-
formatted_sources = match_and_format_sources(sources, search_results)
|
227 |
-
response = f"{main_content}\n\n{formatted_sources}"
|
228 |
-
else:
|
229 |
-
response = main_content
|
230 |
first_line = response.split('\n')[0] if response else ''
|
231 |
logging.info(f"Generated Response (first line): {first_line}")
|
232 |
yield response
|
233 |
else:
|
234 |
-
# PDF search logic (unchanged)
|
235 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
236 |
# Use Cloudflare API
|
237 |
embed = get_embeddings()
|
@@ -243,11 +238,10 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
|
|
243 |
else:
|
244 |
context_str = "No documents available."
|
245 |
|
246 |
-
for
|
247 |
-
|
248 |
-
first_line = response.split('\n')[0] if response else ''
|
249 |
logging.info(f"Generated Response (first line): {first_line}")
|
250 |
-
yield
|
251 |
else:
|
252 |
# Use Hugging Face API
|
253 |
for partial_response in get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature):
|
@@ -305,49 +299,39 @@ After writing the document, please provide a list of sources used in your respon
|
|
305 |
if 'response' in json_response:
|
306 |
chunk = json_response['response']
|
307 |
full_response += chunk
|
308 |
-
|
309 |
-
main_content, sources = split_content_and_sources(full_response)
|
310 |
-
yield main_content, sources
|
311 |
except (json.JSONDecodeError, IndexError) as e:
|
312 |
logging.error(f"Error parsing streaming response: {str(e)}")
|
313 |
continue
|
314 |
else:
|
315 |
logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
|
316 |
-
yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
|
317 |
except Exception as e:
|
318 |
logging.error(f"Error in generating response from Cloudflare: {str(e)}")
|
319 |
-
yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
|
320 |
|
321 |
if not full_response:
|
322 |
-
yield "I apologize, but I couldn't generate a response at this time. Please try again later."
|
323 |
-
|
324 |
-
def split_content_and_sources(text):
|
325 |
-
# Attempt to split the text into main content and sources
|
326 |
-
parts = text.split("Sources:", 1)
|
327 |
-
if len(parts) > 1:
|
328 |
-
return parts[0].strip(), "Sources:" + parts[1]
|
329 |
-
else:
|
330 |
-
return text, ""
|
331 |
|
332 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
333 |
search_results = duckduckgo_search(query)
|
334 |
-
context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['
|
335 |
for result in search_results if 'body' in result)
|
336 |
|
337 |
-
prompt = f"""Using the following context
|
338 |
{context}
|
339 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
340 |
After writing the document, please provide a list of sources used in your response."""
|
341 |
|
342 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
343 |
# Use Cloudflare API
|
344 |
-
for
|
345 |
-
yield
|
346 |
else:
|
347 |
-
# Use Hugging Face API
|
348 |
client = InferenceClient(model, token=huggingface_token)
|
349 |
|
350 |
-
|
351 |
for i in range(num_calls):
|
352 |
for message in client.chat_completion(
|
353 |
messages=[{"role": "user", "content": prompt}],
|
@@ -357,16 +341,8 @@ After writing the document, please provide a list of sources used in your respon
|
|
357 |
):
|
358 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
359 |
chunk = message.choices[0].delta.content
|
360 |
-
|
361 |
-
main_content,
|
362 |
-
yield main_content, sources, search_results
|
363 |
-
|
364 |
-
def split_content_and_sources(text):
|
365 |
-
parts = text.split("Sources:", 1)
|
366 |
-
if len(parts) > 1:
|
367 |
-
return parts[0].strip(), "Sources:" + parts[1].strip()
|
368 |
-
else:
|
369 |
-
return text.strip(), ""
|
370 |
|
371 |
def get_response_from_pdf(query, model, num_calls=3, temperature=0.2):
|
372 |
embed = get_embeddings()
|
@@ -483,4 +459,4 @@ with demo:
|
|
483 |
)
|
484 |
|
485 |
if __name__ == "__main__":
|
486 |
-
demo.launch(share=True)
|
|
|
221 |
|
222 |
try:
|
223 |
if use_web_search:
|
224 |
+
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
|
225 |
+
response = f"{main_content}\n\n{sources}"
|
|
|
|
|
|
|
|
|
226 |
first_line = response.split('\n')[0] if response else ''
|
227 |
logging.info(f"Generated Response (first line): {first_line}")
|
228 |
yield response
|
229 |
else:
|
|
|
230 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
231 |
# Use Cloudflare API
|
232 |
embed = get_embeddings()
|
|
|
238 |
else:
|
239 |
context_str = "No documents available."
|
240 |
|
241 |
+
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
242 |
+
first_line = partial_response.split('\n')[0] if partial_response else ''
|
|
|
243 |
logging.info(f"Generated Response (first line): {first_line}")
|
244 |
+
yield partial_response
|
245 |
else:
|
246 |
# Use Hugging Face API
|
247 |
for partial_response in get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature):
|
|
|
299 |
if 'response' in json_response:
|
300 |
chunk = json_response['response']
|
301 |
full_response += chunk
|
302 |
+
yield full_response
|
|
|
|
|
303 |
except (json.JSONDecodeError, IndexError) as e:
|
304 |
logging.error(f"Error parsing streaming response: {str(e)}")
|
305 |
continue
|
306 |
else:
|
307 |
logging.error(f"HTTP Error: {response.status_code}, Response: {response.text}")
|
308 |
+
yield f"I apologize, but I encountered an HTTP error: {response.status_code}. Please try again later."
|
309 |
except Exception as e:
|
310 |
logging.error(f"Error in generating response from Cloudflare: {str(e)}")
|
311 |
+
yield f"I apologize, but an error occurred: {str(e)}. Please try again later."
|
312 |
|
313 |
if not full_response:
|
314 |
+
yield "I apologize, but I couldn't generate a response at this time. Please try again later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
|
316 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
317 |
search_results = duckduckgo_search(query)
|
318 |
+
context = "\n".join(f"{result['title']}\n{result['body']}\nSource: {result['href']}\n"
|
319 |
for result in search_results if 'body' in result)
|
320 |
|
321 |
+
prompt = f"""Using the following context:
|
322 |
{context}
|
323 |
Write a detailed and complete research document that fulfills the following user request: '{query}'
|
324 |
After writing the document, please provide a list of sources used in your response."""
|
325 |
|
326 |
if model == "@cf/meta/llama-3.1-8b-instruct":
|
327 |
# Use Cloudflare API
|
328 |
+
for response in get_response_from_cloudflare(prompt="", context=context, query=query, num_calls=num_calls, temperature=temperature, search_type="web"):
|
329 |
+
yield response, "" # Yield streaming response without sources
|
330 |
else:
|
331 |
+
# Use Hugging Face API
|
332 |
client = InferenceClient(model, token=huggingface_token)
|
333 |
|
334 |
+
main_content = ""
|
335 |
for i in range(num_calls):
|
336 |
for message in client.chat_completion(
|
337 |
messages=[{"role": "user", "content": prompt}],
|
|
|
341 |
):
|
342 |
if message.choices and message.choices[0].delta and message.choices[0].delta.content:
|
343 |
chunk = message.choices[0].delta.content
|
344 |
+
main_content += chunk
|
345 |
+
yield main_content, "" # Yield partial main content without sources
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
|
347 |
def get_response_from_pdf(query, model, num_calls=3, temperature=0.2):
|
348 |
embed = get_embeddings()
|
|
|
459 |
)
|
460 |
|
461 |
if __name__ == "__main__":
|
462 |
+
demo.launch(share=True)
|