Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ import tempfile
|
|
8 |
from threading import Thread
|
9 |
import base64
|
10 |
import shutil
|
|
|
11 |
|
12 |
import gradio as gr
|
13 |
import spaces
|
@@ -112,6 +113,80 @@ class Model:
|
|
112 |
export_to_ply(images[0], ply_path.name)
|
113 |
return self.to_glb(ply_path.name)
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
# -----------------------------------------------------------------------------
|
116 |
# Gradio UI configuration
|
117 |
# -----------------------------------------------------------------------------
|
@@ -297,7 +372,7 @@ def generate_3d_fn(
|
|
297 |
return glb_path, seed
|
298 |
|
299 |
# -----------------------------------------------------------------------------
|
300 |
-
# Chat Generation Function with support for @tts, @image, and @
|
301 |
# -----------------------------------------------------------------------------
|
302 |
|
303 |
@spaces.GPU
|
@@ -312,12 +387,13 @@ def generate(
|
|
312 |
):
|
313 |
"""
|
314 |
Generates chatbot responses with support for multimodal input, TTS, image generation,
|
315 |
-
|
316 |
-
|
317 |
Special commands:
|
318 |
- "@tts1" or "@tts2": triggers text-to-speech.
|
319 |
- "@image": triggers image generation using the SDXL pipeline.
|
320 |
- "@3d": triggers 3D model generation using the ShapE pipeline.
|
|
|
321 |
"""
|
322 |
text = input_dict["text"]
|
323 |
files = input_dict.get("files", [])
|
@@ -364,6 +440,25 @@ def generate(
|
|
364 |
yield gr.Image(image_paths[0])
|
365 |
return
|
366 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
# --- Text and TTS branch ---
|
368 |
tts_prefix = "@tts"
|
369 |
is_tts = any(text.strip().lower().startswith(f"{tts_prefix}{i}") for i in range(1, 3))
|
@@ -454,12 +549,12 @@ demo = gr.ChatInterface(
|
|
454 |
gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2),
|
455 |
],
|
456 |
examples=[
|
457 |
-
["@
|
458 |
["@3d A birthday cupcake with cherry"],
|
459 |
[{"text": "summarize the letter", "files": ["examples/1.png"]}],
|
460 |
["@image Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic"],
|
461 |
-
["Write a Python
|
462 |
-
["@
|
463 |
],
|
464 |
cache_examples=False,
|
465 |
type="messages",
|
|
|
8 |
from threading import Thread
|
9 |
import base64
|
10 |
import shutil
|
11 |
+
import re
|
12 |
|
13 |
import gradio as gr
|
14 |
import spaces
|
|
|
113 |
export_to_ply(images[0], ply_path.name)
|
114 |
return self.to_glb(ply_path.name)
|
115 |
|
116 |
+
# -----------------------------------------------------------------------------
|
117 |
+
# New Tools for Web Functionality using DuckDuckGo and smolagents
|
118 |
+
# -----------------------------------------------------------------------------
|
119 |
+
|
120 |
+
from typing import Any, Optional
|
121 |
+
from smolagents.tools import Tool
|
122 |
+
import duckduckgo_search
|
123 |
+
|
124 |
+
class DuckDuckGoSearchTool(Tool):
|
125 |
+
name = "web_search"
|
126 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
127 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
128 |
+
output_type = "string"
|
129 |
+
|
130 |
+
def __init__(self, max_results=10, **kwargs):
|
131 |
+
super().__init__()
|
132 |
+
self.max_results = max_results
|
133 |
+
try:
|
134 |
+
from duckduckgo_search import DDGS
|
135 |
+
except ImportError as e:
|
136 |
+
raise ImportError(
|
137 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
138 |
+
) from e
|
139 |
+
self.ddgs = DDGS(**kwargs)
|
140 |
+
|
141 |
+
def forward(self, query: str) -> str:
|
142 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
143 |
+
if len(results) == 0:
|
144 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
145 |
+
postprocessed_results = [
|
146 |
+
f"[{result['title']}]({result['href']})\n{result['body']}" for result in results
|
147 |
+
]
|
148 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
149 |
+
|
150 |
+
class VisitWebpageTool(Tool):
|
151 |
+
name = "visit_webpage"
|
152 |
+
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
153 |
+
inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
|
154 |
+
output_type = "string"
|
155 |
+
|
156 |
+
def __init__(self, *args, **kwargs):
|
157 |
+
self.is_initialized = False
|
158 |
+
|
159 |
+
def forward(self, url: str) -> str:
|
160 |
+
try:
|
161 |
+
import requests
|
162 |
+
from markdownify import markdownify
|
163 |
+
from requests.exceptions import RequestException
|
164 |
+
|
165 |
+
from smolagents.utils import truncate_content
|
166 |
+
except ImportError as e:
|
167 |
+
raise ImportError(
|
168 |
+
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
|
169 |
+
) from e
|
170 |
+
try:
|
171 |
+
# Send a GET request to the URL with a 20-second timeout
|
172 |
+
response = requests.get(url, timeout=20)
|
173 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
174 |
+
|
175 |
+
# Convert the HTML content to Markdown
|
176 |
+
markdown_content = markdownify(response.text).strip()
|
177 |
+
|
178 |
+
# Remove multiple line breaks
|
179 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
180 |
+
|
181 |
+
return truncate_content(markdown_content, 10000)
|
182 |
+
|
183 |
+
except requests.exceptions.Timeout:
|
184 |
+
return "The request timed out. Please try again later or check the URL."
|
185 |
+
except RequestException as e:
|
186 |
+
return f"Error fetching the webpage: {str(e)}"
|
187 |
+
except Exception as e:
|
188 |
+
return f"An unexpected error occurred: {str(e)}"
|
189 |
+
|
190 |
# -----------------------------------------------------------------------------
|
191 |
# Gradio UI configuration
|
192 |
# -----------------------------------------------------------------------------
|
|
|
372 |
return glb_path, seed
|
373 |
|
374 |
# -----------------------------------------------------------------------------
|
375 |
+
# Chat Generation Function with support for @tts, @image, @3d, and now @web commands
|
376 |
# -----------------------------------------------------------------------------
|
377 |
|
378 |
@spaces.GPU
|
|
|
387 |
):
|
388 |
"""
|
389 |
Generates chatbot responses with support for multimodal input, TTS, image generation,
|
390 |
+
3D model generation, and web search/visit.
|
391 |
+
|
392 |
Special commands:
|
393 |
- "@tts1" or "@tts2": triggers text-to-speech.
|
394 |
- "@image": triggers image generation using the SDXL pipeline.
|
395 |
- "@3d": triggers 3D model generation using the ShapE pipeline.
|
396 |
+
- "@web": triggers a web search or webpage visit. Use "visit" after @web to fetch a page.
|
397 |
"""
|
398 |
text = input_dict["text"]
|
399 |
files = input_dict.get("files", [])
|
|
|
440 |
yield gr.Image(image_paths[0])
|
441 |
return
|
442 |
|
443 |
+
# --- Web Search/Visit branch ---
|
444 |
+
if text.strip().lower().startswith("@web"):
|
445 |
+
web_command = text[len("@web"):].strip()
|
446 |
+
# If the command starts with "visit", then treat the rest as a URL
|
447 |
+
if web_command.lower().startswith("visit"):
|
448 |
+
url = web_command[len("visit"):].strip()
|
449 |
+
yield "Visiting webpage..."
|
450 |
+
visitor = VisitWebpageTool()
|
451 |
+
content = visitor.forward(url)
|
452 |
+
yield content
|
453 |
+
else:
|
454 |
+
# Otherwise, treat the rest as a search query.
|
455 |
+
query = web_command
|
456 |
+
yield "Searching the web..."
|
457 |
+
searcher = DuckDuckGoSearchTool()
|
458 |
+
results = searcher.forward(query)
|
459 |
+
yield results
|
460 |
+
return
|
461 |
+
|
462 |
# --- Text and TTS branch ---
|
463 |
tts_prefix = "@tts"
|
464 |
is_tts = any(text.strip().lower().startswith(f"{tts_prefix}{i}") for i in range(1, 3))
|
|
|
549 |
gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2),
|
550 |
],
|
551 |
examples=[
|
552 |
+
["@tts2 What causes rainbows to form?"],
|
553 |
["@3d A birthday cupcake with cherry"],
|
554 |
[{"text": "summarize the letter", "files": ["examples/1.png"]}],
|
555 |
["@image Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic"],
|
556 |
+
["Write a Python Code String Reverse With Example!"],
|
557 |
+
["@web latest breakthroughs in renewable energy"],
|
558 |
],
|
559 |
cache_examples=False,
|
560 |
type="messages",
|