Spaces:
Sleeping
Sleeping
Download app.py from wqerer/aicaptiongenerator: direct link, hf CLI and curl.
- Browser
- Download file 850 Bytes
-
https://huggingface.co/spaces/wqerer/aicaptiongenerator/resolve/main/app.py
- Command line
-
hf download hf://spaces/wqerer/aicaptiongenerator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/wqerer/aicaptiongenerator/resolve/main/app.py
850 Bytes
| from transformers import BlipProcessor, BlipForConditionalGeneration | |
| import gradio as gr | |
| from PIL import Image | |
| import torch | |
| # Load model | |
| processor = BlipProcessor.from_pretrained( | |
| "Salesforce/blip-image-captioning-base" | |
| ) | |
| model = BlipForConditionalGeneration.from_pretrained( | |
| "Salesforce/blip-image-captioning-base" | |
| ) | |
| # Caption function | |
| def generate_caption(image): | |
| inputs = processor(images=image, return_tensors="pt") | |
| output = model.generate(**inputs) | |
| caption = processor.decode( | |
| output[0], | |
| skip_special_tokens=True | |
| ) | |
| return caption | |
| # Interface | |
| demo = gr.Interface( | |
| fn=generate_caption, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.Textbox(label="Generated Caption"), | |
| title="AI Image Caption Generator", | |
| description="Upload an image and AI will generate a caption." | |
| ) | |
| demo.launch() |