Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,31 @@
|
|
| 1 |
-
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
|
|
|
| 4 |
pipe = pipeline("image-text-to-text", model="google/medgemma-4b-it")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
print(result[0]["generated_text"])
|
| 18 |
-
|
| 19 |
-
import gradio as gr
|
| 20 |
-
# 建立 Gradio UI
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=predict,
|
| 23 |
inputs=["text", "text"], # image_url, question
|
| 24 |
outputs="text",
|
| 25 |
-
title="MedGemma Demo",
|
| 26 |
-
description="
|
| 27 |
)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
+
# 建立 pipeline
|
| 5 |
pipe = pipeline("image-text-to-text", model="google/medgemma-4b-it")
|
| 6 |
|
| 7 |
+
# 包裝成 API 函數
|
| 8 |
+
def predict(image_url, question):
|
| 9 |
+
messages = [
|
| 10 |
+
{
|
| 11 |
+
"role": "user",
|
| 12 |
+
"content": [
|
| 13 |
+
{"type": "image", "url": image_url},
|
| 14 |
+
{"type": "text", "text": question}
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
]
|
| 18 |
+
result = pipe(text=messages)
|
| 19 |
+
return result[0]["generated_text"]
|
| 20 |
|
| 21 |
+
# Gradio 介面(同時支援 UI 和 API)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
iface = gr.Interface(
|
| 23 |
fn=predict,
|
| 24 |
inputs=["text", "text"], # image_url, question
|
| 25 |
outputs="text",
|
| 26 |
+
title="MedGemma API + Demo",
|
| 27 |
+
description="呼叫 API 或用 UI 測試"
|
| 28 |
)
|
| 29 |
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
iface.launch()
|