Spaces:
Runtime error
Runtime error
feat: modify script to allow additional models
Browse files12345678910113456789101112131415161718191314202122232425262728293031323334353637383940414243181920444546472248495025265127525354555657583132335960616235636465666 unmodified lines7374754876777879808182import jsonimport gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextGenerationPipeline
# 1) Load a reasonably small code model# MODEL_ID = "bigcode/CodeGen2-7B-multi" # medium, GPU‑recommended# MODEL_ID = "bigcode/starcoder" # Big - GPU‑recommendedMODEL_ID = "Salesforce/codegen-350M-multi" # light, CPU‑only# MODEL_ID = "facebook/opt-iml-1.3b" # medium, GPU‑recommended# MODEL_ID = "google/flan-t5-base" # seq‑to‑seq for prose + code# MODEL_ID = "openai/gpt-3.5-turbo" # via OpenAI API, big context, no echofrom transformers import ( AutoTokenizer, AutoModelForCausalLM, AutoModelForSeq2SeqLM, pipeline)
# ── 1) Pick your model ────────────────────────────────────────────────# ▸ For a causal code model (no T5 errors):# MODEL_ID = "Salesforce/codegen-350M-multi"# ▸ Or, for a seq‑to‑seq model:# MODEL_ID = "google/flan-t5-base"MODEL_ID = "google/flan-t5-small"# MODEL_ID = "Salesforce/codegen-350M-multi"
# ── 2) Load tokenizer + model ────────────────────────────────────────tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID)pipe = TextGenerationPipeline(model, tokenizer, device="cpu")
# Detect T5 vs causal:if "t5" in MODEL_ID.lower() or MODEL_ID.startswith("google/"): # seq‑to‑seq model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID) task = "text2text-generation"else: # causal model = AutoModelForCausalLM.from_pretrained(MODEL_ID) task = "text-generation"
# ── 3) Build the pipeline ────────────────────────────────────────────pipe = pipeline( task, model=model, tokenizer=tokenizer, device=-1, # CPU max_new_tokens=256, temperature=0.2)
# ── 4) Your review function ──────────────────────────────────────────def review_code(diff: str, guidelines: str): prompt = ( "You are an expert code reviewer. " "Given the git diff and these guidelines, return ONLY a JSON array of comments:\n\n" f"DIFF:\n{diff}\n\nGUIDELINES:\n{guidelines}\n\n" "You are an expert code reviewer. Return ONLY a JSON array of comments:\n\n" f"DIFF:\n{diff}\n\n" f"GUIDELINES:\n{guidelines}\n\n" "OUTPUT FORMAT:\n" '[{"line": 12, "comment": "…"}, …]' '[{"line": 12, "comment": "…"}]\n' )
# 2) Generate out = pipe(prompt, max_new_tokens=256, temperature=0.2)[0]["generated_text"] out = pipe(prompt)[0]["generated_text"] # 3) Try to extract the JSON array substring start = out.find("[") end = out.rfind("]") + 1 if start < 0 or end < 0: return {"error": "No JSON array found", "raw": out}
snippet = out[start:end] try: return json.loads(out[start:end]) except: return {"error": "Failed to parse JSON", "raw": out} return json.loads(snippet) except json.JSONDecodeError: return {"error": "JSON parse failed", "raw": snippet}
# 4) Build Gradio interface# ── 5) Gradio interface ──────────────────────────────────────────────iface = gr.Interface( fn=review_code, inputs=[6 unmodified lines)
if __name__ == "__main__": iface.launch() local_url = iface.launch( server_name="0.0.0.0", server_port=7860, share=False ) # print so you can curl it immediately print(f"🔗 endpoint → {local_url}/predict")app.py+57-23