Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
#import torch
|
4 |
+
#from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
+
|
6 |
+
def set_page_config():
|
7 |
+
st.set_page_config(
|
8 |
+
page_title='Caption an Image',
|
9 |
+
page_icon=':camera:',
|
10 |
+
layout='wide',
|
11 |
+
)
|
12 |
+
|
13 |
+
#def initialize_model():
|
14 |
+
# hf_model = "Salesforce/blip-image-captioning-large"
|
15 |
+
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
16 |
+
# processor = BlipProcessor.from_pretrained(hf_model)
|
17 |
+
# model = BlipForConditionalGeneration.from_pretrained(hf_model).to(device) # type: ignore
|
18 |
+
# return processor, model, device
|
19 |
+
|
20 |
+
def upload_image():
|
21 |
+
return st.sidebar.file_uploader("Upload an image (we aren't storing anything)", type=["jpg", "jpeg", "png"])
|
22 |
+
|
23 |
+
def resize_image(image, max_width):
|
24 |
+
width, height = image.size
|
25 |
+
if width > max_width:
|
26 |
+
ratio = max_width / width
|
27 |
+
height = int(height * ratio)
|
28 |
+
image = image.resize((max_width, height))
|
29 |
+
return image
|
30 |
+
|
31 |
+
def generate_caption(processor, model, device, image):
|
32 |
+
#inputs = processor(image, return_tensors='pt').to(device)
|
33 |
+
#out = model.generate(**inputs, max_new_tokens=20)
|
34 |
+
#caption = processor.decode(out[0], skip_special_tokens=True)
|
35 |
+
caption="im here "
|
36 |
+
return caption
|
37 |
+
|
38 |
+
def main():
|
39 |
+
set_page_config()
|
40 |
+
st.header("Caption an Image :camera:")
|
41 |
+
|
42 |
+
uploaded_image = upload_image()
|
43 |
+
|
44 |
+
if uploaded_image is not None:
|
45 |
+
image = Image.open(uploaded_image)
|
46 |
+
image = resize_image(image, max_width=300)
|
47 |
+
|
48 |
+
st.image(image, caption='Your image')
|
49 |
+
|
50 |
+
with st.sidebar:
|
51 |
+
st.divider()
|
52 |
+
if st.sidebar.button('Generate Caption'):
|
53 |
+
with st.spinner('Generating caption...'):
|
54 |
+
#processor, model, device = initialize_model()
|
55 |
+
#caption = generate_caption(processor, model, device, image)
|
56 |
+
caption="im here man"
|
57 |
+
st.header("Caption:")
|
58 |
+
st.markdown(f'**{caption}**')
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == '__main__':
|
63 |
+
main()
|
64 |
+
|
65 |
+
|
66 |
+
st.markdown("""
|
67 |
+
---
|
68 |
+
You are looking at Finetuned image Caption model """)
|
69 |
+
|
70 |
+
|