AezersX
commited on
Commit
·
39aa1b0
1
Parent(s):
1387bc5
Upload 2 files
Browse files- app.py +50 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import os
|
| 4 |
+
import openai
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
#function to generate AI based images using OpenAI Dall-E
|
| 12 |
+
def generate_images_using_openai(text):
|
| 13 |
+
response = openai.Image.create(prompt= text, n=1, size="512x512")
|
| 14 |
+
image_url = response['data'][0]['url']
|
| 15 |
+
return image_url
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
#function to generate AI based images using Huggingface Diffusers
|
| 19 |
+
def generate_images_using_huggingface_diffusers(text):
|
| 20 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 21 |
+
pipe = pipe.to("cuda")
|
| 22 |
+
prompt = text
|
| 23 |
+
image = pipe(prompt).images[0]
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
#Streamlit Code
|
| 27 |
+
choice = st.sidebar.selectbox("Select your Models", ["Home", "DALL-E", "Stable Diffusion"])
|
| 28 |
+
|
| 29 |
+
if choice == "Home":
|
| 30 |
+
st.title("AI Image Generation App")
|
| 31 |
+
with st.expander("About the App"):
|
| 32 |
+
st.write("This is a simple image generation app that uses AI to generates images from text prompt.")
|
| 33 |
+
|
| 34 |
+
elif choice == "DALL-E":
|
| 35 |
+
st.subheader("Image generation using Open AI's DALL-E")
|
| 36 |
+
input_prompt = st.text_input("Enter your text prompt")
|
| 37 |
+
if input_prompt is not None:
|
| 38 |
+
if st.button("Generate Image"):
|
| 39 |
+
image_url = generate_images_using_openai(input_prompt)
|
| 40 |
+
st.image(image_url, caption="Generated by DALL-E")
|
| 41 |
+
|
| 42 |
+
elif choice == "Stable Diffusion":
|
| 43 |
+
st.subheader("Image generation using Stable Diffusion")
|
| 44 |
+
input_prompt = st.text_input("Enter your text prompt")
|
| 45 |
+
if input_prompt is not None:
|
| 46 |
+
if st.button("Generate Image"):
|
| 47 |
+
image_output = generate_images_using_huggingface_diffusers(input_prompt)
|
| 48 |
+
st.info("Generating image.....")
|
| 49 |
+
st.success("Image Generated Successfully")
|
| 50 |
+
st.image(image_output, caption="Generated by Stable Diffusion")
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
openai
|
| 3 |
+
python-dotenv
|
| 4 |
+
diffusers==0.10.2
|
| 5 |
+
transformers
|
| 6 |
+
scipy
|
| 7 |
+
ftfy
|
| 8 |
+
accelerate
|