Spaces:
Sleeping
Sleeping
Commit
·
4f2700e
1
Parent(s):
42246b0
add application files
Browse files- .gitattributes +2 -0
- .gitignore +1 -0
- app.py +95 -0
- example1.jpg +0 -0
- example2.jpg +0 -0
- models/image-caption-model.keras +3 -0
- requirements.txt +4 -0
- tokenizer.p +3 -0
.gitattributes
CHANGED
|
@@ -5,7 +5,9 @@
|
|
| 5 |
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 9 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
|
|
|
| 5 |
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.p filter=lfs diff=lfs merge=lfs -text
|
| 9 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.keras filter=lfs diff=lfs merge=lfs -text
|
| 11 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 12 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 13 |
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.venv
|
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 3 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 4 |
+
from keras.applications.xception import Xception
|
| 5 |
+
from keras.models import load_model
|
| 6 |
+
from pickle import load
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import os
|
| 10 |
+
import zipfile
|
| 11 |
+
|
| 12 |
+
# --- Model and Tokenizer Loading ---
|
| 13 |
+
|
| 14 |
+
# MODIFIED extract_features function: It now accepts a PIL Image object directly
|
| 15 |
+
def extract_features(image_obj, model):
|
| 16 |
+
# Resize the image to the required dimensions
|
| 17 |
+
image = image_obj.resize((299, 299))
|
| 18 |
+
image = np.array(image)
|
| 19 |
+
|
| 20 |
+
# Handle RGBA images (4 channels) by converting to RGB (3 channels)
|
| 21 |
+
if image.shape[2] == 4:
|
| 22 |
+
image = image[..., :3]
|
| 23 |
+
|
| 24 |
+
# Preprocess the image for the Xception model
|
| 25 |
+
image = np.expand_dims(image, axis=0)
|
| 26 |
+
image = image / 127.5
|
| 27 |
+
image = image - 1.0
|
| 28 |
+
|
| 29 |
+
# Extract features
|
| 30 |
+
feature = model.predict(image, verbose=0)
|
| 31 |
+
return feature
|
| 32 |
+
|
| 33 |
+
def word_for_id(integer, tokenizer):
|
| 34 |
+
for word, index in tokenizer.word_index.items():
|
| 35 |
+
if index == integer:
|
| 36 |
+
return word
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
def generate_desc(model, tokenizer, photo, max_length):
|
| 40 |
+
in_text = 'start'
|
| 41 |
+
for i in range(max_length):
|
| 42 |
+
sequence = tokenizer.texts_to_sequences([in_text])[0]
|
| 43 |
+
sequence = pad_sequences([sequence], maxlen=max_length, padding='post')
|
| 44 |
+
pred = model.predict([photo, sequence], verbose=0)
|
| 45 |
+
pred = np.argmax(pred)
|
| 46 |
+
word = word_for_id(pred, tokenizer)
|
| 47 |
+
if word is None:
|
| 48 |
+
break
|
| 49 |
+
in_text += ' ' + word
|
| 50 |
+
if word == 'end':
|
| 51 |
+
break
|
| 52 |
+
# Clean up the output string
|
| 53 |
+
final_desc = in_text.split()
|
| 54 |
+
final_desc = final_desc[1:-1] # Remove 'start' and 'end'
|
| 55 |
+
final_desc = ' '.join(final_desc)
|
| 56 |
+
return final_desc
|
| 57 |
+
|
| 58 |
+
# --- Load Model and Tokenizer ---
|
| 59 |
+
if not os.path.exists('models'):
|
| 60 |
+
print("Models directory not found. Extracting models.zip...")
|
| 61 |
+
with zipfile.ZipFile("models.zip", 'r') as zip_ref:
|
| 62 |
+
zip_ref.extractall(".")
|
| 63 |
+
print("Extraction complete.")
|
| 64 |
+
|
| 65 |
+
max_length = 34
|
| 66 |
+
tokenizer = load(open("tokenizer.p", "rb"))
|
| 67 |
+
xception_model = Xception(include_top=False, pooling="avg")
|
| 68 |
+
# Use a loaded model from the notebook
|
| 69 |
+
# This assumes you have a model file named 'image-caption-model.keras' in the 'models' directory
|
| 70 |
+
model = load_model('models/image-caption-model.keras')
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# --- Gradio Interface ---
|
| 74 |
+
|
| 75 |
+
# MODIFIED predict function: It now passes the PIL image object directly
|
| 76 |
+
def predict(image):
|
| 77 |
+
# The 'image' argument is now a PIL Image object
|
| 78 |
+
photo = extract_features(image, xception_model)
|
| 79 |
+
description = generate_desc(model, tokenizer, photo, max_length)
|
| 80 |
+
return description
|
| 81 |
+
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=predict,
|
| 84 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 85 |
+
outputs=gr.Textbox(label="Generated Caption"),
|
| 86 |
+
title="Image Caption Generator",
|
| 87 |
+
description="Upload an image and see the generated caption.",
|
| 88 |
+
examples=[
|
| 89 |
+
["example1.jpg"],
|
| 90 |
+
["example2.jpg"],
|
| 91 |
+
]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
iface.launch()
|
example1.jpg
ADDED
|
example2.jpg
ADDED
|
models/image-caption-model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8d7f2da7253524fa437bec55fc3a7579fe93a4a244b19af8550ea843f4aa2d1
|
| 3 |
+
size 20053970
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
tensorflow
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
tokenizer.p
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4b4cdabcc91588e1aa60e9fff30e6e4b785f4d661f2d17e7952fcdece1d5610b
|
| 3 |
+
size 299505
|