Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
from diffusers import StableDiffusionPipeline
|
| 8 |
+
|
| 9 |
+
# Function to generate images based on user input
|
| 10 |
+
def generate_images(text, size, num_images, quality):
|
| 11 |
+
# Placeholder function, replace with your actual implementation
|
| 12 |
+
generated_images = []
|
| 13 |
+
for _ in range(num_images):
|
| 14 |
+
# Use your model to generate an image based on the input text
|
| 15 |
+
# Example:
|
| 16 |
+
# image = model.generate_image(text, size, quality)
|
| 17 |
+
# generated_images.append(image)
|
| 18 |
+
pass
|
| 19 |
+
return generated_images
|
| 20 |
+
|
| 21 |
+
# Function to convert PIL image to bytes
|
| 22 |
+
def pil_to_bytes(image):
|
| 23 |
+
img_byte_array = io.BytesIO()
|
| 24 |
+
image.save(img_byte_array, format='PNG')
|
| 25 |
+
return img_byte_array.getvalue()
|
| 26 |
+
|
| 27 |
+
# Streamlit interface
|
| 28 |
+
st.title('Text-to-Image AI-based Chatbot')
|
| 29 |
+
|
| 30 |
+
# Text input box
|
| 31 |
+
user_input = st.text_input("Enter your text here:")
|
| 32 |
+
|
| 33 |
+
# Select models
|
| 34 |
+
model_opts = ["amused/amused-256", "amused/amused-512", "runwayml/stable-diffusion-v1-5"]
|
| 35 |
+
selected_model = st.selectbox("Select model:", model_opts)
|
| 36 |
+
|
| 37 |
+
if selected_model == 'amused/amused-256':
|
| 38 |
+
print("Model selected: 256")
|
| 39 |
+
pipe = DiffusionPipeline.from_pretrained('amused/amused-256', variant="fp16", torch_dtype=torch.float16)
|
| 40 |
+
elif selected_model == 'amused/amused-512':
|
| 41 |
+
print("Model selected: 512")
|
| 42 |
+
pipe = DiffusionPipeline.from_pretrained('amused/amused-512', variant="fp16", torch_dtype=torch.float16)
|
| 43 |
+
else:
|
| 44 |
+
print("Model selected: diffusion")
|
| 45 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", variant="fp16", torch_dtype=torch.float16)
|
| 46 |
+
|
| 47 |
+
# # Image size input
|
| 48 |
+
# image_size_options = ["1080x1080", "1280x720", "1920x1080"]
|
| 49 |
+
# selected_size = st.selectbox("Select image size:", image_size_options)
|
| 50 |
+
|
| 51 |
+
# Convert selected size to width and height
|
| 52 |
+
# width, height = map(int, selected_size.split('x'))
|
| 53 |
+
|
| 54 |
+
# Number of images input
|
| 55 |
+
num_images = st.number_input("Number of images to generate:", min_value=1, max_value=10, value=3, step=1)
|
| 56 |
+
# Image quality input
|
| 57 |
+
image_quality = st.slider("Select image quality:", min_value=1, max_value=100, value=50, step=1)
|
| 58 |
+
|
| 59 |
+
# Generate button
|
| 60 |
+
if st.button("Generate Images"):
|
| 61 |
+
if user_input:
|
| 62 |
+
|
| 63 |
+
# Example image
|
| 64 |
+
# example_image = Image.open("example_img.jpg")
|
| 65 |
+
# st.image(example_image, caption='Example Image', use_column_width=True)
|
| 66 |
+
# img_bytes = pil_to_bytes(example_image)
|
| 67 |
+
# st.download_button(label=f"Download Image ", data=img_bytes, file_name=f"generated_image_example.png", mime='image/png')
|
| 68 |
+
pipe = pipe.to('cuda')
|
| 69 |
+
|
| 70 |
+
with st.spinner('Generating...'):
|
| 71 |
+
image = pipe(
|
| 72 |
+
user_input,
|
| 73 |
+
num_images_per_prompt=num_images,
|
| 74 |
+
num_inference_steps=image_quality,
|
| 75 |
+
generator=torch.Generator('cuda').manual_seed(8)
|
| 76 |
+
).images
|
| 77 |
+
st.image(image , use_column_width=True)
|
| 78 |
+
# img_bytes = pil_to_bytes(image)
|
| 79 |
+
# st.download_button(label=f"Download Image ", data=img_bytes, file_name=f"generated_image_example.png", mime='image/png')
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# # Generate images based on user input
|
| 83 |
+
# generated_images = generate_images(user_input, (width, height), num_images, image_quality)
|
| 84 |
+
# if generated_images:
|
| 85 |
+
# for i, img in enumerate(generated_images):
|
| 86 |
+
# st.image(img, caption=f"Generated Image {i+1}", use_column_width=True)
|
| 87 |
+
# # Download link for each image
|
| 88 |
+
# img_bytes = pil_to_bytes(img)
|
| 89 |
+
# st.download_butt
|