Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,6 +43,10 @@ class StepRequest(BaseModel):
|
|
| 43 |
action: Dict[str, Any]
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# ---------------------------------------------------------------------------
|
| 47 |
# Endpoints
|
| 48 |
# ---------------------------------------------------------------------------
|
|
@@ -143,3 +147,34 @@ def get_state(session_id: str = Query(default="default")):
|
|
| 143 |
detail=f"Session '{session_id}' not found. Call /reset first.",
|
| 144 |
)
|
| 145 |
return env.state().model_dump()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
action: Dict[str, Any]
|
| 44 |
|
| 45 |
|
| 46 |
+
class AutoReviewRequest(BaseModel):
|
| 47 |
+
source_code: str
|
| 48 |
+
file_name: str = "custom_file.py"
|
| 49 |
+
|
| 50 |
# ---------------------------------------------------------------------------
|
| 51 |
# Endpoints
|
| 52 |
# ---------------------------------------------------------------------------
|
|
|
|
| 147 |
detail=f"Session '{session_id}' not found. Call /reset first.",
|
| 148 |
)
|
| 149 |
return env.state().model_dump()
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
@app.post("/auto-review")
|
| 154 |
+
def auto_review(req: AutoReviewRequest):
|
| 155 |
+
"""Run an automated AI review on custom user code."""
|
| 156 |
+
from openai import OpenAI
|
| 157 |
+
import inference
|
| 158 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 159 |
+
if not HF_TOKEN:
|
| 160 |
+
raise HTTPException(
|
| 161 |
+
status_code=500,
|
| 162 |
+
detail="HF_TOKEN not found in deployment environment. The AI needs a Hugging Face Token to perform auto-reviews."
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# We must lazily instantiate the client because HF_TOKEN might be loaded dynamically
|
| 166 |
+
client = OpenAI(base_url=inference.API_BASE_URL, api_key=HF_TOKEN)
|
| 167 |
+
|
| 168 |
+
obs_dict = {
|
| 169 |
+
"snippet": {"file_name": req.file_name, "source": req.source_code},
|
| 170 |
+
"instructions": "Please perform a comprehensive code review on this user-submitted script. Focus on detecting severe bugs, security vulnerabilities, and performance flaws.",
|
| 171 |
+
"previous_comments": []
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
# Re-use the LLM mapping directly from our strictly tested inference script
|
| 175 |
+
action_dict = inference.get_model_action(client, obs_dict)
|
| 176 |
+
|
| 177 |
+
return {
|
| 178 |
+
"comments": action_dict.get("comments", []),
|
| 179 |
+
"summary": action_dict.get("summary", "")
|
| 180 |
+
}
|