Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -346,19 +346,13 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
|
|
346 |
yield "No documents available. Please upload PDF documents to answer questions."
|
347 |
return
|
348 |
|
349 |
-
if model
|
350 |
-
# Use Cloudflare API
|
351 |
-
for partial_response in get_response_from_cloudflare(prompt="", context=context_str, query=message, num_calls=num_calls, temperature=temperature, search_type="pdf"):
|
352 |
-
first_line = partial_response.split('\n')[0] if partial_response else ''
|
353 |
-
logging.info(f"Generated Response (first line): {first_line}")
|
354 |
-
yield partial_response
|
355 |
-
elif model in ["gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"]:
|
356 |
# Use DuckDuckGo Chat API
|
357 |
logging.info(f"Calling DuckDuckGo Chat API with model: {model}")
|
358 |
response = chat(message, model=model, timeout=30)
|
359 |
yield response
|
360 |
else:
|
361 |
-
# Use Hugging Face API
|
362 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
363 |
first_line = partial_response.split('\n')[0] if partial_response else ''
|
364 |
logging.info(f"Generated Response (first line): {first_line}")
|
@@ -372,12 +366,14 @@ def respond(message, history, model, temperature, num_calls, use_web_search, sel
|
|
372 |
else:
|
373 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
374 |
|
375 |
-
|
|
|
|
|
376 |
"""Initiates a chat session with DuckDuckGo AI.
|
377 |
|
378 |
Args:
|
379 |
keywords (str): The initial message or question to send to the AI.
|
380 |
-
model (str): The model to use: "gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b".
|
381 |
timeout (int): Timeout value for the HTTP client. Defaults to 30.
|
382 |
|
383 |
Returns:
|
@@ -385,36 +381,13 @@ def chat(self, keywords: str, model: str = "gpt-4o-mini", timeout: int = 30) ->
|
|
385 |
"""
|
386 |
logging.info(f"Calling DuckDuckGo Chat API with model: {model}")
|
387 |
|
388 |
-
if model == "gpt-4o-mini":
|
389 |
-
url = "https://api.duckduckgo.com/api/d2a/v1/chat/gpt-4o-mini"
|
390 |
-
elif model == "claude-3-haiku":
|
391 |
-
url = "https://api.duckduckgo.com/api/d2a/v1/chat/claude-3-haiku"
|
392 |
-
elif model == "llama-3.1-70b":
|
393 |
-
url = "https://api.duckduckgo.com/api/d2a/v1/chat/llama-3.1-70b"
|
394 |
-
elif model == "mixtral-8x7b":
|
395 |
-
url = "https://api.duckduckgo.com/api/d2a/v1/chat/mixtral-8x7b"
|
396 |
-
else:
|
397 |
-
raise ValueError(f"Invalid model: {model}")
|
398 |
-
|
399 |
-
payload = {"keywords": keywords}
|
400 |
-
headers = {
|
401 |
-
"Content-Type": "application/json",
|
402 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
403 |
-
}
|
404 |
-
|
405 |
try:
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
response.raise_for_status()
|
410 |
-
logging.info(f"DuckDuckGo Chat API response: {response.json()}")
|
411 |
-
return response.json()["response"]
|
412 |
-
except requests.exceptions.RequestException as e:
|
413 |
logging.error(f"Error in DuckDuckGo chat: {str(e)}")
|
414 |
return "Error in DuckDuckGo chat. Please try again later."
|
415 |
|
416 |
-
logging.basicConfig(level=logging.DEBUG)
|
417 |
-
|
418 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
419 |
headers = {
|
420 |
"Authorization": f"Bearer {API_TOKEN}",
|
|
|
346 |
yield "No documents available. Please upload PDF documents to answer questions."
|
347 |
return
|
348 |
|
349 |
+
if model in ["gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
# Use DuckDuckGo Chat API
|
351 |
logging.info(f"Calling DuckDuckGo Chat API with model: {model}")
|
352 |
response = chat(message, model=model, timeout=30)
|
353 |
yield response
|
354 |
else:
|
355 |
+
# Use Hugging Face API or Cloudflare API
|
356 |
for partial_response in get_response_from_pdf(message, model, selected_docs, num_calls=num_calls, temperature=temperature):
|
357 |
first_line = partial_response.split('\n')[0] if partial_response else ''
|
358 |
logging.info(f"Generated Response (first line): {first_line}")
|
|
|
366 |
else:
|
367 |
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
368 |
|
369 |
+
from duckduckgo_search import DDGS
|
370 |
+
|
371 |
+
def chat(keywords: str, model: str, timeout: int = 30) -> str:
|
372 |
"""Initiates a chat session with DuckDuckGo AI.
|
373 |
|
374 |
Args:
|
375 |
keywords (str): The initial message or question to send to the AI.
|
376 |
+
model (str): The model to use: "gpt-4o-mini", "claude-3-haiku", "llama-3.1-70b", "mixtral-8x7b".
|
377 |
timeout (int): Timeout value for the HTTP client. Defaults to 30.
|
378 |
|
379 |
Returns:
|
|
|
381 |
"""
|
382 |
logging.info(f"Calling DuckDuckGo Chat API with model: {model}")
|
383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
try:
|
385 |
+
with DDGS() as ddgs:
|
386 |
+
return ddgs.chat(keywords, model=model, timeout=timeout)
|
387 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
388 |
logging.error(f"Error in DuckDuckGo chat: {str(e)}")
|
389 |
return "Error in DuckDuckGo chat. Please try again later."
|
390 |
|
|
|
|
|
391 |
def get_response_from_cloudflare(prompt, context, query, num_calls=3, temperature=0.2, search_type="pdf"):
|
392 |
headers = {
|
393 |
"Authorization": f"Bearer {API_TOKEN}",
|