Spaces:
Build error
Build error
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer | |
| from threading import Thread | |
| MODEL_ID = "BrainboxAI/code-il-E4B-safetensors" | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_ID, | |
| torch_dtype=torch.float32, | |
| device_map="cpu", | |
| low_cpu_mem_usage=True, | |
| ) | |
| model.eval() | |
| EXAMPLES = [ | |
| ["Implement binary search in TypeScript with full edge case handling and JSDoc comments."], | |
| ["Build a FastAPI endpoint that accepts a file upload, validates it's a PDF under 10MB, and returns its text content."], | |
| ["Write an n8n Code node (JavaScript) that takes input items, deduplicates by 'email' field, and returns the unique ones."], | |
| ["讛住讘专 讗转 讛拽讜讚 讛讘讗 讜转爪讬注 砖讬驻讜专讬诐:\n\nfunction calc(arr) {\n let s = 0;\n for (let i = 0; i < arr.length; i++) s += arr[i];\n return s / arr.length;\n}"], | |
| ] | |
| def generate(message, history, temperature, max_tokens): | |
| messages = [] | |
| for msg in history: | |
| if isinstance(msg, dict): | |
| messages.append({"role": msg["role"], "content": msg["content"]}) | |
| else: | |
| user_msg, assistant_msg = msg | |
| messages.append({"role": "user", "content": user_msg}) | |
| if assistant_msg: | |
| messages.append({"role": "assistant", "content": assistant_msg}) | |
| messages.append({"role": "user", "content": message}) | |
| inputs = tokenizer.apply_chat_template( | |
| messages, | |
| return_tensors="pt", | |
| add_generation_prompt=True, | |
| ) | |
| streamer = TextIteratorStreamer( | |
| tokenizer, skip_prompt=True, skip_special_tokens=True | |
| ) | |
| thread = Thread(target=model.generate, kwargs={ | |
| "input_ids": inputs, | |
| "max_new_tokens": max_tokens, | |
| "temperature": temperature, | |
| "top_p": 0.95, | |
| "do_sample": temperature > 0, | |
| "streamer": streamer, | |
| "pad_token_id": tokenizer.eos_token_id, | |
| }) | |
| thread.start() | |
| output = "" | |
| for token in streamer: | |
| output += token | |
| yield output | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), title="Code-IL E4B") as demo: | |
| gr.Markdown(""" | |
| # code-il-E4B | |
| ### 诪讜讚诇 拽讜讚 讬砖专讗诇讬 - 4 诪讬诇讬讗专讚 驻专诪讟专讬诐, 专抓 注诇 诪讞砖讘 谞讬讬讚 | |
| 诪讜讚诇 诪讘讜住住 Gemma 4 E4B 砖讗讜诪谉 注诇 OpenCodeInstruct 砖诇 NVIDIA + dataset 拽讜讚 注讘专讬-讗谞讙诇讬 诪砖诇讬. | |
| 诪转诪讞讛 讘-Python, TypeScript, n8n, 讜转讜诪讱 讘注讘专讬转. | |
| > 鈿狅笍 **讚诪讜 讝讛 专抓 注诇 CPU - 讬拽讞 10-30 砖谞讬讜转 诇转砖讜讘讛.** | |
| > 诇讛专爪讛 诪讛讬专讛 讘诪讞砖讘 砖诇讱: 专讗讛 讛讜专讗讜转 讘转讞转讬转. | |
| **By [BrainboxAI](https://huggingface.co/BrainboxAI)** - Powered by Unsloth | |
| """) | |
| chat = gr.ChatInterface( | |
| fn=generate, | |
| type="messages", | |
| examples=EXAMPLES, | |
| cache_examples=False, | |
| additional_inputs=[ | |
| gr.Slider(0.0, 1.0, value=0.2, step=0.05, label="Temperature"), | |
| gr.Slider(64, 512, value=256, step=64, label="Max Tokens (谞诪讜讱 = 诪讛讬专 讬讜转专)"), | |
| ], | |
| additional_inputs_accordion=gr.Accordion("鈿欙笍 讛讙讚专讜转 诪转拽讚诪讜转", open=False), | |
| ) | |
| gr.Markdown(""" | |
| --- | |
| ### 讛专爪讛 诪拽讜诪讬转 (诪讛讬专讛) | |
| ```python | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| tokenizer = AutoTokenizer.from_pretrained("BrainboxAI/code-il-E4B-safetensors") | |
| model = AutoModelForCausalLM.from_pretrained("BrainboxAI/code-il-E4B-safetensors", | |
| torch_dtype="auto", device_map="auto") | |
| ``` | |
| **Training**: NVIDIA OpenCodeInstruct (4.97M) + BrainboxAI/code-training-il (40k) + bleugreen/typescript-instruct (41k) | |
| **Format**: Safetensors 16-bit | **License**: Apache 2.0 | **Languages**: English + 注讘专讬转 | |
| """) | |
| if __name__ == "__main__": | |
| demo.queue(max_size=10).launch() | |