Commit
·
78724b5
1
Parent(s):
02da79d
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,90 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Use the Euler scheduler here instead
|
| 7 |
-
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
|
| 8 |
-
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, revision="fp16", torch_dtype=torch.float16)
|
| 9 |
-
pipe = pipe.to("cuda")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
image.save("sd_image.png")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from tensorflow.keras.applications.inception_v3 import InceptionV3, preprocess_input
|
| 7 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
| 8 |
+
from transformers import TFAutoModelWithLMHead, AutoTokenizer
|
| 9 |
+
from stylegan2.tf_api import G_synthesis as StyleGAN2
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Load pre-trained image captioning model
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-large")
|
| 14 |
+
model = TFAutoModelWithLMHead.from_pretrained("t5-large")
|
| 15 |
|
| 16 |
+
# Load pre-trained StyleGAN2 model
|
| 17 |
+
g = StyleGAN2()
|
| 18 |
+
g.load_weights('models/stylegan2-ffhq-config-f.pkl')
|
| 19 |
|
| 20 |
+
# Load pre-trained InceptionV3 model for image preprocessing
|
| 21 |
+
inception_v3 = InceptionV3(weights='imagenet')
|
|
|
|
| 22 |
|
| 23 |
+
# Define function to preprocess image for GAN
|
| 24 |
+
def preprocess_image(image):
|
| 25 |
+
image = image.resize((256, 256))
|
| 26 |
+
image_array = img_to_array(image)
|
| 27 |
+
image_array = preprocess_input(image_array)
|
| 28 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 29 |
+
return image_array
|
| 30 |
|
| 31 |
+
# Define function to generate image from text using StyleGAN2
|
| 32 |
+
def generate_image(description):
|
| 33 |
+
z = tf.random.normal([1, g.input_shape[1]])
|
| 34 |
+
text = "generate image of a " + description
|
| 35 |
+
input_ids = tokenizer.encode(text, return_tensors='tf')
|
| 36 |
+
output = model.generate(input_ids=input_ids)
|
| 37 |
+
caption = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 38 |
+
image = g(z, caption)
|
| 39 |
+
image = (image.numpy()[0] * 255).astype(np.uint8)
|
| 40 |
+
image = Image.fromarray(image, mode='RGB')
|
| 41 |
+
return image
|
| 42 |
|
| 43 |
+
# Define function to generate text description of uploaded image using InceptionV3 and T5
|
| 44 |
+
def generate_description(image_file):
|
| 45 |
+
image = Image.open(BytesIO(image_file.read()))
|
| 46 |
+
image = preprocess_image(image)
|
| 47 |
+
features = inception_v3.predict(image)
|
| 48 |
+
features = tf.keras.backend.flatten(features)
|
| 49 |
+
input_text = tokenizer.encode("generate a description of an image", return_tensors="tf")
|
| 50 |
+
output = model.generate(input_ids=input_text, attention_mask=tf.ones(input_text.shape), max_length=50)
|
| 51 |
+
caption = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 52 |
+
return caption
|
| 53 |
|
| 54 |
+
# Define functionto create the web application using Gradio
|
| 55 |
+
def image_generation(text_input, image_file):
|
| 56 |
+
if image_file is not None:
|
| 57 |
+
# Generate text description of uploaded image
|
| 58 |
+
description = generate_description(image_file)
|
| 59 |
+
# Generate image from text description
|
| 60 |
+
generated_image = generate_image(description)
|
| 61 |
+
else:
|
| 62 |
+
# Generate image from user input text
|
| 63 |
+
generated_image = generate_image(text_input)
|
| 64 |
+
return generated_image
|
| 65 |
|
| 66 |
+
#Define Gradio interface
|
| 67 |
+
inputs = [gr.inputs.Textbox(label="Input text"),
|
| 68 |
+
gr.inputs.Image(label="Upload an image (optional)")
|
| 69 |
+
]
|
| 70 |
+
outputs = gr.outputs.Image(label="Generated Image")
|
| 71 |
+
|
| 72 |
+
gr.Interface(
|
| 73 |
+
fn=image_generation,
|
| 74 |
+
inputs=inputs,
|
| 75 |
+
outputs=outputs,
|
| 76 |
+
title="Image Generation from Text",
|
| 77 |
+
description="Generate high-quality images from text descriptions.",
|
| 78 |
+
theme="default",
|
| 79 |
+
layout="vertical",
|
| 80 |
+
examples=[
|
| 81 |
+
["a red sports car on a mountain road"],
|
| 82 |
+
["a cute puppy"],
|
| 83 |
+
["an elegant woman with a hat and a scarf"],
|
| 84 |
+
["a scenic beach with palm trees and blue water"],
|
| 85 |
+
["a golden retriever sitting on a couch"],
|
| 86 |
+
["a delicious pizza with pepperoni and cheese"],
|
| 87 |
+
["a futuristic city with tall buildings and flying cars"],
|
| 88 |
+
["an adorable kitten playing with a ball of yarn"],
|
| 89 |
+
],
|
| 90 |
+
).launch(debug=True)
|