Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,122 @@
|
|
|
|
|
|
1 |
from smolagents import DuckDuckGoSearchTool
|
|
|
2 |
|
3 |
-
# Initialize the tool
|
4 |
web_search = DuckDuckGoSearchTool()
|
5 |
|
6 |
-
|
7 |
-
results = web_search("Pont des Arts length in meters")
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
from smolagents import DuckDuckGoSearchTool
|
4 |
+
import re
|
5 |
|
|
|
6 |
web_search = DuckDuckGoSearchTool()
|
7 |
|
8 |
+
SYSTEM_PROMPT = """You are an AI research assistant that can search the web. Follow these steps:
|
|
|
9 |
|
10 |
+
1. FIRST ANALYZE the user's question:
|
11 |
+
- If information is missing or ambiguous, ask ONE clarifying question
|
12 |
+
- If clear, proceed to search
|
13 |
+
|
14 |
+
2. When searching:
|
15 |
+
- Generate multiple specific search queries wrapped in <search> tags
|
16 |
+
- Focus on factual keywords, one query per line
|
17 |
+
Example:
|
18 |
+
<search>
|
19 |
+
Pont des Arts exact length meters
|
20 |
+
History of Pont des Arts bridge
|
21 |
+
</search>
|
22 |
+
|
23 |
+
3. After receiving results:
|
24 |
+
- Analyze information from multiple sources
|
25 |
+
- Cross-verify facts
|
26 |
+
- If needed, generate follow-up searches
|
27 |
+
- Provide final answer with:
|
28 |
+
- Clear structure
|
29 |
+
- Key facts with sources
|
30 |
+
- Concise explanations
|
31 |
+
|
32 |
+
Never invent information. Cite sources for all facts. Use neutral, academic tone."""
|
33 |
+
|
34 |
+
def process_searches(response):
|
35 |
+
searches = re.findall(r'<search>(.*?)</search>', response, re.DOTALL)
|
36 |
+
if searches:
|
37 |
+
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
|
38 |
+
results = []
|
39 |
+
for query in queries:
|
40 |
+
search_result = web_search(query)
|
41 |
+
results.append(f"π Search results for '{query}':\n{search_result}\n")
|
42 |
+
return '\n'.join(results)
|
43 |
+
return None
|
44 |
+
|
45 |
+
def respond(
|
46 |
+
message,
|
47 |
+
history: list[tuple[str, str]],
|
48 |
+
system_message,
|
49 |
+
max_tokens,
|
50 |
+
temperature,
|
51 |
+
top_p,
|
52 |
+
hf_token,
|
53 |
+
):
|
54 |
+
client = InferenceClient(
|
55 |
+
provider="hf-inference",
|
56 |
+
api_key=hf_token
|
57 |
+
)
|
58 |
+
|
59 |
+
messages = [{"role": "system", "content": system_message}]
|
60 |
+
|
61 |
+
for val in history:
|
62 |
+
if val[0]:
|
63 |
+
messages.append({"role": "user", "content": val[0]})
|
64 |
+
if val[1]:
|
65 |
+
messages.append({"role": "assistant", "content": val[1]})
|
66 |
+
|
67 |
+
messages.append({"role": "user", "content": message})
|
68 |
+
|
69 |
+
full_response = ""
|
70 |
+
search_cycle = True
|
71 |
+
|
72 |
+
while search_cycle:
|
73 |
+
search_cycle = False
|
74 |
+
|
75 |
+
completion = client.chat.completions.create(
|
76 |
+
model="Qwen/QwQ-32B",
|
77 |
+
messages=messages,
|
78 |
+
max_tokens=10000,
|
79 |
+
temperature=temperature,
|
80 |
+
top_p=top_p,
|
81 |
+
stream=True
|
82 |
+
)
|
83 |
+
|
84 |
+
response = ""
|
85 |
+
for chunk in completion:
|
86 |
+
token = chunk.choices[0].delta.content or ""
|
87 |
+
response += token
|
88 |
+
full_response += token
|
89 |
+
yield full_response
|
90 |
+
|
91 |
+
search_results = process_searches(response)
|
92 |
+
|
93 |
+
if search_results:
|
94 |
+
search_cycle = True
|
95 |
+
messages.append({"role": "assistant", "content": response})
|
96 |
+
messages.append({
|
97 |
+
"role": "user",
|
98 |
+
"content": f"SEARCH RESULTS:\n{search_results}\nAnalyze these results and provide a well-structured answer with sources."
|
99 |
+
})
|
100 |
+
full_response += "\nπ Analyzing search results...\n"
|
101 |
+
yield full_response
|
102 |
+
|
103 |
+
demo = gr.ChatInterface(
|
104 |
+
respond,
|
105 |
+
additional_inputs=[
|
106 |
+
gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", lines=8),
|
107 |
+
gr.Slider(minimum=1000, maximum=15000, value=6000, step=500, label="Max Tokens"),
|
108 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature"),
|
109 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="Top-p"),
|
110 |
+
gr.Textbox(label="HF API Token", type="password")
|
111 |
+
],
|
112 |
+
title="Web Research Agent π€",
|
113 |
+
description="Advanced AI assistant with web search capabilities",
|
114 |
+
examples=[
|
115 |
+
["Compare COVID-19 mortality rates between US and Sweden with sources"],
|
116 |
+
["What's the current consensus on dark matter composition?"],
|
117 |
+
["Latest advancements in fusion energy 2023-2024"]
|
118 |
+
]
|
119 |
+
)
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
demo.launch()
|