Spaces:
Build error
Build error
mookkanvas
commited on
Commit
·
d3e1bc0
1
Parent(s):
454eb3b
Create app.py
Browse files
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 choice", ["Home", "DALL-E", "Huggingface Diffusers"])
|
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 == "Huggingface Diffusers":
|
43 |
+
st.subheader("Image generation using Huggingface Diffusers")
|
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 Huggingface Diffusers")
|