Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,19 +4,27 @@ import torch
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
from threading import Thread
|
| 6 |
|
| 7 |
-
# This pulls the token you just saved in the Space settings
|
| 8 |
hf_token = os.getenv("HF_TOKEN")
|
| 9 |
-
|
| 10 |
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def generate_code(prompt, history):
|
| 22 |
messages = []
|
|
@@ -25,7 +33,13 @@ def generate_code(prompt, history):
|
|
| 25 |
messages.append({"role": "assistant", "content": assistant})
|
| 26 |
messages.append({"role": "user", "content": prompt})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 30 |
|
| 31 |
generation_kwargs = dict(
|
|
@@ -45,33 +59,20 @@ def generate_code(prompt, history):
|
|
| 45 |
response += new_text
|
| 46 |
yield response
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Markdown("#
|
|
|
|
| 51 |
|
| 52 |
-
chatbot = gr.Chatbot(
|
| 53 |
-
height=600,
|
| 54 |
-
show_copy_button=True,
|
| 55 |
-
bubble_full_width=False,
|
| 56 |
-
type="messages"
|
| 57 |
-
)
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
msg = gr.Textbox(
|
| 61 |
-
placeholder="
|
| 62 |
-
show_label=False,
|
| 63 |
scale=9
|
| 64 |
)
|
| 65 |
-
submit = gr.Button("
|
| 66 |
-
|
| 67 |
-
gr.Examples(
|
| 68 |
-
examples=[
|
| 69 |
-
"Create a responsive navigation bar using Tailwind CSS.",
|
| 70 |
-
"Write a JavaScript function to fetch data from an API and display it in a table.",
|
| 71 |
-
"Design a modern dark mode toggle using CSS variables."
|
| 72 |
-
],
|
| 73 |
-
inputs=msg
|
| 74 |
-
)
|
| 75 |
|
| 76 |
msg.submit(generate_code, [msg, chatbot], [chatbot])
|
| 77 |
submit.click(generate_code, [msg, chatbot], [chatbot])
|
|
|
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
from threading import Thread
|
| 6 |
|
|
|
|
| 7 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
| 8 |
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 9 |
|
| 10 |
+
@gr.cache_resource
|
| 11 |
+
def load_assets():
|
| 12 |
+
# Adding use_fast=False can bypass some backend instantiation errors
|
| 13 |
+
# while still providing full functionality.
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 15 |
+
model_id,
|
| 16 |
+
token=hf_token,
|
| 17 |
+
use_fast=False
|
| 18 |
+
)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
model_id,
|
| 21 |
+
torch_dtype=torch.float32,
|
| 22 |
+
device_map="cpu",
|
| 23 |
+
token=hf_token
|
| 24 |
+
)
|
| 25 |
+
return tokenizer, model
|
| 26 |
+
|
| 27 |
+
tokenizer, model = load_assets()
|
| 28 |
|
| 29 |
def generate_code(prompt, history):
|
| 30 |
messages = []
|
|
|
|
| 33 |
messages.append({"role": "assistant", "content": assistant})
|
| 34 |
messages.append({"role": "user", "content": prompt})
|
| 35 |
|
| 36 |
+
# Apply the chat template specific to the ZyperAI model
|
| 37 |
+
inputs = tokenizer.apply_chat_template(
|
| 38 |
+
messages,
|
| 39 |
+
add_generation_prompt=True,
|
| 40 |
+
return_tensors="pt"
|
| 41 |
+
).to("cpu")
|
| 42 |
+
|
| 43 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 44 |
|
| 45 |
generation_kwargs = dict(
|
|
|
|
| 59 |
response += new_text
|
| 60 |
yield response
|
| 61 |
|
| 62 |
+
# Professional Dark-Themed UI
|
| 63 |
+
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", neutral_hue="zinc")) as demo:
|
| 64 |
+
gr.Markdown("# ⚡ **Z-AI Web Coder**")
|
| 65 |
+
gr.Markdown("Optimized 1.1B model for high-speed web development.")
|
| 66 |
|
| 67 |
+
chatbot = gr.Chatbot(height=550, show_copy_button=True, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
msg = gr.Textbox(
|
| 71 |
+
placeholder="E.g., Create a glassmorphism login form with Tailwind CSS...",
|
| 72 |
+
show_label=False,
|
| 73 |
scale=9
|
| 74 |
)
|
| 75 |
+
submit = gr.Button("Generate", variant="primary", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
msg.submit(generate_code, [msg, chatbot], [chatbot])
|
| 78 |
submit.click(generate_code, [msg, chatbot], [chatbot])
|