jr08 commited on
Commit
7763b87
·
verified ·
1 Parent(s): c2ad32c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -35
app.py CHANGED
@@ -1,27 +1,18 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
 
4
 
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
26
  prompt,
27
  negative_prompt,
@@ -35,22 +26,24 @@ def infer(
35
  ):
36
  if randomize_seed:
37
  seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
 
 
 
51
  return image, seed
52
 
53
-
54
  examples = [
55
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
  "An astronaut riding a green horse",
@@ -66,7 +59,7 @@ css = """
66
 
67
  with gr.Blocks(css=css) as demo:
68
  with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
 
71
  with gr.Row():
72
  prompt = gr.Text(
@@ -105,7 +98,7 @@ with gr.Blocks(css=css) as demo:
105
  minimum=256,
106
  maximum=MAX_IMAGE_SIZE,
107
  step=32,
108
- value=1024, # Replace with defaults that work for your model
109
  )
110
 
111
  height = gr.Slider(
@@ -113,7 +106,7 @@ with gr.Blocks(css=css) as demo:
113
  minimum=256,
114
  maximum=MAX_IMAGE_SIZE,
115
  step=32,
116
- value=1024, # Replace with defaults that work for your model
117
  )
118
 
119
  with gr.Row():
@@ -122,7 +115,7 @@ with gr.Blocks(css=css) as demo:
122
  minimum=0.0,
123
  maximum=10.0,
124
  step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
 
128
  num_inference_steps = gr.Slider(
@@ -130,7 +123,7 @@ with gr.Blocks(css=css) as demo:
130
  minimum=1,
131
  maximum=50,
132
  step=1,
133
- value=2, # Replace with defaults that work for your model
134
  )
135
 
136
  gr.Examples(examples=examples, inputs=[prompt])
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ import os
5
+ from huggingface_hub import InferenceClient
6
 
7
+ # 初始化 InferenceClient
8
+ client = InferenceClient(
9
+ provider="fal-ai",
10
+ api_key=os.environ["HF_TOKEN"],
11
+ )
 
 
 
 
 
 
 
 
 
12
 
13
  MAX_SEED = np.iinfo(np.int32).max
14
  MAX_IMAGE_SIZE = 1024
15
 
 
 
16
  def infer(
17
  prompt,
18
  negative_prompt,
 
26
  ):
27
  if randomize_seed:
28
  seed = random.randint(0, MAX_SEED)
29
+ # 拼接 prompt 和 negative_prompt
30
+ full_prompt = prompt
31
+ if negative_prompt:
32
+ full_prompt += f". Negative prompt: {negative_prompt}"
33
+
34
+ # 发送推理请求
35
+ image = client.text_to_image(
36
+ full_prompt,
37
+ model="black-forest-labs/FLUX.1-Krea-dev",
38
+ # 下面参数部分模型可能不支持,可根据实际情况调整
39
+ # width=width,
40
+ # height=height,
41
+ # guidance_scale=guidance_scale,
42
+ # num_inference_steps=num_inference_steps,
43
+ seed=seed,
44
+ )
45
  return image, seed
46
 
 
47
  examples = [
48
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
49
  "An astronaut riding a green horse",
 
59
 
60
  with gr.Blocks(css=css) as demo:
61
  with gr.Column(elem_id="col-container"):
62
+ gr.Markdown(" # FLUX.1-Krea Text-to-Image Demo")
63
 
64
  with gr.Row():
65
  prompt = gr.Text(
 
98
  minimum=256,
99
  maximum=MAX_IMAGE_SIZE,
100
  step=32,
101
+ value=1024,
102
  )
103
 
104
  height = gr.Slider(
 
106
  minimum=256,
107
  maximum=MAX_IMAGE_SIZE,
108
  step=32,
109
+ value=1024,
110
  )
111
 
112
  with gr.Row():
 
115
  minimum=0.0,
116
  maximum=10.0,
117
  step=0.1,
118
+ value=4.5,
119
  )
120
 
121
  num_inference_steps = gr.Slider(
 
123
  minimum=1,
124
  maximum=50,
125
  step=1,
126
+ value=30,
127
  )
128
 
129
  gr.Examples(examples=examples, inputs=[prompt])