adikrmuj commited on
Commit
abd76ea
·
verified ·
1 Parent(s): 9802a29

Initial commit: upload app.py, requirements.txt, and README.md

Browse files
Files changed (3) hide show
  1. README.md +5 -13
  2. app.py +41 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,5 @@
1
- ---
2
- title: Minigpt4 Chat
3
- emoji: 🔥
4
- colorFrom: green
5
- colorTo: yellow
6
- sdk: gradio
7
- sdk_version: 5.36.2
8
- app_file: app.py
9
- pinned: false
10
- license: other
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # MiniGPT-4 Image + Text Chat (CPU-friendly)
2
+
3
+ Upload an image and provide a prompt to get a caption or description.
4
+
5
+ Powered by BLIP for CPU compatibility.
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio
4
+ Pillow