Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import json | |
| import base64 | |
| from io import BytesIO | |
| import requests | |
| import os | |
| HF_TOKEN = os.environ.get("HF_CV_ROBOT_TOKEN") | |
| MODEL = "Qwen/Qwen2.5-VL-7B-Instruct" | |
| def process(payload: dict): | |
| try: | |
| image_b64 = payload["image_b64"] | |
| robot_id = payload.get("robot_id", "unknown") | |
| # Base64 → Bytes | |
| img_bytes = base64.b64decode(image_b64) | |
| # multipart file | |
| files = { | |
| "file": ("image.jpg", BytesIO(img_bytes), "image/jpeg") | |
| } | |
| # Router 要求的 payload 格式(不含 image_data) | |
| data = { | |
| "model": MODEL, | |
| "messages": [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": "Describe this image in detail."}, | |
| {"type": "file", "file": "image.jpg"} | |
| ] | |
| } | |
| ] | |
| } | |
| resp = requests.post( | |
| "https://router.huggingface.co/v1/chat/completions", | |
| headers={"Authorization": f"Bearer {HF_TOKEN}"}, | |
| data={"payload": json.dumps(data)}, | |
| files=files, | |
| timeout=60 | |
| ) | |
| if resp.status_code != 200: | |
| return {"error": f"VLM API error: {resp.status_code}, {resp.text}"} | |
| out = resp.json() | |
| txt = out["choices"][0]["message"]["content"][0]["text"] | |
| return { | |
| "received": True, | |
| "robot_id": robot_id, | |
| "vllm_analysis": txt | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} | |
| demo = gr.Interface( | |
| fn=process, | |
| inputs=gr.JSON(label="Input Payload (Dict)"), | |
| outputs=gr.JSON(label="Reply to Jetson"), | |
| api_name="predict" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) | |