Update app.py
Browse files
app.py
CHANGED
|
@@ -36,9 +36,17 @@ class ConversationManager:
|
|
| 36 |
|
| 37 |
conversation_manager = ConversationManager()
|
| 38 |
|
| 39 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
if not results:
|
| 43 |
print(f"No results found for query: {query}")
|
| 44 |
return results
|
|
@@ -87,10 +95,11 @@ def summarize_web_results(query: str, search_results: List[Dict[str, str]], conv
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"An error occurred during summarization: {str(e)}"
|
| 89 |
|
| 90 |
-
def respond(message, history, model, temperature, num_calls, use_web_search):
|
| 91 |
logging.info(f"User Query: {message}")
|
| 92 |
logging.info(f"Model Used: {model}")
|
| 93 |
logging.info(f"Use Web Search: {use_web_search}")
|
|
|
|
| 94 |
|
| 95 |
if use_web_search:
|
| 96 |
original_query = message
|
|
@@ -100,7 +109,7 @@ def respond(message, history, model, temperature, num_calls, use_web_search):
|
|
| 100 |
|
| 101 |
final_summary = ""
|
| 102 |
for _ in range(num_calls):
|
| 103 |
-
search_results = get_web_search_results(rephrased_query)
|
| 104 |
if not search_results:
|
| 105 |
final_summary += f"No search results found for the query: {rephrased_query}\n\n"
|
| 106 |
elif "error" in search_results[0]:
|
|
@@ -140,7 +149,7 @@ def initial_conversation():
|
|
| 140 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
| 141 |
"1. Make sure the 'Use Web Search' checkbox is enabled in the Additional Inputs section.\n"
|
| 142 |
"2. Ask me any question, and I'll search the web for the most relevant and up-to-date information.\n"
|
| 143 |
-
"3. You can adjust the model, temperature,
|
| 144 |
"To get started, just ask me a question!")
|
| 145 |
]
|
| 146 |
|
|
@@ -151,7 +160,8 @@ demo = gr.ChatInterface(
|
|
| 151 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
|
| 152 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 153 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 154 |
-
gr.Checkbox(label="Use Web Search", value=True)
|
|
|
|
| 155 |
],
|
| 156 |
title="AI-powered Web Search Assistant",
|
| 157 |
description="Ask questions and get answers from the latest web information.",
|
|
|
|
| 36 |
|
| 37 |
conversation_manager = ConversationManager()
|
| 38 |
|
| 39 |
+
def duckduckgo_search(query):
|
| 40 |
+
with DDGS() as ddgs:
|
| 41 |
+
results = ddgs.text(query, max_results=5)
|
| 42 |
+
return list(results)
|
| 43 |
+
|
| 44 |
+
def get_web_search_results(query: str, max_results: int = 10, use_ddgs_text: bool = False) -> List[Dict[str, str]]:
|
| 45 |
try:
|
| 46 |
+
if use_ddgs_text:
|
| 47 |
+
results = duckduckgo_search(query)
|
| 48 |
+
else:
|
| 49 |
+
results = list(DDGS().text(query, max_results=max_results))
|
| 50 |
if not results:
|
| 51 |
print(f"No results found for query: {query}")
|
| 52 |
return results
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
return f"An error occurred during summarization: {str(e)}"
|
| 97 |
|
| 98 |
+
def respond(message, history, model, temperature, num_calls, use_web_search, search_method):
|
| 99 |
logging.info(f"User Query: {message}")
|
| 100 |
logging.info(f"Model Used: {model}")
|
| 101 |
logging.info(f"Use Web Search: {use_web_search}")
|
| 102 |
+
logging.info(f"Search Method: {search_method}")
|
| 103 |
|
| 104 |
if use_web_search:
|
| 105 |
original_query = message
|
|
|
|
| 109 |
|
| 110 |
final_summary = ""
|
| 111 |
for _ in range(num_calls):
|
| 112 |
+
search_results = get_web_search_results(rephrased_query, use_ddgs_text=(search_method == "DDGS.text"))
|
| 113 |
if not search_results:
|
| 114 |
final_summary += f"No search results found for the query: {rephrased_query}\n\n"
|
| 115 |
elif "error" in search_results[0]:
|
|
|
|
| 149 |
(None, "Welcome! I'm your AI assistant for web search. Here's how you can use me:\n\n"
|
| 150 |
"1. Make sure the 'Use Web Search' checkbox is enabled in the Additional Inputs section.\n"
|
| 151 |
"2. Ask me any question, and I'll search the web for the most relevant and up-to-date information.\n"
|
| 152 |
+
"3. You can adjust the model, temperature, number of API calls, and search method in the Additional Inputs section to fine-tune your results.\n\n"
|
| 153 |
"To get started, just ask me a question!")
|
| 154 |
]
|
| 155 |
|
|
|
|
| 160 |
gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0]),
|
| 161 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
|
| 162 |
gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls"),
|
| 163 |
+
gr.Checkbox(label="Use Web Search", value=True),
|
| 164 |
+
gr.Radio(["DDGS.chat", "DDGS.text"], label="Search Method", value="DDGS.chat")
|
| 165 |
],
|
| 166 |
title="AI-powered Web Search Assistant",
|
| 167 |
description="Ask questions and get answers from the latest web information.",
|