forked from run-llama/llama_index
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
86 lines (67 loc) · 2.57 KB
/
Copy pathapp.py
File metadata and controls
86 lines (67 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import gradio as gr
from huggingface_hub import InferenceClient
MODEL_ID = os.getenv("MODEL_ID", "stabilityai/stable-diffusion-xl-base-1.0")
HF_TOKEN = os.getenv("HF_TOKEN", None)
client = InferenceClient(token=HF_TOKEN)
def generate_image(prompt, negative_prompt, guidance_scale, num_steps, width, height, seed):
if not prompt.strip():
raise gr.Error("Please enter a prompt.")
kwargs = {
"prompt": prompt,
"guidance_scale": guidance_scale,
"num_inference_steps": num_steps,
"width": width,
"height": height,
}
if negative_prompt.strip():
kwargs["negative_prompt"] = negative_prompt
if seed >= 0:
kwargs["seed"] = seed
image = client.text_to_image(model=MODEL_ID, **kwargs)
return image
with gr.Blocks(theme=gr.themes.Soft(), title="Text-to-Image Generator") as demo:
gr.Markdown("# Text-to-Image Generator")
gr.Markdown(f"Using model: **{MODEL_ID}**")
with gr.Row():
with gr.Column(scale=3):
prompt = gr.Textbox(
label="Prompt",
placeholder="Describe the image you want to generate...",
lines=3,
)
negative_prompt = gr.Textbox(
label="Negative Prompt",
placeholder="What to avoid in the image...",
lines=2,
)
with gr.Row():
guidance_scale = gr.Slider(
minimum=1.0, maximum=20.0, value=7.5, step=0.5,
label="Guidance Scale",
)
num_steps = gr.Slider(
minimum=1, maximum=100, value=30, step=1,
label="Inference Steps",
)
with gr.Row():
width = gr.Slider(
minimum=256, maximum=1344, value=1024, step=64,
label="Width",
)
height = gr.Slider(
minimum=256, maximum=1344, value=1024, step=64,
label="Height",
)
seed = gr.Number(label="Seed", value=-1, precision=0)
gr.Markdown("*Set seed to -1 for random generation.*")
generate_btn = gr.Button("Generate", variant="primary")
with gr.Column(scale=4):
output_image = gr.Image(label="Generated Image", type="pil")
generate_btn.click(
fn=generate_image,
inputs=[prompt, negative_prompt, guidance_scale, num_steps, width, height, seed],
outputs=output_image,
)
if __name__ == "__main__":
demo.launch()