Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -35,13 +35,29 @@ Never invent information. Cite sources for all facts. Use neutral, academic tone
|
|
35 |
"""
|
36 |
|
37 |
def process_searches(response):
|
38 |
-
|
39 |
-
searches = re.findall(r'<search>(.*?)</search>', formatted_response, re.DOTALL)
|
40 |
if searches:
|
41 |
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
|
42 |
return queries
|
43 |
return None
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def search_with_retry(query, max_retries=3, delay=2):
|
46 |
for attempt in range(max_retries):
|
47 |
try:
|
@@ -53,6 +69,14 @@ def search_with_retry(query, max_retries=3, delay=2):
|
|
53 |
raise
|
54 |
return None
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def respond(
|
57 |
message,
|
58 |
history: list[tuple[str, str]],
|
@@ -79,12 +103,16 @@ def respond(
|
|
79 |
|
80 |
full_response = ""
|
81 |
search_cycle = True
|
|
|
82 |
|
83 |
try:
|
84 |
while search_cycle:
|
85 |
search_cycle = False
|
|
|
|
|
86 |
|
87 |
try:
|
|
|
88 |
completion = client.chat.completions.create(
|
89 |
model="qwen/qwq-32b:free",
|
90 |
messages=messages,
|
@@ -102,10 +130,42 @@ def respond(
|
|
102 |
return
|
103 |
|
104 |
response = ""
|
|
|
|
|
|
|
105 |
for chunk in completion:
|
106 |
token = chunk.choices[0].delta.content or ""
|
107 |
response += token
|
108 |
full_response += token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
yield full_response
|
110 |
|
111 |
queries = process_searches(response)
|
|
|
35 |
"""
|
36 |
|
37 |
def process_searches(response):
|
38 |
+
searches = re.findall(r'<search>(.*?)</search>', response, re.DOTALL)
|
|
|
39 |
if searches:
|
40 |
queries = [q.strip() for q in searches[0].split('\n') if q.strip()]
|
41 |
return queries
|
42 |
return None
|
43 |
|
44 |
+
def process_thinking(response, duration):
|
45 |
+
thinking_blocks = re.findall(r'<thinking>(.*?)</thinking>', response, re.DOTALL)
|
46 |
+
if not thinking_blocks:
|
47 |
+
return response, False
|
48 |
+
|
49 |
+
formatted_response = response
|
50 |
+
for content in thinking_blocks:
|
51 |
+
formatted_think = (
|
52 |
+
f"\nπ THINKING PROCESS:\n{content.strip()}\n"
|
53 |
+
f"Thought for {duration:.1f} seconds.\n"
|
54 |
+
)
|
55 |
+
formatted_response = formatted_response.replace(
|
56 |
+
f'<thinking>{content}</thinking>',
|
57 |
+
formatted_think
|
58 |
+
)
|
59 |
+
return formatted_response, True
|
60 |
+
|
61 |
def search_with_retry(query, max_retries=3, delay=2):
|
62 |
for attempt in range(max_retries):
|
63 |
try:
|
|
|
69 |
raise
|
70 |
return None
|
71 |
|
72 |
+
def animate_thinking():
|
73 |
+
dots = ["", ".", "..", "..."]
|
74 |
+
i = 0
|
75 |
+
while True:
|
76 |
+
yield f"Thinking{dots[i]}"
|
77 |
+
i = (i + 1) % 4
|
78 |
+
time.sleep(0.5)
|
79 |
+
|
80 |
def respond(
|
81 |
message,
|
82 |
history: list[tuple[str, str]],
|
|
|
103 |
|
104 |
full_response = ""
|
105 |
search_cycle = True
|
106 |
+
thinking_animation = animate_thinking()
|
107 |
|
108 |
try:
|
109 |
while search_cycle:
|
110 |
search_cycle = False
|
111 |
+
show_thinking = False
|
112 |
+
thinking_start = None
|
113 |
|
114 |
try:
|
115 |
+
start_time = time.time()
|
116 |
completion = client.chat.completions.create(
|
117 |
model="qwen/qwq-32b:free",
|
118 |
messages=messages,
|
|
|
130 |
return
|
131 |
|
132 |
response = ""
|
133 |
+
thinking_buffer = ""
|
134 |
+
in_thinking_tag = False
|
135 |
+
|
136 |
for chunk in completion:
|
137 |
token = chunk.choices[0].delta.content or ""
|
138 |
response += token
|
139 |
full_response += token
|
140 |
+
|
141 |
+
# Detect thinking tags
|
142 |
+
if not in_thinking_tag and '<thinking>' in token:
|
143 |
+
in_thinking_tag = True
|
144 |
+
thinking_start = time.time()
|
145 |
+
show_thinking = True
|
146 |
+
|
147 |
+
if in_thinking_tag:
|
148 |
+
thinking_buffer += token
|
149 |
+
if '</thinking>' in token:
|
150 |
+
in_thinking_tag = False
|
151 |
+
thinking_duration = time.time() - thinking_start
|
152 |
+
|
153 |
+
# Show animated thinking if needed
|
154 |
+
if show_thinking and not in_thinking_tag:
|
155 |
+
formatted, has_thinking = process_thinking(full_response, time.time() - start_time)
|
156 |
+
if has_thinking:
|
157 |
+
full_response = formatted
|
158 |
+
show_thinking = False
|
159 |
+
yield full_response
|
160 |
+
else:
|
161 |
+
yield next(thinking_animation)
|
162 |
+
else:
|
163 |
+
yield full_response
|
164 |
+
|
165 |
+
# Process final thinking state
|
166 |
+
final_response, has_thinking = process_thinking(full_response, time.time() - start_time)
|
167 |
+
if has_thinking:
|
168 |
+
full_response = final_response
|
169 |
yield full_response
|
170 |
|
171 |
queries = process_searches(response)
|