Spaces:
Sleeping
Sleeping
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
|
|
@@ -16,6 +17,7 @@ import numpy as np
|
|
| 16 |
from PIL import Image
|
| 17 |
import edge_tts
|
| 18 |
import trimesh
|
|
|
|
| 19 |
|
| 20 |
from transformers import (
|
| 21 |
AutoModelForCausalLM,
|
|
@@ -276,28 +278,73 @@ def generate_image_fn(
|
|
| 276 |
return image_paths, seed
|
| 277 |
|
| 278 |
# -----------------------------------------------------------------------------
|
| 279 |
-
#
|
| 280 |
# -----------------------------------------------------------------------------
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
""
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
# -----------------------------------------------------------------------------
|
| 300 |
-
# Chat Generation Function with support for @tts, @image, and @
|
| 301 |
# -----------------------------------------------------------------------------
|
| 302 |
|
| 303 |
@spaces.GPU
|
|
@@ -312,12 +359,14 @@ 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 +413,26 @@ 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))
|
|
@@ -460,6 +529,8 @@ demo = gr.ChatInterface(
|
|
| 460 |
["@image Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic"],
|
| 461 |
["Write a Python function to check if a number is prime."],
|
| 462 |
["@tts2 What causes rainbows to form?"],
|
|
|
|
|
|
|
| 463 |
],
|
| 464 |
cache_examples=False,
|
| 465 |
type="messages",
|
|
|
|
| 8 |
from threading import Thread
|
| 9 |
import base64
|
| 10 |
import shutil
|
| 11 |
+
import re # Added for the new tools
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
import spaces
|
|
|
|
| 17 |
from PIL import Image
|
| 18 |
import edge_tts
|
| 19 |
import trimesh
|
| 20 |
+
import smolagents # For the new tools
|
| 21 |
|
| 22 |
from transformers import (
|
| 23 |
AutoModelForCausalLM,
|
|
|
|
| 278 |
return image_paths, seed
|
| 279 |
|
| 280 |
# -----------------------------------------------------------------------------
|
| 281 |
+
# Tools for Web Search and Webpage Visiting using DuckDuckGo and smolagents
|
| 282 |
# -----------------------------------------------------------------------------
|
| 283 |
|
| 284 |
+
from typing import Any, Optional
|
| 285 |
+
from smolagents.tools import Tool
|
| 286 |
+
import requests
|
| 287 |
+
import markdownify
|
| 288 |
+
import duckduckgo_search
|
| 289 |
+
|
| 290 |
+
class VisitWebpageTool(Tool):
|
| 291 |
+
name = "visit_webpage"
|
| 292 |
+
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 293 |
+
inputs = {'url': {'type': 'string', 'description': 'The url of the webpage to visit.'}}
|
| 294 |
+
output_type = "string"
|
| 295 |
+
|
| 296 |
+
def forward(self, url: str) -> str:
|
| 297 |
+
try:
|
| 298 |
+
from markdownify import markdownify
|
| 299 |
+
from requests.exceptions import RequestException
|
| 300 |
+
from smolagents.utils import truncate_content
|
| 301 |
+
except ImportError as e:
|
| 302 |
+
raise ImportError(
|
| 303 |
+
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
|
| 304 |
+
) from e
|
| 305 |
+
try:
|
| 306 |
+
response = requests.get(url, timeout=20)
|
| 307 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 308 |
+
# Convert the HTML content to Markdown
|
| 309 |
+
markdown_content = markdownify.markdownify(response.text).strip()
|
| 310 |
+
# Remove multiple line breaks
|
| 311 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 312 |
+
return truncate_content(markdown_content, 10000)
|
| 313 |
+
except requests.exceptions.Timeout:
|
| 314 |
+
return "The request timed out. Please try again later or check the URL."
|
| 315 |
+
except RequestException as e:
|
| 316 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 317 |
+
except Exception as e:
|
| 318 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 319 |
+
|
| 320 |
+
class DuckDuckGoSearchTool(Tool):
|
| 321 |
+
name = "web_search"
|
| 322 |
+
description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
|
| 323 |
+
inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
|
| 324 |
+
output_type = "string"
|
| 325 |
+
|
| 326 |
+
def __init__(self, max_results=10, **kwargs):
|
| 327 |
+
super().__init__()
|
| 328 |
+
self.max_results = max_results
|
| 329 |
+
try:
|
| 330 |
+
from duckduckgo_search import DDGS
|
| 331 |
+
except ImportError as e:
|
| 332 |
+
raise ImportError(
|
| 333 |
+
"You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
|
| 334 |
+
) from e
|
| 335 |
+
self.ddgs = DDGS(**kwargs)
|
| 336 |
+
|
| 337 |
+
def forward(self, query: str) -> str:
|
| 338 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 339 |
+
if len(results) == 0:
|
| 340 |
+
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 341 |
+
postprocessed_results = [
|
| 342 |
+
f"[{result['title']}]({result['href']})\n{result['body']}" for result in results
|
| 343 |
+
]
|
| 344 |
+
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 345 |
|
| 346 |
# -----------------------------------------------------------------------------
|
| 347 |
+
# Chat Generation Function with support for @tts, @image, @3d, and @web commands
|
| 348 |
# -----------------------------------------------------------------------------
|
| 349 |
|
| 350 |
@spaces.GPU
|
|
|
|
| 359 |
):
|
| 360 |
"""
|
| 361 |
Generates chatbot responses with support for multimodal input, TTS, image generation,
|
| 362 |
+
3D model generation, and web search/webpage visiting.
|
| 363 |
|
| 364 |
Special commands:
|
| 365 |
- "@tts1" or "@tts2": triggers text-to-speech.
|
| 366 |
- "@image": triggers image generation using the SDXL pipeline.
|
| 367 |
- "@3d": triggers 3D model generation using the ShapE pipeline.
|
| 368 |
+
- "@web": triggers a web command. Use "visit" to visit a URL (e.g., "@web visit https://example.com")
|
| 369 |
+
or "search" to perform a DuckDuckGo search (e.g., "@web search AI news").
|
| 370 |
"""
|
| 371 |
text = input_dict["text"]
|
| 372 |
files = input_dict.get("files", [])
|
|
|
|
| 413 |
yield gr.Image(image_paths[0])
|
| 414 |
return
|
| 415 |
|
| 416 |
+
# --- Web Search/Visit branch ---
|
| 417 |
+
if text.strip().lower().startswith("@web"):
|
| 418 |
+
command_text = text[len("@web"):].strip()
|
| 419 |
+
if command_text.lower().startswith("visit "):
|
| 420 |
+
url = command_text[len("visit"):].strip()
|
| 421 |
+
yield "Visiting webpage..."
|
| 422 |
+
result = VisitWebpageTool().forward(url)
|
| 423 |
+
yield result
|
| 424 |
+
elif command_text.lower().startswith("search "):
|
| 425 |
+
query = command_text[len("search"):].strip()
|
| 426 |
+
yield "Performing web search..."
|
| 427 |
+
result = DuckDuckGoSearchTool().forward(query)
|
| 428 |
+
yield result
|
| 429 |
+
else:
|
| 430 |
+
# Default to web search if no subcommand is specified.
|
| 431 |
+
yield "Performing web search..."
|
| 432 |
+
result = DuckDuckGoSearchTool().forward(command_text)
|
| 433 |
+
yield result
|
| 434 |
+
return
|
| 435 |
+
|
| 436 |
# --- Text and TTS branch ---
|
| 437 |
tts_prefix = "@tts"
|
| 438 |
is_tts = any(text.strip().lower().startswith(f"{tts_prefix}{i}") for i in range(1, 3))
|
|
|
|
| 529 |
["@image Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic"],
|
| 530 |
["Write a Python function to check if a number is prime."],
|
| 531 |
["@tts2 What causes rainbows to form?"],
|
| 532 |
+
["@web search latest AI research"],
|
| 533 |
+
["@web visit https://www.example.com"],
|
| 534 |
],
|
| 535 |
cache_examples=False,
|
| 536 |
type="messages",
|