taesiri commited on
Commit
280da27
1 Parent(s): d20ac21
Files changed (1) hide show
  1. app.py +45 -4
app.py CHANGED
@@ -1,7 +1,48 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import MllamaForConditionalGeneration, AutoProcessor
5
+ from peft import PeftModel
6
 
7
+ # Load model and processor (do this outside the inference function to avoid reloading)
8
+ base_model_path = "meta-llama/Llama-3.2-11B-Vision-Instruct"
9
+ lora_weights_path = "taesiri/BunsBunny-LLama-3.2-11B-Vision-Instruct-DummyTask2"
10
 
11
+ processor = AutoProcessor.from_pretrained(base_model_path)
12
+ model = MllamaForConditionalGeneration.from_pretrained(
13
+ base_model_path,
14
+ torch_dtype=torch.bfloat16,
15
+ device_map="auto",
16
+ )
17
+ model = PeftModel.from_pretrained(model, lora_weights_path)
18
+
19
+ def inference(image, question):
20
+ # Prepare input
21
+ messages = [
22
+ {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": question}]}
23
+ ]
24
+ input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
25
+ inputs = processor(image, input_text, add_special_tokens=False, return_tensors="pt").to(model.device)
26
+
27
+ # Run inference
28
+ with torch.no_grad():
29
+ output = model.generate(**inputs, max_new_tokens=2048)
30
+
31
+ # Decode output
32
+ result = processor.decode(output[0], skip_special_tokens=True)
33
+ return result
34
+
35
+ # Create Gradio interface
36
+ demo = gr.Interface(
37
+ fn=inference,
38
+ inputs=[
39
+ gr.Image(type="pil", label="Upload Image"),
40
+ gr.Textbox(label="Enter your question")
41
+ ],
42
+ outputs=gr.Textbox(label="Response"),
43
+ title="Image Analysis AI",
44
+ description="Upload an image and ask a question about it. The AI will analyze and respond.",
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()