Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import requests
|
|
| 7 |
import nest_asyncio
|
| 8 |
import uvicorn
|
| 9 |
import re
|
|
|
|
| 10 |
|
| 11 |
# β
API config
|
| 12 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
|
@@ -78,9 +79,8 @@ class CodePayload(BaseModel):
|
|
| 78 |
code: str
|
| 79 |
|
| 80 |
@app.post("/code-check")
|
| 81 |
-
async def code_check(
|
| 82 |
-
|
| 83 |
-
code = body.get("code", "")
|
| 84 |
|
| 85 |
if not code.strip():
|
| 86 |
return JSONResponse(content={"suggestions": []})
|
|
@@ -102,7 +102,7 @@ async def code_check(request: Request):
|
|
| 102 |
"Respond ONLY with the JSON array."
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
| 106 |
"model": MODEL,
|
| 107 |
"messages": [
|
| 108 |
{"role": "system", "content": "You are a code analysis tool."},
|
|
@@ -114,13 +114,13 @@ async def code_check(request: Request):
|
|
| 114 |
}
|
| 115 |
|
| 116 |
try:
|
| 117 |
-
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=
|
| 118 |
response.raise_for_status()
|
| 119 |
raw = response.json()["choices"][0]["message"]["content"]
|
| 120 |
|
| 121 |
# Try to extract JSON array from output
|
| 122 |
match = re.search(r"\[\s*{.*?}\s*\]", raw, re.DOTALL)
|
| 123 |
-
suggestions =
|
| 124 |
|
| 125 |
return JSONResponse(content={"suggestions": suggestions})
|
| 126 |
|
|
@@ -131,7 +131,7 @@ async def code_check(request: Request):
|
|
| 131 |
# β
Gradio interface
|
| 132 |
demo = gr.ChatInterface(
|
| 133 |
fn=chat_fn,
|
| 134 |
-
title="
|
| 135 |
description="Ask coding or general programming questions! Short, natural, and helpful responses.",
|
| 136 |
theme="soft"
|
| 137 |
)
|
|
@@ -142,4 +142,4 @@ app = gr.mount_gradio_app(app, demo, path="/")
|
|
| 142 |
# β
Local debug (won't run on HF Spaces, but okay locally)
|
| 143 |
if __name__ == "__main__":
|
| 144 |
nest_asyncio.apply()
|
| 145 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
import nest_asyncio
|
| 8 |
import uvicorn
|
| 9 |
import re
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
# β
API config
|
| 13 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
|
|
|
| 79 |
code: str
|
| 80 |
|
| 81 |
@app.post("/code-check")
|
| 82 |
+
async def code_check(payload: CodePayload):
|
| 83 |
+
code = payload.code
|
|
|
|
| 84 |
|
| 85 |
if not code.strip():
|
| 86 |
return JSONResponse(content={"suggestions": []})
|
|
|
|
| 102 |
"Respond ONLY with the JSON array."
|
| 103 |
)
|
| 104 |
|
| 105 |
+
api_payload = {
|
| 106 |
"model": MODEL,
|
| 107 |
"messages": [
|
| 108 |
{"role": "system", "content": "You are a code analysis tool."},
|
|
|
|
| 114 |
}
|
| 115 |
|
| 116 |
try:
|
| 117 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=api_payload)
|
| 118 |
response.raise_for_status()
|
| 119 |
raw = response.json()["choices"][0]["message"]["content"]
|
| 120 |
|
| 121 |
# Try to extract JSON array from output
|
| 122 |
match = re.search(r"\[\s*{.*?}\s*\]", raw, re.DOTALL)
|
| 123 |
+
suggestions = json.loads(match.group(0)) if match else []
|
| 124 |
|
| 125 |
return JSONResponse(content={"suggestions": suggestions})
|
| 126 |
|
|
|
|
| 131 |
# β
Gradio interface
|
| 132 |
demo = gr.ChatInterface(
|
| 133 |
fn=chat_fn,
|
| 134 |
+
title="\ud83d\udcac CODEX MIRXA KAMRAN",
|
| 135 |
description="Ask coding or general programming questions! Short, natural, and helpful responses.",
|
| 136 |
theme="soft"
|
| 137 |
)
|
|
|
|
| 142 |
# β
Local debug (won't run on HF Spaces, but okay locally)
|
| 143 |
if __name__ == "__main__":
|
| 144 |
nest_asyncio.apply()
|
| 145 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|