RickyChen-Infinirc commited on
Commit
6a6a7b7
1 Parent(s): 4c61a4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -53
app.py CHANGED
@@ -1,63 +1,138 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
27
 
28
- response = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
  """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
  import gradio as gr
3
+ import numpy as np
4
+ import random
5
+ import torch
6
+ from diffusers import StableDiffusion3Pipeline, SD3Transformer2DModel, FlowMatchEulerDiscreteScheduler
7
 
 
 
 
 
8
 
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ dtype = torch.float16
11
 
12
+ repo = "Infinirc/stable-diffusion-3-medium-diffusers"
13
+ pipe = StableDiffusion3Pipeline.from_pretrained(repo, torch_dtype=torch.float16).to(device)
 
 
 
 
 
 
 
14
 
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ MAX_IMAGE_SIZE = 1920
 
 
 
17
 
18
+ @spaces.GPU
19
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
20
 
21
+ if randomize_seed:
22
+ seed = random.randint(0, MAX_SEED)
23
+
24
+ generator = torch.Generator().manual_seed(seed)
25
+
26
+ image = pipe(
27
+ prompt = prompt,
28
+ negative_prompt = negative_prompt,
29
+ guidance_scale = guidance_scale,
30
+ num_inference_steps = num_inference_steps,
31
+ width = width,
32
+ height = height,
33
+ generator = generator
34
+ ).images[0]
35
+
36
+ return image, seed
37
 
38
+ examples = [
39
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
40
+ "An astronaut riding a green horse",
41
+ "A delicious ceviche cheesecake slice",
42
+ ]
 
 
 
43
 
44
+ css="""
45
+ #col-container {
46
+ margin: 0 auto;
47
+ max-width: 580px;
48
+ }
49
  """
50
+
51
+ with gr.Blocks(css=css) as demo:
52
+
53
+ with gr.Column(elem_id="col-container"):
54
+ gr.Markdown(f"""
55
+ # Infinirc [Stable Diffusion 3 Medium](https://huggingface.co/Infinirc/stable-diffusion-3-medium-diffusers)
56
+ [HuggingFace](https://huggingface.co/Infinirc)
57
+ """)
58
+
59
+ with gr.Row():
60
+
61
+ prompt = gr.Text(
62
+ label="Prompt",
63
+ show_label=False,
64
+ max_lines=1,
65
+ placeholder="Enter your prompt",
66
+ container=False,
67
+ )
68
+
69
+ run_button = gr.Button("Run", scale=0)
70
+
71
+ result = gr.Image(label="Result", show_label=False)
72
+
73
+ with gr.Accordion("Advanced Settings", open=False):
74
+
75
+ negative_prompt = gr.Text(
76
+ label="Negative prompt",
77
+ max_lines=1,
78
+ placeholder="Enter a negative prompt",
79
+ )
80
+
81
+ seed = gr.Slider(
82
+ label="Seed",
83
+ minimum=0,
84
+ maximum=MAX_SEED,
85
+ step=1,
86
+ value=0,
87
+ )
88
+
89
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
90
+
91
+ with gr.Row():
92
+
93
+ width = gr.Slider(
94
+ label="Width",
95
+ minimum=256,
96
+ maximum=MAX_IMAGE_SIZE,
97
+ step=64,
98
+ value=1024,
99
+ )
100
+
101
+ height = gr.Slider(
102
+ label="Height",
103
+ minimum=256,
104
+ maximum=MAX_IMAGE_SIZE,
105
+ step=64,
106
+ value=1024,
107
+ )
108
+
109
+ with gr.Row():
110
+
111
+ guidance_scale = gr.Slider(
112
+ label="Guidance scale",
113
+ minimum=0.0,
114
+ maximum=10.0,
115
+ step=0.1,
116
+ value=5.0,
117
+ )
118
+
119
+ num_inference_steps = gr.Slider(
120
+ label="Number of inference steps",
121
+ minimum=1,
122
+ maximum=50,
123
+ step=1,
124
+ value=12,
125
+ )
126
+
127
+ gr.Examples(
128
+ examples = examples,
129
+ inputs = [prompt]
130
+ )
131
+ gr.on(
132
+ triggers=[run_button.click, prompt.submit, negative_prompt.submit],
133
+ fn = infer,
134
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
135
+ outputs = [result, seed]
136
+ )
137
+
138
+ demo.launch()