Spaces:
Runtime error
Runtime error
feat: modify script to allow additional models
Browse files12345678910111213141516171819202122232425262728293031323334353637386 unmodified lines45464748import 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 echotokenizer = AutoTokenizer.from_pretrained(MODEL_ID)model = AutoModelForCausalLM.from_pretrained(MODEL_ID)pipe = TextGenerationPipeline(model, tokenizer, device="cpu")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""OUTPUT FORMAT:\n"'[{"line": 12, "comment": "…"}, …]')# 2) Generateout = pipe(prompt, max_new_tokens=256, temperature=0.2)[0]["generated_text"]# 3) Try to extract the JSON array substringstart = out.find("[")end = out.rfind("]") + 1try:return json.loads(out[start:end])except:return {"error": "Failed to parse JSON", "raw": out}# 4) Build Gradio interfaceiface = gr.Interface(fn=review_code,inputs=[6 unmodified lines)if __name__ == "__main__":iface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666 unmodified lines73747576777879808182import jsonimport gradio as grfrom 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)# Detect T5 vs causal:if "t5" in MODEL_ID.lower() or MODEL_ID.startswith("google/"):# seq‑to‑seqmodel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)task = "text2text-generation"else:# causalmodel = AutoModelForCausalLM.from_pretrained(MODEL_ID)task = "text-generation"# ── 3) Build the pipeline ────────────────────────────────────────────pipe = pipeline(task,model=model,tokenizer=tokenizer,device=-1, # CPUmax_new_tokens=256,temperature=0.2)# ── 4) Your review function ──────────────────────────────────────────def review_code(diff: str, guidelines: str):prompt = ("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": "…"}]\n')out = pipe(prompt)[0]["generated_text"]start = out.find("[")end = out.rfind("]") + 1if start < 0 or end < 0:return {"error": "No JSON array found", "raw": out}snippet = out[start:end]try:return json.loads(snippet)except json.JSONDecodeError:return {"error": "JSON parse failed", "raw": snippet}# ── 5) Gradio interface ──────────────────────────────────────────────iface = gr.Interface(fn=review_code,inputs=[6 unmodified lines)if __name__ == "__main__":local_url = iface.launch(server_name="0.0.0.0",server_port=7860,share=False)# print so you can curl it immediatelyprint(f"🔗 endpoint → {local_url}/predict")
app.py+57-23