OppaAI's picture
Update app.py
d117752 verified
raw
history blame
1.85 kB
import gradio as gr
import json
import base64
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:
if not HF_TOKEN:
return {"error": "Missing token"}
robot_id = payload.get("robot_id", "unknown")
image_b64 = payload["image_b64"]
data = {
"model": MODEL,
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image in detail."},
{
"type": "image_data",
"image_data": {"b64": image_b64}
}
]
}
]
}
resp = requests.post(
"https://router.huggingface.co/v1/chat/completions",
headers={
"Authorization": f"Bearer {HF_TOKEN}",
"Content-Type": "application/json"
},
data=json.dumps(data),
timeout=60
)
if resp.status_code != 200:
return {"error": f"VLM API error: {resp.status_code}, {resp.text}"}
try:
content = resp.json()["choices"][0]["message"]["content"]
first = content[0]["text"]
except:
return {"error": f"Bad response: {resp.text}"}
return {
"received": True,
"robot_id": robot_id,
"vllm_analysis": first
}
except Exception as e:
return {"error": str(e)}
demo = gr.Interface(
fn=process,
inputs=gr.JSON(label="Input Payload"),
outputs=gr.JSON(label="Reply"),
api_name="predict"
)
if __name__ == "__main__":
demo.launch(mcp_server=True)