Spaces:
Sleeping
Sleeping
AkshayM commited on
Commit Β·
ddc3343
1
Parent(s): 720b066
Add Qwen2.5-Coder-3B streaming demo on ZeroGPU
Browse filesFiles changed (3)
- README.md+23-4
- app.pyAdded+81
- requirements.txtAdded+1
12345678910111213---title: Small Code Assistantemoji: πcolorFrom: graycolorTo: indigosdk: gradiosdk_version: 6.29.0python_version: '3.12'app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132---title: Small Code Assistantemoji: π§βπ»colorFrom: bluecolorTo: indigosdk: gradiosdk_version: 6.29.0app_file: app.pypinned: falseshort_description: Streaming 3B coding model on ZeroGPUpython_version: "3.12"startup_duration_timeout: 30m---# Small Code AssistantA streaming chat demo for code generation, running [Qwen/Qwen2.5-Coder-3B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-3B-Instruct)(6.2 GB bf16) on ZeroGPU.## Why this modelThe demo was originally scoped to [CohereLabs/North-Mini-Code-1.0](https://huggingface.co/CohereLabs/North-Mini-Code-1.0),which is a 30.5B MoE (~61 GB bf16, 49 safetensors shards) β far beyond a free Space.Qwen2.5-Coder-3B is the closest small stand-in that fits the brief (under 10 GB).To swap models, change `MODEL_ID` at the top of `app.py`. North-Mini-Code itself isserved remotely by Cohere (see the [official demo](https://huggingface.co/spaces/akhaliq/North-Mini-Code-1.0))and would need a Cohere API key rather than local weights.## APIOpen `/api` or use the MCP endpoint exposed via `demo.launch(mcp_server=True)`.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import spaces # noqa: F401 must be imported before torchimport torchimport gradio as grfrom threading import Threadfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
MODEL_ID = "Qwen/Qwen2.5-Coder-3B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype=torch.bfloat16, attn_implementation="sdpa",).to("cuda").eval()
@spaces.GPU(duration=60)def respond( message: str, history: list[dict[str, str]], system_message: str, max_tokens: int, temperature: float, top_p: float,) -> str: """Generate a reply from the coding assistant, streaming it token by token.""" messages = [{"role": "system", "content": system_message}] messages += history messages.append({"role": "user", "content": message})
input_ids = tokenizer.apply_chat_template( messages, add_generation_prompt=True, return_tensors="pt" ).to("cuda")
streamer = TextIteratorStreamer( tokenizer, skip_prompt=True, skip_special_tokens=True ) gen_kwargs = dict( input_ids=input_ids, streamer=streamer, max_new_tokens=max_tokens ) if temperature > 0.1: gen_kwargs.update(do_sample=True, temperature=temperature, top_p=top_p) else: gen_kwargs.update(do_sample=False)
Thread(target=model.generate, kwargs=gen_kwargs, daemon=True).start()
response = "" for new_text in streamer: response += new_text yield response
demo = gr.ChatInterface( fn=respond, title="Small Code Assistant", description="Qwen2.5-Coder-3B-Instruct streaming on ZeroGPU.", additional_inputs=[ gr.Textbox( value="You are a helpful coding assistant.", label="System message" ), gr.Slider( minimum=64, maximum=2048, value=512, step=64, label="Max new tokens" ), gr.Slider( minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature" ), gr.Slider( minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p" ), ], examples=[ ["Write a Python function that checks if a string is a palindrome."], ["Explain what this regex does: ^\\d{3}-\\d{4}$"], ["Fix the bug: `for i in range(len(items)): print(items[i+1])`"], ], cache_examples=False,)
if __name__ == "__main__": demo.launch(mcp_server=True)1transformersREADME.md+23-4
app.pyAdded+81
requirements.txtAdded+1