tejani commited on
Commit
a44ba0b
·
verified ·
1 Parent(s): 90b3e82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -4
app.py CHANGED
@@ -1,8 +1,140 @@
1
  import gradio as gr
 
 
2
  import os
3
 
4
- # Load the API token from the environment variable
5
- api_token = os.getenv("HF_TOKEN")
6
- interface = gr.load("models/ZB-Tech/Text-to-Image", token=api_token)
7
 
8
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
  import os
5
 
6
+ hf_token = os.getenv("HF_TOKEN")
7
+ interface = gr.load("models/ZB-Tech/Text-to-Image", token=hf_token)
 
8
 
9
+ MAX_SEED = np.iinfo(np.int32).max
10
+ MAX_IMAGE_SIZE = 1024
11
+
12
+ def infer(
13
+ prompt,
14
+ negative_prompt,
15
+ seed,
16
+ randomize_seed,
17
+ width,
18
+ height,
19
+ guidance_scale,
20
+ num_inference_steps,
21
+ progress=gr.Progress(track_tqdm=True),
22
+ ):
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+
26
+ # Use the loaded interface to generate the image
27
+ # Note: Parameters depend on what the ZB-Tech/Text-to-Image model supports via the API
28
+ image = interface(
29
+ prompt=prompt,
30
+ negative_prompt=negative_prompt if negative_prompt else None,
31
+ seed=seed,
32
+ width=width,
33
+ height=height,
34
+ guidance_scale=guidance_scale,
35
+ num_inference_steps=num_inference_steps,
36
+ )
37
+
38
+ return image, seed
39
+
40
+ examples = [
41
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
+ "An astronaut riding a green horse",
43
+ "A delicious ceviche cheesecake slice",
44
+ ]
45
+
46
+ css = """
47
+ #col-container {
48
+ margin: 0 auto;
49
+ max-width: 640px;
50
+ }
51
+ """
52
+
53
+ with gr.Blocks(css=css) as demo:
54
+ with gr.Column(elem_id="col-container"):
55
+ gr.Markdown(" # Text-to-Image Gradio Template (ZB-Tech Model via API)")
56
+
57
+ with gr.Row():
58
+ prompt = gr.Text(
59
+ label="Prompt",
60
+ show_label=False,
61
+ max_lines=1,
62
+ placeholder="Enter your prompt",
63
+ container=False,
64
+ )
65
+
66
+ run_button = gr.Button("Run", scale=0, variant="primary")
67
+
68
+ result = gr.Image(label="Result", show_label=False)
69
+
70
+ with gr.Accordion("Advanced Settings", open=False):
71
+ negative_prompt = gr.Text(
72
+ label="Negative prompt",
73
+ max_lines=1,
74
+ placeholder="Enter a negative prompt",
75
+ visible=True, # Assuming the API supports it
76
+ )
77
+
78
+ seed = gr.Slider(
79
+ label="Seed",
80
+ minimum=0,
81
+ maximum=MAX_SEED,
82
+ step=1,
83
+ value=0,
84
+ )
85
+
86
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
87
+
88
+ with gr.Row():
89
+ width = gr.Slider(
90
+ label="Width",
91
+ minimum=256,
92
+ maximum=MAX_IMAGE_SIZE,
93
+ step=32,
94
+ value=512, # Reasonable default for API
95
+ )
96
+
97
+ height = gr.Slider(
98
+ label="Height",
99
+ minimum=256,
100
+ maximum=MAX_IMAGE_SIZE,
101
+ step=32,
102
+ value=512, # Reasonable default for API
103
+ )
104
+
105
+ with gr.Row():
106
+ guidance_scale = gr.Slider(
107
+ label="Guidance scale",
108
+ minimum=0.0,
109
+ maximum=10.0,
110
+ step=0.1,
111
+ value=1.0, # Default may vary by model
112
+ )
113
+
114
+ num_inference_steps = gr.Slider(
115
+ label="Number of inference steps",
116
+ minimum=1,
117
+ maximum=50,
118
+ step=1,
119
+ value=10, # Default may vary by model
120
+ )
121
+
122
+ gr.Examples(examples=examples, inputs=[prompt])
123
+ gr.on(
124
+ triggers=[run_button.click, prompt.submit],
125
+ fn=infer,
126
+ inputs=[
127
+ prompt,
128
+ negative_prompt,
129
+ seed,
130
+ randomize_seed,
131
+ width,
132
+ height,
133
+ guidance_scale,
134
+ num_inference_steps,
135
+ ],
136
+ outputs=[result, seed],
137
+ )
138
+
139
+ if __name__ == "__main__":
140
+ demo.launch()