Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,36 @@
|
|
1 |
-
# app.py
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
self.
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
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()
|
|
|
|
|
|
|
|
|
|