Spaces:
Runtime error
Runtime error
ShreyMehra
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,25 +2,26 @@ import easyocr as ocr #OCR
|
|
2 |
import streamlit as st #Web App
|
3 |
from PIL import Image #Image Processing
|
4 |
import numpy as np #Image Processing
|
|
|
|
|
5 |
|
6 |
-
#title
|
7 |
-
st.title("Easy OCR - Extract Text from Images")
|
8 |
|
9 |
-
#
|
10 |
-
st.
|
11 |
|
12 |
-
st.markdown("Link to the
|
13 |
|
14 |
#image uploader
|
15 |
image = st.file_uploader(label = "Upload your image here",type=['png','jpg','jpeg'])
|
16 |
|
17 |
|
18 |
@st.cache
|
19 |
-
def load_model():
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
if image is not None:
|
26 |
|
@@ -28,18 +29,17 @@ if image is not None:
|
|
28 |
st.image(input_image) #display image
|
29 |
|
30 |
with st.spinner("π€ AI is at Work! "):
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
result = reader.readtext(np.array(input_image))
|
34 |
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.write(result_text)
|
42 |
-
#st.success("Here you go!")
|
43 |
st.balloons()
|
44 |
else:
|
45 |
st.write("Upload an Image")
|
|
|
2 |
import streamlit as st #Web App
|
3 |
from PIL import Image #Image Processing
|
4 |
import numpy as np #Image Processing
|
5 |
+
from transformers import AutoProcessor, Blip2ForConditionalGeneration
|
6 |
+
import torch
|
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 |
|
18 |
@st.cache
|
19 |
+
def load_model():
|
20 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
21 |
+
model = Blip2ForConditionalGeneration.from_pretrained("Shrey23/Image-Captioning", device_map="auto", load_in_8bit=True)
|
22 |
+
return processor, model
|
23 |
+
|
24 |
+
processor, model = load_model() #load model
|
25 |
|
26 |
if image is not None:
|
27 |
|
|
|
29 |
st.image(input_image) #display image
|
30 |
|
31 |
with st.spinner("π€ AI is at Work! "):
|
32 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
33 |
+
inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)
|
34 |
+
pixel_values = inputs.pixel_values
|
35 |
|
|
|
36 |
|
37 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=25)
|
38 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
39 |
|
40 |
+
st.write(generated_caption)
|
41 |
+
|
42 |
+
st.success("Here you go!")
|
|
|
|
|
|
|
43 |
st.balloons()
|
44 |
else:
|
45 |
st.write("Upload an Image")
|