daqc commited on
Commit
e728c21
·
verified ·
1 Parent(s): 3cb7acd

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -491
app.py DELETED
@@ -1,491 +0,0 @@
1
- import json
2
- import mimetypes
3
- import os
4
- import re
5
- import shutil
6
- import threading
7
- from typing import Optional
8
- from loguru import logger
9
-
10
- import gradio as gr
11
- from dotenv import load_dotenv
12
- # from huggingface_hub import login
13
- from smolagents import (
14
- CodeAgent,
15
- # HfApiModel,
16
- # LiteLLMModel,
17
- OpenAIServerModel,
18
- Tool,
19
- # GoogleSearchTool,
20
- DuckDuckGoSearchTool,
21
- )
22
- from smolagents.agent_types import (
23
- AgentAudio,
24
- AgentImage,
25
- AgentText,
26
- handle_agent_output_types,
27
- )
28
- from smolagents.gradio_ui import stream_to_gradio
29
-
30
- from scripts.text_inspector_tool import TextInspectorTool
31
- from scripts.text_web_browser import (
32
- ArchiveSearchTool,
33
- FinderTool,
34
- FindNextTool,
35
- PageDownTool,
36
- PageUpTool,
37
- SimpleTextBrowser,
38
- VisitTool,
39
- )
40
- from scripts.visual_qa import visualizer
41
-
42
-
43
- # web_search = GoogleSearchTool(provider="serper")
44
- web_search = DuckDuckGoSearchTool()
45
-
46
- # print(web_search(query="Donald Trump news"))
47
- # TODO fix ValueError: {'message': 'Unauthorized.', 'statusCode': 403}
48
-
49
- # quit()
50
- AUTHORIZED_IMPORTS = [
51
- "requests",
52
- "zipfile",
53
- "pandas",
54
- "numpy",
55
- "sympy",
56
- "json",
57
- "bs4",
58
- "pubchempy",
59
- "xml",
60
- "yahoo_finance",
61
- "Bio",
62
- "sklearn",
63
- "scipy",
64
- "pydub",
65
- "PIL",
66
- "chess",
67
- "PyPDF2",
68
- "pptx",
69
- "torch",
70
- "datetime",
71
- "fractions",
72
- "csv",
73
- ]
74
- load_dotenv(override=True)
75
- # login(os.getenv("HF_TOKEN")) # this is not necessary if env var HF_TOKEN is set
76
-
77
- append_answer_lock = threading.Lock()
78
-
79
- custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
80
-
81
- user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
82
-
83
- BROWSER_CONFIG = {
84
- "viewport_size": 1024 * 5,
85
- "downloads_folder": "downloads_folder",
86
- "request_kwargs": {
87
- "headers": {"User-Agent": user_agent},
88
- "timeout": 300,
89
- },
90
- "serpapi_key": os.getenv("SERPAPI_API_KEY"),
91
- }
92
-
93
- os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
94
-
95
- model_id = os.getenv("MODEL_ID", "deepseek-ai/DeepSeek-V3")
96
- _ = "" if os.getenv("OPENAI_API_KEY") is None else os.getenv("OPENAI_API_KEY")[:8] + "..."
97
-
98
- if os.getenv("MODEL_ID") and os.getenv("OPENAI_API_BASE"):
99
- logger.debug(f"""using OpenAIServerModel: {model_id=}, {os.getenv("OPENAI_API_BASE")=}, os.getenv("OPENAI_API_BASE")={_}""")
100
- # model = LiteLLMModel(
101
- model = OpenAIServerModel(
102
- # "gpt-4o",
103
- # os.getenv("MODEL_ID", "gpt-4o-mini"),
104
- model_id,
105
- custom_role_conversions=custom_role_conversions,
106
- api_base=os.getenv("OPENAI_API_BASE"),
107
- api_key=os.getenv("OPENAI_API_KEY"),
108
- )
109
- else:
110
- logger.debug(f"""using LiteLLMModel: HfApiModel default model_id=Qwen/Qwen2.5-Coder-32B-Instruct""")
111
- model = HfApiModel(
112
- custom_role_conversions=custom_role_conversions,
113
- )
114
-
115
- text_limit = 20000
116
- ti_tool = TextInspectorTool(model, text_limit)
117
-
118
- browser = SimpleTextBrowser(**BROWSER_CONFIG)
119
-
120
- WEB_TOOLS = [
121
- web_search, # duckduckgo
122
- VisitTool(browser),
123
- PageUpTool(browser),
124
- PageDownTool(browser),
125
- FinderTool(browser),
126
- FindNextTool(browser),
127
- ArchiveSearchTool(browser),
128
- TextInspectorTool(model, text_limit),
129
- ]
130
-
131
-
132
- # Agent creation in a factory function
133
- def create_agent():
134
- """Creates a fresh agent instance for each session"""
135
- return CodeAgent(
136
- model=model,
137
- tools=[visualizer] + WEB_TOOLS,
138
- max_steps=10,
139
- verbosity_level=1,
140
- additional_authorized_imports=AUTHORIZED_IMPORTS,
141
- planning_interval=4,
142
- )
143
-
144
-
145
- document_inspection_tool = TextInspectorTool(model, 20000)
146
-
147
-
148
- class GradioUI:
149
- """A one-line interface to launch your agent in Gradio"""
150
-
151
- def __init__(self, file_upload_folder: str | None = None):
152
- self.file_upload_folder = file_upload_folder
153
- if self.file_upload_folder is not None:
154
- if not os.path.exists(file_upload_folder):
155
- os.mkdir(file_upload_folder)
156
-
157
- def interact_with_agent(self, prompt, messages, session_state):
158
- # Get or create session-specific agent
159
- if "agent" not in session_state:
160
- session_state["agent"] = create_agent()
161
-
162
- # Adding monitoring
163
- try:
164
- # log the existence of agent memory
165
- has_memory = hasattr(session_state["agent"], "memory")
166
- print(f"Agent has memory: {has_memory}")
167
- if has_memory:
168
- print(f"Memory type: {type(session_state['agent'].memory)}")
169
-
170
- messages.append(gr.ChatMessage(role="user", content=prompt))
171
- yield messages
172
-
173
- for msg in stream_to_gradio(
174
- session_state["agent"], task=prompt, reset_agent_memory=False
175
- ):
176
- messages.append(msg)
177
- yield messages
178
- yield messages
179
- except Exception as e:
180
- print(f"Error in interaction: {str(e)}")
181
- raise
182
-
183
- def upload_file(
184
- self,
185
- file,
186
- file_uploads_log,
187
- allowed_file_types=[
188
- "application/pdf",
189
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
190
- "text/plain",
191
- ],
192
- ):
193
- """
194
- Handle file uploads, default allowed types are .pdf, .docx, and .txt
195
- """
196
- if file is None:
197
- return gr.Textbox("No file uploaded", visible=True), file_uploads_log
198
-
199
- try:
200
- mime_type, _ = mimetypes.guess_type(file.name)
201
- except Exception as e:
202
- return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
203
-
204
- if mime_type not in allowed_file_types:
205
- return gr.Textbox("File type disallowed", visible=True), file_uploads_log
206
-
207
- # Sanitize file name
208
- original_name = os.path.basename(file.name)
209
- sanitized_name = re.sub(
210
- r"[^\w\-.]", "_", original_name
211
- ) # Replace any non-alphanumeric, non-dash, or non-dot characters with underscores
212
-
213
- type_to_ext = {}
214
- for ext, t in mimetypes.types_map.items():
215
- if t not in type_to_ext:
216
- type_to_ext[t] = ext
217
-
218
- # Ensure the extension correlates to the mime type
219
- sanitized_name = sanitized_name.split(".")[:-1]
220
- sanitized_name.append("" + type_to_ext[mime_type])
221
- sanitized_name = "".join(sanitized_name)
222
-
223
- # Save the uploaded file to the specified folder
224
- file_path = os.path.join(
225
- self.file_upload_folder, os.path.basename(sanitized_name)
226
- )
227
- shutil.copy(file.name, file_path)
228
-
229
- return gr.Textbox(
230
- f"File uploaded: {file_path}", visible=True
231
- ), file_uploads_log + [file_path]
232
-
233
- def log_user_message(self, text_input, file_uploads_log):
234
- return (
235
- text_input
236
- + (
237
- f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
238
- if len(file_uploads_log) > 0
239
- else ""
240
- ),
241
- gr.Textbox(
242
- value="",
243
- interactive=False,
244
- placeholder="Please wait while Steps are getting populated",
245
- ),
246
- gr.Button(interactive=False),
247
- )
248
-
249
- def detect_device(self, request: gr.Request):
250
- # Check whether the user device is a mobile or a computer
251
-
252
- if not request:
253
- return "Unknown device"
254
- # Method 1: Check sec-ch-ua-mobile header
255
- is_mobile_header = request.headers.get("sec-ch-ua-mobile")
256
- if is_mobile_header:
257
- return "Mobile" if "?1" in is_mobile_header else "Desktop"
258
-
259
- # Method 2: Check user-agent string
260
- user_agent = request.headers.get("user-agent", "").lower()
261
- mobile_keywords = ["android", "iphone", "ipad", "mobile", "phone"]
262
-
263
- if any(keyword in user_agent for keyword in mobile_keywords):
264
- return "Mobile"
265
-
266
- # Method 3: Check platform
267
- platform = request.headers.get("sec-ch-ua-platform", "").lower()
268
- if platform:
269
- if platform in ['"android"', '"ios"']:
270
- return "Mobile"
271
- elif platform in ['"windows"', '"macos"', '"linux"']:
272
- return "Desktop"
273
-
274
- # Default case if no clear indicators
275
- return "Desktop"
276
-
277
- def launch(self, **kwargs):
278
- with gr.Blocks(theme="ocean", fill_height=True) as demo:
279
- # Different layouts for mobile and computer devices
280
- @gr.render()
281
- def layout(request: gr.Request):
282
- device = self.detect_device(request)
283
- print(f"device - {device}")
284
- # Render layout with sidebar
285
- if device == "Desktop":
286
- with gr.Blocks(
287
- fill_height=True,
288
- ):
289
- file_uploads_log = gr.State([])
290
- with gr.Sidebar():
291
- with gr.Group():
292
- gr.Markdown("**Your request**", container=True)
293
- text_input = gr.Textbox(
294
- lines=3,
295
- label="Your request",
296
- container=False,
297
- placeholder="Enter your prompt here and press Shift+Enter or press the button",
298
- )
299
- launch_research_btn = gr.Button(
300
- "Run", variant="primary"
301
- )
302
-
303
- # If an upload folder is provided, enable the upload feature
304
- if self.file_upload_folder is not None:
305
- upload_file = gr.File(label="Upload a file")
306
- upload_status = gr.Textbox(
307
- label="Upload Status",
308
- interactive=False,
309
- visible=False,
310
- )
311
- upload_file.change(
312
- self.upload_file,
313
- [upload_file, file_uploads_log],
314
- [upload_status, file_uploads_log],
315
- )
316
-
317
- # gr.HTML("<h4><center>Powered by huggingface/smolagents</center></h4>")
318
- # gr.Markdown("Powered by [huggingface/smolagents](https://github.com/huggingface/smolagents)")
319
- # _ = '''
320
- with gr.Row():
321
- gr.HTML("""<div style="display: flex; align-items: center; gap: 8px; font-family: system-ui, -apple-system, sans-serif;">Powered by
322
- <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png" style="width: 32px; height: 32px; object-fit: contain;" alt="logo">
323
- <a target="_blank" href="https://github.com/huggingface/smolagents"><b>hf/smolagents</b></a>
324
- </div>""")
325
- # '''
326
- # -----
327
- with gr.Accordion("🎈 Info", open=False):
328
- gr.Markdown("""### open Deep Research - free the AI agents!
329
-
330
- OpenAI just (February 2, 2025) published [Deep Research](https://openai.com/index/introducing-deep-research/), an amazing assistant that can perform deep searches on the web to answer user questions.
331
-
332
- However, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our (Huggingface's) resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! ✨
333
-
334
- You can try a simplified version here that uses `Qwen-Coder-32B` (via smolagnet.HfApiModel) instead of `o1`. Modified: if you set MODEL_ID, OPENAI_API_BASE and OPENAI_API_KEY in the .env or env vars (in hf space these can be set in settings, .env will override env vars), the correspoding model will be used. N.B. if you see errors, it might be because whatever quota is exceeded, clone/duplicate this space and plug in your own resources and run your own deep-research.<br><br>""")
335
-
336
- # Add session state to store session-specific data
337
- session_state = gr.State(
338
- {}
339
- ) # Initialize empty state for each session
340
- stored_messages = gr.State([])
341
- chatbot = gr.Chatbot(
342
- label="open-Deep-Research",
343
- type="messages",
344
- avatar_images=(
345
- None,
346
- "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
347
- ),
348
- resizeable=False,
349
- scale=1,
350
- elem_id="my-chatbot",
351
- )
352
-
353
- text_input.submit(
354
- self.log_user_message,
355
- [text_input, file_uploads_log],
356
- [stored_messages, text_input, launch_research_btn],
357
- ).then(
358
- self.interact_with_agent,
359
- # Include session_state in function calls
360
- [stored_messages, chatbot, session_state],
361
- [chatbot],
362
- ).then(
363
- lambda: (
364
- gr.Textbox(
365
- interactive=True,
366
- placeholder="Enter your prompt here and press the button",
367
- ),
368
- gr.Button(interactive=True),
369
- ),
370
- None,
371
- [text_input, launch_research_btn],
372
- )
373
- launch_research_btn.click(
374
- self.log_user_message,
375
- [text_input, file_uploads_log],
376
- [stored_messages, text_input, launch_research_btn],
377
- ).then(
378
- self.interact_with_agent,
379
- # Include session_state in function calls
380
- [stored_messages, chatbot, session_state],
381
- [chatbot],
382
- ).then(
383
- lambda: (
384
- gr.Textbox(
385
- interactive=True,
386
- placeholder="Enter your prompt here and press the button",
387
- ),
388
- gr.Button(interactive=True),
389
- ),
390
- None,
391
- [text_input, launch_research_btn],
392
- )
393
-
394
- # Render simple layout
395
- else:
396
- with gr.Blocks(
397
- fill_height=True,
398
- ):
399
- gr.Markdown("""# open Deep Research - free the AI agents!
400
- _Built with [smolagents](https://github.com/huggingface/smolagents)_
401
-
402
- OpenAI just published [Deep Research](https://openai.com/index/introducing-deep-research/), a very nice assistant that can perform deep searches on the web to answer user questions.
403
-
404
- However, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! ✨
405
-
406
- You can try a simplified version below (uses `Qwen-Coder-32B` instead of `o1`, so much less powerful than the original open-Deep-Research)👇""")
407
- # Add session state to store session-specific data
408
- session_state = gr.State(
409
- {}
410
- ) # Initialize empty state for each session
411
- stored_messages = gr.State([])
412
- file_uploads_log = gr.State([])
413
- chatbot = gr.Chatbot(
414
- label="open-Deep-Research",
415
- type="messages",
416
- avatar_images=(
417
- None,
418
- "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
419
- ),
420
- resizeable=True,
421
- scale=1,
422
- )
423
- # If an upload folder is provided, enable the upload feature
424
- if self.file_upload_folder is not None:
425
- upload_file = gr.File(label="Upload a file")
426
- upload_status = gr.Textbox(
427
- label="Upload Status", interactive=False, visible=False
428
- )
429
- upload_file.change(
430
- self.upload_file,
431
- [upload_file, file_uploads_log],
432
- [upload_status, file_uploads_log],
433
- )
434
- text_input = gr.Textbox(
435
- lines=1,
436
- label="Your request",
437
- placeholder="Enter your prompt here and press the button",
438
- )
439
- launch_research_btn = gr.Button(
440
- "Run",
441
- variant="primary",
442
- )
443
-
444
- text_input.submit(
445
- self.log_user_message,
446
- [text_input, file_uploads_log],
447
- [stored_messages, text_input, launch_research_btn],
448
- ).then(
449
- self.interact_with_agent,
450
- # Include session_state in function calls
451
- [stored_messages, chatbot, session_state],
452
- [chatbot],
453
- ).then(
454
- lambda: (
455
- gr.Textbox(
456
- interactive=True,
457
- placeholder="Enter your prompt here and press the button",
458
- ),
459
- gr.Button(interactive=True),
460
- ),
461
- None,
462
- [text_input, launch_research_btn],
463
- )
464
- launch_research_btn.click(
465
- self.log_user_message,
466
- [text_input, file_uploads_log],
467
- [stored_messages, text_input, launch_research_btn],
468
- ).then(
469
- self.interact_with_agent,
470
- # Include session_state in function calls
471
- [stored_messages, chatbot, session_state],
472
- [chatbot],
473
- ).then(
474
- lambda: (
475
- gr.Textbox(
476
- interactive=True,
477
- placeholder="Enter your prompt here and press the button",
478
- ),
479
- gr.Button(interactive=True),
480
- ),
481
- None,
482
- [text_input, launch_research_btn],
483
- )
484
-
485
- demo.launch(debug=True, **kwargs)
486
-
487
- # can this fix ctrl-c no response? no
488
- try:
489
- GradioUI().launch()
490
- except KeyboardInterrupt:
491
- ...