Spaces:
Sleeping
Sleeping
Update backend.py
Browse files1234147 unmodified lines1521531541551561571581591601611621631641651661671681697 unmodified lines177178179180181182183234 unmodified lines418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468# backend.py — ENHANCED WITH AGENT CONVERSATION LOGS + RAM/VRAM MONITORINGimport sqlite3import osimport json147 unmodified linesexcept:return 0.0# ------------------------------ MODEL LOADING ------------------------------MODEL_REGISTRY = {"understander": "Qwen/Qwen2.5-Coder-0.6B-Instruct","architect": "Qwen/Qwen2.5-Coder-0.6B-Instruct","ceo": "google/gemma-2-2b-it","manager": "microsoft/Phi-3-mini-4k-instruct","worker": "Qwen/Qwen2.5-Coder-0.6B-Instruct","reviewer": "microsoft/Phi-3-mini-4k-instruct","editor": "Qwen/Qwen2.5-Coder-0.6B-Instruct","tester": "microsoft/Phi-3-mini-4k-instruct","publisher": "Qwen/Qwen2.5-Coder-0.6B-Instruct","final_ceo": "google/gemma-2-2b-it",}_MODEL_CACHE = {}7 unmodified linesmodel_name,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto",trust_remote_code=True)_MODEL_CACHE[model_name] = (tokenizer, model)return tokenizer, model234 unmodified lineszf.write(full_path, arcname)return zip_path# ------------------------------ LLM GENERATOR (WITH LOGGING) ------------------------------def generate_with_model(role: str, prompt: str, context: dict = {}) -> str:try:model_name = MODEL_REGISTRY[role]tokenizer, model = load_model(model_name)# Format message based on roleif role == "understander":messages = [{"role": "system", "content": ROLE_PROMPTS["understander"]},{"role": "user", "content": prompt}]elif role == "architect":messages = [{"role": "system", "content": ROLE_PROMPTS["architect"]},{"role": "user", "content": prompt}]elif role == "ceo":messages = [{"role": "system", "content": ROLE_PROMPTS["ceo"]},{"role": "user", "content": prompt}]elif role == "manager":messages = [{"role": "system", "content": ROLE_PROMPTS["manager"]},{"role": "user", "content": prompt}]elif role == "worker":messages = [{"role": "system", "content": ROLE_PROMPTS["worker"].format(file=context.get("file"), instructions=context.get("instructions"))},{"role": "user", "content": ""}]elif role == "reviewer":messages = [{"role": "system", "content": ROLE_PROMPTS["reviewer"]},{"role": "user", "content": prompt}]elif role == "editor":messages = [{"role": "system", "content": ROLE_PROMPTS["editor"]},{"role": "user", "content": prompt}]elif role == "tester":messages = [{"role": "system", "content": ROLE_PROMPTS["tester"]},{"role": "user", "content": prompt}]elif role == "publisher":messages = [{"role": "system", "content": ROLE_PROMPTS["publisher"]},{"role": "user", "content": prompt}]elif role == "final_ceo":messages = [{"role": "system", "content": ROLE_PROMPTS["final_ceo"]},{"role": "user", "content": prompt}]else:messages = [{"role": "user", "content": prompt}]text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)inputs = tokenizer(text, return_tensors="pt").to(model.device)1234147 unmodified lines1521531541551561571581591601611621631641651661671681697 unmodified lines177178179180181182183184234 unmodified lines419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453# backend.py — FINAL VERSION — PHASE-3 MINI INSTRUCT + CACHING + LOGGINGimport sqlite3import osimport json147 unmodified linesexcept:return 0.0# ------------------------------ MODEL LOADING (PER ROLE) ------------------------------MODEL_REGISTRY = {"understander": "Qwen/Qwen2.5-Coder-0.6B-Instruct","architect": "Qwen/Qwen2.5-Coder-0.6B-Instruct","ceo": "microsoft/Phi-3-mini-4k-instruct","manager": "microsoft/Phi-3-mini-4k-instruct","worker": "Qwen/Qwen2.5-Coder-0.6B-Instruct","reviewer": "microsoft/Phi-3-mini-4k-instruct","editor": "Qwen/Qwen2.5-Coder-0.6B-Instruct","tester": "microsoft/Phi-3-mini-4k-instruct","publisher": "Qwen/Qwen2.5-Coder-0.6B-Instruct","final_ceo": "microsoft/Phi-3-mini-4k-instruct",}_MODEL_CACHE = {}7 unmodified linesmodel_name,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto",trust_remote_code=True,attn_implementation="eager" # ✅ Avoids flash-attn requirement — safe on all hardware)_MODEL_CACHE[model_name] = (tokenizer, model)return tokenizer, model234 unmodified lineszf.write(full_path, arcname)return zip_path# ------------------------------ LLM GENERATOR (WITH CHAT TEMPLATE FOR PHI-3) ------------------------------def generate_with_model(role: str, prompt: str, context: dict = {}) -> str:try:model_name = MODEL_REGISTRY[role]tokenizer, model = load_model(model_name)# Format message using Phi-3 or Qwen chat templateif "Phi-3" in model_name:# Phi-3 formatmessages = [{"role": "system", "content": ROLE_PROMPTS[role]},{"role": "user", "content": prompt}]text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)else:# Qwen formatmessages = [{"role": "system", "content": ROLE_PROMPTS[role]},{"role": "user", "content": prompt}]text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True)inputs = tokenizer(text, return_tensors="pt").to(model.device)
backend.py+29-44