File size: 2,709 Bytes
a822033
e6d82d8
1e909a6
d150f44
 
a822033
b44998f
 
d150f44
b44998f
d150f44
1318091
 
 
 
 
 
 
 
b44998f
1318091
 
 
 
 
d150f44
1318091
 
 
 
 
b44998f
1318091
 
 
 
b44998f
 
 
d8bdad3
38784bb
 
 
 
 
 
 
 
 
 
 
7e4efac
38784bb
d8bdad3
 
 
7e4efac
d8bdad3
7e4efac
 
1318091
b44998f
1318091
 
b44998f
1318091
 
b44998f
1318091
 
 
d8bdad3
1318091
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from transformers import pipeline
import gradio as gr
from PIL import Image
import requests
from io import BytesIO

# 修正:使用正確的 pipeline 類型
pipe = pipeline("image-text-to-text", model="google/medgemma-4b-it")

# 檢查輸入是否為 URL,如果是,則下載並轉換為 PIL 圖片
def load_image_from_input(image_input):
    if isinstance(image_input, str) and (image_input.startswith("http://") or image_input.startswith("https://")):
        try:
            response = requests.get(image_input)
            img = Image.open(BytesIO(response.content))
            return img
        except Exception as e:
            raise gr.Error(f"無法從 URL 下載圖片: {e}")
    else:
        # 如果不是 URL,就假設是檔案路徑,由 Gradio 自動處理
        return Image.open(image_input)

# 包裝成 API 函數
def predict(image_input, question):
    image = load_image_from_input(image_input)
    
    # 將輸入轉換為模型所需的 messages 格式
    messages = [
        {
            "role": "user",
            "content": [
                {"type": "image", "image": image},  # 使用 "image" 而不是 "url"
                {"type": "text", "text": question}
            ]
        },
    ]
    
    # 修正:使用 text 參數而不是直接傳遞 messages
    result = pipe(text=messages)
    
    # 修正輸出格式:處理新的結果格式
    # 檢查是否是新格式:包含 generated_text 的 dict
    if isinstance(result, list) and len(result) > 0 and isinstance(result[0], dict):
        first_result = result[0]
        if "generated_text" in first_result:
            generated_text = first_result["generated_text"]
            # generated_text 是一個 list,找到 assistant 的回應
            if isinstance(generated_text, list):
                for message in generated_text:
                    if isinstance(message, dict) and message.get("role") == "assistant":
                        return message.get("content", "無法獲取回應")
    
    # 備用:舊格式處理
    if isinstance(result, list):
        for message in result:
            if isinstance(message, dict) and message.get("role") == "assistant":
                return message.get("content", "無法獲取回應")
    
    # 最後備用方案
    return str(result)

# Gradio 介面(同時支援 UI 和 API)
iface = gr.Interface(
    fn=predict,
    inputs=[gr.Image(type="filepath"), "text"],
    outputs="text",
    title="MedGemma API + Demo",
    description="上傳圖片或輸入圖片 URL,以 API 或 UI 測試 MedGemma。"
)

# 啟動應用程式
if __name__ == "__main__":  # 修正語法錯誤:__name__ 不是 **name**
    iface.launch()