Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
@@ -14,14 +15,12 @@ from sklearn.feature_extraction.text import TfidfVectorizer
|
|
14 |
from sklearn.metrics.pairwise import cosine_similarity
|
15 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
16 |
|
17 |
-
#
|
18 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
19 |
-
|
20 |
-
# Load the feature extractor and tokenizer
|
21 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
22 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
23 |
|
24 |
-
|
25 |
def generate_captions(image):
|
26 |
image = Image.open(image).convert("RGB")
|
27 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
@@ -30,7 +29,7 @@ def generate_captions(image):
|
|
30 |
generated_caption = sentence.replace(text_to_remove, "")
|
31 |
return generated_caption
|
32 |
|
33 |
-
#
|
34 |
def image_text(image):
|
35 |
img_np = np.array(image)
|
36 |
reader = easyocr.Reader(['en'])
|
@@ -47,17 +46,13 @@ stop_words = set(stopwords.words('english'))
|
|
47 |
# Add hashtags to keywords, which have been generated from image captioing
|
48 |
def add_hashtags(keywords):
|
49 |
hashtags = []
|
50 |
-
|
51 |
for keyword in keywords:
|
52 |
-
# Generate hashtag from the keyword (you can modify this part as per your requirements)
|
53 |
hashtag = '#' + keyword.lower()
|
54 |
-
|
55 |
hashtags.append(hashtag)
|
56 |
-
|
57 |
return hashtags
|
58 |
|
|
|
59 |
def trending_hashtags(caption):
|
60 |
-
# Read trending hashtags from a file separated by commas
|
61 |
with open("hashies.txt", "r") as file:
|
62 |
hashtags_string = file.read()
|
63 |
|
@@ -90,21 +85,17 @@ def trending_hashtags(caption):
|
|
90 |
# Sort trending hashtags based on similarity in descending order
|
91 |
sorted_hashtags = [hashtag for _, hashtag in sorted(zip(similarities, df["Hashtags"]), reverse=True)]
|
92 |
|
93 |
-
# Select top k relevant hashtags (e.g., top 5) without duplicates
|
94 |
selected_hashtags = list(set(sorted_hashtags[:5]))
|
95 |
-
|
96 |
selected_hashtag = [word.strip("'") for word in selected_hashtags]
|
97 |
-
|
98 |
return selected_hashtag
|
99 |
|
100 |
-
#
|
101 |
def app():
|
102 |
-
st.title('
|
103 |
-
|
104 |
-
st.write('Upload an image to see what we have in store.')
|
105 |
-
|
106 |
# create file uploader
|
107 |
-
uploaded_file = st.file_uploader("
|
108 |
|
109 |
# check if file has been uploaded
|
110 |
if uploaded_file is not None:
|
@@ -122,19 +113,15 @@ def app():
|
|
122 |
|
123 |
#Final Hashtags Generation
|
124 |
web_hashtags = trending_hashtags(string)
|
125 |
-
|
126 |
combined_hashtags = hashtags + extracted_text + web_hashtags
|
127 |
|
128 |
# Shuffle the list randomly
|
129 |
random.shuffle(combined_hashtags)
|
130 |
-
|
131 |
combined_hashtags = list(set(item for item in combined_hashtags[:15] if not re.search(r'\d$', item)))
|
132 |
|
133 |
-
|
134 |
# display the image
|
135 |
st.image(image, caption='The Uploaded File')
|
136 |
-
st.write("
|
137 |
-
st.write("Magical hashies have arrived : ", combined_hashtags)
|
138 |
|
139 |
# run the app
|
140 |
if __name__ == '__main__':
|
|
|
1 |
+
# Import all necessary libraries and don't forget to check out Dependencies
|
2 |
import streamlit as st
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
15 |
from sklearn.metrics.pairwise import cosine_similarity
|
16 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
17 |
|
18 |
+
# Load the model-pretrained
|
19 |
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
|
|
|
|
20 |
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
21 |
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
22 |
|
23 |
+
# Function to generate captions
|
24 |
def generate_captions(image):
|
25 |
image = Image.open(image).convert("RGB")
|
26 |
generated_caption = tokenizer.decode(model.generate(feature_extractor(image, return_tensors="pt").pixel_values.to("cpu"))[0])
|
|
|
29 |
generated_caption = sentence.replace(text_to_remove, "")
|
30 |
return generated_caption
|
31 |
|
32 |
+
# kinda-Function easyocr to extract text from the image
|
33 |
def image_text(image):
|
34 |
img_np = np.array(image)
|
35 |
reader = easyocr.Reader(['en'])
|
|
|
46 |
# Add hashtags to keywords, which have been generated from image captioing
|
47 |
def add_hashtags(keywords):
|
48 |
hashtags = []
|
|
|
49 |
for keyword in keywords:
|
|
|
50 |
hashtag = '#' + keyword.lower()
|
|
|
51 |
hashtags.append(hashtag)
|
|
|
52 |
return hashtags
|
53 |
|
54 |
+
# function to get and add trending Hashtags
|
55 |
def trending_hashtags(caption):
|
|
|
56 |
with open("hashies.txt", "r") as file:
|
57 |
hashtags_string = file.read()
|
58 |
|
|
|
85 |
# Sort trending hashtags based on similarity in descending order
|
86 |
sorted_hashtags = [hashtag for _, hashtag in sorted(zip(similarities, df["Hashtags"]), reverse=True)]
|
87 |
|
88 |
+
# Select top k relevant hashtags (e.g., top 5) without duplicates and return them
|
89 |
selected_hashtags = list(set(sorted_hashtags[:5]))
|
|
|
90 |
selected_hashtag = [word.strip("'") for word in selected_hashtags]
|
|
|
91 |
return selected_hashtag
|
92 |
|
93 |
+
# Streamlit app Creation
|
94 |
def app():
|
95 |
+
st.title('Have a :green[Bueatiful pic!] Looking for :orange[Trending Hashtags to post it on your social handle?]. Here is some Help')
|
96 |
+
|
|
|
|
|
97 |
# create file uploader
|
98 |
+
uploaded_file = st.file_uploader("Upload Picture of your wish!, :violet[magic on the Way! ]", type=["jpg", "jpeg", "png"])
|
99 |
|
100 |
# check if file has been uploaded
|
101 |
if uploaded_file is not None:
|
|
|
113 |
|
114 |
#Final Hashtags Generation
|
115 |
web_hashtags = trending_hashtags(string)
|
|
|
116 |
combined_hashtags = hashtags + extracted_text + web_hashtags
|
117 |
|
118 |
# Shuffle the list randomly
|
119 |
random.shuffle(combined_hashtags)
|
|
|
120 |
combined_hashtags = list(set(item for item in combined_hashtags[:15] if not re.search(r'\d$', item)))
|
121 |
|
|
|
122 |
# display the image
|
123 |
st.image(image, caption='The Uploaded File')
|
124 |
+
st.write("Magical hashies have arrived* :sparkles: ", combined_hashtags)
|
|
|
125 |
|
126 |
# run the app
|
127 |
if __name__ == '__main__':
|