Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
import time
|
| 4 |
-
import
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from gradio_client import Client
|
| 7 |
from dotenv import load_dotenv
|
|
@@ -52,11 +53,9 @@ def pretty_print_response(resp: dict):
|
|
| 52 |
def process_webcam_stream(image):
|
| 53 |
"""Send webcam image to HF MCP Server and get result"""
|
| 54 |
if image is None:
|
| 55 |
-
return
|
| 56 |
|
| 57 |
# Convert to base64
|
| 58 |
-
import io
|
| 59 |
-
from PIL import Image
|
| 60 |
buffered = io.BytesIO()
|
| 61 |
img = Image.fromarray(image)
|
| 62 |
img.save(buffered, format="JPEG")
|
|
@@ -70,18 +69,20 @@ def process_webcam_stream(image):
|
|
| 70 |
"hf_token": HF_TOKEN
|
| 71 |
}
|
| 72 |
|
| 73 |
-
# Send to HF Space
|
| 74 |
client = Client(HF_SPACE)
|
| 75 |
try:
|
| 76 |
resp = client.predict(payload, api_name=API_NAME)
|
| 77 |
-
# Print table in console
|
| 78 |
pretty_print_response(resp)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
console.print(f"[bold red]Error sending to HF:[/bold red] {e}")
|
| 84 |
-
return
|
| 85 |
|
| 86 |
|
| 87 |
with gr.Blocks() as demo:
|
|
@@ -94,7 +95,6 @@ with gr.Blocks() as demo:
|
|
| 94 |
objects_out = gr.Textbox(label="Objects")
|
| 95 |
environment_out = gr.Textbox(label="Environment")
|
| 96 |
|
| 97 |
-
# Connect streaming
|
| 98 |
webcam_input.stream(
|
| 99 |
process_webcam_stream,
|
| 100 |
inputs=[webcam_input],
|
|
|
|
| 1 |
import os
|
| 2 |
import base64
|
| 3 |
import time
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
import gradio as gr
|
| 7 |
from gradio_client import Client
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
| 53 |
def process_webcam_stream(image):
|
| 54 |
"""Send webcam image to HF MCP Server and get result"""
|
| 55 |
if image is None:
|
| 56 |
+
return "", "", "", ""
|
| 57 |
|
| 58 |
# Convert to base64
|
|
|
|
|
|
|
| 59 |
buffered = io.BytesIO()
|
| 60 |
img = Image.fromarray(image)
|
| 61 |
img.save(buffered, format="JPEG")
|
|
|
|
| 69 |
"hf_token": HF_TOKEN
|
| 70 |
}
|
| 71 |
|
| 72 |
+
# Send to HF Space
|
| 73 |
client = Client(HF_SPACE)
|
| 74 |
try:
|
| 75 |
resp = client.predict(payload, api_name=API_NAME)
|
|
|
|
| 76 |
pretty_print_response(resp)
|
| 77 |
+
return (
|
| 78 |
+
resp.get("description", ""),
|
| 79 |
+
resp.get("human", ""),
|
| 80 |
+
", ".join(resp.get("objects", [])) if resp.get("objects") else "",
|
| 81 |
+
resp.get("environment", "")
|
| 82 |
+
)
|
| 83 |
except Exception as e:
|
| 84 |
console.print(f"[bold red]Error sending to HF:[/bold red] {e}")
|
| 85 |
+
return "", "", "", ""
|
| 86 |
|
| 87 |
|
| 88 |
with gr.Blocks() as demo:
|
|
|
|
| 95 |
objects_out = gr.Textbox(label="Objects")
|
| 96 |
environment_out = gr.Textbox(label="Environment")
|
| 97 |
|
|
|
|
| 98 |
webcam_input.stream(
|
| 99 |
process_webcam_stream,
|
| 100 |
inputs=[webcam_input],
|