levena commited on
Commit
adf314d
·
1 Parent(s): f0a4346

feat: 動かなかったのでデモをマルコピしてきた

Browse files
Files changed (2) hide show
  1. app.py +115 -17
  2. app_py.old +28 -0
app.py CHANGED
@@ -1,26 +1,124 @@
1
- import spaces
2
- import transformers
3
  import gradio as gr
 
 
 
 
 
4
 
5
- def greet(name):
6
- return "Hello " + name + "!!"
7
 
 
 
8
 
9
  @spaces.GPU
10
- def infer(input_text: str = "Who are you?"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # messages = [
13
- # {"role": "user", "content": name},
14
- # ]
15
- model = transformers.AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
16
- token = transformers.AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
17
- token.encode(input_text, return_tensors="pt" )
18
- output = model(input_ids)
19
- print(output)
20
- return output
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- text_input = gr.Textbox(label="Input Text", placeholder="test")
 
 
 
 
24
 
25
- demo = gr.Interface(fn=infer, inputs="text", outputs="text")
26
- demo.launch()
 
1
+ #Lisence: Apache 2.0
 
2
  import gradio as gr
3
+ import numpy as np
4
+ import random
5
+ from diffusers import DiffusionPipeline
6
+ import torch
7
+ import spaces
8
 
9
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
10
+ pipe = pipe.to("cuda")
11
 
12
+ MAX_SEED = np.iinfo(np.int32).max
13
+ MAX_IMAGE_SIZE = 1024
14
 
15
  @spaces.GPU
16
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
17
+
18
+ if randomize_seed:
19
+ seed = random.randint(0, MAX_SEED)
20
+
21
+ generator = torch.Generator().manual_seed(seed)
22
+
23
+ image = pipe(
24
+ prompt = prompt,
25
+ negative_prompt = negative_prompt,
26
+ guidance_scale = guidance_scale,
27
+ num_inference_steps = num_inference_steps,
28
+ width = width,
29
+ height = height,
30
+ generator = generator
31
+ ).images[0]
32
+
33
+ return image
34
+
35
+ css="""
36
+ #col-container {
37
+ margin: 0 auto;
38
+ max-width: 520px;
39
+ }
40
+ """
41
 
42
+ with gr.Blocks(css=css) as demo:
43
+
44
+ with gr.Column(elem_id="col-container"):
45
+ gr.Markdown(f"""
46
+ # Text-to-Image Gradio Template
47
+ """)
48
+
49
+ with gr.Row():
50
+
51
+ prompt = gr.Text(
52
+ label="Prompt",
53
+ show_label=False,
54
+ max_lines=1,
55
+ placeholder="Enter your prompt",
56
+ container=False,
57
+ )
58
+
59
+ run_button = gr.Button("Run", scale=0)
60
+
61
+ result = gr.Image(label="Result", show_label=False)
62
 
63
+ with gr.Accordion("Advanced Settings", open=False):
64
+
65
+ negative_prompt = gr.Text(
66
+ label="Negative prompt",
67
+ max_lines=1,
68
+ placeholder="Enter a negative prompt",
69
+ visible=False,
70
+ )
71
+
72
+ seed = gr.Slider(
73
+ label="Seed",
74
+ minimum=0,
75
+ maximum=MAX_SEED,
76
+ step=1,
77
+ value=0,
78
+ )
79
+
80
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
81
+
82
+ with gr.Row():
83
+
84
+ width = gr.Slider(
85
+ label="Width",
86
+ minimum=256,
87
+ maximum=MAX_IMAGE_SIZE,
88
+ step=32,
89
+ value=512,
90
+ )
91
+
92
+ height = gr.Slider(
93
+ label="Height",
94
+ minimum=256,
95
+ maximum=MAX_IMAGE_SIZE,
96
+ step=32,
97
+ value=512,
98
+ )
99
+
100
+ with gr.Row():
101
+
102
+ guidance_scale = gr.Slider(
103
+ label="Guidance scale",
104
+ minimum=0.0,
105
+ maximum=10.0,
106
+ step=0.1,
107
+ value=0.0,
108
+ )
109
+
110
+ num_inference_steps = gr.Slider(
111
+ label="Number of inference steps",
112
+ minimum=1,
113
+ maximum=12,
114
+ step=1,
115
+ value=2,
116
+ )
117
 
118
+ run_button.click(
119
+ fn = infer,
120
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
121
+ outputs = [result]
122
+ )
123
 
124
+ demo.queue().launch()
 
app_py.old ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import spaces
2
+ # import transformers
3
+ # import gradio as gr
4
+
5
+ # def greet(name):
6
+ # return "Hello " + name + "!!"
7
+
8
+
9
+ # # @spaces.GPU
10
+ # # def infer(input_text: str = "Who are you?"):
11
+
12
+ # # # messages = [
13
+ # # # {"role": "user", "content": name},
14
+ # # # ]
15
+ # # model = transformers.AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
16
+ # # token = transformers.AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
17
+ # # input_ids = token.encode(input_text, return_tensors="pt" )
18
+ # # output = model(input_ids)
19
+ # # print(output)
20
+ # # return output
21
+
22
+ # @spaces.GPU
23
+ # def infer_demo(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
24
+
25
+ # text_input = gr.Textbox(label="Input Text", placeholder="test")
26
+
27
+ # demo = gr.Interface(fn=infer, inputs="text", outputs="text")
28
+ # demo.launch()