Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,71 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import numpy as np
|
4 |
-
import tensorflow as tf
|
5 |
-
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
|
6 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
7 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
-
import pickle
|
9 |
-
|
10 |
-
# Load your pre-trained model and tokenizer
|
11 |
-
model = tf.keras.models.load_model("
|
12 |
-
with open("tokenizer.pkl", "rb") as handle:
|
13 |
-
tokenizer = pickle.load(handle)
|
14 |
-
|
15 |
-
# Load your precomputed features if required (else comment out)
|
16 |
-
# with open("features.pkl", "rb") as f:
|
17 |
-
# features = pickle.load(f)
|
18 |
-
|
19 |
-
# Image feature extractor model
|
20 |
-
feature_extractor = VGG16()
|
21 |
-
feature_extractor = tf.keras.Model(feature_extractor.input, feature_extractor.layers[-2].output)
|
22 |
-
|
23 |
-
# Description generation function
|
24 |
-
def generate_caption(image):
|
25 |
-
# Preprocess the image
|
26 |
-
image = image.resize((224, 224))
|
27 |
-
image = img_to_array(image)
|
28 |
-
image = np.expand_dims(image, axis=0)
|
29 |
-
image = preprocess_input(image)
|
30 |
-
|
31 |
-
# Extract features
|
32 |
-
feature = feature_extractor.predict(image, verbose=0)
|
33 |
-
|
34 |
-
# Generate caption (mock example: replace with your real inference loop)
|
35 |
-
input_text = 'startseq'
|
36 |
-
max_length = 34 # set this to your model's max_length
|
37 |
-
|
38 |
-
for _ in range(max_length):
|
39 |
-
sequence = tokenizer.texts_to_sequences([input_text])[0]
|
40 |
-
sequence = pad_sequences([sequence], maxlen=max_length)
|
41 |
-
yhat = model.predict([feature, sequence], verbose=0)
|
42 |
-
yhat = np.argmax(yhat)
|
43 |
-
word = ''
|
44 |
-
for w, i in tokenizer.word_index.items():
|
45 |
-
if i == yhat:
|
46 |
-
word = w
|
47 |
-
break
|
48 |
-
if word == 'endseq' or word == '':
|
49 |
-
break
|
50 |
-
input_text += ' ' + word
|
51 |
-
|
52 |
-
caption = input_text.replace('startseq', '').strip()
|
53 |
-
return caption
|
54 |
-
|
55 |
-
# Gradio Interface
|
56 |
-
title = "📸 Image Caption Generator"
|
57 |
-
description = "Upload an image and let the AI generate a descriptive caption for it."
|
58 |
-
theme = "soft"
|
59 |
-
|
60 |
-
iface = gr.Interface(
|
61 |
-
fn=generate_caption,
|
62 |
-
inputs=gr.Image(type="pil"),
|
63 |
-
outputs=gr.Textbox(label="Generated Caption"),
|
64 |
-
title=title,
|
65 |
-
description=description,
|
66 |
-
theme=theme,
|
67 |
-
allow_flagging="never"
|
68 |
-
)
|
69 |
-
|
70 |
-
if __name__ == "__main__":
|
71 |
-
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
|
6 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
+
import pickle
|
9 |
+
|
10 |
+
# Load your pre-trained model and tokenizer
|
11 |
+
model = tf.keras.models.load_model("caption_model.h5")
|
12 |
+
with open("tokenizer.pkl", "rb") as handle:
|
13 |
+
tokenizer = pickle.load(handle)
|
14 |
+
|
15 |
+
# Load your precomputed features if required (else comment out)
|
16 |
+
# with open("features.pkl", "rb") as f:
|
17 |
+
# features = pickle.load(f)
|
18 |
+
|
19 |
+
# Image feature extractor model
|
20 |
+
feature_extractor = VGG16()
|
21 |
+
feature_extractor = tf.keras.Model(feature_extractor.input, feature_extractor.layers[-2].output)
|
22 |
+
|
23 |
+
# Description generation function
|
24 |
+
def generate_caption(image):
|
25 |
+
# Preprocess the image
|
26 |
+
image = image.resize((224, 224))
|
27 |
+
image = img_to_array(image)
|
28 |
+
image = np.expand_dims(image, axis=0)
|
29 |
+
image = preprocess_input(image)
|
30 |
+
|
31 |
+
# Extract features
|
32 |
+
feature = feature_extractor.predict(image, verbose=0)
|
33 |
+
|
34 |
+
# Generate caption (mock example: replace with your real inference loop)
|
35 |
+
input_text = 'startseq'
|
36 |
+
max_length = 34 # set this to your model's max_length
|
37 |
+
|
38 |
+
for _ in range(max_length):
|
39 |
+
sequence = tokenizer.texts_to_sequences([input_text])[0]
|
40 |
+
sequence = pad_sequences([sequence], maxlen=max_length)
|
41 |
+
yhat = model.predict([feature, sequence], verbose=0)
|
42 |
+
yhat = np.argmax(yhat)
|
43 |
+
word = ''
|
44 |
+
for w, i in tokenizer.word_index.items():
|
45 |
+
if i == yhat:
|
46 |
+
word = w
|
47 |
+
break
|
48 |
+
if word == 'endseq' or word == '':
|
49 |
+
break
|
50 |
+
input_text += ' ' + word
|
51 |
+
|
52 |
+
caption = input_text.replace('startseq', '').strip()
|
53 |
+
return caption
|
54 |
+
|
55 |
+
# Gradio Interface
|
56 |
+
title = "📸 Image Caption Generator"
|
57 |
+
description = "Upload an image and let the AI generate a descriptive caption for it."
|
58 |
+
theme = "soft"
|
59 |
+
|
60 |
+
iface = gr.Interface(
|
61 |
+
fn=generate_caption,
|
62 |
+
inputs=gr.Image(type="pil"),
|
63 |
+
outputs=gr.Textbox(label="Generated Caption"),
|
64 |
+
title=title,
|
65 |
+
description=description,
|
66 |
+
theme=theme,
|
67 |
+
allow_flagging="never"
|
68 |
+
)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
iface.launch()
|