Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,19 +3,19 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
-
import spaces # Mandatory library for Hugging Face ZeroGPU
|
| 7 |
|
| 8 |
MODEL_ID = "DevStudio-AI/Devstudio-Coder-1.5B"
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
|
| 11 |
print("Loading tokenizer and base model...")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 15 |
"Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# Load
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
subfolder="models/final_merged",
|
|
@@ -23,57 +23,103 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 23 |
device_map="cpu",
|
| 24 |
token=HF_TOKEN
|
| 25 |
)
|
|
|
|
| 26 |
print("Model successfully loaded on CPU. Awaiting ZeroGPU allocation...")
|
| 27 |
|
| 28 |
-
|
| 29 |
@spaces.GPU
|
| 30 |
def generate_code(prompt, temperature, max_tokens):
|
| 31 |
try:
|
| 32 |
-
# Move model to
|
| 33 |
model.to("cuda")
|
| 34 |
-
|
| 35 |
-
# --- FIXED: Apply the identical System Prompt and ChatML formatting ---
|
| 36 |
system_prompt = (
|
| 37 |
"You are DevStudio-1.5B, an in-editor coding assistant developed by DevStudio AI. "
|
| 38 |
-
"You are a highly specialized master of modern single-file HTML and Tailwind CSS designs.
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
# Build conversational structure matching your training data
|
| 42 |
messages = [
|
| 43 |
{"role": "system", "content": system_prompt},
|
| 44 |
{"role": "user", "content": prompt}
|
| 45 |
]
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
formatted_prompt = tokenizer.apply_chat_template(
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
outputs = model.generate(
|
| 55 |
**inputs,
|
| 56 |
max_new_tokens=int(max_tokens),
|
| 57 |
temperature=float(temperature),
|
| 58 |
do_sample=True,
|
| 59 |
-
eos_token_id=tokenizer.eos_token_id
|
|
|
|
| 60 |
)
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error during generation: {str(e)}"
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=generate_code,
|
| 71 |
inputs=[
|
| 72 |
-
gr.Textbox(
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
],
|
| 76 |
-
outputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 77 |
title="DevStudio-1.5B API Engine",
|
| 78 |
description="Static HTML + Tailwind CSS specialized completion endpoint running on ZeroGPU."
|
| 79 |
)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
import spaces # Mandatory library for Hugging Face ZeroGPU
|
| 7 |
|
| 8 |
MODEL_ID = "DevStudio-AI/Devstudio-Coder-1.5B"
|
| 9 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 10 |
|
| 11 |
print("Loading tokenizer and base model...")
|
| 12 |
|
| 13 |
+
# Load tokenizer
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 15 |
"Qwen/Qwen2.5-Coder-1.5B-Instruct"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Load model
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
MODEL_ID,
|
| 21 |
subfolder="models/final_merged",
|
|
|
|
| 23 |
device_map="cpu",
|
| 24 |
token=HF_TOKEN
|
| 25 |
)
|
| 26 |
+
|
| 27 |
print("Model successfully loaded on CPU. Awaiting ZeroGPU allocation...")
|
| 28 |
|
| 29 |
+
|
| 30 |
@spaces.GPU
|
| 31 |
def generate_code(prompt, temperature, max_tokens):
|
| 32 |
try:
|
| 33 |
+
# Move model to GPU inside the ZeroGPU context
|
| 34 |
model.to("cuda")
|
| 35 |
+
|
|
|
|
| 36 |
system_prompt = (
|
| 37 |
"You are DevStudio-1.5B, an in-editor coding assistant developed by DevStudio AI. "
|
| 38 |
+
"You are a highly specialized master of modern single-file HTML and Tailwind CSS designs."
|
| 39 |
)
|
| 40 |
+
|
|
|
|
| 41 |
messages = [
|
| 42 |
{"role": "system", "content": system_prompt},
|
| 43 |
{"role": "user", "content": prompt}
|
| 44 |
]
|
| 45 |
+
|
| 46 |
+
# Apply ChatML formatting
|
| 47 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
| 48 |
+
messages,
|
| 49 |
+
tokenize=False,
|
| 50 |
+
add_generation_prompt=True
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
inputs = tokenizer(
|
| 54 |
+
formatted_prompt,
|
| 55 |
+
return_tensors="pt"
|
| 56 |
+
).to("cuda")
|
| 57 |
+
|
| 58 |
outputs = model.generate(
|
| 59 |
**inputs,
|
| 60 |
max_new_tokens=int(max_tokens),
|
| 61 |
temperature=float(temperature),
|
| 62 |
do_sample=True,
|
| 63 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 64 |
+
pad_token_id=tokenizer.eos_token_id
|
| 65 |
)
|
| 66 |
+
|
| 67 |
+
# Decode only the generated part
|
| 68 |
generated_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 69 |
+
result = tokenizer.decode(
|
| 70 |
+
generated_ids,
|
| 71 |
+
skip_special_tokens=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# ---------------------------
|
| 75 |
+
# Post-process escaped output
|
| 76 |
+
# ---------------------------
|
| 77 |
+
result = result.replace("\\n", "\n")
|
| 78 |
+
result = result.replace("\\t", "\t")
|
| 79 |
+
result = result.replace('\\"', '"')
|
| 80 |
+
result = result.replace("\\'", "'")
|
| 81 |
+
|
| 82 |
+
# Remove escaped markdown fences if present
|
| 83 |
+
result = result.replace("\\`", "`")
|
| 84 |
+
|
| 85 |
+
return result
|
| 86 |
+
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error during generation: {str(e)}"
|
| 89 |
|
| 90 |
+
finally:
|
| 91 |
+
# Free GPU memory after each request
|
| 92 |
+
if torch.cuda.is_available():
|
| 93 |
+
model.to("cpu")
|
| 94 |
+
torch.cuda.empty_cache()
|
| 95 |
+
|
| 96 |
+
|
| 97 |
demo = gr.Interface(
|
| 98 |
fn=generate_code,
|
| 99 |
inputs=[
|
| 100 |
+
gr.Textbox(
|
| 101 |
+
label="Prompt",
|
| 102 |
+
placeholder="Enter your prompt here...",
|
| 103 |
+
lines=8
|
| 104 |
+
),
|
| 105 |
+
gr.Slider(
|
| 106 |
+
minimum=0.1,
|
| 107 |
+
maximum=1.0,
|
| 108 |
+
value=0.3,
|
| 109 |
+
label="Temperature"
|
| 110 |
+
),
|
| 111 |
+
gr.Slider(
|
| 112 |
+
minimum=64,
|
| 113 |
+
maximum=2048,
|
| 114 |
+
value=1024,
|
| 115 |
+
step=64,
|
| 116 |
+
label="Max Tokens"
|
| 117 |
+
)
|
| 118 |
],
|
| 119 |
+
outputs=gr.Textbox(
|
| 120 |
+
label="Generated Code",
|
| 121 |
+
lines=30
|
| 122 |
+
),
|
| 123 |
title="DevStudio-1.5B API Engine",
|
| 124 |
description="Static HTML + Tailwind CSS specialized completion endpoint running on ZeroGPU."
|
| 125 |
)
|