Lokesh1024 commited on
Commit
b5e8600
·
verified ·
1 Parent(s): 48445b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -5,49 +5,58 @@ import requests
5
  from PIL import Image
6
  from model import get_caption_model, generate_caption
7
 
8
-
9
- @st.cache_resource(allow_output_mutation=True)
10
  def get_model():
11
  return get_caption_model()
12
 
13
  caption_model = get_model()
14
 
15
-
16
  def predict():
17
  captions = []
18
- pred_caption = generate_caption('tmp.jpg', caption_model)
19
-
20
- st.markdown('#### Predicted Captions:')
21
- captions.append(pred_caption)
22
-
23
- for _ in range(4):
24
- pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
25
- if pred_caption not in captions:
26
- captions.append(pred_caption)
27
-
28
- for c in captions:
29
- st.write(c)
 
 
30
 
31
  st.title('Image Captioner')
32
- img_url = st.text_input(label='Enter Image URL')
33
 
34
- if (img_url != "") and (img_url != None):
35
- img = Image.open(requests.get(img_url, stream=True).raw)
36
- img = img.convert('RGB')
37
- st.image(img)
38
- img.save('tmp.jpg')
39
- predict()
40
- os.remove('tmp.jpg')
41
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
 
 
44
  img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
45
 
46
- if img_upload != None:
47
- img = img_upload.read()
48
- img = Image.open(io.BytesIO(img))
49
- img = img.convert('RGB')
50
- img.save('tmp.jpg')
51
- st.image(img)
52
- predict()
53
- os.remove('tmp.jpg')
 
 
 
5
  from PIL import Image
6
  from model import get_caption_model, generate_caption
7
 
8
+ # Use st.experimental_memo instead of st.cache_resource
9
+ @st.experimental_memo(allow_output_mutation=True)
10
  def get_model():
11
  return get_caption_model()
12
 
13
  caption_model = get_model()
14
 
 
15
  def predict():
16
  captions = []
17
+ try:
18
+ pred_caption = generate_caption('tmp.jpg', caption_model)
19
+ st.markdown('#### Predicted Captions:')
20
+ captions.append(pred_caption)
21
+
22
+ for _ in range(4):
23
+ pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
24
+ if pred_caption not in captions:
25
+ captions.append(pred_caption)
26
+
27
+ for c in captions:
28
+ st.write(c)
29
+ except Exception as e:
30
+ st.error(f"An error occurred during prediction: {e}")
31
 
32
  st.title('Image Captioner')
 
33
 
34
+ # Handle image URL input
35
+ img_url = st.text_input(label='Enter Image URL')
 
 
 
 
 
36
 
37
+ if img_url:
38
+ try:
39
+ img = Image.open(requests.get(img_url, stream=True).raw)
40
+ img = img.convert('RGB')
41
+ st.image(img)
42
+ img.save('tmp.jpg')
43
+ predict()
44
+ os.remove('tmp.jpg')
45
+ except Exception as e:
46
+ st.error(f"An error occurred with the image URL: {e}")
47
 
48
  st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
49
+
50
+ # Handle image file upload
51
  img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
52
 
53
+ if img_upload:
54
+ try:
55
+ img = Image.open(io.BytesIO(img_upload.read()))
56
+ img = img.convert('RGB')
57
+ img.save('tmp.jpg')
58
+ st.image(img)
59
+ predict()
60
+ os.remove('tmp.jpg')
61
+ except Exception as e:
62
+ st.error(f"An error occurred with the image upload: {e}")