Spaces:
Runtime error
Runtime error
feat: deploy llm model on public space
Browse filesFiles changed (2)
- app.pyAdded+48
- requirements.txtAdded+4
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import jsonimport gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizer, TextGenerationPipeline
# 1) Load a reasonably small code model# MODEL_ID = "bigcode/CodeGen2-7B-multi" # medium, GPU‑recommendedMODEL_ID = "bigcode/starcoder"# MODEL_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) Generate out = pipe(prompt, max_new_tokens=256, temperature=0.2)[0]["generated_text"] # 3) Try to extract the JSON array substring start = out.find("[") end = out.rfind("]") + 1 try: 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=[ gr.Textbox(lines=10, label="Git Diff"), gr.Textbox(lines=5, label="Review Guidelines") ], outputs=gr.JSON(label="Comments"), title="🤖 Code Review LLM", description="Paste your git diff and guidelines; get back JSON comments.")
if __name__ == "__main__": iface.launch()1234transformersacceleratetorch # or torch-cpugradioapp.pyAdded+48
requirements.txtAdded+4