Spaces:
Running
Running
dboa9 commited on
Commit ·
7fae270
1
Parent(s): f61ef17
Fix app.py syntax errors and variable ordering
Browse files
app.py
CHANGED
|
@@ -1,74 +1,57 @@
|
|
| 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 |
-
x_api_key
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
missing_files = data.get("missing_total", 0)
|
| 59 |
-
path_issues = data.get("path_issues_total", 0)
|
| 60 |
-
structure_issues = data.get("page_structure_analysis", {}).get("bundles_with_structure_issues", 0)
|
| 61 |
-
suggestions = []
|
| 62 |
-
|
| 63 |
-
if missing_files > 0:
|
| 64 |
-
# Extract root causes for missing files
|
| 65 |
-
root_causes = data.get("root_cause_analysis", {}).get("failures_by_root_cause", {})
|
| 66 |
-
for cause, details in root_causes.items():
|
| 67 |
-
suggestions.append(f"Found {details.get('count', 0)} files with root cause '{cause}'. Fix suggestion: {details.get('fix_suggestion')}")
|
| 68 |
-
|
| 69 |
-
if structure_issues > 0:
|
| 70 |
-
suggestions.append(f"Found {structure_issues} bundles with page structure problems (e.g., cover page in wrong place). Check 'generate_bundles_final_corrected.py' ordering.")
|
| 71 |
-
|
| 72 |
-
except Exception as e:
|
| 73 |
-
raise HTTPException(status_code=500, detail=f"Failed to process report: {str(e)}")
|
| 74 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Header
|
| 4 |
+
|
| 5 |
+
# 1. Initialize the App FIRST (Critical Fix)
|
| 6 |
+
app = FastAPI(title="Moltbot Hybrid Engine")
|
| 7 |
+
|
| 8 |
+
# 2. Set up API Key
|
| 9 |
+
API_KEY = os.environ.get("MOLTBOT_API_KEY", "default_insecure_key")
|
| 10 |
+
if API_KEY == "default_insecure_key":
|
| 11 |
+
print("WARNING: MOLTBOT_API_KEY secret not set. Using insecure default key.")
|
| 12 |
+
|
| 13 |
+
# 3. Health Check Endpoint (Keep server alive)
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def read_root():
|
| 16 |
+
return {"Status": "Running", "Message": "Moltbot Engine is Online"}
|
| 17 |
+
|
| 18 |
+
# 4. Analysis Tool Endpoint
|
| 19 |
+
@app.post("/tools/analyze_report")
|
| 20 |
+
async def analyze_verification_report(
|
| 21 |
+
report_file: UploadFile = File(...),
|
| 22 |
+
x_api_key: str = Header(None)
|
| 23 |
+
):
|
| 24 |
+
# Validation
|
| 25 |
+
if not x_api_key or x_api_key != API_KEY:
|
| 26 |
+
raise HTTPException(status_code=401, detail="Invalid or missing API Key")
|
| 27 |
+
|
| 28 |
+
if not report_file.filename.endswith('.json'):
|
| 29 |
+
raise HTTPException(status_code=400, detail="Invalid file type. Please upload a JSON report.")
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
content = await report_file.read()
|
| 33 |
+
data = json.loads(content)
|
| 34 |
+
|
| 35 |
+
# Logic: Extract issues
|
| 36 |
+
missing_files = data.get("missing_total", 0)
|
| 37 |
+
structure_issues = data.get("page_structure_analysis", {}).get("bundles_with_structure_issues", 0)
|
| 38 |
+
suggestions = []
|
| 39 |
+
|
| 40 |
+
if missing_files > 0:
|
| 41 |
+
root_causes = data.get("root_cause_analysis", {}).get("failures_by_root_cause", {})
|
| 42 |
+
for cause, details in root_causes.items():
|
| 43 |
+
suggestions.append(f"Found {details.get('count', 0)} files with root cause '{cause}'. Fix: {details.get('fix_suggestion')}")
|
| 44 |
+
|
| 45 |
+
if structure_issues > 0:
|
| 46 |
+
suggestions.append(f"Found {structure_issues} bundles with structure problems.")
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
"status": "success",
|
| 50 |
+
"analysis": {
|
| 51 |
+
"critical_issues": missing_files + structure_issues,
|
| 52 |
+
"suggestions": suggestions
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=f"Failed to process report: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|