amirkhanbloch commited on
Commit
d881e99
·
verified ·
1 Parent(s): 9530fd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -1,14 +1,25 @@
1
  import torch
2
  import gradio as gr
3
  from diffusers import StableDiffusion3Pipeline
 
 
 
 
 
 
 
 
 
 
4
 
5
  def image_generator(prompt):
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
- pipeline = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers",
8
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
9
- text_encoder_3=None,
10
- tokenizer_3 = None)
11
- #pipeline.enable_model_cpu_offload()
 
12
  pipeline.to(device)
13
 
14
  image = pipeline(
@@ -24,9 +35,9 @@ def image_generator(prompt):
24
 
25
  interface = gr.Interface(
26
  fn=image_generator,
27
- inputs=gr.Textbox(lines=2, placeholder = "Enter your prompt..."),
28
- outputs=gr.Image(type = "pil"),
29
- title = "Image Generator App",
30
- description = "This is a simple image generator app using HuggingFace's Stable Diffusion 3 model.")
 
31
  interface.launch()
32
- print(interface)
 
1
  import torch
2
  import gradio as gr
3
  from diffusers import StableDiffusion3Pipeline
4
+ import os
5
+
6
+ # Get the API token from environment variable (e.g., from Hugging Face Spaces Secrets)
7
+ api_token = os.getenv("HF_API_TOKEN")
8
+
9
+ if not api_token:
10
+ raise ValueError("Hugging Face API token not found. Please set it in the Secrets.")
11
+
12
+ # Set the environment variable for Hugging Face API token (if needed explicitly)
13
+ os.environ["HUGGING_FACE_HUB_TOKEN"] = api_token
14
 
15
  def image_generator(prompt):
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ pipeline = StableDiffusion3Pipeline.from_pretrained(
18
+ "stabilityai/stable-diffusion-3-medium-diffusers",
19
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
20
+ text_encoder_3=None,
21
+ tokenizer_3=None
22
+ )
23
  pipeline.to(device)
24
 
25
  image = pipeline(
 
35
 
36
  interface = gr.Interface(
37
  fn=image_generator,
38
+ inputs=gr.Textbox(lines=2, placeholder="Enter your prompt..."),
39
+ outputs=gr.Image(type="pil"),
40
+ title="Image Generator App",
41
+ description="This is a simple image generator app using HuggingFace's Stable Diffusion 3 model."
42
+ )
43
  interface.launch()