MANIKANDAN A commited on
Commit
9aeba21
1 Parent(s): c41b62c

Create orginal.py

Browse files
Files changed (1) hide show
  1. orginal.py +72 -0
orginal.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ import streamlit as st
4
+ import requests
5
+ from PIL import Image
6
+ from model import get_caption_model, generate_caption
7
+ from googletrans import Translator
8
+
9
+ translator = Translator()
10
+
11
+ @st.cache_resource
12
+ def get_model():
13
+ return get_caption_model()
14
+
15
+ caption_model = get_model()
16
+
17
+ def translate_caption(caption, target_language='en'):
18
+ translated = translator.translate(caption, dest=target_language)
19
+ return translated.text
20
+
21
+ def predict(cap_col):
22
+ captions = []
23
+ pred_caption = generate_caption('tmp.jpg', caption_model)
24
+
25
+ cap_col.markdown('#### Predicted Captions:')
26
+ translated_caption = translate_caption(pred_caption, target_language)
27
+ captions.append(translated_caption)
28
+
29
+ for _ in range(4):
30
+ pred_caption = generate_caption('tmp.jpg', caption_model, add_noise=True)
31
+ if pred_caption not in captions:
32
+ translated_caption = translate_caption(pred_caption, target_language)
33
+ captions.append(translated_caption)
34
+
35
+ cap_col.markdown('<div class="caption-container">', unsafe_allow_html=True)
36
+ for c in captions:
37
+ cap_col.markdown(f'<div class="cap-line" style="color: black; background-color: light grey; padding: 5px; margin-bottom: 5px; font-family: \'Palatino Linotype\', \'Book Antiqua\', Palatino, serif;">{c}</div>', unsafe_allow_html=True)
38
+ cap_col.markdown('</div>', unsafe_allow_html=True)
39
+
40
+ st.markdown('<h1 style="text-align:center; font-family:Arial; width:fit-content; font-size:3em; color:black; text-shadow: 2px 2px 4px #000000;">IMAGE CAPTION GENERATOR</h1>', unsafe_allow_html=True)
41
+ col1, col2 = st.columns(2)
42
+
43
+ # Image URL input
44
+ img_url = st.text_input(label='Enter Image URL')
45
+
46
+ # Image upload input
47
+ img_upload = st.file_uploader(label='Upload Image', type=['jpg', 'png', 'jpeg'])
48
+
49
+ # Language selection dropdown
50
+ target_language = st.selectbox('Select Target Language', ['en', 'ta', 'hi', 'es', 'fr', 'zh-cn'], index=0)
51
+
52
+ # Process image and generate captions
53
+ if img_url:
54
+ img = Image.open(requests.get(img_url, stream=True).raw)
55
+ img = img.convert('RGB')
56
+ col1.image(img, caption="Input Image", use_column_width=True)
57
+ img.save('tmp.jpg')
58
+ predict(col2)
59
+
60
+ st.markdown('<center style="opacity: 70%">OR</center>', unsafe_allow_html=True)
61
+
62
+ elif img_upload:
63
+ img = img_upload.read()
64
+ img = Image.open(io.BytesIO(img))
65
+ img = img.convert('RGB')
66
+ col1.image(img, caption="Input Image", use_column_width=True)
67
+ img.save('tmp.jpg')
68
+ predict(col2)
69
+
70
+ # Remove temporary image file
71
+ if img_url or img_upload:
72
+ os.remove('tmp.jpg')