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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
@@ -7,7 +9,7 @@ from huggingface_hub import InferenceClient
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
@@ -24,25 +26,23 @@ def infer(
24
  num_inference_steps,
25
  progress=gr.Progress(track_tqdm=True),
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",
@@ -72,7 +72,7 @@ with gr.Blocks(css=css) as demo:
72
 
73
  run_button = gr.Button("Run", scale=0, variant="primary")
74
 
75
- result = gr.Image(label="Result", show_label=False)
76
 
77
  with gr.Accordion("Advanced Settings", open=False):
78
  negative_prompt = gr.Text(
 
1
+ # app.py
2
+
3
  import gradio as gr
4
  import numpy as np
5
  import random
 
9
  # 初始化 InferenceClient
10
  client = InferenceClient(
11
  provider="fal-ai",
12
+ api_key=os.environ.get("HF_TOKEN", ""),
13
  )
14
 
15
  MAX_SEED = np.iinfo(np.int32).max
 
26
  num_inference_steps,
27
  progress=gr.Progress(track_tqdm=True),
28
  ):
29
+ if not prompt or prompt.strip() == "":
30
+ return None, seed
31
+
32
+ # 只传递 prompt,避免参数过多导致报错
33
  full_prompt = prompt
34
  if negative_prompt:
35
  full_prompt += f". Negative prompt: {negative_prompt}"
36
 
37
+ try:
38
+ image = client.text_to_image(
39
+ full_prompt,
40
+ model="black-forest-labs/FLUX.1-Krea-dev",
41
+ )
42
+ return image, seed
43
+ except Exception as e:
44
+ # 返回错误信息到前端
45
+ return f"生成图片出错: {str(e)}", seed
 
 
 
46
 
47
  examples = [
48
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
72
 
73
  run_button = gr.Button("Run", scale=0, variant="primary")
74
 
75
+ result = gr.Image(label="Result", show_label=False, type="pil")
76
 
77
  with gr.Accordion("Advanced Settings", open=False):
78
  negative_prompt = gr.Text(