Spaces:
Runtime error
Runtime error
Dua Rajper commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from diffusers import StableDiffusionPipeline
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import CLIPTextModel, CLIPFeatureExtractor
|
| 6 |
+
import numpy as np
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
|
| 9 |
+
# Load the model and tokenizer
|
| 10 |
+
@st.cache(allow_output_mutation=True)
|
| 11 |
+
def load_model():
|
| 12 |
+
model_id = "CompVis/stable-diffusion-2-1"
|
| 13 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 14 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
return pipe
|
| 16 |
+
|
| 17 |
+
# Main function
|
| 18 |
+
def main():
|
| 19 |
+
st.title("Stable Diffusion 2.1 Image Generator")
|
| 20 |
+
st.header("Generate images from your prompts!")
|
| 21 |
+
|
| 22 |
+
prompt = st.text_input("Enter your prompt:", "")
|
| 23 |
+
steps = st.slider("Number of steps:", min_value=20, max_value=200, value=50)
|
| 24 |
+
guidance = st.slider("Guidance scale:", min_value=1.0, max_value=20.0, value=7.5)
|
| 25 |
+
|
| 26 |
+
if st.button("Generate"):
|
| 27 |
+
if not prompt:
|
| 28 |
+
st.error("Please enter a prompt")
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
with st.spinner("Generating image..."):
|
| 32 |
+
pipe = load_model()
|
| 33 |
+
image = pipe(prompt, guidance_scale=guidance, num_inference_steps=steps).images[0]
|
| 34 |
+
st.image(image, caption="Generated Image")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
main()
|