File size: 4,190 Bytes
c402060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Flask,request
import google.generativeai as palm
import re
import pickle
import numpy as np
import requests
from PIL import Image
from io import BytesIO
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.layers import Input, Dense, LSTM, Embedding, Dropout, add
from tensorflow.keras.models import load_model


#tokenizer=pickle.load(open('tokenizer.pkl','rb'))
#vgg_model = load_model('vgg_model.h5')
model = load_model('best_model.h5')
max_len=35


with open('captions.txt','r') as f:
    next(f)
    caption_file=f.read()

captions={}
for line in caption_file.split('\n'):
    values=line.split(",")
    if(len(line)<2):
        continue
    #get image_id
    image_id=values[0]
    image_id=image_id.split('.')[0]
    #get caption
    caption=values[1:]
    caption=" ".join(caption)
    #mapping caption
    if image_id not in captions:
        captions[image_id]=[]
    captions[image_id].append(caption)

def clean(captions):
    for key,caption_ in captions.items():
        for i in range(len(caption_)):
            caption=caption_[i]
            #process caption
            caption=caption.lower()
            caption = re.sub('[^a-zA-Z]', ' ', caption)
            caption = re.sub('\s+', ' ', caption)
            caption=" ".join([word for word in caption.split() if len(word)>1])
            caption="startseq "+caption+" endseq"
            caption_[i]=caption

clean(captions)

all_captions=[]
for key,caption_ in captions.items():
        for i in range(len(caption_)):
            all_captions.append(caption_[i])

tokenizer=Tokenizer()
tokenizer.fit_on_texts(all_captions)

# load vgg16 model
vgg_model = VGG16()
# restructure the model
vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-2].output)

def index_to_word(indx,tokenizer):
  for word,index in tokenizer.word_index.items():
    if index == indx:
      return word
  return None

def predict_captions(model,image,tokenizer,max_len):
  in_text='startseq'
  for i in range(max_len):
    seq=tokenizer.texts_to_sequences([in_text])[0]
    seq=pad_sequences([seq],max_len)[0]
    if len(image.shape) == 3:
            image = np.expand_dims(image, axis=0)
    y_pred=model.predict([image, np.expand_dims(seq, axis=0)],verbose=0)
    y_pred=np.argmax(y_pred)

    word=index_to_word(y_pred,tokenizer)
    if word == None:
      break
    in_text += " " + word
    if word == 'endseq':
      break
  return in_text

def caption_generator(url):
    #load image
    response = requests.get(url)
    image= Image.open(BytesIO(response.content))
    image = image.resize((224,224))
    #convert image into numpy array
    image=img_to_array(image)
    #reshape image
    image=image.reshape((1,image.shape[0],image.shape[1],image.shape[2]))
    #preprrocess image for vgg16
    image=preprocess_input(image)
    #extract features
    feature=vgg_model.predict(image,verbose=0)
    y_pred = predict_captions(model, feature, tokenizer, max_len)
    #plt.imshow(image_pic)
    return y_pred

app=Flask(__name__)

@app.route('/')
def home():
    return "HELLO WORLD"

@app.route('/predict',methods=['POST'])
def predict():
    url=request.get_json()
    print(url)
    result=caption_generator(url['url'])
    palm.configure(api_key='AIzaSyDDXOjF1BBgJM6g1tMV-6tcI7xh9-ctvQU')
    #models = [m for m in palm.list_models() if 'generateText' in m.supported_generation_methods]
    #model = models[0].name
    model="models/text-bison-001"
    prompt = "Generate a creative & attractive instagram caption of 10-30 words words for" + str(result)
    completion = palm.generate_text(
        model=model,
        prompt=prompt,
        temperature=0,
        # The maximum length of the response
        max_output_tokens=100,
    )
    return completion.result
    #return {'caption':str(result)}

if __name__ == '__main__':
    app.run(debug=True)