AiCoderv2 commited on
Commit
6c8368c
·
verified ·
1 Parent(s): 472f7cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +145 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Download and load the model
6
+ model_name = "baidu/ERNIE-4.5-21B-A3B-Thinking"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.bfloat16,
11
+ device_map="auto"
12
+ )
13
+
14
+ def generate_code(prompt):
15
+ """Generate HTML code using ERNIE model"""
16
+ full_prompt = f"Create a complete single HTML file with embedded CSS and JavaScript for: {prompt}. Return only valid HTML code."
17
+
18
+ inputs = tokenizer(full_prompt, return_tensors="pt").to("cuda")
19
+
20
+ with torch.no_grad():
21
+ outputs = model.generate(
22
+ **inputs,
23
+ max_new_tokens=1000,
24
+ temperature=0.7,
25
+ top_p=0.9,
26
+ do_sample=True
27
+ )
28
+
29
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+ result = generated_text[len(full_prompt):]
31
+
32
+ # Extract HTML if there's extra text
33
+ if '<!DOCTYPE html>' in result:
34
+ start = result.find('<!DOCTYPE html>')
35
+ return result[start:]
36
+ return result
37
+
38
+ def improve_code(description, current_code):
39
+ """Improve existing code"""
40
+ prompt = f"Improve this HTML code based on: {description}\n\nCurrent code:\n{current_code}\n\nReturn only the improved HTML code."
41
+
42
+ inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
43
+
44
+ with torch.no_grad():
45
+ outputs = model.generate(
46
+ **inputs,
47
+ max_new_tokens=800,
48
+ temperature=0.7,
49
+ top_p=0.9,
50
+ do_sample=True
51
+ )
52
+
53
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
54
+ result = generated_text[len(prompt):]
55
+
56
+ # Extract HTML if there's extra text
57
+ if '<!DOCTYPE html>' in result:
58
+ start = result.find('<!DOCTYPE html>')
59
+ return result[start:]
60
+ return result
61
+
62
+ with gr.Blocks(theme=gr.themes.Soft()) as app:
63
+ gr.Markdown("# AI Website Builder")
64
+ gr.Markdown("Powered by baidu/ERNIE-4.5-21B-A3B-Thinking (Local Model)")
65
+
66
+ with gr.Tab("Builder"):
67
+ with gr.Row():
68
+ with gr.Column(scale=1):
69
+ gr.Markdown("### Instructions")
70
+ desc_input = gr.Textbox(
71
+ label="Describe your website",
72
+ placeholder="e.g., A modern portfolio with dark theme and animated cards",
73
+ lines=4
74
+ )
75
+ gen_btn = gr.Button("Generate Website", variant="primary")
76
+ imp_btn = gr.Button("Improve Current Code")
77
+
78
+ gr.Markdown("### Tips")
79
+ gr.Markdown("""
80
+ - Be specific about layout and features
81
+ - Mention color schemes and styling
82
+ - Describe interactive elements needed
83
+ - Request responsive design for mobile
84
+ """)
85
+
86
+ with gr.Column(scale=2):
87
+ code_editor = gr.Code(
88
+ label="HTML Code Editor",
89
+ language="html",
90
+ lines=30,
91
+ value="<!DOCTYPE html>\n<html>\n<head>\n <title>AI Generated Website</title>\n</head>\n<body>\n <h1>Your website will appear here</h1>\n</body>\n</html>"
92
+ )
93
+
94
+ with gr.Row():
95
+ gr.Markdown("### Live Preview")
96
+
97
+ with gr.Row():
98
+ preview = gr.HTML(label="Website Preview")
99
+
100
+ with gr.Tab("About"):
101
+ gr.Markdown("""
102
+ # AI Website Builder
103
+
104
+ This tool uses Baidu's ERNIE 4.5 model to generate complete websites from text descriptions.
105
+
106
+ ## Features:
107
+ - Local model execution (no API keys required)
108
+ - Real-time code generation
109
+ - Live website preview
110
+ - Code editing and improvement
111
+ - Syntax-highlighted editor
112
+
113
+ ## How to Use:
114
+ 1. Describe your desired website in detail
115
+ 2. Click "Generate Website" to create code
116
+ 3. Edit the generated code if needed
117
+ 4. See live preview of your website
118
+ 5. Use "Improve Current Code" to refine specific aspects
119
+
120
+ ## Model Information:
121
+ - Model: baidu/ERNIE-4.5-21B-A3B-Thinking
122
+ - Framework: Hugging Face Transformers
123
+ - Execution: Local inference
124
+ """)
125
+
126
+ # Event handling
127
+ gen_btn.click(
128
+ fn=generate_code,
129
+ inputs=desc_input,
130
+ outputs=code_editor
131
+ )
132
+
133
+ imp_btn.click(
134
+ fn=improve_code,
135
+ inputs=[desc_input, code_editor],
136
+ outputs=code_editor
137
+ )
138
+
139
+ code_editor.change(
140
+ fn=lambda x: x,
141
+ inputs=code_editor,
142
+ outputs=preview
143
+ )
144
+
145
+ app.launch()