Sebukpor commited on
Commit
94d8d44
·
verified ·
1 Parent(s): f096b1c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ from unsloth import FastVisionModel
5
+ from peft import PeftModel
6
+
7
+ # -----------------------------------------------------------------------------
8
+ # 1. Load Model Correctly (No Double Patching)
9
+ # -----------------------------------------------------------------------------
10
+
11
+
12
+ # Load model with 4-bit quantization
13
+ model, tokenizer = FastVisionModel.from_pretrained(
14
+ "unsloth/Llama-3.2-11B-Vision-Instruct",
15
+ load_in_4bit = True,
16
+ device_map = "auto",
17
+ )
18
+
19
+ # -----------------------------------------------------------------------------
20
+ # 2. Load Your Fine-Tuned Adapter CORRECTLY
21
+ # -----------------------------------------------------------------------------
22
+
23
+ # Loading adapter WITHOUT get_peft_model()
24
+ model = PeftModel.from_pretrained(model, "/content/fine_tuned_model")
25
+ model = model.merge_and_unload() # Merge adapters into base model
26
+ model.to("cuda")
27
+ model.eval()
28
+
29
+ # -----------------------------------------------------------------------------
30
+ # 3. Data preprocessing step
31
+ # -----------------------------------------------------------------------------
32
+
33
+ def analyze(image, user_prompt):
34
+ if image.mode != "RGB":
35
+ image = image.convert("RGB")
36
+
37
+ messages = [
38
+ {"role": "user", "content": [
39
+ {"type": "image", "image": image},
40
+ {"type": "text", "text": user_prompt}
41
+ ]}
42
+ ]
43
+
44
+ input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
45
+
46
+ inputs = tokenizer(
47
+ image,
48
+ input_text,
49
+ return_tensors = "pt",
50
+ add_special_tokens = False,
51
+ ).to("cuda")
52
+
53
+ with torch.no_grad():
54
+ outputs = model.generate(
55
+ **inputs,
56
+ max_new_tokens = 512,
57
+ use_cache = True,
58
+ temperature = 1.0,
59
+ min_p = 0.1,
60
+ )
61
+
62
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
63
+
64
+ # -----------------------------------------------------------------------------
65
+ # 4. Launch Interface
66
+ # -----------------------------------------------------------------------------
67
+
68
+ gr.Interface(
69
+ fn=analyze,
70
+ inputs=[
71
+ gr.Image(type="pil", label="Upload Medical Scan"),
72
+ gr.Textbox(
73
+ placeholder="Example: 'Describe any abnormalities in this chest X-ray'",
74
+ label="Your Question",
75
+ lines=2
76
+ )
77
+ ],
78
+ outputs=gr.Textbox(label="Expert Analysis"),
79
+ title=" DAS medhub Radiology AI Assistant (Fine-Tuned)",
80
+ description="Upload a medical image and ask questions about it"
81
+ ).launch(server_port=7860, debug=False)