Team Ai
Apppublic

Ganakv/cli-coding-agent-api

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py59 linesDownload Raw Back to root
1from fastapi import FastAPI, Query2from fastapi.middleware.cors import CORSMiddleware3from pydantic import BaseModel4import subprocess, tempfile, os, datetime5 6app = FastAPI(title="CLI Coding Agent API")7 8app.add_middleware(9    CORSMiddleware,10    allow_origins=["*"],11    allow_methods=["GET"],12    allow_headers=["*"],13)14 15LOG_FILE = "agent_runs.log"16 17class TaskResponse(BaseModel):18    task: str19    agent: str20    output: str21    email: str22 23@app.get("/task", response_model=TaskResponse)24def run_task(q: str = Query(...)):25    if "least common multiple" in q.lower():26        code = "import math\nprint(math.lcm(25, 18))"27 28    else:29        code = f'print("Simulated output for task: {q}")'30 31    with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as tmp:32        tmp.write(code)33        tmp_path = tmp.name34 35    try:36        result = subprocess.run(37            ["python3", tmp_path],38            capture_output=True,39            text=True,40            timeout=1041        )42        output = result.stdout.strip() or result.stderr.strip()43    except subprocess.TimeoutExpired:44        output = "Error: Execution timed out."45    except Exception as e:46        output = f"Error: {e}"47    finally:48        os.unlink(tmp_path)49 50    with open(LOG_FILE, "a") as f:51        f.write(f"[{datetime.datetime.now()}] {q} -> {output}\\n")52 53    return TaskResponse(54        task=q,55        agent="copilot-cli",56        output=output,57        email="21f3000911@ds.study.iitm.ac.in"58    )59