Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import clip_model
|
| 2 |
+
from clip_model import clip_image_search
|
| 3 |
+
from clip_model import get_image_embeddings
|
| 4 |
+
from clip_model import make_train_valid_dfs
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import subprocess
|
| 9 |
+
import zipfile
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
image_path = "./Images"
|
| 13 |
+
captions_path = "."
|
| 14 |
+
data_source = 'flickr8k.zip'
|
| 15 |
+
|
| 16 |
+
print("\n\n")
|
| 17 |
+
print("Going to unzip dataset")
|
| 18 |
+
with zipfile.ZipFile(data_source, 'r') as zip_ref:
|
| 19 |
+
zip_ref.extractall('.')
|
| 20 |
+
print("unzip of dataset is done")
|
| 21 |
+
|
| 22 |
+
#=============================================
|
| 23 |
+
|
| 24 |
+
cmd = "pwd"
|
| 25 |
+
output1 = subprocess.check_output(cmd, shell=True).decode("utf-8")
|
| 26 |
+
print("result of pwd command")
|
| 27 |
+
print(output1)
|
| 28 |
+
|
| 29 |
+
print("Going to prepare captions.csv")
|
| 30 |
+
df = pd.read_csv("captions.txt")
|
| 31 |
+
df['id'] = [id_ for id_ in range(df.shape[0] // 5) for _ in range(5)]
|
| 32 |
+
df.to_csv("captions.csv", index=False)
|
| 33 |
+
df = pd.read_csv("captions.csv")
|
| 34 |
+
print("Finished in preparing captions.csv")
|
| 35 |
+
print("\n\n")
|
| 36 |
+
|
| 37 |
+
print("Going to invoke make_train_valid_dfs")
|
| 38 |
+
_, valid_df = make_train_valid_dfs()
|
| 39 |
+
print("Going to invoke make_train_valid_dfs")
|
| 40 |
+
model, image_embeddings = get_image_embeddings(valid_df, "best.pt")
|
| 41 |
+
|
| 42 |
+
def generate_images(text, num_images=6):
|
| 43 |
+
|
| 44 |
+
# # Generate image embeddings
|
| 45 |
+
# Generate images using a suitable image generation model (not included here)
|
| 46 |
+
# generated_images = clip_image_search(text)
|
| 47 |
+
generated_images = clip_image_search(model,
|
| 48 |
+
image_embeddings,
|
| 49 |
+
query="desert food",
|
| 50 |
+
image_filenames=valid_df['image'].values,
|
| 51 |
+
n=9)
|
| 52 |
+
|
| 53 |
+
return generated_images
|
| 54 |
+
|
| 55 |
+
# Gradio interface
|
| 56 |
+
def create_demo():
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
text_input = gr.Textbox(label="Enter text")
|
| 59 |
+
submit_button = gr.Button("Generate Images")
|
| 60 |
+
image_gallery = gr.Gallery(label="Generated Images")
|
| 61 |
+
|
| 62 |
+
def generate_and_update(text):
|
| 63 |
+
if text:
|
| 64 |
+
generated_images = generate_images(text)
|
| 65 |
+
else:
|
| 66 |
+
generated_images = [] # Handle empty input
|
| 67 |
+
return generated_images
|
| 68 |
+
|
| 69 |
+
submit_button.click(fn=generate_and_update, inputs=text_input, outputs=image_gallery)
|
| 70 |
+
return demo
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo = create_demo()
|
| 75 |
+
demo.launch()
|