Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +39 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# โหลดโมเดลและ pipeline
|
6 |
+
model, tokenizer = FastVisionModel.from_pretrained(
|
7 |
+
"unsloth/Llama-3.2-11B-Vision-Instruct",
|
8 |
+
load_in_4bit=True,
|
9 |
+
use_gradient_checkpointing="unsloth",
|
10 |
+
device_map="auto"
|
11 |
+
)
|
12 |
+
|
13 |
+
vision_pipeline = pipeline(
|
14 |
+
"image-to-text",
|
15 |
+
model=model,
|
16 |
+
tokenizer=tokenizer,
|
17 |
+
device=0
|
18 |
+
)
|
19 |
+
|
20 |
+
# ฟังก์ชันสำหรับ Gradio
|
21 |
+
def analyze_image(image, instruction):
|
22 |
+
result = vision_pipeline(images=image, instruction=instruction, max_new_tokens=128)
|
23 |
+
return result[0]["generated_text"]
|
24 |
+
|
25 |
+
# สร้าง UI ด้วย Gradio
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=analyze_image,
|
28 |
+
inputs=[
|
29 |
+
gr.Image(type="pil"),
|
30 |
+
gr.Textbox(label="Instruction", value="You are an expert radiographer. Describe accurately what you see in this image.")
|
31 |
+
],
|
32 |
+
outputs=gr.Textbox(label="Generated Text"),
|
33 |
+
title="Medical Image Analysis",
|
34 |
+
description="Analyze X-ray images and generate a diagnostic report."
|
35 |
+
)
|
36 |
+
|
37 |
+
# รันแอป
|
38 |
+
if __name__ == "__main__":
|
39 |
+
interface.launch()
|
requirements.txt
ADDED
Binary file (5.89 kB). View file
|
|