Spaces:
Running on Zero
Running on Zero
Deploy Qwen2.5 Coder 3B Gradio app
Browse files- README.md +17 -13
- app.py +114 -0
- deploy.py +81 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Qwen2.5 Coder 3B
|
| 3 |
+
emoji: 💻
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
python_version: 3.12
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Qwen2.5 Coder 3B
|
| 13 |
+
|
| 14 |
+
A coding assistant using Qwen2.5-Coder-3B-Instruct with Hugging Face ZeroGPU.
|
| 15 |
+
The model is downloaded from Hugging Face on first startup. Inference requests
|
| 16 |
+
use the largest ZeroGPU allocation; xlarge requests consume twice the daily
|
| 17 |
+
GPU quota of the default large size.
|
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-3B-Instruct"
|
| 10 |
+
MAX_INPUT_TOKENS = 8192
|
| 11 |
+
MAX_NEW_TOKENS = 512
|
| 12 |
+
DEFAULT_SYSTEM_PROMPT = (
|
| 13 |
+
"You are a helpful coding assistant. "
|
| 14 |
+
"Give accurate, clear answers and use code examples when useful."
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
torch.set_num_threads(min(4, os.cpu_count() or 1))
|
| 18 |
+
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
low_cpu_mem_usage=True,
|
| 24 |
+
attn_implementation="sdpa",
|
| 25 |
+
)
|
| 26 |
+
model.to("cuda")
|
| 27 |
+
model.eval()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def normalize_history(history):
|
| 31 |
+
messages = []
|
| 32 |
+
|
| 33 |
+
for item in history or []:
|
| 34 |
+
if isinstance(item, dict):
|
| 35 |
+
role = item.get("role")
|
| 36 |
+
content = item.get("content")
|
| 37 |
+
if role in ("user", "assistant") and isinstance(content, str):
|
| 38 |
+
messages.append({"role": role, "content": content})
|
| 39 |
+
elif isinstance(item, (list, tuple)) and len(item) == 2:
|
| 40 |
+
user_message, assistant_message = item
|
| 41 |
+
if isinstance(user_message, str):
|
| 42 |
+
messages.append({"role": "user", "content": user_message})
|
| 43 |
+
if isinstance(assistant_message, str):
|
| 44 |
+
messages.append({"role": "assistant", "content": assistant_message})
|
| 45 |
+
|
| 46 |
+
return messages
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def prepare_inputs(system_prompt, messages):
|
| 50 |
+
chat_messages = [{"role": "system", "content": system_prompt}, *messages]
|
| 51 |
+
encoded = tokenizer.apply_chat_template(
|
| 52 |
+
chat_messages,
|
| 53 |
+
tokenize=True,
|
| 54 |
+
add_generation_prompt=True,
|
| 55 |
+
return_tensors="pt",
|
| 56 |
+
return_dict=True,
|
| 57 |
+
)
|
| 58 |
+
return encoded
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@spaces.GPU(size="xlarge", duration=60)
|
| 62 |
+
def generate(message, history, system_prompt):
|
| 63 |
+
system_prompt = (
|
| 64 |
+
DEFAULT_SYSTEM_PROMPT if system_prompt is None else system_prompt
|
| 65 |
+
)
|
| 66 |
+
messages = normalize_history(history)
|
| 67 |
+
messages.append({"role": "user", "content": message})
|
| 68 |
+
|
| 69 |
+
inputs = prepare_inputs(system_prompt, messages)
|
| 70 |
+
while inputs["input_ids"].shape[-1] > MAX_INPUT_TOKENS:
|
| 71 |
+
if len(messages) <= 1:
|
| 72 |
+
raise gr.Error(
|
| 73 |
+
"This message or system prompt is too long. Shorten it and try again."
|
| 74 |
+
)
|
| 75 |
+
messages = messages[2:] if len(messages) > 2 else messages[1:]
|
| 76 |
+
inputs = prepare_inputs(system_prompt, messages)
|
| 77 |
+
|
| 78 |
+
inputs = {key: value.to("cuda") for key, value in inputs.items()}
|
| 79 |
+
with torch.inference_mode():
|
| 80 |
+
output = model.generate(
|
| 81 |
+
**inputs,
|
| 82 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 83 |
+
do_sample=True,
|
| 84 |
+
temperature=0.2,
|
| 85 |
+
top_p=0.95,
|
| 86 |
+
repetition_penalty=1.05,
|
| 87 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 88 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
new_tokens = output[0, inputs["input_ids"].shape[-1]:]
|
| 92 |
+
return tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
system_prompt_input = gr.Textbox(
|
| 96 |
+
label="System prompt",
|
| 97 |
+
value=DEFAULT_SYSTEM_PROMPT,
|
| 98 |
+
lines=4,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
demo = gr.ChatInterface(
|
| 102 |
+
fn=generate,
|
| 103 |
+
chatbot=gr.Chatbot(type="messages", height=500),
|
| 104 |
+
additional_inputs=[system_prompt_input],
|
| 105 |
+
title="Qwen2.5 Coder 3B",
|
| 106 |
+
description=(
|
| 107 |
+
"A coding assistant powered by Qwen2.5-Coder-3B on Hugging Face ZeroGPU."
|
| 108 |
+
),
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
demo.queue()
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|
deploy.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
from huggingface_hub import HfApi
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
ROOT = Path(__file__).resolve().parent
|
| 9 |
+
CREDENTIALS_FILE = ROOT / "credentials.json"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def read_token():
|
| 13 |
+
if not CREDENTIALS_FILE.is_file():
|
| 14 |
+
raise FileNotFoundError(
|
| 15 |
+
f"Missing {CREDENTIALS_FILE}. Add your Hugging Face access token "
|
| 16 |
+
'as {"token": "hf_..."} in that file.'
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
with CREDENTIALS_FILE.open(encoding="utf-8") as credentials_file:
|
| 20 |
+
credentials = json.load(credentials_file)
|
| 21 |
+
|
| 22 |
+
token = credentials.get("token")
|
| 23 |
+
if not isinstance(token, str) or not token.strip():
|
| 24 |
+
raise ValueError(
|
| 25 |
+
f"No token found in {CREDENTIALS_FILE}. Add it as "
|
| 26 |
+
'{"token": "hf_..."} and try again.'
|
| 27 |
+
)
|
| 28 |
+
return token.strip()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
parser = argparse.ArgumentParser(
|
| 33 |
+
description="Create a Hugging Face Gradio Space and upload this app."
|
| 34 |
+
)
|
| 35 |
+
parser.add_argument(
|
| 36 |
+
"--name",
|
| 37 |
+
default="devcoder-ai",
|
| 38 |
+
help="Space name (default: devcoder-ai)",
|
| 39 |
+
)
|
| 40 |
+
parser.add_argument(
|
| 41 |
+
"--private",
|
| 42 |
+
action="store_true",
|
| 43 |
+
help="Create the Space as private (public by default).",
|
| 44 |
+
)
|
| 45 |
+
args = parser.parse_args()
|
| 46 |
+
|
| 47 |
+
api = HfApi(token=read_token())
|
| 48 |
+
account = api.whoami()
|
| 49 |
+
username = account.get("name")
|
| 50 |
+
if not username:
|
| 51 |
+
raise RuntimeError("Could not determine the Hugging Face account name.")
|
| 52 |
+
|
| 53 |
+
repo_id = f"{username}/{args.name}"
|
| 54 |
+
api.create_repo(
|
| 55 |
+
repo_id=repo_id,
|
| 56 |
+
repo_type="space",
|
| 57 |
+
space_sdk="gradio",
|
| 58 |
+
space_hardware="zero-a10g",
|
| 59 |
+
private=args.private,
|
| 60 |
+
exist_ok=True,
|
| 61 |
+
)
|
| 62 |
+
print(f"Created Space: https://huggingface.co/spaces/{repo_id}")
|
| 63 |
+
|
| 64 |
+
result = api.upload_folder(
|
| 65 |
+
repo_id=repo_id,
|
| 66 |
+
repo_type="space",
|
| 67 |
+
folder_path=str(ROOT),
|
| 68 |
+
ignore_patterns=[
|
| 69 |
+
".gitignore",
|
| 70 |
+
"credentials.json",
|
| 71 |
+
"__pycache__/**",
|
| 72 |
+
"*.py[cod]",
|
| 73 |
+
],
|
| 74 |
+
commit_message="Deploy Qwen2.5 Coder 3B Gradio app",
|
| 75 |
+
)
|
| 76 |
+
print(f"Uploaded app. Deployment status: https://huggingface.co/spaces/{repo_id}")
|
| 77 |
+
print(f"Upload commit: {result.commit_url}")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.8.0
|
| 2 |
+
transformers>=4.48.0,<5.0.0
|
| 3 |
+
accelerate>=1.2.0,<2.0.0
|
| 4 |
+
spaces>=0.37.0
|
| 5 |
+
gradio>=5.0,<6.0
|
| 6 |
+
huggingface_hub>=0.26.0,<1.0
|