Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,30 @@ from threading import Thread
|
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return tokenizer, model
|
| 26 |
-
|
| 27 |
-
tokenizer, model = load_assets()
|
| 28 |
|
| 29 |
def generate_code(prompt, history):
|
| 30 |
-
messages
|
| 31 |
-
|
| 32 |
-
messages.append({"role": "user", "content": human})
|
| 33 |
-
messages.append({"role": "assistant", "content": assistant})
|
| 34 |
-
messages.append({"role": "user", "content": prompt})
|
| 35 |
|
| 36 |
-
#
|
|
|
|
|
|
|
|
|
|
| 37 |
inputs = tokenizer.apply_chat_template(
|
| 38 |
messages,
|
| 39 |
add_generation_prompt=True,
|
|
@@ -59,23 +56,26 @@ def generate_code(prompt, history):
|
|
| 59 |
response += new_text
|
| 60 |
yield response
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
with gr.Blocks(theme=gr.themes.
|
| 64 |
gr.Markdown("# ⚡ **Z-AI Web Coder**")
|
| 65 |
-
gr.Markdown("Optimized 1.1B model for high-speed web development.")
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
msg = gr.Textbox(
|
| 71 |
-
placeholder="E.g., Create a
|
| 72 |
show_label=False,
|
| 73 |
scale=9
|
| 74 |
)
|
| 75 |
-
submit = gr.Button("
|
| 76 |
|
|
|
|
| 77 |
msg.submit(generate_code, [msg, chatbot], [chatbot])
|
| 78 |
submit.click(generate_code, [msg, chatbot], [chatbot])
|
|
|
|
|
|
|
| 79 |
msg.submit(lambda: "", None, [msg])
|
| 80 |
submit.click(lambda: "", None, [msg])
|
| 81 |
|
|
|
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 9 |
|
| 10 |
+
# Professional loading logic: Since Gradio 6.x runs as a persistent server,
|
| 11 |
+
# global variables are naturally 'cached' for the duration of the process.
|
| 12 |
+
print("Loading model and tokenizer...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
+
model_id,
|
| 15 |
+
token=hf_token,
|
| 16 |
+
use_fast=False
|
| 17 |
+
)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
model_id,
|
| 20 |
+
torch_dtype=torch.float32,
|
| 21 |
+
device_map="cpu",
|
| 22 |
+
token=hf_token
|
| 23 |
+
)
|
| 24 |
+
print("Model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def generate_code(prompt, history):
|
| 27 |
+
# Gradio 6.x uses the 'messages' format by default
|
| 28 |
+
# history will look like: [{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Prepend existing history to the current prompt
|
| 31 |
+
messages = history + [{"role": "user", "content": prompt}]
|
| 32 |
+
|
| 33 |
+
# Prepare inputs using the model's chat template
|
| 34 |
inputs = tokenizer.apply_chat_template(
|
| 35 |
messages,
|
| 36 |
add_generation_prompt=True,
|
|
|
|
| 56 |
response += new_text
|
| 57 |
yield response
|
| 58 |
|
| 59 |
+
# Gradio 6.x UI setup
|
| 60 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 61 |
gr.Markdown("# ⚡ **Z-AI Web Coder**")
|
|
|
|
| 62 |
|
| 63 |
+
# In Gradio 6, type="messages" is the standard for the chatbot component
|
| 64 |
+
chatbot = gr.Chatbot(height=500, show_copy_button=True, type="messages")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
msg = gr.Textbox(
|
| 68 |
+
placeholder="E.g., Create a responsive navigation bar with CSS...",
|
| 69 |
show_label=False,
|
| 70 |
scale=9
|
| 71 |
)
|
| 72 |
+
submit = gr.Button("Build", variant="primary", scale=1)
|
| 73 |
|
| 74 |
+
# Wire up the events
|
| 75 |
msg.submit(generate_code, [msg, chatbot], [chatbot])
|
| 76 |
submit.click(generate_code, [msg, chatbot], [chatbot])
|
| 77 |
+
|
| 78 |
+
# Clear the input box after submission
|
| 79 |
msg.submit(lambda: "", None, [msg])
|
| 80 |
submit.click(lambda: "", None, [msg])
|
| 81 |
|