Spaces:
Runtime error
Runtime error
Upload gradio_app.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564"""Legion Coder - Gradio InterfaceA powerful coding assistant powered by the Legion Coder 8M model.10k Edition - 2026
MADE WITH BY DEATH LEGIONPOWERED BY nvdya-kit
2026 DEATH LEGION. All rights reserved."""
import osimport sysimport torchimport gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizer
# Model configurationMODEL_ID = "dineth554/legion-coder-8m-10k"
# Custom CSS for dark professional themecustom_css = """@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600;700&family=Inter:wght@400;500;600;700&family=Orbitron:wght@400;700&display=swap');
:root { --primary-gradient: linear-gradient(90deg, #ff0040 0%, #ff6b6b 25%, #7c4dff 75%, #9c27b0 100%); --nvdya-gradient: linear-gradient(90deg, #00d4ff 0%, #7c4dff 100%); --bg-dark: #0a0a0f; --bg-card: #1a1a2e; --border-color: rgba(255, 0, 64, 0.3); --text-primary: #ffffff; --text-secondary: rgba(255, 255, 255, 0.7); --accent-pink: #ff4081; --accent-cyan: #00d4ff;}
body { font-family: 'Inter', sans-serif !important; background: var(--bg-dark) !important;}
.gradio-container { background: linear-gradient(135deg, #0a0a0f 0%, #1a1a2e 50%, #16213e 100%) !important; min-height: 100vh !important;}
/* Header styling */.header-container { background: var(--primary-gradient) !important; background-size: 200% 200% !important; padding: 2rem !important; border-radius: 16px !important; margin-bottom: 1.5rem !important; text-align: center !important; animation: gradientShift 3s ease infinite !important;}
@keyframes gradientShift { 0% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0% 50%; }}
.header-title { font-family: 'Orbitron', sans-serif !important; font-size: 2.5rem !important; font-weight: 700 !important; color: #ffffff !important; text-shadow: 2px 2px 4px rgba(0,0,0,0.4) !important; margin: 0 !important;}
.header-subtitle { font-size: 1rem !important; color: rgba(255,255,255,0.9) !important; margin-top: 0.5rem !important;}
/* Banner styling */.death-legion-banner { background: var(--primary-gradient) !important; background-size: 200% 200% !important; padding: 1rem !important; border-radius: 12px !important; text-align: center !important; margin-bottom: 1rem !important; font-weight: 700 !important; font-size: 1.1rem !important; color: white !important; font-family: 'Orbitron', sans-serif !important; letter-spacing: 2px !important; animation: gradientShift 3s ease infinite, pulse 2s infinite !important;}
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(255, 0, 64, 0.4); } 70% { box-shadow: 0 0 0 15px rgba(255, 0, 64, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 0, 64, 0); }}
.nvdya-banner { background: var(--nvdya-gradient) !important; padding: 0.6rem !important; border-radius: 8px !important; text-align: center !important; margin-bottom: 1rem !important; font-weight: 600 !important; font-size: 0.95rem !important; color: white !important; font-family: 'Orbitron', sans-serif !important; letter-spacing: 1px !important;}
/* Sidebar styling */.sidebar-title { font-family: 'Orbitron', sans-serif !important; font-size: 1.2rem !important; font-weight: 700 !important; color: var(--accent-pink) !important; margin-bottom: 1rem !important; text-align: center !important; text-transform: uppercase !important; letter-spacing: 2px !important;}
.sidebar-section { background: rgba(255,255,255,0.05) !important; border-radius: 12px !important; padding: 1rem !important; margin-bottom: 1rem !important; border: 1px solid rgba(255,255,255,0.1) !important;}
.sidebar-label { font-size: 0.85rem !important; color: rgba(255,255,255,0.7) !important; margin-bottom: 0.3rem !important;}
.sidebar-value { font-family: 'JetBrains Mono', monospace !important; font-size: 1rem !important; font-weight: 600 !important; color: #ffffff !important;}
/* Downloads badge */.downloads-badge { background: linear-gradient(135deg, rgba(255,0,64,0.2) 0%, rgba(124,77,255,0.2) 100%) !important; border: 2px solid rgba(255,0,64,0.5) !important; border-radius: 16px !important; padding: 1.2rem !important; margin-bottom: 1rem !important; text-align: center !important;}
.downloads-label { color: var(--accent-pink) !important; font-weight: 700 !important; font-size: 0.8rem !important; margin-bottom: 0.5rem !important; font-family: 'Orbitron', sans-serif !important;}
.downloads-number { font-family: 'JetBrains Mono', monospace !important; font-size: 2rem !important; font-weight: 800 !important; background: linear-gradient(90deg, #ff0040, #ff6b6b) !important; -webkit-background-clip: text !important; -webkit-text-fill-color: transparent !important; margin: 0.5rem 0 !important;}
/* Chat interface */.chat-container { background: var(--bg-card) !important; border-radius: 16px !important; border: 1px solid var(--border-color) !important;}
.message.user { background: rgba(255, 0, 64, 0.1) !important; border-left: 3px solid #ff0040 !important;}
.message.bot { background: rgba(0, 212, 255, 0.1) !important; border-left: 3px solid #00d4ff !important;}
/* Input styling */.input-container textarea { background: rgba(255,255,255,0.05) !important; border: 1px solid rgba(255,255,255,0.1) !important; border-radius: 12px !important; color: white !important; font-family: 'JetBrains Mono', monospace !important;}
.input-container textarea:focus { border-color: var(--accent-pink) !important; box-shadow: 0 0 0 2px rgba(255, 0, 64, 0.2) !important;}
/* Button styling */button.primary { background: var(--primary-gradient) !important; border: none !important; border-radius: 8px !important; font-weight: 600 !important; text-transform: uppercase !important; letter-spacing: 1px !important;}
button.secondary { background: rgba(255,255,255,0.1) !important; border: 1px solid rgba(255,255,255,0.2) !important; border-radius: 8px !important;}
/* Code blocks */pre { background: #0d1117 !important; border-radius: 12px !important; border: 1px solid rgba(255,255,255,0.1) !important;}
code { font-family: 'JetBrains Mono', monospace !important; color: #e6edf3 !important;}
/* Footer */.footer { text-align: center !important; padding: 2rem !important; color: rgba(255,255,255,0.5) !important; font-size: 0.9rem !important; border-top: 2px solid rgba(255,255,255,0.1) !important; margin-top: 2rem !important;}
/* Deploy section */.deploy-section { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%) !important; border: 2px solid rgba(255, 0, 64, 0.4) !important; border-radius: 16px !important; padding: 1.5rem !important; margin: 1rem 0 !important;}
.deploy-title { color: var(--accent-pink) !important; font-weight: 700 !important; font-size: 1.2rem !important; margin-bottom: 1rem !important; font-family: 'Orbitron', sans-serif !important;}
/* Typing cursor */.typing-cursor { display: inline-block !important; width: 10px !important; height: 1.2em !important; background: linear-gradient(180deg, #ff4081, #ff0040) !important; animation: blink 0.8s step-end infinite !important; vertical-align: text-bottom !important; margin-left: 3px !important; border-radius: 2px !important;}
@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; }}
/* Scrollbar */::-webkit-scrollbar { width: 8px !important;}
::-webkit-scrollbar-track { background: rgba(255,255,255,0.05) !important;}
::-webkit-scrollbar-thumb { background: rgba(255, 0, 64, 0.5) !important; border-radius: 4px !important;}
::-webkit-scrollbar-thumb:hover { background: rgba(255, 0, 64, 0.7) !important;}"""
# Global model and tokenizermodel = Nonetokenizer = None
def load_model(): """Load the Legion Coder model and tokenizer.""" global model, tokenizer if model is None or tokenizer is None: try: tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained( MODEL_ID, torch_dtype=torch.float32, device_map="cpu", trust_remote_code=True ) return True except Exception as e: print(f"Error loading model: {e}") return False return True
def generate_code(message, history, temperature, max_tokens): """Generate code response using the Legion Coder model.""" global model, tokenizer if not load_model(): return "Error: Failed to load model. Please check the repository configuration." try: # Prepare system prompt system_prompt = "You are a helpful coding assistant. Write clean, efficient code." # Build conversation history conversation = system_prompt + "\n\n" for human, assistant in history: conversation += f"User: {human}\nAssistant: {assistant}\n\n" conversation += f"User: {message}\nAssistant:" # Tokenize inputs = tokenizer(conversation, return_tensors="pt", max_length=1024, truncation=True) # Generate with torch.no_grad(): outputs = model.generate( inputs["input_ids"], max_new_tokens=max_tokens, temperature=temperature, top_p=0.95, do_sample=True, pad_token_id=tokenizer.eos_token_id ) # Decode response = tokenizer.decode(outputs[0], skip_special_tokens=True) # Extract just the assistant response if "Assistant:" in response: response = response.split("Assistant:")[-1].strip() return response except Exception as e: return f"Error generating response: {str(e)}"
# Create the Gradio interfacedef create_interface(): """Create the Gradio chat interface.""" with gr.Blocks(css=custom_css, title="Legion Coder 2026") as demo: # Header gr.HTML(""" <div class="header-container"> <h1 class="header-title">LEGION CODER 2026</h1> <p class="header-subtitle">Advanced AI Code Generation by DEATH LEGION</p> <div style="margin-top: 0.8rem;"> <span style="background: rgba(0,0,0,0.3); padding: 0.4rem 1rem; border-radius: 25px; font-size: 0.8rem; font-weight: 600; color: #ff4081; border: 1px solid rgba(255,64,129,0.3);"> POWERED BY nvdya-kit </span> </div> </div> """) # Death Legion Banner gr.HTML(""" <div class="death-legion-banner"> MADE WITH BY DEATH LEGION 2026 </div> """) # nvdya-kit Banner gr.HTML(""" <div class="nvdya-banner"> Powered by nvdya-kit | Next-Gen AI Infrastructure </div> """) with gr.Row(): # Sidebar with gr.Column(scale=1): gr.HTML(""" <div class="sidebar-title">Model Specs 2026</div> <div class="sidebar-section"> <div class="sidebar-label">[ARCH] Architecture</div> <div class="sidebar-value">Transformer 2026</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[PARAMS] Parameters</div> <div class="sidebar-value">44,341,632</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[SIZE] Model Size</div> <div class="sidebar-value">~170 MB</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[LAYERS] Layers</div> <div class="sidebar-value">13</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[HEADS] Attention Heads</div> <div class="sidebar-value">16</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[CONTEXT] Context Length</div> <div class="sidebar-value">1,024 tokens</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[VOCAB] Vocabulary</div> <div class="sidebar-value">16,000 tokens</div> </div> <div class="sidebar-section"> <div class="sidebar-label">[FORMAT] Format</div> <div class="sidebar-value">Safetensors</div> </div> <div class="downloads-badge"> <div class="downloads-label">10K+ DOWNLOADS MILESTONE</div> <div class="downloads-number">10,000+</div> <div style="font-size: 0.75rem; color: rgba(255,255,255,0.6); margin-top: 0.3rem;">Downloads and counting</div> <div style="display: inline-flex; align-items: center; gap: 5px; background: rgba(255,0,64,0.2); padding: 0.3rem 0.8rem; border-radius: 20px; font-size: 0.75rem; color: #ff4081; margin-top: 0.5rem;"> <span style="width: 8px; height: 8px; background: #ff0040; border-radius: 50%; animation: pulse-dot 1.5s infinite;"></span> <span>TRENDING</span> </div> </div> # Parameters temperature = gr.Slider( minimum=0.1, maximum=1.0, value=0.8, step=0.1, label="Temperature", info="Controls creativity vs determinism" ) max_tokens = gr.Slider( minimum=50, maximum=500, value=200, step=50, label="Max Tokens", info="Maximum response length" ) # Deploy section gr.HTML(""" <div class="deploy-section"> <div class="deploy-title">Deploy 2026</div> <div style="display: flex; flex-direction: column; gap: 0.5rem;"> <a href="https://huggingface.co/pnny13/legion-coder-8m/deploy/sagemaker" style="display: block; background: linear-gradient(90deg, #ff9900 0%, #ff6600 100%); color: white; padding: 0.7rem 1.2rem; border-radius: 8px; text-decoration: none; font-weight: 600; text-align: center;">AWS SageMaker</a> <a href="https://huggingface.co/pnny13/legion-coder-8m" style="display: block; background: linear-gradient(90deg, #ff9900 0%, #ff6600 100%); color: white; padding: 0.7rem 1.2rem; border-radius: 8px; text-decoration: none; font-weight: 600; text-align: center;">Model Hub</a> </div> </div> """) # Main chat area with gr.Column(scale=3): gr.HTML(""" <h3 style="color: #ff4081; font-family: 'Orbitron', sans-serif; margin-bottom: 1rem;"> [CHAT] Start Coding </h3> """) chatbot = gr.Chatbot( height=500, bubble_full_width=False, show_copy_button=True, avatar_images=( "https://img.icons8.com/color/48/000000/user-male-circle.png", "https://img.icons8.com/color/48/000000/code.png" ) ) msg = gr.Textbox( placeholder="Ask Legion Coder to write or explain code...", label="", container=False, scale=7 ) with gr.Row(): submit_btn = gr.Button("Send", variant="primary", scale=1) clear_btn = gr.Button("Clear", variant="secondary", scale=1) # Footer gr.HTML(""" <div class="footer"> <span style="color: #ff4081; font-weight: 700; font-family: 'Orbitron', sans-serif;">MADE WITH BY DEATH LEGION</span> <br><br> <span style="color: #00d4ff; font-weight: 600;">Powered by nvdya-kit</span> <br><br> <span style="color: rgba(255,255,255,0.5);">2026 DEATH LEGION. All rights reserved.</span> </div> """) # Event handlers def respond(message, chat_history, temp, max_tok): """Handle message response.""" bot_message = generate_code(message, chat_history, temp, max_tok) chat_history.append([message, bot_message]) return "", chat_history def clear_chat(): """Clear chat history.""" return None, [] submit_btn.click( respond, inputs=[msg, chatbot, temperature, max_tokens], outputs=[msg, chatbot] ) msg.submit( respond, inputs=[msg, chatbot, temperature, max_tokens], outputs=[msg, chatbot] ) clear_btn.click( clear_chat, outputs=[msg, chatbot] ) return demo
# Create and launch the interfaceif __name__ == "__main__": demo = create_interface() demo.launch( server_name="0.0.0.0", server_port=7860, share=False, show_error=True )gradio_app.pyAdded+564