FT_Llama / app.py
0llheaven's picture
Create app.py
4b589f5 verified
raw
history blame
1.99 kB
import gradio as gr
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
# โหลดโมเดลและตัวประมวลผล
model_id = "0llheaven/Llama-3.2-11B-Vision-Radiology-mini"
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_id)
# ฟังก์ชันประมวลผลภาพและสร้างคำบรรยาย
def generate_caption(image):
# แปลงภาพเป็น RGB และปรับขนาด
image = image.convert("RGB")
instruction = "You are an expert radiographer. Describe accurately what you see in this image."
messages = [
{"role": "user", "content": [
{"type": "image"},
{"type": "text", "text": instruction}
]}
]
input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(
image,
input_text,
add_special_tokens=False,
return_tensors="pt"
).to(model.device)
# สร้างข้อความตอบกลับจากโมเดล
output = model.generate(**inputs, max_new_tokens=256, use_cache=True, temperature=1.5, min_p=0.1)
return processor.decode(output[0])
# สร้างอินเตอร์เฟซด้วย Gradio
with gr.Blocks() as demo:
gr.Markdown("# Radiology Image Captioning")
with gr.Row():
image_input = gr.Image(type="pil", label="Upload Image")
output_text = gr.Textbox(label="Generated Caption")
title="Medical Vision Analysis"
generate_button = gr.Button("Generate Caption")
# กำหนดการทำงานเมื่อกดปุ่ม
generate_button.click(fn=generate_caption, inputs=image_input, outputs=output_text)
# รันแอป
demo.launch()