Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ import tempfile
|
|
| 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,7 +16,6 @@ import numpy as np
|
|
| 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,73 +276,28 @@ def generate_image_fn(
|
|
| 278 |
return image_paths, seed
|
| 279 |
|
| 280 |
# -----------------------------------------------------------------------------
|
| 281 |
-
#
|
| 282 |
# -----------------------------------------------------------------------------
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 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,
|
| 348 |
# -----------------------------------------------------------------------------
|
| 349 |
|
| 350 |
@spaces.GPU
|
|
@@ -359,14 +312,12 @@ def generate(
|
|
| 359 |
):
|
| 360 |
"""
|
| 361 |
Generates chatbot responses with support for multimodal input, TTS, image generation,
|
| 362 |
-
3D model generation
|
| 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,26 +364,6 @@ def generate(
|
|
| 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))
|
|
@@ -525,10 +456,9 @@ demo = gr.ChatInterface(
|
|
| 525 |
examples=[
|
| 526 |
["@tts1 Who is Nikola Tesla, and why did he die?"],
|
| 527 |
["@3d A birthday cupcake with cherry"],
|
| 528 |
-
["@web Is Grok-3 Beats DeepSeek-R1 at Reasoning ?"],
|
| 529 |
[{"text": "summarize the letter", "files": ["examples/1.png"]}],
|
| 530 |
["@image Chocolate dripping from a donut against a yellow background, in the style of brocore, hyper-realistic"],
|
| 531 |
-
["
|
| 532 |
["@tts2 What causes rainbows to form?"],
|
| 533 |
],
|
| 534 |
cache_examples=False,
|
|
@@ -550,4 +480,5 @@ from fastapi.staticfiles import StaticFiles
|
|
| 550 |
demo.app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 551 |
|
| 552 |
if __name__ == "__main__":
|
|
|
|
| 553 |
demo.queue(max_size=20).launch(share=True)
|
|
|
|
| 8 |
from threading import Thread
|
| 9 |
import base64
|
| 10 |
import shutil
|
|
|
|
| 11 |
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
|
|
|
| 16 |
from PIL import Image
|
| 17 |
import edge_tts
|
| 18 |
import trimesh
|
|
|
|
| 19 |
|
| 20 |
from transformers import (
|
| 21 |
AutoModelForCausalLM,
|
|
|
|
| 276 |
return image_paths, seed
|
| 277 |
|
| 278 |
# -----------------------------------------------------------------------------
|
| 279 |
+
# Text-to-3D Generation using the ShapE Pipeline
|
| 280 |
# -----------------------------------------------------------------------------
|
| 281 |
|
| 282 |
+
@spaces.GPU(duration=120, enable_queue=True)
|
| 283 |
+
def generate_3d_fn(
|
| 284 |
+
prompt: str,
|
| 285 |
+
seed: int = 1,
|
| 286 |
+
guidance_scale: float = 15.0,
|
| 287 |
+
num_steps: int = 64,
|
| 288 |
+
randomize_seed: bool = False,
|
| 289 |
+
):
|
| 290 |
+
"""
|
| 291 |
+
Generate a 3D model from text using the ShapE pipeline.
|
| 292 |
+
Returns a tuple of (glb_file_path, used_seed).
|
| 293 |
+
"""
|
| 294 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
| 295 |
+
model3d = Model()
|
| 296 |
+
glb_path = model3d.run_text(prompt, seed=seed, guidance_scale=guidance_scale, num_steps=num_steps)
|
| 297 |
+
return glb_path, seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
# -----------------------------------------------------------------------------
|
| 300 |
+
# Chat Generation Function with support for @tts, @image, and @3d commands
|
| 301 |
# -----------------------------------------------------------------------------
|
| 302 |
|
| 303 |
@spaces.GPU
|
|
|
|
| 312 |
):
|
| 313 |
"""
|
| 314 |
Generates chatbot responses with support for multimodal input, TTS, image generation,
|
| 315 |
+
and 3D model generation.
|
| 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 |
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))
|
|
|
|
| 456 |
examples=[
|
| 457 |
["@tts1 Who is Nikola Tesla, and why did he die?"],
|
| 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 function to check if a number is prime."],
|
| 462 |
["@tts2 What causes rainbows to form?"],
|
| 463 |
],
|
| 464 |
cache_examples=False,
|
|
|
|
| 480 |
demo.app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 481 |
|
| 482 |
if __name__ == "__main__":
|
| 483 |
+
# Launch without the unsupported static_dirs parameter.
|
| 484 |
demo.queue(max_size=20).launch(share=True)
|