Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,110 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if "
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py (Python_ai)
|
| 2 |
+
import difflib
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
from typing import Dict, Any
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI, Request
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
+
|
| 11 |
+
APP_NAME = "Python_ai"
|
| 12 |
+
app = FastAPI(title=APP_NAME)
|
| 13 |
+
|
| 14 |
+
# Allow CORS just in case you call it directly from the Brain UI
|
| 15 |
+
app.add_middleware(
|
| 16 |
+
CORSMiddleware,
|
| 17 |
+
allow_origins=["*"], allow_credentials=True,
|
| 18 |
+
allow_methods=[""], allow_headers=[""],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def ok(payload: Dict[str, Any] | None = None) -> JSONResponse:
|
| 22 |
+
return JSONResponse({"ok": True, **(payload or {})})
|
| 23 |
+
|
| 24 |
+
@app.get("/health")
|
| 25 |
+
def health():
|
| 26 |
+
return ok()
|
| 27 |
+
|
| 28 |
+
def make_unified_diff(filename: str, old_text: str, new_text: str) -> str:
|
| 29 |
+
# Ensure both end with newline so ranges are sane
|
| 30 |
+
if not old_text.endswith("\n"): old_text += "\n"
|
| 31 |
+
if not new_text.endswith("\n"): new_text += "\n"
|
| 32 |
+
diff = difflib.unified_diff(
|
| 33 |
+
old_text.splitlines(keepends=True),
|
| 34 |
+
new_text.splitlines(keepends=True),
|
| 35 |
+
fromfile=f"a/{filename}",
|
| 36 |
+
tofile=f"b/{filename}",
|
| 37 |
+
n=3,
|
| 38 |
+
)
|
| 39 |
+
return "".join(diff)
|
| 40 |
+
|
| 41 |
+
def minimal_fix(view_text: str) -> tuple[str, str, str]:
|
| 42 |
+
"""
|
| 43 |
+
Very small heuristic fixer:
|
| 44 |
+
- reslt -> result (common typo)
|
| 45 |
+
- NameError 'bar' if foo returns bar but bar undefined -> return 'result' instead
|
| 46 |
+
Only edits when the change is obvious; otherwise returns original text.
|
| 47 |
+
"""
|
| 48 |
+
old = view_text
|
| 49 |
+
new = old
|
| 50 |
+
|
| 51 |
+
# Trivial typo fix
|
| 52 |
+
if "reslt" in new:
|
| 53 |
+
new = new.replace("reslt", "result")
|
| 54 |
+
|
| 55 |
+
# If there's a pattern like:
|
| 56 |
+
# def foo():\n return bar\n\nresult = foo()
|
| 57 |
+
# we can't know bar; safer to leave it alone here.
|
| 58 |
+
# Keep this service minimal/fast.
|
| 59 |
+
|
| 60 |
+
return old, new, "Fixed trivial misspelling(s) where obvious."
|
| 61 |
+
|
| 62 |
+
@app.post("/code_help")
|
| 63 |
+
async def code_help(req: Request):
|
| 64 |
+
"""
|
| 65 |
+
Accepts ANY JSON shape to avoid 422. We only read fields we need.
|
| 66 |
+
Returns a PATCH-first JSON as your 'PythonFixer' contract expects.
|
| 67 |
+
"""
|
| 68 |
+
try:
|
| 69 |
+
body = await req.json()
|
| 70 |
+
except Exception:
|
| 71 |
+
body = {}
|
| 72 |
+
|
| 73 |
+
# Extract with defaults (never 422)
|
| 74 |
+
utterance = body.get("utterance", "")
|
| 75 |
+
telemetry = body.get("telemetry", {}) or {}
|
| 76 |
+
filename = telemetry.get("file", "main.py")
|
| 77 |
+
viewport = telemetry.get("viewport", {}) or {}
|
| 78 |
+
view_text = viewport.get("text", "")
|
| 79 |
+
|
| 80 |
+
# Minimal edit logic
|
| 81 |
+
old_text, new_text, why = minimal_fix(view_text)
|
| 82 |
+
|
| 83 |
+
if new_text != old_text:
|
| 84 |
+
patch = make_unified_diff(filename, old_text, new_text)
|
| 85 |
+
resp = {
|
| 86 |
+
"mode": "patch",
|
| 87 |
+
"patch": patch,
|
| 88 |
+
"full_text": "",
|
| 89 |
+
"explanation": why,
|
| 90 |
+
"confidence": 0.96,
|
| 91 |
+
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 92 |
+
}
|
| 93 |
+
else:
|
| 94 |
+
# Nothing obvious to fix; ask for more context (keeps contract)
|
| 95 |
+
resp = {
|
| 96 |
+
"mode": "ask",
|
| 97 |
+
"patch": "",
|
| 98 |
+
"full_text": "",
|
| 99 |
+
"explanation": "Need more context or a specific error message to propose a safe minimal patch.",
|
| 100 |
+
"confidence": 0.55,
|
| 101 |
+
"need": {"function": False, "xrefs": [], "page_ids": []},
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
# Always 200 with the contract object
|
| 105 |
+
return JSONResponse(resp)
|
| 106 |
+
|
| 107 |
+
# HF entry
|
| 108 |
+
if _name_ == "_main_":
|
| 109 |
+
import uvicorn
|
| 110 |
+
uvicorn.run("app:app", host="0.0.0.0", port=int(os.getenv("PORT", "7860")), reload=False)
|