Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,30 @@
|
|
1 |
-
import os
|
2 |
from huggingface_hub import login
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
from diffusers import StableDiffusion3Pipeline
|
6 |
|
7 |
-
|
8 |
# Get Hugging Face token from environment variables
|
9 |
hf_token = os.getenv("HF_API_TOKEN")
|
10 |
|
11 |
if hf_token:
|
12 |
login(token=hf_token)
|
13 |
-
print("
|
14 |
else:
|
15 |
raise ValueError("Hugging Face token is missing. Please set it in the environment variables.")
|
16 |
-
|
|
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
-
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
pipeline.to(device)
|
24 |
|
|
|
25 |
image = pipeline(
|
26 |
prompt=prompt,
|
27 |
negative_prompt="blurred, ugly, watermark, low, resolution, blurry",
|
@@ -33,11 +36,15 @@ else:
|
|
33 |
|
34 |
return image
|
35 |
|
|
|
36 |
interface = gr.Interface(
|
37 |
fn=image_generator,
|
38 |
-
inputs=gr.Textbox(lines=2, placeholder
|
39 |
-
outputs=gr.Image(type
|
40 |
-
title
|
41 |
-
description
|
|
|
|
|
|
|
42 |
interface.launch()
|
43 |
print(interface)
|
|
|
1 |
+
import os
|
2 |
from huggingface_hub import login
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
from diffusers import StableDiffusion3Pipeline
|
6 |
|
|
|
7 |
# Get Hugging Face token from environment variables
|
8 |
hf_token = os.getenv("HF_API_TOKEN")
|
9 |
|
10 |
if hf_token:
|
11 |
login(token=hf_token)
|
12 |
+
print("Login successful")
|
13 |
else:
|
14 |
raise ValueError("Hugging Face token is missing. Please set it in the environment variables.")
|
15 |
+
|
16 |
+
def image_generator(prompt):
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
19 |
+
"stabilityai/stable-diffusion-3-medium-diffusers",
|
20 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
21 |
+
text_encoder_3=None,
|
22 |
+
tokenizer_3=None
|
23 |
+
)
|
24 |
+
# Move the pipeline to the appropriate device
|
25 |
pipeline.to(device)
|
26 |
|
27 |
+
# Generate the image
|
28 |
image = pipeline(
|
29 |
prompt=prompt,
|
30 |
negative_prompt="blurred, ugly, watermark, low, resolution, blurry",
|
|
|
36 |
|
37 |
return image
|
38 |
|
39 |
+
# Create a Gradio interface
|
40 |
interface = gr.Interface(
|
41 |
fn=image_generator,
|
42 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt..."),
|
43 |
+
outputs=gr.Image(type="pil"),
|
44 |
+
title="Image Generator App",
|
45 |
+
description="This is a simple image generator app using HuggingFace's Stable Diffusion 3 model."
|
46 |
+
)
|
47 |
+
|
48 |
+
# Launch the interface
|
49 |
interface.launch()
|
50 |
print(interface)
|