dboa9 commited on
Commit
7fae270
·
1 Parent(s): f61ef17

Fix app.py syntax errors and variable ordering

Browse files
Files changed (1) hide show
  1. app.py +57 -74
app.py CHANGED
@@ -1,74 +1,57 @@
1
- # FILE: /home/mrdbo/projects/moltbot-hybrid-engine/app.py
2
- # PURPOSE: This is the server running on Hugging Face. It's a set of tools.
3
- # CURRENT (BROKEN):
4
- #
5
-
6
- from fastapi import FastAPI, UploadFile, File, HTTPException, Header
7
- import json
8
- import os
9
-
10
- import requests
11
- import sys
12
-
13
- # SHOULD BE:
14
- import requests
15
- import sys
16
- import json # <-- Missing!
17
- from fastapi import FastAPI, UploadFile, File, HTTPException
18
- import json
19
-
20
- API_KEY = os.environ.get("MOLTBOT_API_KEY", "")
21
-
22
- @app.post("/tools/analyze_report")
23
- async def analyze_verification_report(
24
- report_file: UploadFile = File(...),
25
- x_api_key: str = Header(...)
26
- ):
27
- if x_api_key != API_KEY:
28
- raise HTTPException(status_code=401, detail="Invalid API key")
29
- # ... rest of code
30
- app = FastAPI(title="Moltbot Hybrid Engine - Toolbox")
31
- # Add this section to get the API key from HF Secrets
32
- API_KEY = os.environ.get("MOLTBOT_API_KEY", "default_insecure_key")
33
- if API_KEY == "default_insecure_key":
34
- print("WARNING: MOLTBOT_API_KEY secret not set. Using insecure default key.")
35
-
36
- @app.get("/")
37
- def read_root():
38
- return {"message": "Moltbot Toolbox is online. Ready to receive files for analysis."}
39
-
40
- @app.post("/tools/analyze_report")
41
- async def analyze_verification_report(report_file: UploadFile = File(...), x_api_key: str = Header(None)):
42
- """
43
- TOOL 1: Reads a JSON verification report, finds root causes, and suggests fixes.
44
- Your local machine will send the 'ENHANCED_STRUCTURE_REPORT.json' to this tool.
45
- """
46
-
47
- if not x_api_key or x_api_key != API_KEY:
48
- raise HTTPException(status_code=401, detail="Invalid or missing API Key")
49
- if not report_file.filename.endswith('.json'):
50
- raise HTTPException(status_code=400, detail="Invalid file type. Please upload a JSON report.")
51
-
52
- try:
53
- content = await report_file.read()
54
- data = json.loads(content)
55
-
56
- # --- Moltbot's "Brain" Logic ---
57
- # Correctly parse the structure of your ENHANCED_STRUCTURE_REPORT.json
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)}")