amirkhanbloch commited on
Commit
d4004ce
·
verified ·
1 Parent(s): f9f7e8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
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("lgin sucessfull")
14
  else:
15
  raise ValueError("Hugging Face token is missing. Please set it in the environment variables.")
16
- def image_generator(prompt):
 
17
  device = "cuda" if torch.cuda.is_available() else "cpu"
18
- pipeline = StableDiffusion3Pipeline.from_pretrained("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
- #pipeline.enable_model_cpu_offload()
 
 
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 = "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
  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)