Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -39,32 +39,66 @@ def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature
|
|
39 |
token = message.choices[0].delta.content
|
40 |
if isinstance(token, str):
|
41 |
full_response += token
|
|
|
42 |
cleaned_response = full_response.strip()
|
43 |
cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
|
44 |
cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
|
45 |
-
cleaned_response = re.sub(r"<\s*\|?\s*(user|assistant)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE)
|
|
|
|
|
46 |
common_phrases = [
|
47 |
"Here is the code:", "Okay, here is the code:", "Here's the code:",
|
48 |
-
"Sure, here is the code you requested:", "Let me know if you need anything else."
|
|
|
|
|
49 |
]
|
|
|
50 |
for phrase in common_phrases:
|
51 |
-
if
|
52 |
cleaned_response = cleaned_response[len(phrase):].lstrip()
|
|
|
|
|
|
|
|
|
|
|
53 |
return cleaned_response
|
54 |
except Exception as e:
|
55 |
return f"## Error\n\nFailed to generate code.\n**Reason:** {e}"
|
56 |
|
57 |
def split_files(full_code_text):
|
58 |
file_blocks = []
|
59 |
-
splits = re.split(r'TAB\.NAME=\{
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
if
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
return file_blocks
|
69 |
|
70 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
@@ -72,38 +106,39 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
72 |
gr.Markdown(
|
73 |
"Describe the website you want. The AI will generate website code.\n\n"
|
74 |
"**Rules:**\n"
|
75 |
-
"-
|
76 |
-
"-
|
77 |
-
"- Only
|
78 |
-
"-
|
79 |
)
|
80 |
with gr.Row():
|
81 |
with gr.Column(scale=2):
|
82 |
prompt_input = gr.Textbox(
|
83 |
label="Website Description",
|
84 |
-
placeholder="e.g., A simple
|
85 |
lines=6,
|
86 |
)
|
87 |
backend_radio = gr.Radio(
|
88 |
["Static", "Flask", "Node.js"],
|
89 |
-
label="Backend Context",
|
90 |
value="Static",
|
91 |
)
|
92 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
93 |
with gr.Column(scale=3):
|
94 |
-
|
95 |
-
|
|
|
96 |
language="html",
|
97 |
-
lines=
|
98 |
interactive=False,
|
99 |
)
|
100 |
-
|
101 |
|
102 |
with gr.Accordion("Advanced Settings", open=False):
|
103 |
max_tokens_slider = gr.Slider(
|
104 |
minimum=512,
|
105 |
-
maximum=
|
106 |
-
value=
|
107 |
step=256,
|
108 |
label="Max New Tokens"
|
109 |
)
|
@@ -122,20 +157,86 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
122 |
label="Top-P"
|
123 |
)
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
def generate_and_display(prompt, backend, max_tokens, temperature, top_p):
|
126 |
full_code = generate_code(prompt, backend, max_tokens, temperature, top_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
files = split_files(full_code)
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
generate_button.click(
|
135 |
fn=generate_and_display,
|
136 |
inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
|
137 |
-
outputs=[
|
138 |
)
|
139 |
|
140 |
if __name__ == "__main__":
|
141 |
-
demo.queue(
|
|
|
39 |
token = message.choices[0].delta.content
|
40 |
if isinstance(token, str):
|
41 |
full_response += token
|
42 |
+
|
43 |
cleaned_response = full_response.strip()
|
44 |
cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
|
45 |
cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
|
46 |
+
cleaned_response = re.sub(r"<\s*\|?\s*(user|assistant|system|endoftext)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE)
|
47 |
+
cleaned_response = cleaned_response.replace("<|im_end|>", "").replace("<|im_start|>", "").strip()
|
48 |
+
|
49 |
common_phrases = [
|
50 |
"Here is the code:", "Okay, here is the code:", "Here's the code:",
|
51 |
+
"Sure, here is the code you requested:", "Let me know if you need anything else.",
|
52 |
+
"Here is the website code you requested:", "Here are the files for your website:",
|
53 |
+
"Okay, here are the files:"
|
54 |
]
|
55 |
+
lower_response = cleaned_response.lower()
|
56 |
for phrase in common_phrases:
|
57 |
+
if lower_response.startswith(phrase.lower()):
|
58 |
cleaned_response = cleaned_response[len(phrase):].lstrip()
|
59 |
+
lower_response = cleaned_response.lower()
|
60 |
+
|
61 |
+
if not cleaned_response:
|
62 |
+
return "Error: Empty response from model after cleaning."
|
63 |
+
|
64 |
return cleaned_response
|
65 |
except Exception as e:
|
66 |
return f"## Error\n\nFailed to generate code.\n**Reason:** {e}"
|
67 |
|
68 |
def split_files(full_code_text):
|
69 |
file_blocks = []
|
70 |
+
splits = re.split(r'(TAB\.NAME=\{.+?\})', full_code_text)
|
71 |
+
initial_content = splits[0].strip()
|
72 |
+
|
73 |
+
if len(splits) == 1:
|
74 |
+
if initial_content:
|
75 |
+
default_name = "index.html"
|
76 |
+
if "def " in initial_content or "import " in initial_content: default_name = "app.py"
|
77 |
+
elif "function " in initial_content or "const " in initial_content or "let " in initial_content: default_name = "script.js"
|
78 |
+
elif "<!DOCTYPE html>" in initial_content or "<html" in initial_content: default_name = "index.html"
|
79 |
+
elif "@app.route" in initial_content: default_name = "app.py"
|
80 |
+
elif "require(" in initial_content or "module.exports" in initial_content: default_name = "server.js"
|
81 |
+
elif "<?php" in initial_content: default_name = "index.php"
|
82 |
+
elif "package main" in initial_content: default_name = "main.go"
|
83 |
+
|
84 |
+
file_blocks.append((default_name, initial_content))
|
85 |
+
else:
|
86 |
+
for i in range(1, len(splits), 2):
|
87 |
+
marker = splits[i]
|
88 |
+
content = splits[i+1].strip() if (i+1) < len(splits) else ""
|
89 |
+
filename_match = re.search(r'TAB\.NAME=\{(.+?)\}', marker)
|
90 |
+
if filename_match:
|
91 |
+
filename = filename_match.group(1).strip()
|
92 |
+
if content:
|
93 |
+
file_blocks.append((filename, content))
|
94 |
+
elif i == 1 and initial_content: # Handle content before the first explicit marker
|
95 |
+
file_blocks.append(("file_0.txt", initial_content))
|
96 |
+
if content: # Add the content after the first marker if it exists
|
97 |
+
filename_match_fallback = re.search(r'TAB\.NAME=\{(.+?)\}', marker)
|
98 |
+
if filename_match_fallback:
|
99 |
+
filename = filename_match_fallback.group(1).strip()
|
100 |
+
file_blocks.append((filename, content))
|
101 |
+
|
102 |
return file_blocks
|
103 |
|
104 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
|
106 |
gr.Markdown(
|
107 |
"Describe the website you want. The AI will generate website code.\n\n"
|
108 |
"**Rules:**\n"
|
109 |
+
"- Provide a backend hint (Static / Flask / Node.js).\n"
|
110 |
+
"- Generated code should be functional and SFW.\n"
|
111 |
+
"- Only generates website-related code.\n"
|
112 |
+
"- If multiple files are generated, they will be separated below using the format `TAB.NAME={filename}`."
|
113 |
)
|
114 |
with gr.Row():
|
115 |
with gr.Column(scale=2):
|
116 |
prompt_input = gr.Textbox(
|
117 |
label="Website Description",
|
118 |
+
placeholder="e.g., A simple Flask app with one route that displays 'Hello World'.",
|
119 |
lines=6,
|
120 |
)
|
121 |
backend_radio = gr.Radio(
|
122 |
["Static", "Flask", "Node.js"],
|
123 |
+
label="Backend Context / Hint",
|
124 |
value="Static",
|
125 |
)
|
126 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
127 |
with gr.Column(scale=3):
|
128 |
+
main_output_label = gr.Markdown("### Full Generated Code / Main File")
|
129 |
+
main_output_code = gr.Code(
|
130 |
+
label="Generated Code", # Label is less prominent now
|
131 |
language="html",
|
132 |
+
lines=15,
|
133 |
interactive=False,
|
134 |
)
|
135 |
+
extra_outputs_column = gr.Column(visible=False) # Initially hidden
|
136 |
|
137 |
with gr.Accordion("Advanced Settings", open=False):
|
138 |
max_tokens_slider = gr.Slider(
|
139 |
minimum=512,
|
140 |
+
maximum=8192,
|
141 |
+
value=4096,
|
142 |
step=256,
|
143 |
label="Max New Tokens"
|
144 |
)
|
|
|
157 |
label="Top-P"
|
158 |
)
|
159 |
|
160 |
+
def get_language(filename):
|
161 |
+
if filename.endswith(".html") or filename.endswith(".htm"): return "html"
|
162 |
+
if filename.endswith(".css"): return "css"
|
163 |
+
if filename.endswith(".js"): return "javascript"
|
164 |
+
if filename.endswith(".py"): return "python"
|
165 |
+
if filename.endswith(".json"): return "json"
|
166 |
+
if filename.endswith(".sql"): return "sql"
|
167 |
+
if filename.endswith(".php"): return "php"
|
168 |
+
if filename.endswith(".go"): return "go"
|
169 |
+
if filename.endswith(".java"): return "java"
|
170 |
+
if filename.endswith(".rb"): return "ruby"
|
171 |
+
if filename.endswith(".sh"): return "shell"
|
172 |
+
if filename.endswith(".yml") or filename.endswith(".yaml"): return "yaml"
|
173 |
+
if filename.endswith(".md"): return "markdown"
|
174 |
+
return "text"
|
175 |
+
|
176 |
def generate_and_display(prompt, backend, max_tokens, temperature, top_p):
|
177 |
full_code = generate_code(prompt, backend, max_tokens, temperature, top_p)
|
178 |
+
|
179 |
+
if full_code.startswith("## Error"):
|
180 |
+
return {
|
181 |
+
main_output_label: gr.Markdown.update(value="### Error Occurred"),
|
182 |
+
main_output_code: gr.Code.update(value=full_code, language="markdown"),
|
183 |
+
extra_outputs_column: gr.Column.update(visible=False, children=[])
|
184 |
+
}
|
185 |
+
if full_code.startswith("Error: Empty response"):
|
186 |
+
return {
|
187 |
+
main_output_label: gr.Markdown.update(value="### Error Occurred"),
|
188 |
+
main_output_code: gr.Code.update(value=full_code, language="text"),
|
189 |
+
extra_outputs_column: gr.Column.update(visible=False, children=[])
|
190 |
+
}
|
191 |
+
|
192 |
files = split_files(full_code)
|
193 |
+
dynamic_components = []
|
194 |
+
main_file_content = full_code
|
195 |
+
main_file_lang = "text"
|
196 |
+
main_file_label = "### Full Generated Code"
|
197 |
+
|
198 |
+
|
199 |
+
if not files:
|
200 |
+
main_file_content = full_code # Show full code if split failed but we got output
|
201 |
+
main_file_lang = get_language("output.txt") # Basic guess
|
202 |
+
main_file_label = "### Full Generated Output (No Files Detected)"
|
203 |
+
return {
|
204 |
+
main_output_label: gr.Markdown.update(value=main_file_label),
|
205 |
+
main_output_code: gr.Code.update(value=main_file_content, language=main_file_lang),
|
206 |
+
extra_outputs_column: gr.Column.update(visible=False, children=[])
|
207 |
+
}
|
208 |
+
|
209 |
+
if len(files) == 1:
|
210 |
+
main_file_content = files[0][1]
|
211 |
+
main_file_lang = get_language(files[0][0])
|
212 |
+
main_file_label = f"### File: {files[0][0]}"
|
213 |
+
return {
|
214 |
+
main_output_label: gr.Markdown.update(value=main_file_label),
|
215 |
+
main_output_code: gr.Code.update(value=main_file_content, language=main_file_lang),
|
216 |
+
extra_outputs_column: gr.Column.update(visible=False, children=[])
|
217 |
+
}
|
218 |
+
else:
|
219 |
+
main_file_content = files[0][1]
|
220 |
+
main_file_lang = get_language(files[0][0])
|
221 |
+
main_file_label = f"### File: {files[0][0]}"
|
222 |
+
|
223 |
+
for i, (filename, content) in enumerate(files[1:], start=1): # Start from the second file for extras
|
224 |
+
lang = get_language(filename)
|
225 |
+
dynamic_components.append(gr.Markdown(f"### File: {filename}"))
|
226 |
+
dynamic_components.append(gr.Code(value=content, language=lang, label=filename, interactive=False))
|
227 |
+
|
228 |
+
return {
|
229 |
+
main_output_label: gr.Markdown.update(value=main_file_label),
|
230 |
+
main_output_code: gr.Code.update(value=main_file_content, language=main_file_lang),
|
231 |
+
extra_outputs_column: gr.Column.update(visible=True, children=dynamic_components)
|
232 |
+
}
|
233 |
+
|
234 |
|
235 |
generate_button.click(
|
236 |
fn=generate_and_display,
|
237 |
inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
|
238 |
+
outputs=[main_output_label, main_output_code, extra_outputs_column]
|
239 |
)
|
240 |
|
241 |
if __name__ == "__main__":
|
242 |
+
demo.queue().launch()
|