Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,47 +3,35 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
|
| 6 |
-
# --- Configuration ---
|
| 7 |
API_TOKEN = os.getenv("HF_TOKEN", None)
|
| 8 |
MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 9 |
|
| 10 |
-
# --- Initialize Inference Client ---
|
| 11 |
try:
|
| 12 |
-
print(f"Initializing Inference Client for model: {MODEL}")
|
| 13 |
client = InferenceClient(model=MODEL, token=API_TOKEN) if API_TOKEN else InferenceClient(model=MODEL)
|
| 14 |
except Exception as e:
|
| 15 |
raise gr.Error(f"Failed to initialize model client. Error: {e}")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
def generate_code(
|
| 19 |
-
prompt: str,
|
| 20 |
-
backend_choice: str,
|
| 21 |
-
max_tokens: int,
|
| 22 |
-
temperature: float,
|
| 23 |
-
top_p: float,
|
| 24 |
-
):
|
| 25 |
-
print(f"Generating code for: {prompt[:100]}... | Backend: {backend_choice}")
|
| 26 |
-
|
| 27 |
-
# --- Dynamically Build System Message ---
|
| 28 |
system_message = (
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
-
|
| 37 |
user_prompt = f"USER_PROMPT = {prompt}\nUSER_BACKEND = {backend_choice}"
|
| 38 |
-
|
| 39 |
messages = [
|
| 40 |
{"role": "system", "content": system_message},
|
| 41 |
{"role": "user", "content": user_prompt}
|
| 42 |
]
|
| 43 |
-
|
| 44 |
response_stream = ""
|
| 45 |
full_response = ""
|
| 46 |
-
|
| 47 |
try:
|
| 48 |
stream = client.chat_completion(
|
| 49 |
messages=messages,
|
|
@@ -58,12 +46,10 @@ def generate_code(
|
|
| 58 |
response_stream += token
|
| 59 |
full_response += token
|
| 60 |
yield response_stream
|
| 61 |
-
|
| 62 |
cleaned_response = full_response.strip()
|
| 63 |
cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
|
| 64 |
cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
|
| 65 |
cleaned_response = re.sub(r"<\s*\|?\s*(user|assistant)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE)
|
| 66 |
-
|
| 67 |
common_phrases = [
|
| 68 |
"Here is the code:", "Okay, here is the code:", "Here's the code:",
|
| 69 |
"Sure, here is the code you requested:", "Let me know if you need anything else."
|
|
@@ -71,13 +57,10 @@ def generate_code(
|
|
| 71 |
for phrase in common_phrases:
|
| 72 |
if cleaned_response.lower().startswith(phrase.lower()):
|
| 73 |
cleaned_response = cleaned_response[len(phrase):].lstrip()
|
| 74 |
-
|
| 75 |
yield cleaned_response.strip()
|
| 76 |
-
|
| 77 |
except Exception as e:
|
| 78 |
yield f"## Error\n\nFailed to generate code.\n**Reason:** {e}"
|
| 79 |
|
| 80 |
-
# --- Build Gradio Interface ---
|
| 81 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
| 82 |
gr.Markdown("# ✨ Website Code Generator ✨")
|
| 83 |
gr.Markdown(
|
|
@@ -88,7 +71,6 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 88 |
"- Only generates websites. No other codes.\n"
|
| 89 |
"- Minimal necessary comments only."
|
| 90 |
)
|
| 91 |
-
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column(scale=2):
|
| 94 |
prompt_input = gr.Textbox(
|
|
@@ -103,15 +85,13 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 103 |
info="Hint only. Always generates only index.html."
|
| 104 |
)
|
| 105 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
| 106 |
-
|
| 107 |
with gr.Column(scale=3):
|
| 108 |
code_output = gr.Code(
|
| 109 |
-
label="Generated
|
| 110 |
language="html",
|
| 111 |
lines=30,
|
| 112 |
interactive=False,
|
| 113 |
)
|
| 114 |
-
|
| 115 |
with gr.Accordion("Advanced Settings", open=False):
|
| 116 |
max_tokens_slider = gr.Slider(
|
| 117 |
minimum=512,
|
|
@@ -124,9 +104,11 @@ with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
|
| 124 |
minimum=0.1, maximum=1.2, value=0.7, step=0.1, label="Temperature"
|
| 125 |
)
|
| 126 |
top_p_slider = gr.Slider(
|
| 127 |
-
minimum=0.1, maximum=1.0,
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
-
|
| 130 |
generate_button.click(
|
| 131 |
fn=generate_code,
|
| 132 |
inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
|
|
|
|
| 3 |
import os
|
| 4 |
import re
|
| 5 |
|
|
|
|
| 6 |
API_TOKEN = os.getenv("HF_TOKEN", None)
|
| 7 |
MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 8 |
|
|
|
|
| 9 |
try:
|
|
|
|
| 10 |
client = InferenceClient(model=MODEL, token=API_TOKEN) if API_TOKEN else InferenceClient(model=MODEL)
|
| 11 |
except Exception as e:
|
| 12 |
raise gr.Error(f"Failed to initialize model client. Error: {e}")
|
| 13 |
|
| 14 |
+
def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature: float, top_p: float):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
system_message = (
|
| 16 |
+
"You are an AI assistant programmed to generate website codes only. "
|
| 17 |
+
"You must not use triple backticks (```html, ```python, ```js, etc.) anywhere. "
|
| 18 |
+
"If multiple files are needed, separate them clearly using:\n"
|
| 19 |
+
"{index.html}\n.TAB{NAME=nextfile.ext}\n{anotherfile.ext}\n"
|
| 20 |
+
"You must not add unnecessary comments; only critical modification instructions are allowed. "
|
| 21 |
+
"Only generate code and nothing else — no explanations, no phrases like 'Here is the code'. "
|
| 22 |
+
"The user will select backend type (Static / Flask / Node.js), and you must generate accordingly: "
|
| 23 |
+
"- For Static: simple index.html.\n"
|
| 24 |
+
"- For Flask or Node.js: include minimal backend scripts with index.html frontend.\n"
|
| 25 |
+
"If the user requests non-website code, reply with:\n"
|
| 26 |
+
"'hey there! am here to create websites for you unfortunately am programmed to not create codes! otherwise I would go on the naughty list :-('."
|
| 27 |
)
|
|
|
|
| 28 |
user_prompt = f"USER_PROMPT = {prompt}\nUSER_BACKEND = {backend_choice}"
|
|
|
|
| 29 |
messages = [
|
| 30 |
{"role": "system", "content": system_message},
|
| 31 |
{"role": "user", "content": user_prompt}
|
| 32 |
]
|
|
|
|
| 33 |
response_stream = ""
|
| 34 |
full_response = ""
|
|
|
|
| 35 |
try:
|
| 36 |
stream = client.chat_completion(
|
| 37 |
messages=messages,
|
|
|
|
| 46 |
response_stream += token
|
| 47 |
full_response += token
|
| 48 |
yield response_stream
|
|
|
|
| 49 |
cleaned_response = full_response.strip()
|
| 50 |
cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
|
| 51 |
cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
|
| 52 |
cleaned_response = re.sub(r"<\s*\|?\s*(user|assistant)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE)
|
|
|
|
| 53 |
common_phrases = [
|
| 54 |
"Here is the code:", "Okay, here is the code:", "Here's the code:",
|
| 55 |
"Sure, here is the code you requested:", "Let me know if you need anything else."
|
|
|
|
| 57 |
for phrase in common_phrases:
|
| 58 |
if cleaned_response.lower().startswith(phrase.lower()):
|
| 59 |
cleaned_response = cleaned_response[len(phrase):].lstrip()
|
|
|
|
| 60 |
yield cleaned_response.strip()
|
|
|
|
| 61 |
except Exception as e:
|
| 62 |
yield f"## Error\n\nFailed to generate code.\n**Reason:** {e}"
|
| 63 |
|
|
|
|
| 64 |
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
|
| 65 |
gr.Markdown("# ✨ Website Code Generator ✨")
|
| 66 |
gr.Markdown(
|
|
|
|
| 71 |
"- Only generates websites. No other codes.\n"
|
| 72 |
"- Minimal necessary comments only."
|
| 73 |
)
|
|
|
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=2):
|
| 76 |
prompt_input = gr.Textbox(
|
|
|
|
| 85 |
info="Hint only. Always generates only index.html."
|
| 86 |
)
|
| 87 |
generate_button = gr.Button("✨ Generate Website Code", variant="primary")
|
|
|
|
| 88 |
with gr.Column(scale=3):
|
| 89 |
code_output = gr.Code(
|
| 90 |
+
label="Generated Code",
|
| 91 |
language="html",
|
| 92 |
lines=30,
|
| 93 |
interactive=False,
|
| 94 |
)
|
|
|
|
| 95 |
with gr.Accordion("Advanced Settings", open=False):
|
| 96 |
max_tokens_slider = gr.Slider(
|
| 97 |
minimum=512,
|
|
|
|
| 104 |
minimum=0.1, maximum=1.2, value=0.7, step=0.1, label="Temperature"
|
| 105 |
)
|
| 106 |
top_p_slider = gr.Slider(
|
| 107 |
+
minimum=0.1, maximum=1.0,
|
| 108 |
+
value=0.9,
|
| 109 |
+
step=0.05,
|
| 110 |
+
label="Top-P"
|
| 111 |
)
|
|
|
|
| 112 |
generate_button.click(
|
| 113 |
fn=generate_code,
|
| 114 |
inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
|