Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,6 @@ 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 |
-
# 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,
|
|
@@ -24,19 +22,31 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 24 |
print("Model loaded successfully.")
|
| 25 |
|
| 26 |
def generate_code(prompt, history):
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 37 |
|
| 38 |
generation_kwargs = dict(
|
| 39 |
-
|
| 40 |
streamer=streamer,
|
| 41 |
max_new_tokens=1024,
|
| 42 |
do_sample=True,
|
|
@@ -47,16 +57,19 @@ def generate_code(prompt, history):
|
|
| 47 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 48 |
thread.start()
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
for new_text in streamer:
|
| 52 |
-
|
| 53 |
-
yield
|
| 54 |
|
| 55 |
# Gradio 6.x UI setup
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 57 |
gr.Markdown("# ⚡ **Z-AI Web Coder**")
|
| 58 |
|
| 59 |
-
# In Gradio 6, type="messages" is the standard for the chatbot component
|
| 60 |
chatbot = gr.Chatbot(height=500, show_copy_button=True, type="messages")
|
| 61 |
with gr.Row():
|
| 62 |
msg = gr.Textbox(
|
|
@@ -65,10 +78,20 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 65 |
scale=9
|
| 66 |
)
|
| 67 |
submit = gr.Button("Build", variant="primary", scale=1)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
submit.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
-
demo.launch()
|
|
|
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 9 |
|
|
|
|
|
|
|
| 10 |
print("Loading model and tokenizer...")
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
model_id,
|
|
|
|
| 22 |
print("Model loaded successfully.")
|
| 23 |
|
| 24 |
def generate_code(prompt, history):
|
| 25 |
+
# Fix 1: Properly structure history for Gradio 6's list-of-dicts style
|
| 26 |
+
messages = []
|
| 27 |
+
for msg in history:
|
| 28 |
+
# Prevent appending empty or broken dictionary structures
|
| 29 |
+
if msg.get("content"):
|
| 30 |
+
messages.append({"role": msg["role"], "content": msg["content"]})
|
| 31 |
+
|
| 32 |
+
messages.append({"role": "user", "content": prompt})
|
| 33 |
|
| 34 |
+
# Fix 2: Explicitly handle chat template errors if tokens are missing
|
| 35 |
+
try:
|
| 36 |
+
inputs = tokenizer.apply_chat_template(
|
| 37 |
+
messages,
|
| 38 |
+
add_generation_prompt=True,
|
| 39 |
+
return_tensors="pt"
|
| 40 |
+
).to("cpu")
|
| 41 |
+
except Exception:
|
| 42 |
+
# Fallback if the specific model lacks a pre-configured chat template
|
| 43 |
+
fallback_prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages]) + "\nassistant:"
|
| 44 |
+
inputs = tokenizer(fallback_prompt, return_tensors="pt").input_ids.to("cpu")
|
| 45 |
|
| 46 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 47 |
|
| 48 |
generation_kwargs = dict(
|
| 49 |
+
input_ids=inputs, # Fix 3: Transformers generation kwargs expects 'input_ids', not 'inputs'
|
| 50 |
streamer=streamer,
|
| 51 |
max_new_tokens=1024,
|
| 52 |
do_sample=True,
|
|
|
|
| 57 |
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 58 |
thread.start()
|
| 59 |
|
| 60 |
+
# Fix 4: Gradio 6 gr.Chatbot (type="messages") yields back the full history list,
|
| 61 |
+
# not just a single raw string.
|
| 62 |
+
updated_history = messages.copy()
|
| 63 |
+
updated_history.append({"role": "assistant", "content": ""})
|
| 64 |
+
|
| 65 |
for new_text in streamer:
|
| 66 |
+
updated_history[-1]["content"] += new_text
|
| 67 |
+
yield updated_history
|
| 68 |
|
| 69 |
# Gradio 6.x UI setup
|
| 70 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
| 71 |
gr.Markdown("# ⚡ **Z-AI Web Coder**")
|
| 72 |
|
|
|
|
| 73 |
chatbot = gr.Chatbot(height=500, show_copy_button=True, type="messages")
|
| 74 |
with gr.Row():
|
| 75 |
msg = gr.Textbox(
|
|
|
|
| 78 |
scale=9
|
| 79 |
)
|
| 80 |
submit = gr.Button("Build", variant="primary", scale=1)
|
| 81 |
+
|
| 82 |
+
# Fix 5: Use a unified event pipeline so input clearing
|
| 83 |
+
# doesn't disrupt the streaming text generator.
|
| 84 |
+
submit_click = submit.click(
|
| 85 |
+
generate_code,
|
| 86 |
+
inputs=[msg, chatbot],
|
| 87 |
+
outputs=[chatbot]
|
| 88 |
+
).then(lambda: "", None, [msg])
|
| 89 |
+
|
| 90 |
+
msg_submit = msg.submit(
|
| 91 |
+
generate_code,
|
| 92 |
+
inputs=[msg, chatbot],
|
| 93 |
+
outputs=[chatbot]
|
| 94 |
+
).then(lambda: "", None, [msg])
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
demo.launch()
|