Hannah commited on
Commit
c281498
1 Parent(s): 3cdd919

add app.py

Browse files
Files changed (1) hide show
  1. app.py +310 -0
app.py ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import re
3
+ from typing import Dict, List, Tuple
4
+
5
+ import gradio as gr
6
+ from transformers import pipeline
7
+
8
+ History = List[Tuple[str, str]]
9
+ Messages = List[Dict[str, str]]
10
+
11
+ css = """
12
+ #submit-btn {
13
+ background-color: #8ac926;
14
+ border: none;
15
+ border-radius: 50px;
16
+ padding: 12px 24px;
17
+ font-size: 2rem;
18
+ font-weight: bold;
19
+ text-transform: uppercase;
20
+ gap: 8px;
21
+ transition: all 0.3s ease;
22
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
23
+ transform: rotate(-1deg) scale(1);
24
+ margin: 0 auto;
25
+ max-width: 400px;
26
+ margin-top: 1rem;
27
+ }
28
+
29
+ #submit-btn:hover {
30
+ background-color: #6ca91d;
31
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
32
+ transform: rotate(-1deg) scale(1.05);
33
+ }
34
+
35
+ .yap-button {
36
+ background-color: #8ac926;
37
+ border: none;
38
+ border-radius: 50px;
39
+ padding: 12px 24px;
40
+ font-size: 2rem;
41
+ font-weight: bold;
42
+ text-transform: uppercase;
43
+ gap: 8px;
44
+ transition: all 0.3s ease;
45
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
46
+ transform: rotate(-1deg) scale(1);
47
+ margin: 0 auto;
48
+ max-width: 400px;
49
+ margin-top: 1rem;
50
+ transition:
51
+ background-color 0.3s ease,
52
+ box-shadow 0.3s ease,
53
+ transform 0.5s ease;
54
+ }
55
+
56
+ .yap-button:hover {
57
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
58
+ transform: rotate(-1deg) scale(1.05);
59
+ animation: unset;
60
+ }
61
+
62
+ .yap-button:active {
63
+ outline: none;
64
+ }
65
+
66
+ .yap-button img {
67
+ margin-right: 0;
68
+ width: 1.7rem;
69
+ height: 1.7rem;
70
+ }
71
+
72
+ .stop-yap-button {
73
+ background-color: #EE4141;
74
+ }
75
+
76
+ .column {
77
+ margin: 0 auto;
78
+ width: 90vw;
79
+ max-width: 1024px;
80
+ }
81
+
82
+ .logo-row {
83
+ display: flex;
84
+ justify-content: center;
85
+ margin: 0 10px;
86
+ }
87
+
88
+ .logo-row svg {
89
+ width: 100%;
90
+ height: 100%;
91
+ transition: all 0.3s ease;
92
+ }
93
+
94
+ .logo-row svg:hover {
95
+ transform: rotate(-1deg) scale(1.05);
96
+ }
97
+
98
+ .browser-window {
99
+ background-color: #f0f0f0;
100
+ height: 400px;
101
+ width: 100%;
102
+ border-radius: 10px;
103
+ }
104
+
105
+ .url-bar {
106
+ background-color: #ddd;
107
+ height: 40px;
108
+ width: 100%;
109
+ z-index: 100;
110
+ top: 10px;
111
+ position: relative;
112
+ display: flex;
113
+ justify-content: center;
114
+ }
115
+
116
+ .url-bar-input {
117
+ background-color: white;
118
+ width: 98%;
119
+ height: 30px;
120
+ position: absolute;
121
+ }
122
+
123
+ #browser-window {
124
+ background-color: #2c2c2c;
125
+ border-radius: 8px;
126
+ overflow: hidden;
127
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
128
+ margin: 0 auto;
129
+ margin-bottom: 0rem;
130
+ display: flex;
131
+ flex-direction: column;
132
+ height: 65vh;
133
+ }
134
+
135
+ #browser-address-bar {
136
+ background-color: #3c3c3c;
137
+ padding: 8px;
138
+ display: flex;
139
+ align-items: center;
140
+ }
141
+
142
+ #browser-address {
143
+ background-color: #2c2c2c;
144
+
145
+ border: none;
146
+ padding: 6px 12px;
147
+ border-radius: 4px;
148
+ flex-grow: 1;
149
+ margin: 0 8px;
150
+ font-size: 0.9rem;
151
+ font-family: "Ubuntu Mono";
152
+ }
153
+
154
+ .browser-button {
155
+ background-color: transparent;
156
+ border: none;
157
+ color: #888;
158
+ font-size: 1.2rem;
159
+ cursor: pointer;
160
+ padding: 0 8px;
161
+ }
162
+
163
+ #preview-window {
164
+ flex-grow: 1;
165
+ background-color: #fff;
166
+ overflow: auto;
167
+ position: relative;
168
+ }
169
+
170
+ #version-navigation {
171
+ display: flex;
172
+ justify-content: space-between;
173
+ align-items: center;
174
+ font-size: 14px;
175
+ }
176
+
177
+ .version-nav-button {
178
+ background-color: #3c3c3c;
179
+ color: #fff;
180
+ border: none;
181
+ padding: 8px 16px;
182
+ border-radius: 4px;
183
+ cursor: pointer;
184
+ transition: background-color 0.3s ease;
185
+ margin-bottom: 0px;
186
+ }
187
+
188
+ .version-nav-button:hover {
189
+ background-color: #4c4c4c;
190
+ }
191
+
192
+ .version-nav-button:disabled {
193
+ opacity: 0.5;
194
+ cursor: not-allowed;
195
+ }
196
+
197
+ .textbox {
198
+ width: 100%;
199
+ max-width: 100%;
200
+ }
201
+
202
+ .form {
203
+ width: 100%;
204
+ max-width: 100%;
205
+ }
206
+ """
207
+
208
+ svg = """
209
+ <svg xmlns="http://www.w3.org/2000/svg" width="260" viewBox="0 0 150 40">
210
+ <defs><clipPath id="a"><path d="M0 .05h149.945v39.9H0Zm0 0"/>
211
+ </clipPath><clipPath id="b"><path d="M129.797.05c11.129 0 20.148 8.934 20.148 19.95s-9.02 19.95-20.148 19.95H20.148C9.02 39.95 0 31.015 0 20S9.02.05 20.148.05Zm0 0"/>
212
+ </clipPath></defs><g clip-path="url(#a)"><g clip-path="url(#b)"><path style="stroke:none;fill-rule:nonzero;fill:#0d63e7;fill-opacity:1" d="M0 .05h150v39.9H0Zm0 0"/></g></g>
213
+ <path style="stroke:none;fill-rule:nonzero;fill:#edeff4;fill-opacity:1" d="M40.305 12.145c.722.5 1.086.984 1.086 1.453 0 .277-.172.664-.512 1.164l-4.797 7.105v4.203c0 .336-.012.59-.031.758a1.701 1.701 0 0 1-.215.582c-.117.223-.316.375-.598.465-.285.086-.664.129-1.136.129-.473 0-.852-.043-1.133-.129-.282-.09-.48-.25-.598-.477a2.01 2.01 0 0 1-.215-.593 7.328 7.328 0 0 1-.031-.778v-4.16l-4.797-7.105c-.344-.5-.512-.887-.512-1.164 0-.47.325-.922.973-1.356.652-.43 1.121-.648 1.398-.648.286 0 .508.062.676.18.25.16.516.46.801.902l3.441 5.449 3.442-5.45c.281-.44.523-.73.734-.87.207-.14.45-.211.723-.211.273 0 .707.183 1.3.55ZM52.008 12.98l6.004 12.278c.25.5.375.875.375 1.12 0 .528-.43 1.005-1.29 1.43-.503.25-.902.376-1.19.376-.29 0-.528-.067-.712-.196a1.309 1.309 0 0 1-.402-.422 23.181 23.181 0 0 1-.336-.664l-1.156-2.375h-6.156l-1.153 2.375c-.152.297-.27.508-.351.641-.078.133-.211.266-.399.406-.187.14-.422.211-.715.211-.285 0-.68-.125-1.18-.37-.863-.411-1.292-.884-1.292-1.411 0-.246.125-.621.383-1.121l5.996-12.297c.164-.336.41-.61.742-.816a1.962 1.962 0 0 1 1.058-.31c.797 0 1.387.384 1.774 1.145Zm-1.801 5.282-1.46 3.011h2.952ZM71.625 15.094c.383.777.574 1.64.574 2.594 0 .953-.191 1.816-.574 2.585-.383.77-.879 1.383-1.484 1.836-1.23.942-2.504 1.41-3.825 1.41h-2.773v2.489c0 .332-.016.586-.04.754a1.75 1.75 0 0 1-.206.582c-.207.394-.785.593-1.734.593-1.04 0-1.645-.269-1.82-.812-.09-.25-.138-.629-.138-1.14V13.772c0-.335.008-.585.032-.757a1.85 1.85 0 0 1 .21-.586c.208-.395.79-.594 1.739-.594h4.754c1.305 0 2.57.469 3.8 1.41.606.453 1.102 1.07 1.485 1.848Zm-5.285 4.531c.441 0 .875-.164 1.305-.484.433-.32.652-.805.652-1.446 0-.648-.219-1.136-.652-1.468-.43-.329-.872-.493-1.329-.493h-2.773v3.891ZM88.66 12.98l6.008 12.278c.246.5.371.875.371 1.12 0 .528-.426 1.005-1.285 1.43-.504.25-.902.376-1.192.376-.289 0-.527-.067-.714-.196a1.336 1.336 0 0 1-.399-.422 21.218 21.218 0 0 1-.34-.664l-1.152-2.375h-6.156l-1.156 2.375a8.617 8.617 0 0 1-.348.641 1.565 1.565 0 0 1-.402.406c-.184.14-.422.211-.711.211-.29 0-.68-.125-1.18-.37-.863-.411-1.293-.884-1.293-1.411 0-.246.125-.621.379-1.121l6-12.297c.16-.336.406-.61.742-.816a1.959 1.959 0 0 1 1.055-.31c.8 0 1.39.384 1.773 1.145Zm-1.797 5.282-1.46 3.011h2.952ZM108.281 15.094c.383.777.574 1.64.574 2.594 0 .953-.19 1.816-.574 2.585-.386.77-.879 1.383-1.488 1.836-1.227.942-2.504 1.41-3.824 1.41h-2.774v2.489c0 .332-.011.586-.035.754a1.843 1.843 0 0 1-.207.582c-.21.394-.789.593-1.738.593-1.035 0-1.64-.269-1.817-.812-.093-.25-.136-.629-.136-1.14V13.772c0-.335.008-.585.031-.757a1.85 1.85 0 0 1 .21-.586c.208-.395.79-.594 1.74-.594h4.753c1.3 0 2.57.469 3.797 1.41.61.453 1.102 1.07 1.488 1.848Zm-5.285 4.531c.442 0 .875-.164 1.305-.484.433-.32.648-.805.648-1.446 0-.648-.215-1.136-.648-1.468-.43-.329-.871-.493-1.332-.493h-2.774v3.891ZM122.32 15.094c.383.777.575 1.64.575 2.594 0 .953-.192 1.816-.575 2.585-.382.77-.879 1.383-1.488 1.836-1.227.942-2.504 1.41-3.82 1.41h-2.778v2.489c0 .332-.011.586-.035.754a1.75 1.75 0 0 1-.207.582c-.21.394-.785.593-1.738.593-1.035 0-1.64-.269-1.817-.812-.093-.25-.136-.629-.136-1.14V13.772c0-.335.008-.585.031-.757a1.85 1.85 0 0 1 .211-.586c.207-.395.789-.594 1.738-.594h4.754c1.3 0 2.57.469 3.797 1.41.61.453 1.106 1.07 1.488 1.848Zm-5.285 4.531c.442 0 .875-.164 1.305-.484.433-.32.652-.805.652-1.446 0-.648-.219-1.136-.652-1.468-.43-.329-.871-.493-1.328-.493h-2.778v3.891Zm0 0"/></svg>
214
+ """
215
+
216
+ def remove_code_block(text):
217
+ pattern = r'```html\n(.+?)\n```'
218
+ match = re.search(pattern, text, re.DOTALL)
219
+ if match:
220
+ return match.group(1).strip()
221
+ else:
222
+ return text.strip()
223
+
224
+ def send_to_preview(code):
225
+ encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
226
+ iframe_src = f"data:text/html;charset=utf-8;base64,{encoded_html}"
227
+
228
+ browser_template = """
229
+ <div id="browser-window">
230
+ <div id="browser-address-bar">
231
+ <button class="browser-button">&#8592;</button>
232
+ <button class="browser-button">&#8594;</button>
233
+ <button class="browser-button">&#8635;</button>
234
+ <input
235
+ type="text"
236
+ id="browser-address"
237
+ />
238
+ <button class="browser-button maximize-toggle">
239
+
240
+ </button>
241
+ </div>
242
+ <div id="preview-window" class="gradient-background">
243
+ <iframe
244
+ src="{}"
245
+ style="width: 100%; height: 100%; border: none; background: white;"
246
+ ></iframe>
247
+ </div>
248
+ <div id="version-navigation">
249
+ <button class="version-nav-button">&#8592;</button>
250
+ <span class="version-info">Version x of x</span>
251
+ <button class="version-nav-button">&#8594;</button>
252
+ </div>
253
+ </div>
254
+ """
255
+ return browser_template.format(iframe_src)
256
+
257
+ def generate_website(query: str):
258
+ system_prompt = "You are a helpful web development assistant. When asked to create a website, respond with clean, modern HTML code wrapped in ```html``` tags. Include any necessary CSS within a style tag. Make the design responsive and visually appealing."
259
+ pipe = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B")
260
+ input_text = f"{system_prompt}\n\nUser: {query}\nAssistant:"
261
+
262
+ try:
263
+ response = pipe(input_text, max_length=2048, num_return_sequences=1)[0]['generated_text']
264
+ assistant_response = response.split("Assistant:")[-1].strip()
265
+ clean_html = remove_code_block(assistant_response)
266
+ preview_html = send_to_preview(clean_html)
267
+ return preview_html
268
+
269
+ except Exception as e:
270
+ return send_to_preview(f"<p>Error: {str(e)}</p>")
271
+
272
+
273
+
274
+ with gr.Blocks(css=css) as demo:
275
+ with gr.Column(elem_classes="column"):
276
+ with gr.Row():
277
+ gr.HTML(value=svg, elem_classes="logo-row")
278
+ with gr.Row():
279
+ with gr.Column():
280
+ chatbot = gr.Chatbot(label="Chatbot", type="messages")
281
+
282
+ text_input = gr.Textbox(
283
+ elem_classes="textbox",
284
+ label="Describe the website you want to build",
285
+ placeholder="Example: Create a modern landing page with a hero section and contact form",
286
+ lines=3
287
+ )
288
+ with gr.Column(elem_classes="column"):
289
+ preview = gr.HTML(elem_id="preview-window", value=send_to_preview(""))
290
+
291
+ submit_btn = gr.Button("Generate", variant="primary", elem_id="submit-btn")
292
+
293
+ def on_submit(message, history):
294
+ history = history or []
295
+ print(message)
296
+ history.append({"role": "user", "content": message})
297
+
298
+ preview_html = generate_website(message)
299
+
300
+ return history, preview_html, gr.Textbox(value=None)
301
+
302
+ submit_btn.click(
303
+ fn=on_submit,
304
+ inputs=[text_input, chatbot],
305
+ outputs=[chatbot, preview, text_input],
306
+ )
307
+
308
+
309
+ if __name__ == "__main__":
310
+ demo.queue().launch()