Spaces:
Runtime error
Runtime error
ShreyMehra
commited on
Delete app.py
Browse files
app.py
DELETED
@@ -1,53 +0,0 @@
|
|
1 |
-
import streamlit as st #Web App
|
2 |
-
from PIL import Image #Image Processing
|
3 |
-
import numpy as np #Image Processing
|
4 |
-
from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
5 |
-
import torch
|
6 |
-
from peft import PeftModel, PeftConfig
|
7 |
-
|
8 |
-
|
9 |
-
#title
|
10 |
-
st.title("Image Captioner - Caption the images")
|
11 |
-
|
12 |
-
st.markdown("Link to the model - [Image-to-Caption-App on 🤗 Spaces](https://huggingface.co/spaces/Shrey23/Image-Captioning)")
|
13 |
-
|
14 |
-
#image uploader
|
15 |
-
image = st.file_uploader(label = "Upload your image here",type=['png','jpg','jpeg'])
|
16 |
-
|
17 |
-
@st.cache_data
|
18 |
-
def load_model():
|
19 |
-
peft_model_id = "Shrey23/Image-Captioning"
|
20 |
-
config = PeftConfig.from_pretrained(peft_model_id)
|
21 |
-
model = Blip2ForConditionalGeneration.from_pretrained(config.base_model_name_or_path, torch_dtype=torch.float16) #, device_map="auto", load_in_8bit=True
|
22 |
-
model = PeftModel.from_pretrained(model, peft_model_id)
|
23 |
-
|
24 |
-
processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
25 |
-
return processor, model
|
26 |
-
|
27 |
-
|
28 |
-
processor, model = load_model() #load model
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
if image is not None:
|
33 |
-
|
34 |
-
input_image = Image.open(image) #read image
|
35 |
-
st.image(input_image) #display image
|
36 |
-
|
37 |
-
with st.spinner("🤖 AI is at Work! "):
|
38 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
39 |
-
inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)
|
40 |
-
pixel_values = inputs.pixel_values
|
41 |
-
|
42 |
-
|
43 |
-
generated_ids = model.generate(pixel_values=pixel_values, max_length=25)
|
44 |
-
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
45 |
-
|
46 |
-
st.write(generated_caption)
|
47 |
-
|
48 |
-
st.success("Here you go!")
|
49 |
-
st.balloons()
|
50 |
-
else:
|
51 |
-
st.write("Upload an Image")
|
52 |
-
|
53 |
-
st.caption("Made with ❤️ by @1littlecoder. Credits to 🤗 Spaces for Hosting this ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|