vama09 commited on
Commit
bb7b80b
·
1 Parent(s): 1903d94

Add application file

Browse files
Files changed (2) hide show
  1. app.py +76 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer
4
+ import itertools
5
+ from nltk.corpus import stopwords
6
+ import nltk
7
+ import easyocr
8
+ import torch
9
+ import numpy as np
10
+ nltk.download('stopwords')
11
+
12
+ # load the model and tokenizer
13
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
14
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to("cuda")
15
+
16
+ tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
17
+ reader = easyocr.Reader(['en'])
18
+
19
+ # set up Streamlit app
20
+ st.set_page_config(layout='wide', page_title='Image Hashtag Recommender')
21
+
22
+ def generate_hashtags(image_file):
23
+ # get image and convert to RGB mode
24
+ image = Image.open(image_file).convert('RGB')
25
+
26
+ # extract image features
27
+ inputs = processor(image, return_tensors="pt").to("cuda")
28
+
29
+ output_ids = model.generate(**inputs)
30
+ # out_text = processor.decode(out[0], skip_special_tokens=True)
31
+
32
+ # decode the model output to text and extract caption words
33
+ output_text = processor.decode(output_ids[0], skip_special_tokens=True)
34
+ caption_words = [word.lower() for word in output_text.split() if not word.startswith("#")]
35
+
36
+ # remove stop words from caption words
37
+ stop_words = set(stopwords.words('english'))
38
+ caption_words = [word for word in caption_words if word not in stop_words]
39
+
40
+ # use easyocr to extract text from the image
41
+ text = reader.readtext(np.array(image))
42
+ detected_text = " ".join([item[1] for item in text])
43
+
44
+ # combine caption words and detected text
45
+ all_words = caption_words + detected_text.split()
46
+
47
+ # generate combinations of words for hashtags
48
+ hashtags = []
49
+ for n in range(1, 4):
50
+ word_combinations = list(itertools.combinations(all_words, n))
51
+ for combination in word_combinations:
52
+ hashtag = "#" + "".join(combination)
53
+ hashtags.append(hashtag)
54
+
55
+ # return top 10 hashtags by frequency
56
+ top_hashtags = [tag for tag in sorted(set(hashtags), key=hashtags.count, reverse=True) if tag != "#"]
57
+ return [top_hashtags[:10], output_text]
58
+
59
+ st.title("Image Caption and HashTag Recommender")
60
+
61
+ image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
62
+
63
+ # if the user has submitted an image, generate hashtags
64
+ if image_file is not None:
65
+ try:
66
+ hashtags = generate_hashtags(image_file)
67
+ if len(hashtags) > 0:
68
+ st.write(f"Caption : {hashtags[1]}")
69
+ st.write("Top 10 hashtags for this image:")
70
+ for tag in hashtags[0]:
71
+ st.write(tag)
72
+ else:
73
+ st.write("No hashtags found for this image.")
74
+ except Exception as e:
75
+ st.write(f"Error: {e}")
76
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ easyocr==1.6.2
2
+ nltk==3.7
3
+ numpy==1.23.5
4
+ Pillow==9.4.0
5
+ Pillow==9.5.0
6
+ streamlit==1.21.0
7
+ torch==2.0.0
8
+ transformers==4.28.1