OppaAI commited on
Commit
73ea45e
Β·
verified Β·
1 Parent(s): 0cefe4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -47
app.py CHANGED
@@ -6,9 +6,6 @@ import io
6
  import gradio as gr
7
  from gradio_client import Client
8
  from dotenv import load_dotenv
9
- from rich.console import Console
10
- from rich.table import Table
11
- from rich import box
12
 
13
  # Load environment variables
14
  load_dotenv()
@@ -18,47 +15,15 @@ HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN")
18
  HF_SPACE = "OppaAI/Robot_MCP_Server"
19
  API_NAME = "/predict"
20
 
21
- console = Console()
22
-
23
-
24
- def pretty_print_response(resp: dict):
25
- """Rich table output with row lines, no URL."""
26
- table = Table(
27
- title="😎 Robot Vision Result",
28
- title_style="bold cyan",
29
- title_justify="left",
30
- box=box.ROUNDED,
31
- show_lines=True,
32
- show_header=False,
33
- style="bold cyan"
34
- )
35
-
36
- objects_list = resp.get("objects", [])
37
- objects_str = ", ".join(objects_list) if isinstance(objects_list, list) else str(objects_list)
38
-
39
- table.add_column("Field", style="bold magenta")
40
- table.add_column("Value", style="white")
41
-
42
- table.add_row("πŸ€– Robot ID", str(resp.get("robot_id", "N/A")))
43
- table.add_row("🏞️ Image Size", str(resp.get("file_size_bytes", "N/A")))
44
- table.add_row("πŸ“ Description", str(resp.get("description", "N/A")))
45
- table.add_row("πŸ‘₯ Human", str(resp.get("human", "N/A")))
46
- table.add_row("πŸ“¦ Objects", objects_str)
47
- table.add_row("πŸ›οΈ Environment", str(resp.get("environment", "N/A")))
48
-
49
- console.print(table)
50
- return resp.get("description", ""), resp.get("human", ""), objects_str, resp.get("environment", "")
51
-
52
 
53
  def process_webcam_stream(image):
54
  """Send webcam image to HF MCP Server and get result"""
55
  if image is None:
56
- return None, None, None, None
57
 
58
  # Convert PIL Image to base64
59
- import io
60
  buffered = io.BytesIO()
61
- image.save(buffered, format="JPEG") # <--- directly save PIL Image
62
  b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
63
 
64
  # Prepare payload
@@ -73,19 +38,18 @@ def process_webcam_stream(image):
73
  client = Client(HF_SPACE)
74
  try:
75
  resp = client.predict(payload, api_name=API_NAME)
76
- # Print table in console
77
- pretty_print_response(resp)
78
 
79
- # Return selected fields for Gradio display
80
  return (
81
  resp.get("description", ""),
82
  resp.get("human", ""),
83
- ", ".join(resp.get("objects", [])),
84
  resp.get("environment", "")
85
  )
86
  except Exception as e:
87
- console.print(f"[bold red]Error sending to HF:[/bold red] {e}")
88
- return None, None, None, None
89
 
90
  with gr.Blocks() as demo:
91
  gr.Markdown("## πŸŽ₯ Robot Vision Webcam Stream")
@@ -93,7 +57,7 @@ with gr.Blocks() as demo:
93
  with gr.Row():
94
  webcam_input = gr.Image(
95
  label="Captured from Web-Cam",
96
- sources=['upload', 'webcam'],
97
  type="pil"
98
  )
99
  description_out = gr.Textbox(label="Description")
@@ -108,8 +72,5 @@ with gr.Blocks() as demo:
108
  stream_every=0.5
109
  )
110
 
111
- demo.launch()
112
-
113
-
114
  if __name__ == "__main__":
115
  demo.launch()
 
6
  import gradio as gr
7
  from gradio_client import Client
8
  from dotenv import load_dotenv
 
 
 
9
 
10
  # Load environment variables
11
  load_dotenv()
 
15
  HF_SPACE = "OppaAI/Robot_MCP_Server"
16
  API_NAME = "/predict"
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def process_webcam_stream(image):
20
  """Send webcam image to HF MCP Server and get result"""
21
  if image is None:
22
+ return "", "", "", ""
23
 
24
  # Convert PIL Image to base64
 
25
  buffered = io.BytesIO()
26
+ image.save(buffered, format="JPEG")
27
  b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
28
 
29
  # Prepare payload
 
38
  client = Client(HF_SPACE)
39
  try:
40
  resp = client.predict(payload, api_name=API_NAME)
41
+ objects_list = resp.get("objects", [])
42
+ objects_str = ", ".join(objects_list) if isinstance(objects_list, list) else str(objects_list)
43
 
 
44
  return (
45
  resp.get("description", ""),
46
  resp.get("human", ""),
47
+ objects_str,
48
  resp.get("environment", "")
49
  )
50
  except Exception as e:
51
+ return f"Error: {e}", "", "", ""
52
+
53
 
54
  with gr.Blocks() as demo:
55
  gr.Markdown("## πŸŽ₯ Robot Vision Webcam Stream")
 
57
  with gr.Row():
58
  webcam_input = gr.Image(
59
  label="Captured from Web-Cam",
60
+ sources=["upload", "webcam"],
61
  type="pil"
62
  )
63
  description_out = gr.Textbox(label="Description")
 
72
  stream_every=0.5
73
  )
74
 
 
 
 
75
  if __name__ == "__main__":
76
  demo.launch()