HuyRemy commited on
Commit
42a2d33
·
verified ·
1 Parent(s): 5f89ef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +215 -16
app.py CHANGED
@@ -1,22 +1,221 @@
1
- from transformers import pipeline
2
  import gradio as gr
 
 
3
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- model_name = "HuyRemy/Ai-bocphet"
6
- chatbot = pipeline("text-generation", model=model_name)
7
 
8
- def generate_response(prompt):
9
- response = chatbot(prompt, max_length=150, num_return_sequences=1)
10
- return response[0]['generated_text']
11
 
12
- # Ví dụ sử dụng
13
- iface = gr.Interface(
14
- fn=generate_response,
15
- inputs="text",
16
- outputs="text",
17
- title="AI-Bốc Phét (sáng tác truyện)",
18
- description="Example: Extend this story. HuyRemy is a super hero in Vietnam, he's like Iron Man from USA",
19
- )
20
 
21
- # Bắt đầu ứng dụng
22
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from random import randint
3
+ from all_models import models
4
 
5
+ def load_fn(models):
6
+ global models_load
7
+ models_load = {}
8
+ for model in models:
9
+ if model not in models_load.keys():
10
+ try:
11
+ m = gr.load(f'models/{model}')
12
+ except Exception as error:
13
+ m = gr.Interface(lambda txt: None, ['text'], ['image'])
14
+ models_load.update({model: m})
15
 
16
+ load_fn(models)
 
17
 
18
+ num_models = len(models)
19
+ default_models = models[:num_models]
 
20
 
21
+ def extend_choices(choices):
22
+ return choices + (num_models - len(choices)) * ['NA']
 
 
 
 
 
 
23
 
24
+ def update_imgbox(choices):
25
+ choices_plus = extend_choices(choices)
26
+ return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
27
+
28
+ def gen_fn(model_str, prompt):
29
+ if model_str == 'NA':
30
+ return None
31
+ noise = str(randint(0, 9999999))
32
+ return models_load[model_str](f'{prompt} {noise}')
33
+
34
+ def make_me():
35
+ with gr.Row():
36
+ with gr.Column(scale=1):
37
+ txt_input = gr.Textbox(label='Your prompt:', lines=3, container=False, elem_id="custom_textbox", placeholder="Prompt")
38
+ with gr.Row():
39
+ gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
40
+ stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
41
+
42
+ def on_generate_click():
43
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
44
+
45
+ def on_stop_click():
46
+ return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
47
+
48
+ gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
49
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
50
+
51
+ with gr.Row():
52
+ output = [gr.Image(label=m, min_width=250, height=250, elem_id="custom_image") for m in default_models]
53
+ current_models = [gr.Textbox(m, visible=False) for m in default_models]
54
+ for m, o in zip(current_models, output):
55
+ gen_event = gen_button.click(gen_fn, [m, txt_input], o)
56
+ stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
57
+
58
+ with gr.Accordion('Model selection', elem_id="custom_accordion"):
59
+ model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected', value=default_models, interactive=True, elem_id="custom_checkbox_group")
60
+ model_choice.change(update_imgbox, model_choice, output)
61
+ model_choice.change(extend_choices, model_choice, current_models)
62
+
63
+ with gr.Row():
64
+ gr.HTML("")
65
+
66
+ custom_css = """
67
+ :root {
68
+ --body-background-fill: #2d3d4f;
69
+ }
70
+ body {
71
+ background-color: var(--body-background-fill) !important;
72
+ color: #2d3d4f;
73
+ margin: 0;
74
+ padding: 0;
75
+ font-family: Arial, sans-serif;
76
+ height: 100vh;
77
+ overflow-y: auto;
78
+ }
79
+ .gradio-container {
80
+ background-color: #2d3d4f;
81
+ color: #c5c6c7;
82
+ padding: 20px;
83
+ border-radius: 8px;
84
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
85
+ width: 100%;
86
+ max-width: 1200px;
87
+ margin: 20px auto;
88
+ display: block;
89
+ min-height: 100vh;
90
+ }
91
+ .app_title {
92
+ background-color: #2d3d4f;
93
+ color: #c5c6c7;
94
+ padding: 10px 20px;
95
+ border-bottom: 1px solid #3b4252;
96
+ text-align: center;
97
+ font-size: 24px;
98
+ font-weight: bold;
99
+ width: 100%;
100
+ box-sizing: border-box;
101
+ margin-bottom: 20px;
102
+ }
103
+ .custom_textbox {
104
+ background-color: #2d343f;
105
+ border: 1px solid #3b4252;
106
+ color: #7f8184;
107
+ padding: 10px;
108
+ border-radius: 4px;
109
+ margin-bottom: 10px;
110
+ width: 100%;
111
+ box-sizing: border-box;
112
+ }
113
+ .custom_gen_button {
114
+ background-color: #8b38ff;
115
+ border: 1px solid #ffffff;
116
+ color: blue;
117
+ padding: 15px 32px;
118
+ text-align: center;
119
+ text-decoration: none;
120
+ display: inline-block;
121
+ font-size: 16px;
122
+ margin: 4px 2px;
123
+ cursor: pointer;
124
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
125
+ transition: transform 0.2s, box-shadow 0.2s;
126
+ border-radius: 4px;
127
+ }
128
+ .custom_gen_button:hover {
129
+ transform: translateY(-2px);
130
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
131
+ }
132
+ .custom_stop_button {
133
+ background-color: #6200ea;
134
+ border: 1px solid #ffffff;
135
+ color: blue;
136
+ padding: 15px 32px;
137
+ text-align: center;
138
+ text-decoration: none;
139
+ display: inline-block;
140
+ font-size: 16px;
141
+ margin: 4px 2px;
142
+ cursor: pointer;
143
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
144
+ transition: transform 0.2s, box-shadow 0.2s;
145
+ border-radius: 4px;
146
+ }
147
+ .custom_stop_button:hover {
148
+ transform: translateY(-2px);
149
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3);
150
+ }
151
+ .custom_image {
152
+ border: 1px solid #3b4252;
153
+ background-color: #2d343f;
154
+ border-radius: 4px;
155
+ margin: 10px;
156
+ max-width: 100%;
157
+ box-sizing: border-box;
158
+ }
159
+ .custom_accordion {
160
+ background-color: #2d3d4f;
161
+ color: #7f8184;
162
+ border: 1px solid #3b4252;
163
+ border-radius: 4px;
164
+ margin-top: 20px;
165
+ width: 100%;
166
+ box-sizing: border-box;
167
+ transition: margin 0.2s ease;
168
+ }
169
+ .custom_accordion .gr-accordion-header {
170
+ background-color: #2d3d4f;
171
+ color: #7f8184;
172
+ padding: 10px 20px;
173
+ border-bottom: 1px solid #5b6270;
174
+ cursor: pointer;
175
+ font-size: 18px;
176
+ font-weight: bold;
177
+ height: 40px;
178
+ display: flex;
179
+ align-items: center;
180
+ }
181
+ .custom_accordion .gr-accordion-header:hover {
182
+ background-color: #2d3d4f;
183
+ }
184
+ .custom_accordion .gr-accordion-content {
185
+ padding: 10px 20px;
186
+ background-color: #2d3d4f;
187
+ border-top: 1px solid #5b6270;
188
+ max-height: 0;
189
+ overflow: hidden;
190
+ transition: max-height 0.2s ease;
191
+ }
192
+ .custom_accordion .gr-accordion-content.open {
193
+ max-height: 500px;
194
+ }
195
+ .custom_checkbox_group {
196
+ background-color: #2d343f;
197
+ border: 1px solid #3b4252;
198
+ color: #7f8184;
199
+ border-radius: 4px;
200
+ padding: 10px;
201
+ width: 100%;
202
+ box-sizing: border-box;
203
+ }
204
+ @media (max-width: 768px) {
205
+ .gradio-container {
206
+ width: 100%;
207
+ margin: 0;
208
+ padding: 10px;
209
+ }
210
+ .custom_textbox,.custom_image,.custom_checkbox_group {
211
+ width: 100%;
212
+ box-sizing: border-box;
213
+ }
214
+ }
215
+ """
216
+
217
+ with gr.Blocks(css=custom_css) as demo:
218
+ make_me()
219
+
220
+ demo.queue(concurrency_count=50)
221
+ demo.launch()