Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,26 @@ def glb_to_data_url(glb_path: str) -> str:
|
|
| 56 |
b64_data = base64.b64encode(data).decode("utf-8")
|
| 57 |
return f"data:model/gltf-binary;base64,{b64_data}"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Model class for Text-to-3D Generation (ShapE)
|
| 60 |
|
| 61 |
class Model:
|
|
@@ -204,7 +224,7 @@ SYSTEM_PROMPT = """
|
|
| 204 |
"2. **Code**: Write Python code to implement your solution.\n"
|
| 205 |
"3. **Observation**: Analyze the output of the code and summarize the results.\n"
|
| 206 |
"4. **Final Answer**: Provide a concise conclusion or final result.\n\n"
|
| 207 |
-
f"Task: {task}"
|
| 208 |
|
| 209 |
"""
|
| 210 |
|
|
@@ -357,7 +377,7 @@ def save_image(img: Image.Image) -> str:
|
|
| 357 |
return unique_name
|
| 358 |
|
| 359 |
@spaces.GPU(duration=60, enable_queue=True)
|
| 360 |
-
#SG161222/RealVisXL_V5.0_Lightning
|
| 361 |
def generate_image_fn(
|
| 362 |
prompt: str,
|
| 363 |
negative_prompt: str = "",
|
|
@@ -472,7 +492,7 @@ def generate(
|
|
| 472 |
# --- 3D Generation branch ---
|
| 473 |
if text.strip().lower().startswith("@3d"):
|
| 474 |
prompt = text[len("@3d"):].strip()
|
| 475 |
-
yield "
|
| 476 |
glb_path, used_seed = generate_3d_fn(
|
| 477 |
prompt=prompt,
|
| 478 |
seed=1,
|
|
@@ -481,7 +501,7 @@ def generate(
|
|
| 481 |
randomize_seed=True,
|
| 482 |
)
|
| 483 |
# Copy the GLB file to a static folder.
|
| 484 |
-
yield "
|
| 485 |
static_folder = os.path.join(os.getcwd(), "static")
|
| 486 |
if not os.path.exists(static_folder):
|
| 487 |
os.makedirs(static_folder)
|
|
@@ -495,7 +515,7 @@ def generate(
|
|
| 495 |
# --- Image Generation branch ---
|
| 496 |
if text.strip().lower().startswith("@image"):
|
| 497 |
prompt = text[len("@image"):].strip()
|
| 498 |
-
yield "
|
| 499 |
image_paths, used_seed = generate_image_fn(
|
| 500 |
prompt=prompt,
|
| 501 |
negative_prompt="",
|
|
@@ -518,14 +538,14 @@ def generate(
|
|
| 518 |
# If the command starts with "visit", then treat the rest as a URL
|
| 519 |
if web_command.lower().startswith("visit"):
|
| 520 |
url = web_command[len("visit"):].strip()
|
| 521 |
-
yield "
|
| 522 |
visitor = VisitWebpageTool()
|
| 523 |
content = visitor.forward(url)
|
| 524 |
yield content
|
| 525 |
else:
|
| 526 |
# Otherwise, treat the rest as a search query.
|
| 527 |
query = web_command
|
| 528 |
-
yield "
|
| 529 |
searcher = DuckDuckGoSearchTool()
|
| 530 |
results = searcher.forward(query)
|
| 531 |
yield results
|
|
@@ -534,7 +554,7 @@ def generate(
|
|
| 534 |
# --- rAgent Reasoning branch ---
|
| 535 |
if text.strip().lower().startswith("@ragent"):
|
| 536 |
prompt = text[len("@ragent"):].strip()
|
| 537 |
-
yield "
|
| 538 |
# Pass the current chat history (cleaned) to help inform the chain.
|
| 539 |
for partial in ragent_reasoning(prompt, clean_chat_history(chat_history)):
|
| 540 |
yield partial
|
|
@@ -542,7 +562,7 @@ def generate(
|
|
| 542 |
|
| 543 |
# --- YOLO Object Detection branch ---
|
| 544 |
if text.strip().lower().startswith("@yolo"):
|
| 545 |
-
yield "
|
| 546 |
if not files or len(files) == 0:
|
| 547 |
yield "Error: Please attach an image for YOLO object detection."
|
| 548 |
return
|
|
@@ -617,7 +637,7 @@ def generate(
|
|
| 617 |
|
| 618 |
# Stream the response
|
| 619 |
buffer = ""
|
| 620 |
-
yield "
|
| 621 |
for new_text in streamer:
|
| 622 |
buffer += new_text
|
| 623 |
time.sleep(0.01) # Small delay to simulate real-time streaming
|
|
@@ -661,7 +681,7 @@ def generate(
|
|
| 661 |
thread.start()
|
| 662 |
|
| 663 |
buffer = ""
|
| 664 |
-
yield "
|
| 665 |
for new_text in streamer:
|
| 666 |
buffer += new_text
|
| 667 |
buffer = buffer.replace("<|im_end|>", "")
|
|
@@ -689,6 +709,7 @@ def generate(
|
|
| 689 |
t.start()
|
| 690 |
|
| 691 |
outputs = []
|
|
|
|
| 692 |
for new_text in streamer:
|
| 693 |
outputs.append(new_text)
|
| 694 |
yield "".join(outputs)
|
|
|
|
| 56 |
b64_data = base64.b64encode(data).decode("utf-8")
|
| 57 |
return f"data:model/gltf-binary;base64,{b64_data}"
|
| 58 |
|
| 59 |
+
def progress_bar_html(label: str) -> str:
|
| 60 |
+
"""
|
| 61 |
+
Returns an HTML snippet for a thin progress bar with a label.
|
| 62 |
+
The progress bar is styled as a dark red animated bar.
|
| 63 |
+
"""
|
| 64 |
+
return f'''
|
| 65 |
+
<div style="display: flex; align-items: center;">
|
| 66 |
+
<span style="margin-right: 10px; font-size: 14px;">{label}</span>
|
| 67 |
+
<div style="width: 110px; height: 5px; background-color: #f0f0f0; border-radius: 2px; overflow: hidden;">
|
| 68 |
+
<div style="width: 100%; height: 100%; background-color: darkred; animation: loading 1.5s linear infinite;"></div>
|
| 69 |
+
</div>
|
| 70 |
+
</div>
|
| 71 |
+
<style>
|
| 72 |
+
@keyframes loading {{
|
| 73 |
+
0% {{ transform: translateX(-100%); }}
|
| 74 |
+
100% {{ transform: translateX(100%); }}
|
| 75 |
+
}}
|
| 76 |
+
</style>
|
| 77 |
+
'''
|
| 78 |
+
|
| 79 |
# Model class for Text-to-3D Generation (ShapE)
|
| 80 |
|
| 81 |
class Model:
|
|
|
|
| 224 |
"2. **Code**: Write Python code to implement your solution.\n"
|
| 225 |
"3. **Observation**: Analyze the output of the code and summarize the results.\n"
|
| 226 |
"4. **Final Answer**: Provide a concise conclusion or final result.\n\n"
|
| 227 |
+
f"Task: {{task}}"
|
| 228 |
|
| 229 |
"""
|
| 230 |
|
|
|
|
| 377 |
return unique_name
|
| 378 |
|
| 379 |
@spaces.GPU(duration=60, enable_queue=True)
|
| 380 |
+
# SG161222/RealVisXL_V5.0_Lightning
|
| 381 |
def generate_image_fn(
|
| 382 |
prompt: str,
|
| 383 |
negative_prompt: str = "",
|
|
|
|
| 492 |
# --- 3D Generation branch ---
|
| 493 |
if text.strip().lower().startswith("@3d"):
|
| 494 |
prompt = text[len("@3d"):].strip()
|
| 495 |
+
yield progress_bar_html("Processing 3D Mesh Generation")
|
| 496 |
glb_path, used_seed = generate_3d_fn(
|
| 497 |
prompt=prompt,
|
| 498 |
seed=1,
|
|
|
|
| 501 |
randomize_seed=True,
|
| 502 |
)
|
| 503 |
# Copy the GLB file to a static folder.
|
| 504 |
+
yield progress_bar_html("Finalizing 3D Mesh Generation")
|
| 505 |
static_folder = os.path.join(os.getcwd(), "static")
|
| 506 |
if not os.path.exists(static_folder):
|
| 507 |
os.makedirs(static_folder)
|
|
|
|
| 515 |
# --- Image Generation branch ---
|
| 516 |
if text.strip().lower().startswith("@image"):
|
| 517 |
prompt = text[len("@image"):].strip()
|
| 518 |
+
yield progress_bar_html("Generating Image")
|
| 519 |
image_paths, used_seed = generate_image_fn(
|
| 520 |
prompt=prompt,
|
| 521 |
negative_prompt="",
|
|
|
|
| 538 |
# If the command starts with "visit", then treat the rest as a URL
|
| 539 |
if web_command.lower().startswith("visit"):
|
| 540 |
url = web_command[len("visit"):].strip()
|
| 541 |
+
yield progress_bar_html("Visiting Webpage")
|
| 542 |
visitor = VisitWebpageTool()
|
| 543 |
content = visitor.forward(url)
|
| 544 |
yield content
|
| 545 |
else:
|
| 546 |
# Otherwise, treat the rest as a search query.
|
| 547 |
query = web_command
|
| 548 |
+
yield progress_bar_html("Performing Web Search")
|
| 549 |
searcher = DuckDuckGoSearchTool()
|
| 550 |
results = searcher.forward(query)
|
| 551 |
yield results
|
|
|
|
| 554 |
# --- rAgent Reasoning branch ---
|
| 555 |
if text.strip().lower().startswith("@ragent"):
|
| 556 |
prompt = text[len("@ragent"):].strip()
|
| 557 |
+
yield progress_bar_html("Processing Reasoning Chain")
|
| 558 |
# Pass the current chat history (cleaned) to help inform the chain.
|
| 559 |
for partial in ragent_reasoning(prompt, clean_chat_history(chat_history)):
|
| 560 |
yield partial
|
|
|
|
| 562 |
|
| 563 |
# --- YOLO Object Detection branch ---
|
| 564 |
if text.strip().lower().startswith("@yolo"):
|
| 565 |
+
yield progress_bar_html("Performing Object Detection")
|
| 566 |
if not files or len(files) == 0:
|
| 567 |
yield "Error: Please attach an image for YOLO object detection."
|
| 568 |
return
|
|
|
|
| 637 |
|
| 638 |
# Stream the response
|
| 639 |
buffer = ""
|
| 640 |
+
yield progress_bar_html("Processing Phi-4 Multimodal")
|
| 641 |
for new_text in streamer:
|
| 642 |
buffer += new_text
|
| 643 |
time.sleep(0.01) # Small delay to simulate real-time streaming
|
|
|
|
| 681 |
thread.start()
|
| 682 |
|
| 683 |
buffer = ""
|
| 684 |
+
yield progress_bar_html("Processing with Qwen2VL OCR")
|
| 685 |
for new_text in streamer:
|
| 686 |
buffer += new_text
|
| 687 |
buffer = buffer.replace("<|im_end|>", "")
|
|
|
|
| 709 |
t.start()
|
| 710 |
|
| 711 |
outputs = []
|
| 712 |
+
yield progress_bar_html("Processing Chat Response")
|
| 713 |
for new_text in streamer:
|
| 714 |
outputs.append(new_text)
|
| 715 |
yield "".join(outputs)
|