Upyaya commited on
Commit
3d3369a
1 Parent(s): f29da3c

Clean previous upload to try new sample

Browse files
Files changed (1) hide show
  1. app.py +49 -45
app.py CHANGED
@@ -58,70 +58,74 @@ st.caption("So, for this project have downloaded the pre-trained model [ybelkada
58
  st.caption("For more detail: [Github link](https://github.com/SmithaUpadhyaya/fashion_image_caption)") #write
59
 
60
  #Select few sample images for the catagory of cloths
61
- st.caption("Select image:")
62
- option = st.selectbox('From sample', ('None', 'dress', 'earrings', 'sweater', 'sunglasses', 'shoe', 'hat', 'heels', 'socks', 'tee', 'bracelet'), index = 0)
63
- st.text("Or")
64
- file_name = st.file_uploader(label = "Upload an image", accept_multiple_files = False)
65
 
 
 
 
 
 
 
 
 
 
66
 
67
- btn_click = st.button('Generate')
68
- st.caption("Application deployed on CPU basic with 16GB RAM")
69
 
70
- if btn_click:
 
71
 
72
- if file_name is not None:
73
 
74
- image = Image.open(file_name)
 
75
 
76
- elif option is not 'None':
77
 
78
- file_name = os.path.join(sample_img_path, map_sampleid_name[option])
79
- image = Image.open(file_name)
80
 
81
- if image is not None:
 
82
 
83
- image_col, caption_text = st.columns(2)
84
- image_col.header("Image")
85
- caption_text.header("Generated Caption")
86
- image_col.image(image.resize((252,252)), use_column_width = True)
87
 
88
- if 'init_model_required' not in st.session_state:
89
- with st.spinner('Initializing model...'):
 
 
90
 
91
- init_model_required = True
92
- processor, model, init_model_required = init_model(init_model_required)
93
 
94
- #Save session init model in session state
95
- if 'init_model_required' not in st.session_state:
96
- st.session_state.init_model_required = init_model_required
97
- st.session_state.processor = processor
98
- st.session_state.model = model
99
- else:
100
- processor = st.session_state.processor
101
- model = st.session_state.model
102
 
103
- with st.spinner('Generating Caption...'):
 
 
 
 
 
 
 
104
 
105
- #Preprocess the image
106
- #Inferance on GPU. When used this on GPU will get errors like: "slow_conv2d_cpu" not implemented for 'Half'" , " Input type (float) and bias type (struct c10::Half)"
107
- #inputs = processor(images = image, return_tensors = "pt").to('cuda', torch.float16)
108
 
109
- #Inferance on CPU
110
- inputs = processor(images = image, return_tensors = "pt")
 
111
 
112
- pixel_values = inputs.pixel_values
 
113
 
114
- #Predict the caption for the imahe
115
- generated_ids = model.generate(pixel_values = pixel_values, max_length = 25)
116
- generated_caption = processor.batch_decode(generated_ids, skip_special_tokens = True)[0]
117
 
118
- #Output the predict text
119
- caption_text.text(generated_caption)
 
120
 
121
- #Reset the variable
122
- option = 'None'
123
- image = None
124
- file_name = None
125
 
126
  #if __name__ == "__main__":
127
  # main()
 
58
  st.caption("For more detail: [Github link](https://github.com/SmithaUpadhyaya/fashion_image_caption)") #write
59
 
60
  #Select few sample images for the catagory of cloths
61
+ with st.form("app", clear_on_submit = True):
 
 
 
62
 
63
+ st.caption("Select image:")
64
+
65
+ option = 'None'
66
+ option = st.selectbox('From sample', ('None', 'dress', 'earrings', 'sweater', 'sunglasses', 'shoe', 'hat', 'heels', 'socks', 'tee', 'bracelet'), index = 0)
67
+
68
+ st.text("Or")
69
+
70
+ file_name = None
71
+ file_name = st.file_uploader(label = "Upload an image", accept_multiple_files = False)
72
 
 
 
73
 
74
+ btn_click = st.form_submit_button('Generate')
75
+ st.caption("Application deployed on CPU basic with 16GB RAM")
76
 
77
+ if btn_click:
78
 
79
+ image = None
80
+ if file_name is not None:
81
 
82
+ image = Image.open(file_name)
83
 
84
+ elif option is not 'None':
 
85
 
86
+ file_name = os.path.join(sample_img_path, map_sampleid_name[option])
87
+ image = Image.open(file_name)
88
 
89
+ if image is not None:
 
 
 
90
 
91
+ image_col, caption_text = st.columns(2)
92
+ image_col.header("Image")
93
+ caption_text.header("Generated Caption")
94
+ image_col.image(image.resize((252,252)), use_column_width = True)
95
 
96
+ if 'init_model_required' not in st.session_state:
97
+ with st.spinner('Initializing model...'):
98
 
99
+ init_model_required = True
100
+ processor, model, init_model_required = init_model(init_model_required)
 
 
 
 
 
 
101
 
102
+ #Save session init model in session state
103
+ if 'init_model_required' not in st.session_state:
104
+ st.session_state.init_model_required = init_model_required
105
+ st.session_state.processor = processor
106
+ st.session_state.model = model
107
+ else:
108
+ processor = st.session_state.processor
109
+ model = st.session_state.model
110
 
111
+ with st.spinner('Generating Caption...'):
 
 
112
 
113
+ #Preprocess the image
114
+ #Inferance on GPU. When used this on GPU will get errors like: "slow_conv2d_cpu" not implemented for 'Half'" , " Input type (float) and bias type (struct c10::Half)"
115
+ #inputs = processor(images = image, return_tensors = "pt").to('cuda', torch.float16)
116
 
117
+ #Inferance on CPU
118
+ inputs = processor(images = image, return_tensors = "pt")
119
 
120
+ pixel_values = inputs.pixel_values
 
 
121
 
122
+ #Predict the caption for the imahe
123
+ generated_ids = model.generate(pixel_values = pixel_values, max_length = 25)
124
+ generated_caption = processor.batch_decode(generated_ids, skip_special_tokens = True)[0]
125
 
126
+ #Output the predict text
127
+ caption_text.text(generated_caption)
128
+
 
129
 
130
  #if __name__ == "__main__":
131
  # main()