Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
|
| 6 |
+
model_id = "ZyperAI/Z-AI-0.1-1.1B-Code.web"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_id,
|
| 10 |
+
torch_dtype=torch.float32,
|
| 11 |
+
device_map="cpu"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def generate_code(prompt, history):
|
| 15 |
+
messages = []
|
| 16 |
+
for human, assistant in history:
|
| 17 |
+
messages.append({"role": "user", "content": human})
|
| 18 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 19 |
+
messages.append({"role": "user", "content": prompt})
|
| 20 |
+
|
| 21 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to("cpu")
|
| 22 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
generation_kwargs = dict(
|
| 25 |
+
inputs=inputs,
|
| 26 |
+
streamer=streamer,
|
| 27 |
+
max_new_tokens=1024,
|
| 28 |
+
do_sample=True,
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
top_p=0.9
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 34 |
+
thread.start()
|
| 35 |
+
|
| 36 |
+
response = ""
|
| 37 |
+
for new_text in streamer:
|
| 38 |
+
response += new_text
|
| 39 |
+
yield response
|
| 40 |
+
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate")) as demo:
|
| 42 |
+
gr.Markdown("# **Z-AI Web Coder**")
|
| 43 |
+
gr.Markdown("### *Professional AI Coding Assistant Powered by ZyperAI*")
|
| 44 |
+
|
| 45 |
+
chatbot = gr.Chatbot(
|
| 46 |
+
height=600,
|
| 47 |
+
show_copy_button=True,
|
| 48 |
+
bubble_full_width=False,
|
| 49 |
+
type="messages"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
placeholder="Describe the web component or function you need...",
|
| 55 |
+
show_label=False,
|
| 56 |
+
scale=9
|
| 57 |
+
)
|
| 58 |
+
submit = gr.Button("Send", variant="primary", scale=1)
|
| 59 |
+
|
| 60 |
+
gr.Examples(
|
| 61 |
+
examples=[
|
| 62 |
+
"Create a responsive navigation bar using Tailwind CSS.",
|
| 63 |
+
"Write a JavaScript function to fetch data from an API and display it in a table.",
|
| 64 |
+
"Design a modern dark mode toggle using CSS variables."
|
| 65 |
+
],
|
| 66 |
+
inputs=msg
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
msg.submit(generate_code, [msg, chatbot], [chatbot])
|
| 70 |
+
submit.click(generate_code, [msg, chatbot], [chatbot])
|
| 71 |
+
msg.submit(lambda: "", None, [msg])
|
| 72 |
+
submit.click(lambda: "", None, [msg])
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch()
|