SpyC0der77 commited on
Commit
7685695
·
verified ·
1 Parent(s): fd1302e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -121
app.py CHANGED
@@ -1,72 +1,20 @@
1
  import gradio as gr
2
- import numpy as np
3
- import random
4
  import torch
5
  import os
6
- from diffusers import DiffusionPipeline
7
- from huggingface_hub import login
8
-
9
- # Retrieve the Hugging Face API token from the environment variable
10
- api_token = os.getenv('HUGGINGFACE_API_TOKEN')
11
- if not api_token:
12
- raise ValueError("HUGGINGFACE_API_TOKEN environment variable not set.")
13
-
14
- # Log in to Hugging Face Hub
15
- login(token=api_token)
16
-
17
- MAX_SEED = np.iinfo(np.int32).max
18
- MAX_IMAGE_SIZE = 1024
19
-
20
- def infer(
21
- prompt,
22
- negative_prompt,
23
- seed,
24
- randomize_seed,
25
- width,
26
- height,
27
- guidance_scale,
28
- num_inference_steps,
29
- ):
30
- # Load the pipeline only when this function is called
31
- pipe = DiffusionPipeline.from_pretrained(
32
- "black-forest-labs/FLUX.1-dev", use_auth_token=api_token
33
- )
34
- pipe.load_lora_weights("EvanZhouDev/open-genmoji", use_auth_token=api_token)
35
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
36
- pipe = pipe.to(device)
37
-
38
- # Handle seed randomization
39
- if randomize_seed:
40
- seed = random.randint(0, MAX_SEED)
41
- generator = torch.manual_seed(seed)
42
-
43
- # Generate the image using the pipeline
44
- result = pipe(
45
- prompt=prompt,
46
- negative_prompt=negative_prompt if negative_prompt else None,
47
- width=width,
48
- height=height,
49
- guidance_scale=guidance_scale,
50
- num_inference_steps=num_inference_steps,
51
- generator=generator,
52
- ).images[0]
53
-
54
- return result, seed
55
-
56
- examples = [
57
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
58
- "An astronaut riding a green horse",
59
- "A delicious ceviche cheesecake slice",
60
- ]
61
-
62
- css = """
63
- #col-container {
64
- margin: 0 auto;
65
- max-width: 640px;
66
- }
67
- """
68
-
69
- with gr.Blocks(css=css) as demo:
70
  with gr.Column(elem_id="col-container"):
71
  gr.Markdown(" # Text-to-Image Gradio Template")
72
 
@@ -81,68 +29,15 @@ with gr.Blocks(css=css) as demo:
81
  run_button = gr.Button("Run", scale=0, variant="primary")
82
  result = gr.Image(label="Result", show_label=False)
83
 
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=True,
90
- )
91
- seed = gr.Slider(
92
- label="Seed",
93
- minimum=0,
94
- maximum=MAX_SEED,
95
- step=1,
96
- value=0,
97
- )
98
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
99
- with gr.Row():
100
- width = gr.Slider(
101
- label="Width",
102
- minimum=256,
103
- maximum=MAX_IMAGE_SIZE,
104
- step=32,
105
- value=512,
106
- )
107
- height = gr.Slider(
108
- label="Height",
109
- minimum=256,
110
- maximum=MAX_IMAGE_SIZE,
111
- step=32,
112
- value=512,
113
- )
114
- with gr.Row():
115
- guidance_scale = gr.Slider(
116
- label="Guidance scale",
117
- minimum=0.0,
118
- maximum=10.0,
119
- step=0.1,
120
- value=7.5,
121
- )
122
- num_inference_steps = gr.Slider(
123
- label="Number of inference steps",
124
- minimum=1,
125
- maximum=50,
126
- step=1,
127
- value=25,
128
- )
129
 
130
- gr.Examples(examples=examples, inputs=[prompt])
131
 
132
  # Run inference when run_button is clicked
133
  run_button.click(
134
  infer,
135
  inputs=[
136
- prompt,
137
- negative_prompt,
138
- seed,
139
- randomize_seed,
140
- width,
141
- height,
142
- guidance_scale,
143
- num_inference_steps,
144
  ],
145
- outputs=[result, seed],
146
  )
147
 
148
  if __name__ == "__main__":
 
1
  import gradio as gr
 
 
2
  import torch
3
  import os
4
+ import requests
5
+ import io
6
+ from PIL import Image
7
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
8
+ headers = {"Authorization": "Bearer " + os.getenv("HF_TOKEN")}
9
+ def infer(prompt):
10
+ def query(payload):
11
+ response = requests.post(API_URL, headers=headers, json=payload)
12
+ return response.content
13
+ image_bytes = query({
14
+ "inputs": prompt,
15
+ })
16
+
17
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  with gr.Column(elem_id="col-container"):
19
  gr.Markdown(" # Text-to-Image Gradio Template")
20
 
 
29
  run_button = gr.Button("Run", scale=0, variant="primary")
30
  result = gr.Image(label="Result", show_label=False)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
33
 
34
  # Run inference when run_button is clicked
35
  run_button.click(
36
  infer,
37
  inputs=[
38
+ prompt
 
 
 
 
 
 
 
39
  ],
40
+ outputs=[result],
41
  )
42
 
43
  if __name__ == "__main__":