Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
"""
|
| 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 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 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}
|
| 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:
|