Spaces:
Running
Running
Update app.py
Browse files
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
|
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
|
28 |
-
|
29 |
-
|
|
|
30 |
full_prompt = prompt
|
31 |
if negative_prompt:
|
32 |
full_prompt += f". Negative prompt: {negative_prompt}"
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
#
|
42 |
-
|
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(
|