Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,51 +3,41 @@ import json
|
|
| 3 |
import base64
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
-
from huggingface_hub import
|
| 7 |
|
| 8 |
-
HF_TOKEN = os.environ.get("
|
|
|
|
| 9 |
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 10 |
-
REPO_ID = "OppaAI/Robot_MCP" # Replace with your HF repo
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
api = HfApi()
|
| 16 |
-
HfFolder.save_token(HF_TOKEN)
|
| 17 |
-
|
| 18 |
-
def upload_to_hf(filepath: str):
|
| 19 |
-
filename = os.path.basename(filepath)
|
| 20 |
-
# Upload to HF repo
|
| 21 |
-
api.upload_file(
|
| 22 |
-
path_or_fileobj=filepath,
|
| 23 |
-
path_in_repo=filename,
|
| 24 |
-
repo_id=REPO_ID,
|
| 25 |
-
repo_type="dataset",
|
| 26 |
-
token=HF_TOKEN,
|
| 27 |
-
overwrite=True
|
| 28 |
-
)
|
| 29 |
-
# Construct raw URL
|
| 30 |
-
url = f"https://huggingface.co/datasets/{REPO_ID}/resolve/main/{filename}"
|
| 31 |
-
return url
|
| 32 |
-
|
| 33 |
-
def process(payload: dict):
|
| 34 |
try:
|
| 35 |
-
if not HF_TOKEN:
|
| 36 |
-
return {"error": "Hugging Face token missing."}
|
| 37 |
-
|
| 38 |
robot_id = payload.get("robot_id", "unknown")
|
| 39 |
image_b64 = payload["image_b64"]
|
|
|
|
| 40 |
|
| 41 |
-
# 1️⃣ Save
|
| 42 |
-
|
| 43 |
-
with open(
|
| 44 |
-
f.write(
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
image_url =
|
| 49 |
|
| 50 |
-
#
|
| 51 |
data = {
|
| 52 |
"model": MODEL,
|
| 53 |
"messages": [
|
|
@@ -76,22 +66,22 @@ def process(payload: dict):
|
|
| 76 |
except Exception as e:
|
| 77 |
vlm_text = f"Failed to parse VLM response: {e}, Response={resp.text}"
|
| 78 |
|
| 79 |
-
# 4️⃣ Return combined info
|
| 80 |
return {
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
|
|
|
|
|
|
| 84 |
"robot_id": robot_id,
|
| 85 |
-
"vlm_description": vlm_text
|
| 86 |
-
"image_url": image_url
|
| 87 |
}
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
-
return {"error": str(e)}
|
| 91 |
|
| 92 |
demo = gr.Interface(
|
| 93 |
-
fn=
|
| 94 |
-
inputs=gr.JSON(label="Input Payload (Dict format)"),
|
| 95 |
outputs=gr.JSON(label="Reply to Jetson"),
|
| 96 |
api_name="predict"
|
| 97 |
)
|
|
|
|
| 3 |
import base64
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
+
from huggingface_hub import upload_file
|
| 7 |
|
| 8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
+
HF_DATASET_REPO = "OppaAI/Robot_MCP" # Replace with your dataset repo
|
| 10 |
MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
|
|
|
|
| 11 |
|
| 12 |
+
def process_and_describe(payload: dict):
|
| 13 |
+
if not HF_TOKEN:
|
| 14 |
+
return {"error": "HF_TOKEN secret not found in Space settings."}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
|
|
|
|
|
|
|
|
|
| 17 |
robot_id = payload.get("robot_id", "unknown")
|
| 18 |
image_b64 = payload["image_b64"]
|
| 19 |
+
image_bytes = base64.b64decode(image_b64)
|
| 20 |
|
| 21 |
+
# 1️⃣ Save temporarily
|
| 22 |
+
local_tmp_path = "/tmp/uploaded_image.jpg"
|
| 23 |
+
with open(local_tmp_path, "wb") as f:
|
| 24 |
+
f.write(image_bytes)
|
| 25 |
+
|
| 26 |
+
# 2️⃣ Upload to HF dataset repo
|
| 27 |
+
path_in_repo = f"images/uploaded_image_{len(image_bytes)}.jpg"
|
| 28 |
+
upload_file(
|
| 29 |
+
path_or_fileobj=local_tmp_path,
|
| 30 |
+
path_in_repo=path_in_repo,
|
| 31 |
+
repo_id=HF_DATASET_REPO,
|
| 32 |
+
token=HF_TOKEN,
|
| 33 |
+
repo_type="dataset"
|
| 34 |
+
)
|
| 35 |
+
os.remove(local_tmp_path)
|
| 36 |
|
| 37 |
+
# 3️⃣ Construct public URL
|
| 38 |
+
image_url = f"https://huggingface.co/datasets/{HF_DATASET_REPO}/resolve/main/{path_in_repo}"
|
| 39 |
|
| 40 |
+
# 4️⃣ Call VLM
|
| 41 |
data = {
|
| 42 |
"model": MODEL,
|
| 43 |
"messages": [
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
vlm_text = f"Failed to parse VLM response: {e}, Response={resp.text}"
|
| 68 |
|
|
|
|
| 69 |
return {
|
| 70 |
+
"saved_to_hf_hub": True,
|
| 71 |
+
"repo_id": HF_DATASET_REPO,
|
| 72 |
+
"path_in_repo": path_in_repo,
|
| 73 |
+
"image_url": image_url,
|
| 74 |
+
"file_size_bytes": len(image_bytes),
|
| 75 |
"robot_id": robot_id,
|
| 76 |
+
"vlm_description": vlm_text
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
+
return {"error": f"Failed to upload/describe image: {str(e)}"}
|
| 81 |
|
| 82 |
demo = gr.Interface(
|
| 83 |
+
fn=process_and_describe,
|
| 84 |
+
inputs=gr.JSON(label="Input Payload (Dict format with 'image_b64')"),
|
| 85 |
outputs=gr.JSON(label="Reply to Jetson"),
|
| 86 |
api_name="predict"
|
| 87 |
)
|