Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from duckduckgo_search import DDGS
|
|
4 |
import re
|
5 |
|
6 |
client = InferenceClient("Pinkstack/Superthoughts-lite-v1")
|
7 |
-
|
8 |
def format_search_results(query, results, result_type):
|
9 |
formatted = f"{result_type} search results for '{query}':\n"
|
10 |
for i, result in enumerate(results):
|
@@ -22,30 +21,6 @@ def extract_key_phrases(message):
|
|
22 |
phrases.append(f"{words[i]} {words[i+1]}")
|
23 |
return phrases[:3]
|
24 |
|
25 |
-
def collect_sources(search_option, message):
|
26 |
-
sources = []
|
27 |
-
if search_option != "No search":
|
28 |
-
with DDGS() as ddgs:
|
29 |
-
if search_option == "Normal search":
|
30 |
-
web_results = ddgs.text(message, max_results=3)
|
31 |
-
sources.extend([(r.get('title', 'No title'), r.get('href', '') or r.get('url', '')) for r in web_results])
|
32 |
-
elif search_option == "Deep research":
|
33 |
-
queries = extract_key_phrases(message)
|
34 |
-
for query in queries:
|
35 |
-
web_results = ddgs.text(query, max_results=3)
|
36 |
-
news_results = ddgs.news(query, max_results=2)
|
37 |
-
sources.extend([(r.get('title', 'No title'), r.get('href', '') or r.get('url', '')) for r in web_results])
|
38 |
-
sources.extend([(r.get('title', 'No title'), r.get('url', '')) for r in news_results])
|
39 |
-
return sources
|
40 |
-
|
41 |
-
def format_sources(sources):
|
42 |
-
if not sources:
|
43 |
-
return ""
|
44 |
-
formatted = "\n\n**Sources:**\n"
|
45 |
-
for i, (title, url) in enumerate(sources, 1):
|
46 |
-
formatted += f"{i}. [{title}]({url})\n"
|
47 |
-
return formatted
|
48 |
-
|
49 |
def respond(
|
50 |
message,
|
51 |
history: list[tuple[str, str]],
|
@@ -56,8 +31,7 @@ def respond(
|
|
56 |
search_option,
|
57 |
):
|
58 |
search_text = ""
|
59 |
-
|
60 |
-
if sources:
|
61 |
with DDGS() as ddgs:
|
62 |
if search_option == "Normal search":
|
63 |
web_results = ddgs.text(message, max_results=3)
|
@@ -93,12 +67,9 @@ def respond(
|
|
93 |
if token:
|
94 |
response += token
|
95 |
|
96 |
-
# Process response to convert tags to HTML divs
|
97 |
formatted_response = response
|
98 |
formatted_response = formatted_response.replace("<think>", '<div class="thinking">').replace("</think>", "</div>")
|
99 |
-
if search_option != "No search":
|
100 |
-
# Append sources to the output section
|
101 |
-
formatted_response = formatted_response.replace("</output>", f"{format_sources(sources)}</output>")
|
102 |
formatted_response = formatted_response.replace("<output>", '<div class="output">').replace("</output>", "</div>")
|
103 |
|
104 |
yield formatted_response
|
@@ -140,20 +111,26 @@ css = """
|
|
140 |
|
141 |
system_message_default = """
|
142 |
Respond in the following format:
|
|
|
143 |
<think>
|
144 |
-
...
|
145 |
</think>
|
|
|
146 |
<output>
|
147 |
-
...
|
|
|
|
|
|
|
|
|
148 |
</output>
|
149 |
"""
|
150 |
|
151 |
demo = gr.ChatInterface(
|
152 |
respond,
|
153 |
additional_inputs=[
|
154 |
-
gr.Textbox(value=system_message_default, label="System message", lines=
|
155 |
-
gr.Slider(minimum=1, maximum=
|
156 |
-
gr.Slider(minimum=0.1, maximum=
|
157 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
158 |
gr.Dropdown(
|
159 |
choices=["No search", "Normal search", "Deep research"],
|
|
|
4 |
import re
|
5 |
|
6 |
client = InferenceClient("Pinkstack/Superthoughts-lite-v1")
|
|
|
7 |
def format_search_results(query, results, result_type):
|
8 |
formatted = f"{result_type} search results for '{query}':\n"
|
9 |
for i, result in enumerate(results):
|
|
|
21 |
phrases.append(f"{words[i]} {words[i+1]}")
|
22 |
return phrases[:3]
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def respond(
|
25 |
message,
|
26 |
history: list[tuple[str, str]],
|
|
|
31 |
search_option,
|
32 |
):
|
33 |
search_text = ""
|
34 |
+
if search_option != "No search":
|
|
|
35 |
with DDGS() as ddgs:
|
36 |
if search_option == "Normal search":
|
37 |
web_results = ddgs.text(message, max_results=3)
|
|
|
67 |
if token:
|
68 |
response += token
|
69 |
|
70 |
+
# Process response to convert tags to HTML divs
|
71 |
formatted_response = response
|
72 |
formatted_response = formatted_response.replace("<think>", '<div class="thinking">').replace("</think>", "</div>")
|
|
|
|
|
|
|
73 |
formatted_response = formatted_response.replace("<output>", '<div class="output">').replace("</output>", "</div>")
|
74 |
|
75 |
yield formatted_response
|
|
|
111 |
|
112 |
system_message_default = """
|
113 |
Respond in the following format:
|
114 |
+
|
115 |
<think>
|
116 |
+
Your thought process here...
|
117 |
</think>
|
118 |
+
|
119 |
<output>
|
120 |
+
Your final answer here...
|
121 |
+
|
122 |
+
If search results are provided, cite relevant sources at the end of this section as a numbered list in the format:
|
123 |
+
1. [Title](URL)
|
124 |
+
2. [Title](URL)
|
125 |
</output>
|
126 |
"""
|
127 |
|
128 |
demo = gr.ChatInterface(
|
129 |
respond,
|
130 |
additional_inputs=[
|
131 |
+
gr.Textbox(value=system_message_default, label="System message", lines=8),
|
132 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
133 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
134 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
135 |
gr.Dropdown(
|
136 |
choices=["No search", "Normal search", "Deep research"],
|