Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,6 @@ def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature
|
|
| 26 |
{"role": "system", "content": system_message},
|
| 27 |
{"role": "user", "content": user_prompt}
|
| 28 |
]
|
| 29 |
-
response_stream = ""
|
| 30 |
full_response = ""
|
| 31 |
try:
|
| 32 |
stream = client.chat_completion(
|
|
@@ -39,9 +38,7 @@ def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature
|
|
| 39 |
for message in stream:
|
| 40 |
token = message.choices[0].delta.content
|
| 41 |
if isinstance(token, str):
|
| 42 |
-
response_stream += token
|
| 43 |
full_response += token
|
| 44 |
-
yield {"raw": response_stream}
|
| 45 |
cleaned_response = full_response.strip()
|
| 46 |
cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
|
| 47 |
cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
|
|
@@ -53,26 +50,22 @@ def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature
|
|
| 53 |
for phrase in common_phrases:
|
| 54 |
if cleaned_response.lower().startswith(phrase.lower()):
|
| 55 |
cleaned_response = cleaned_response[len(phrase):].lstrip()
|
| 56 |
-
|
| 57 |
except Exception as e:
|
| 58 |
-
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
content = file_splits[i+1].strip() if (i+1) < len(file_splits) else ""
|
| 73 |
-
components.append(gr.Markdown(f"### {filename}"))
|
| 74 |
-
components.append(gr.Code(value=content, label=filename, language="html" if filename.endswith(".html") else "python" if filename.endswith(".py") else "javascript" if filename.endswith(".js") else "text", interactive=False))
|
| 75 |
-
return components
|
| 76 |
|
| 77 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
| 78 |
gr.Markdown("# ✨ Website Code Generator ✨")
|
|
@@ -98,7 +91,14 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 98 |
)
|
| 99 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
| 100 |
with gr.Column(scale=3):
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with gr.Accordion("Advanced Settings", open=False):
|
| 103 |
max_tokens_slider = gr.Slider(
|
| 104 |
minimum=512,
|
|
@@ -108,7 +108,8 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 108 |
label="Max New Tokens"
|
| 109 |
)
|
| 110 |
temperature_slider = gr.Slider(
|
| 111 |
-
minimum=0.1,
|
|
|
|
| 112 |
value=0.7,
|
| 113 |
step=0.1,
|
| 114 |
label="Temperature"
|
|
@@ -120,14 +121,20 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 120 |
step=0.05,
|
| 121 |
label="Top-P"
|
| 122 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
generate_button.click(
|
| 124 |
-
fn=
|
| 125 |
inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
|
| 126 |
-
outputs=
|
| 127 |
-
).then(
|
| 128 |
-
fn=display_output,
|
| 129 |
-
inputs=output_display,
|
| 130 |
-
outputs=output_display,
|
| 131 |
)
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
|
|
|
| 26 |
{"role": "system", "content": system_message},
|
| 27 |
{"role": "user", "content": user_prompt}
|
| 28 |
]
|
|
|
|
| 29 |
full_response = ""
|
| 30 |
try:
|
| 31 |
stream = client.chat_completion(
|
|
|
|
| 38 |
for message in stream:
|
| 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)
|
|
|
|
| 50 |
for phrase in common_phrases:
|
| 51 |
if cleaned_response.lower().startswith(phrase.lower()):
|
| 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=\{(.+?)\}', full_code_text)
|
| 60 |
+
if not splits[0].strip():
|
| 61 |
+
splits = splits[1:]
|
| 62 |
+
for i in range(0, len(splits), 2):
|
| 63 |
+
if i+1 >= len(splits):
|
| 64 |
+
continue
|
| 65 |
+
filename = splits[i].strip()
|
| 66 |
+
content = splits[i+1].strip()
|
| 67 |
+
file_blocks.append((filename, content))
|
| 68 |
+
return file_blocks
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
| 71 |
gr.Markdown("# ✨ Website Code Generator ✨")
|
|
|
|
| 91 |
)
|
| 92 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
| 93 |
with gr.Column(scale=3):
|
| 94 |
+
main_output = gr.Code(
|
| 95 |
+
label="Full Generated Code",
|
| 96 |
+
language="html",
|
| 97 |
+
lines=25,
|
| 98 |
+
interactive=False,
|
| 99 |
+
)
|
| 100 |
+
extra_outputs = gr.Column()
|
| 101 |
+
|
| 102 |
with gr.Accordion("Advanced Settings", open=False):
|
| 103 |
max_tokens_slider = gr.Slider(
|
| 104 |
minimum=512,
|
|
|
|
| 108 |
label="Max New Tokens"
|
| 109 |
)
|
| 110 |
temperature_slider = gr.Slider(
|
| 111 |
+
minimum=0.1,
|
| 112 |
+
maximum=1.2,
|
| 113 |
value=0.7,
|
| 114 |
step=0.1,
|
| 115 |
label="Temperature"
|
|
|
|
| 121 |
step=0.05,
|
| 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 |
+
file_outputs = []
|
| 129 |
+
for filename, content in files:
|
| 130 |
+
file_outputs.append(gr.Markdown.update(value=f"### {filename}"))
|
| 131 |
+
file_outputs.append(gr.Code.update(value=content, language="html" if filename.endswith(".html") else "python" if filename.endswith(".py") else "javascript" if filename.endswith(".js") else "text"))
|
| 132 |
+
return full_code, file_outputs
|
| 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=[main_output, extra_outputs]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|