adikrmuj commited on
Commit
d3ad3ff
·
verified ·
1 Parent(s): b1550a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -41
app.py CHANGED
@@ -1,41 +1,36 @@
1
- # app.py
2
- import os
3
- import torch
4
- import gradio as gr
5
- from PIL import Image
6
- from transformers import BlipProcessor, BlipForConditionalGeneration
7
-
8
- # Placeholder for MiniGPT-4 (use real model if you have GPU)
9
- class MiniGPT4:
10
- def __init__(self):
11
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
- self.processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
13
- self.model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(self.device)
14
-
15
- def chat(self, image, prompt=""):
16
- if image is None:
17
- return "Please upload an image."
18
- inputs = self.processor(images=image, return_tensors="pt").to(self.device)
19
- out = self.model.generate(**inputs, max_new_tokens=75)
20
- caption = self.processor.decode(out[0], skip_special_tokens=True)
21
- return f"{prompt}\n\nImage description: {caption}"
22
-
23
- # Initialize model
24
- minigpt4 = MiniGPT4()
25
-
26
- def respond(image, prompt):
27
- return minigpt4.chat(image, prompt)
28
-
29
- demo = gr.Interface(
30
- fn=respond,
31
- inputs=[
32
- gr.Image(type="pil", label="Upload an Image"),
33
- gr.Textbox(lines=2, placeholder="Ask something about the image...", label="Your Prompt")
34
- ],
35
- outputs=gr.Textbox(label="MiniGPT-4 Response"),
36
- title="MiniGPT-4 Chat (Image + Text)",
37
- description="Upload an image and ask a question. The model will respond based on image and prompt."
38
- )
39
-
40
- if __name__ == "__main__":
41
- demo.launch()
 
1
+ # app.py
2
+ import torch
3
+ import gradio as gr
4
+ from PIL import Image
5
+ from transformers import Blip2Processor, Blip2ForConditionalGeneration
6
+
7
+ class MiniGPT4Lite:
8
+ def __init__(self):
9
+ self.device = torch.device("cpu") # Force CPU for Hugging Face Space
10
+ self.processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
11
+ self.model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl").to(self.device)
12
+
13
+ def chat(self, image, prompt="What is in this image?"):
14
+ inputs = self.processor(images=image, text=prompt, return_tensors="pt").to(self.device)
15
+ out = self.model.generate(**inputs, max_new_tokens=100)
16
+ return self.processor.tokenizer.decode(out[0], skip_special_tokens=True)
17
+
18
+ # Initialize model
19
+ minigpt = MiniGPT4Lite()
20
+
21
+ def respond(image, prompt):
22
+ return minigpt.chat(image, prompt)
23
+
24
+ demo = gr.Interface(
25
+ fn=respond,
26
+ inputs=[
27
+ gr.Image(type="pil", label="Upload an Image"),
28
+ gr.Textbox(lines=2, placeholder="Ask a question about the image...", label="Prompt")
29
+ ],
30
+ outputs=gr.Textbox(label="MiniGPT-4 Lite Response"),
31
+ title="MiniGPT-4 Lite (CPU)",
32
+ description="Upload an image and ask a question. Powered by BLIP-2 + Flan-T5. Works on CPU."
33
+ )
34
+
35
+ if __name__ == "__main__":
36
+ demo.launch()