Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,10 @@ import gradio as gr
|
|
| 2 |
from duckduckgo_search import DDGS
|
| 3 |
from typing import List, Dict
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Environment variables and configurations
|
| 7 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
@@ -37,7 +41,7 @@ def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str,
|
|
| 37 |
def rephrase_query(original_query: str, conversation_manager: ConversationManager) -> str:
|
| 38 |
context = conversation_manager.get_context()
|
| 39 |
if context:
|
| 40 |
-
prompt = f"""Given the following context and a new query, rephrase the query to make it more specific and contextual:
|
| 41 |
|
| 42 |
Context: {context}
|
| 43 |
|
|
@@ -45,8 +49,10 @@ def rephrase_query(original_query: str, conversation_manager: ConversationManage
|
|
| 45 |
|
| 46 |
Rephrased query:"""
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
return original_query
|
| 51 |
|
| 52 |
def summarize_results(query: str, search_results: List[Dict[str, str]], model: str, conversation_manager: ConversationManager) -> str:
|
|
@@ -74,8 +80,12 @@ conversation_manager = ConversationManager()
|
|
| 74 |
|
| 75 |
def respond(message, chat_history, model, temperature, num_api_calls):
|
| 76 |
final_summary = ""
|
|
|
|
| 77 |
rephrased_query = rephrase_query(message, conversation_manager)
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
for _ in range(num_api_calls):
|
| 80 |
search_results = get_web_search_results(rephrased_query)
|
| 81 |
|
|
@@ -88,7 +98,7 @@ def respond(message, chat_history, model, temperature, num_api_calls):
|
|
| 88 |
final_summary += summary + "\n\n"
|
| 89 |
|
| 90 |
if final_summary:
|
| 91 |
-
conversation_manager.add_interaction(
|
| 92 |
return final_summary
|
| 93 |
else:
|
| 94 |
return "Unable to generate a response. Please try a different query."
|
|
|
|
| 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 |
|
| 10 |
# Environment variables and configurations
|
| 11 |
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
|
|
|
|
| 41 |
def rephrase_query(original_query: str, conversation_manager: ConversationManager) -> str:
|
| 42 |
context = conversation_manager.get_context()
|
| 43 |
if context:
|
| 44 |
+
prompt = f"""Given the following context and a new query, rephrase the query to make it more specific and contextual. Provide ONLY the rephrased query without any additional explanation:
|
| 45 |
|
| 46 |
Context: {context}
|
| 47 |
|
|
|
|
| 49 |
|
| 50 |
Rephrased query:"""
|
| 51 |
|
| 52 |
+
response = DDGS().chat(prompt, model="llama-3-70b")
|
| 53 |
+
# Extract only the rephrased query, removing any explanations
|
| 54 |
+
rephrased_query = response.split('\n')[0].strip()
|
| 55 |
+
return rephrased_query
|
| 56 |
return original_query
|
| 57 |
|
| 58 |
def summarize_results(query: str, search_results: List[Dict[str, str]], model: str, conversation_manager: ConversationManager) -> str:
|
|
|
|
| 80 |
|
| 81 |
def respond(message, chat_history, model, temperature, num_api_calls):
|
| 82 |
final_summary = ""
|
| 83 |
+
original_query = message
|
| 84 |
rephrased_query = rephrase_query(message, conversation_manager)
|
| 85 |
|
| 86 |
+
logging.info(f"Original query: {original_query}")
|
| 87 |
+
logging.info(f"Rephrased query: {rephrased_query}")
|
| 88 |
+
|
| 89 |
for _ in range(num_api_calls):
|
| 90 |
search_results = get_web_search_results(rephrased_query)
|
| 91 |
|
|
|
|
| 98 |
final_summary += summary + "\n\n"
|
| 99 |
|
| 100 |
if final_summary:
|
| 101 |
+
conversation_manager.add_interaction(original_query, final_summary)
|
| 102 |
return final_summary
|
| 103 |
else:
|
| 104 |
return "Unable to generate a response. Please try a different query."
|