0llheaven commited on
Commit
d7f27b4
·
verified ·
1 Parent(s): 9a7e503

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -1,13 +1,14 @@
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
4
- from transformers import MllamaForConditionalGeneration, AutoProcessor
5
 
6
  # โหลดโมเดลและตัวประมวลผล
7
  model_id = "0llheaven/Llama-3.2-11B-Vision-Radiology-mini"
8
 
9
- model = MllamaForConditionalGeneration.from_pretrained(
10
  model_id,
 
11
  torch_dtype=torch.bfloat16,
12
  device_map="auto",
13
  )
@@ -35,20 +36,22 @@ def generate_caption(image):
35
  ).to(model.device)
36
 
37
  # สร้างข้อความตอบกลับจากโมเดล
38
- output = model.generate(**inputs, max_new_tokens=256, use_cache=True, temperature=1.5, min_p=0.1)
39
- return processor.decode(output[0])
 
 
 
 
 
40
 
41
  # สร้างอินเตอร์เฟซด้วย Gradio
42
- with gr.Blocks() as demo:
43
- gr.Markdown("# Radiology Image Captioning")
44
- with gr.Row():
45
- image_input = gr.Image(type="pil", label="Upload Image")
46
- output_text = gr.Textbox(label="Generated Caption")
47
- title="Medical Vision Analysis"
48
- generate_button = gr.Button("Generate Caption")
49
-
50
- # กำหนดการทำงานเมื่อกดปุ่ม
51
- generate_button.click(fn=generate_caption, inputs=image_input, outputs=output_text)
52
 
53
  # รันแอป
54
- demo.launch()
 
 
1
  import gradio as gr
2
  import torch
3
  from PIL import Image
4
+ from transformers import AutoModelForVision2Seq, AutoProcessor
5
 
6
  # โหลดโมเดลและตัวประมวลผล
7
  model_id = "0llheaven/Llama-3.2-11B-Vision-Radiology-mini"
8
 
9
+ model = AutoModelForVision2Seq.from_pretrained(
10
  model_id,
11
+ load_in_4bit = True,
12
  torch_dtype=torch.bfloat16,
13
  device_map="auto",
14
  )
 
36
  ).to(model.device)
37
 
38
  # สร้างข้อความตอบกลับจากโมเดล
39
+ output = model.generate(**inputs,
40
+ max_new_tokens=256,
41
+ use_cache=True,
42
+ temperature=1.5,
43
+ min_p=0.1)
44
+
45
+ return processor.tokenizer.decode(output[0], skip_special_tokens=True).strip()
46
 
47
  # สร้างอินเตอร์เฟซด้วย Gradio
48
+ demo = gr.Interface(
49
+ fn=generate_caption,
50
+ image_input = gr.Image(type="pil", label="Upload Image"),
51
+ output_text = gr.Textbox(label="Generated Caption"),
52
+ title="Medical Vision Analysis"
53
+ )
 
 
 
 
54
 
55
  # รันแอป
56
+ if __name__ == "__main__":
57
+ demo.launch()