Vivian55 commited on
Commit
536ef76
·
verified ·
1 Parent(s): 360f166

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -47,31 +47,23 @@ def caption_image(image: Image.Image):
47
 
48
 
49
  def vqa_image(image: Image.Image, question: str):
50
- """嘗試呼叫 VQA 專用模型;在錯誤時回傳說明。
51
- 註:不同模型的 API 需稍微不同的 input 格式。我們採取通用方式:呼叫 models/{id} 的推理端點,並把 image bytes 與 question 一併傳入。
52
- """
53
  if client is None:
54
  return "Error: 未設定 HF_TOKEN。請在 Space settings -> Secrets 新增 HF_TOKEN。"
55
 
56
  try:
57
  b = pil_to_bytes(image)
58
- # 直接呼叫 post 路徑,讓 huggingface_hub 自行處理 model-specific 格式
59
- # 儘量把 image bytes 和 question 一併傳入
60
- response = client.post(model=VQA_MODEL, data={"image": b, "question": question})
61
- # response 內容依 model 會不同,嘗試解析
62
- if isinstance(response, dict):
63
- # 常見欄位:generated_text / answer / answers / output
64
- for k in ("generated_text", "answer", "answers", "output", "text"):
65
- if k in response:
66
- return response[k]
67
- # 否則回傳整個 dict 的簡短字串
68
- return str(response)
69
- # 如果回傳 list
70
- if isinstance(response, list):
71
- return str(response[0])
72
- return str(response)
73
  except Exception as e:
74
- return f"VQA 錯誤:{e}\n(請確認模型 ID 是否正確、Space 有無 HF_TOKEN、或模型是否支援 VQA 任務)"
75
 
76
 
77
  with gr.Blocks(title="Vision-Language Web UI") as demo:
 
47
 
48
 
49
  def vqa_image(image: Image.Image, question: str):
50
+ """使用 Hugging Face Inference API 執行圖片問答 (VQA)"""
 
 
51
  if client is None:
52
  return "Error: 未設定 HF_TOKEN。請在 Space settings -> Secrets 新增 HF_TOKEN。"
53
 
54
  try:
55
  b = pil_to_bytes(image)
56
+
57
+ # 把問題放在文字 prompt,傳給支援 VQA 的模型
58
+ # 例如 BLIP VQA 模型支援 'image_to_text' 輸入
59
+ result = client.image_to_text(b, model=VQA_MODEL, parameters={"question": question})
60
+
61
+ # 統一輸出格式
62
+ if isinstance(result, list):
63
+ return result[0]
64
+ return str(result)
 
 
 
 
 
 
65
  except Exception as e:
66
+ return f"VQA 錯誤:{e}"
67
 
68
 
69
  with gr.Blocks(title="Vision-Language Web UI") as demo: