Spaces:
Sleeping
Sleeping
Commit ·
4ac4222
0
Parent(s):
The image captioning project has been committed.
Browse files- .gitattributes +3 -0
- README.md +37 -0
- app.py +75 -0
- data/image1.jpg +3 -0
- data/image10.jpg +3 -0
- data/image2.png +3 -0
- data/image3.jpg +3 -0
- data/image4.jpg +3 -0
- data/image5.jpg +3 -0
- data/image6.png +3 -0
- data/image7.png +3 -0
- data/image8.jpeg +3 -0
- data/image9.jpeg +3 -0
- requirements.txt +7 -0
.gitattributes
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
data/*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
data/*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
data/*.png filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Image Captioning with BLIP
|
| 3 |
+
|
| 4 |
+
This project uses the Salesforce BLIP model to generate captions for images. It provides a simple web interface built with Gradio to upload an image and view the generated caption.
|
| 5 |
+
|
| 6 |
+
## Setup
|
| 7 |
+
|
| 8 |
+
1. **Clone the repository:**
|
| 9 |
+
```bash
|
| 10 |
+
git clone https://huggingface.co/spaces/electro-sb/image_captioning
|
| 11 |
+
cd image_captioning
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
2. **Install dependencies:**
|
| 15 |
+
```bash
|
| 16 |
+
pip install -r requirements.txt
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
3. **Set up your Hugging Face token:**
|
| 20 |
+
Create a `.env` file in the root of the project and add your Hugging Face API key:
|
| 21 |
+
```
|
| 22 |
+
HF_API_KEY=<your-hugging-face-api-key>
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
4. **Run the application:**
|
| 26 |
+
```bash
|
| 27 |
+
python app.py
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
The application will be available at `http://localhost:7860`.
|
| 31 |
+
|
| 32 |
+
## Usage
|
| 33 |
+
|
| 34 |
+
1. Open your web browser and navigate to `http://localhost:7860`.
|
| 35 |
+
2. Upload an image using the provided interface.
|
| 36 |
+
3. Click the "Caption" button to generate a caption for the image.
|
| 37 |
+
4. The generated caption will be displayed in the "Caption" textbox.
|
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline, AutoTokenizer
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
model = "Salesforce/blip-image-captioning-large"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model, use_fast=True)
|
| 9 |
+
|
| 10 |
+
pipe = pipeline(task="image-to-text",
|
| 11 |
+
model=model,
|
| 12 |
+
tokenizer=tokenizer)
|
| 13 |
+
|
| 14 |
+
def image_to_base64(image: Image) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Convert an image to a base64 string.
|
| 17 |
+
"""
|
| 18 |
+
bytearray= io.BytesIO()
|
| 19 |
+
image.save(bytearray, format="PNG")
|
| 20 |
+
return str(base64.b64encode(bytearray.getvalue()).decode('utf-8'))
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def caption_image(image):
|
| 24 |
+
result = pipe(
|
| 25 |
+
image_to_base64(image),
|
| 26 |
+
#Temperature=0.7,
|
| 27 |
+
# max_length=130,
|
| 28 |
+
# min_length=30,
|
| 29 |
+
#do_sample=True
|
| 30 |
+
)
|
| 31 |
+
return result[0]['generated_text'].upper()
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
gr.close_all()
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as interface:
|
| 37 |
+
gr.Markdown("### Image Captioning using BLIP Large")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
image_input = gr.Image(type="pil", label="Image")
|
| 40 |
+
with gr.Row():
|
| 41 |
+
caption_output = gr.Textbox(lines=2, label="Caption")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
clear_button = gr.ClearButton()
|
| 44 |
+
caption_button = gr.Button("Caption", variant="primary")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
example_images = gr.Examples(
|
| 48 |
+
examples=[
|
| 49 |
+
"data/image1.jpg",
|
| 50 |
+
"data/image2.png",
|
| 51 |
+
"data/image3.jpg",
|
| 52 |
+
"data/image4.jpg",
|
| 53 |
+
"data/image5.jpg",
|
| 54 |
+
"data/image6.png",
|
| 55 |
+
"data/image7.png",
|
| 56 |
+
"data/image8.jpeg",
|
| 57 |
+
"data/image9.jpeg",
|
| 58 |
+
"data/image10.jpg",
|
| 59 |
+
],
|
| 60 |
+
inputs=[image_input],
|
| 61 |
+
label="Example Images"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
caption_button.click(fn=caption_image,
|
| 66 |
+
inputs=[image_input],
|
| 67 |
+
outputs=[caption_output]
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
clear_button.click(fn=lambda: [None,""],
|
| 71 |
+
inputs=[],
|
| 72 |
+
outputs=[image_input, caption_output])
|
| 73 |
+
|
| 74 |
+
interface.launch(share=True, server_port=7860)
|
| 75 |
+
|
data/image1.jpg
ADDED
|
Git LFS Details
|
data/image10.jpg
ADDED
|
Git LFS Details
|
data/image2.png
ADDED
|
Git LFS Details
|
data/image3.jpg
ADDED
|
Git LFS Details
|
data/image4.jpg
ADDED
|
Git LFS Details
|
data/image5.jpg
ADDED
|
Git LFS Details
|
data/image6.png
ADDED
|
Git LFS Details
|
data/image7.png
ADDED
|
Git LFS Details
|
data/image8.jpeg
ADDED
|
Git LFS Details
|
data/image9.jpeg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|
| 4 |
+
pillow
|
| 5 |
+
torch
|
| 6 |
+
sentencepiece
|
| 7 |
+
huggingface_hub
|