xd11yggy commited on
Commit
e901cd6
Β·
verified Β·
1 Parent(s): f85b689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -37
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from smolagents import DuckDuckGoSearchTool
4
  import re
 
5
 
6
  web_search = DuckDuckGoSearchTool()
7
 
@@ -34,16 +35,22 @@ Never invent information. Cite sources for all facts. Use neutral, academic tone
34
  """
35
 
36
  def process_searches(response):
37
- # Preserve thinking tags while processing searches
38
  formatted_response = response.replace("<thinking>", "\nπŸ’­ THINKING PROCESS:\n").replace("</thinking>", "\n")
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
- results = []
43
- for query in queries:
44
- search_result = web_search(query)
45
- results.append(f"πŸ” SEARCH: {query}\nRESULTS: {search_result}\n")
46
- return '\n'.join(results)
 
 
 
 
 
 
 
47
  return None
48
 
49
  def respond(
@@ -73,38 +80,54 @@ def respond(
73
  full_response = ""
74
  search_cycle = True
75
 
76
- while search_cycle:
77
- search_cycle = False
78
-
79
- completion = client.chat.completions.create(
80
- model="Qwen/QwQ-32B",
81
- messages=messages,
82
- max_tokens=10000,
83
- temperature=temperature,
84
- top_p=top_p,
85
- stream=True
86
- )
87
-
88
- response = ""
89
- for chunk in completion:
90
- token = chunk.choices[0].delta.content or ""
91
- response += token
92
- full_response += token
93
- # Display thinking tags immediately
94
- if "<thinking>" in token.lower() or "</thinking>" in token.lower():
 
 
 
95
  yield full_response
96
-
97
- if search_results:
98
- search_cycle = True
99
- messages.append({"role": "assistant", "content": response})
100
- messages.append({
101
- "role": "user",
102
- "content": f"SEARCH RESULTS:\n{search_results}\nAnalyze these results..."
103
- })
104
- # Add this line to display queries
105
- full_response += f"\nπŸ” SEARCH QUERIES USED:\n{chr(10).join(queries)}\n\n" # ← NEW
106
- full_response += "\nπŸ” Analyzing search results...\n"
107
- yield full_response
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  demo = gr.ChatInterface(
110
  respond,
 
2
  from huggingface_hub import InferenceClient
3
  from smolagents import DuckDuckGoSearchTool
4
  import re
5
+ import time
6
 
7
  web_search = DuckDuckGoSearchTool()
8
 
 
35
  """
36
 
37
  def process_searches(response):
 
38
  formatted_response = response.replace("<thinking>", "\nπŸ’­ THINKING PROCESS:\n").replace("</thinking>", "\n")
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:
48
+ return web_search(query)
49
+ except Exception as e:
50
+ if attempt < max_retries - 1:
51
+ time.sleep(delay)
52
+ continue
53
+ raise
54
  return None
55
 
56
  def respond(
 
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",
90
+ messages=messages,
91
+ max_tokens=max_tokens,
92
+ temperature=temperature,
93
+ top_p=top_p,
94
+ stream=True
95
+ )
96
+ except Exception as e:
97
+ yield f"⚠️ API Error: {str(e)}\n\nPlease check your HF token and model access."
98
+ return
99
+
100
+ response = ""
101
+ for chunk in completion:
102
+ token = chunk.choices[0].delta.content or ""
103
+ response += token
104
+ full_response += token
105
  yield full_response
106
+
107
+ queries = process_searches(response)
108
+
109
+ if queries:
110
+ search_cycle = True
111
+ messages.append({"role": "assistant", "content": response})
112
+
113
+ search_results = []
114
+ for query in queries:
115
+ try:
116
+ result = search_with_retry(query)
117
+ search_results.append(f"πŸ” SEARCH: {query}\nRESULTS: {result}\n")
118
+ except Exception as e:
119
+ search_results.append(f"⚠️ Search Error: {str(e)}\nQuery: {query}")
120
+ time.sleep(2)
121
+
122
+ messages.append({
123
+ "role": "user",
124
+ "content": f"SEARCH RESULTS:\n{chr(10).join(search_results)}\nAnalyze these results..."
125
+ })
126
+ full_response += "\nπŸ” Analyzing search results...\n"
127
+ yield full_response
128
+
129
+ except Exception as e:
130
+ yield f"⚠️ Critical Error: {str(e)}\n\nPlease try again later."
131
 
132
  demo = gr.ChatInterface(
133
  respond,