Spaces:
Sleeping
Sleeping
fix: add PYTHONPATH setup in app.py for openenv imports
Browse files- Add openenv-course-tmp to sys.path before importing main
- Enables FastAPI app to start when app.py is used as entry point
- Confirms app:app entry point works with uvicorn
app.py
CHANGED
|
@@ -1,160 +1,17 @@
|
|
| 1 |
-
"""
|
| 2 |
FastAPI entry point for Hugging Face Spaces and production deployment.
|
| 3 |
This file simply imports the main FastAPI application to ensure
|
| 4 |
consistent behavior across localhost and HF Spaces deployments.
|
| 5 |
"""
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
__all__ = ["app"]
|
| 10 |
-
|
| 11 |
-
if 'file' not in request.files:
|
| 12 |
-
return jsonify({"error": "No file provided"}), 400
|
| 13 |
-
file = request.files['file']
|
| 14 |
-
if file.filename == '':
|
| 15 |
-
return jsonify({"error": "Empty file"}), 400
|
| 16 |
-
if not allowed_file(file.filename):
|
| 17 |
-
return jsonify({"error": "File type not supported"}), 400
|
| 18 |
-
|
| 19 |
-
code = file.read().decode('utf-8')
|
| 20 |
-
note = None
|
| 21 |
-
if len(code) > 10000:
|
| 22 |
-
code = code[:10000]
|
| 23 |
-
note = "User uploaded file — truncated to first 10,000 characters"
|
| 24 |
-
|
| 25 |
-
return process_raw_code(code, file.filename, note)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
code = data.get("code", "")
|
| 32 |
-
filename = data.get("filename", "pasted_code.py")
|
| 33 |
-
|
| 34 |
-
if not code:
|
| 35 |
-
return jsonify({"error": "Empty code"}), 400
|
| 36 |
-
|
| 37 |
-
note = None
|
| 38 |
-
if len(code) > 10000:
|
| 39 |
-
code = code[:10000]
|
| 40 |
-
note = "Pasted code — truncated to first 10,000 characters"
|
| 41 |
-
|
| 42 |
-
return process_raw_code(code, filename, note)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
# 1. Get AI Review
|
| 46 |
-
ai_action = review(code)
|
| 47 |
-
|
| 48 |
-
# 2. Build fake ground truth
|
| 49 |
-
ground_truth = {
|
| 50 |
-
"bug": {
|
| 51 |
-
"type": ai_action["category"] if ai_action["type"] == "flag" else "none",
|
| 52 |
-
"severity": ai_action.get("severity", 0),
|
| 53 |
-
"description": "User uploaded file — no ground truth",
|
| 54 |
-
"line_hint": ai_action.get("line_hint", "")
|
| 55 |
-
}
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
# 3. Grade
|
| 59 |
-
res = grade(ai_action, ground_truth)
|
| 60 |
-
reward = res.reward
|
| 61 |
-
|
| 62 |
-
# 4. Update state
|
| 63 |
-
STATE["total_reward"] += reward
|
| 64 |
-
STATE["episode_count"] += 1
|
| 65 |
-
|
| 66 |
-
record = {
|
| 67 |
-
"episode_id": f"upload_{STATE['episode_count']}",
|
| 68 |
-
"snippet_id": filename,
|
| 69 |
-
"ai_said": f"{ai_action.get('type')} / {ai_action.get('category')}",
|
| 70 |
-
"reward": reward,
|
| 71 |
-
"correct": res.correct,
|
| 72 |
-
"breakdown": res.breakdown,
|
| 73 |
-
"bug_type": res.bug_type
|
| 74 |
-
}
|
| 75 |
-
STATE["history"].append(record)
|
| 76 |
-
save_history_file()
|
| 77 |
-
|
| 78 |
-
return jsonify({
|
| 79 |
-
"filename": filename,
|
| 80 |
-
"code": code,
|
| 81 |
-
"line_count": len(code.splitlines()),
|
| 82 |
-
"ai_action": ai_action,
|
| 83 |
-
"reward": reward,
|
| 84 |
-
"correct": res.correct,
|
| 85 |
-
"breakdown": res.breakdown,
|
| 86 |
-
"total_reward": STATE["total_reward"],
|
| 87 |
-
"episode_count": STATE["episode_count"],
|
| 88 |
-
"summary": res.summary(),
|
| 89 |
-
"note": note or "User uploaded file — reward based on AI confidence",
|
| 90 |
-
"highlighted_lines": find_bug_lines(code, ai_action),
|
| 91 |
-
"fix": get_fix_suggestion(code, ai_action)
|
| 92 |
-
})
|
| 93 |
-
|
| 94 |
-
@app.route("/api/comparison", methods=["GET"])
|
| 95 |
-
def get_comparison():
|
| 96 |
-
# Pick episode ep_001 (SQL injection — best visual example)
|
| 97 |
-
code = ""
|
| 98 |
-
for ep in env.episodes:
|
| 99 |
-
if ep["episode_id"] == "ep_001":
|
| 100 |
-
code = ep["code"]
|
| 101 |
-
break
|
| 102 |
-
|
| 103 |
-
truth = env._get_answer("ep_001")
|
| 104 |
-
|
| 105 |
-
# Simulate untrained AI
|
| 106 |
-
untrained_action = {
|
| 107 |
-
"type": "approve",
|
| 108 |
-
"category": "ok",
|
| 109 |
-
"line_hint": "",
|
| 110 |
-
"comment": "Code looks fine, no issues found.",
|
| 111 |
-
"severity": 0
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
trained_action = review(code)
|
| 115 |
-
|
| 116 |
-
untrained_result = grade(untrained_action, truth)
|
| 117 |
-
trained_result = grade(trained_action, truth)
|
| 118 |
-
|
| 119 |
-
highlighted = find_bug_lines(code, trained_action)
|
| 120 |
-
|
| 121 |
-
return jsonify({
|
| 122 |
-
"code": code,
|
| 123 |
-
"highlighted_lines": highlighted,
|
| 124 |
-
"untrained": {
|
| 125 |
-
"action": untrained_action,
|
| 126 |
-
"reward": untrained_result.reward,
|
| 127 |
-
"correct": untrained_result.correct,
|
| 128 |
-
"breakdown": untrained_result.breakdown
|
| 129 |
-
},
|
| 130 |
-
"trained": {
|
| 131 |
-
"action": trained_action,
|
| 132 |
-
"reward": trained_result.reward,
|
| 133 |
-
"correct": trained_result.correct,
|
| 134 |
-
"breakdown": trained_result.breakdown
|
| 135 |
-
},
|
| 136 |
-
"ground_truth": truth.get("bug", {})
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
@app.route("/api/stats", methods=["GET"])
|
| 140 |
-
def get_stats():
|
| 141 |
-
return jsonify({
|
| 142 |
-
"total_reward": STATE["total_reward"],
|
| 143 |
-
"episode_count": STATE["episode_count"],
|
| 144 |
-
"accuracy": calculate_accuracy(),
|
| 145 |
-
"best_score": calculate_best_score(),
|
| 146 |
-
"is_running": STATE["is_running"]
|
| 147 |
-
})
|
| 148 |
-
|
| 149 |
-
@app.route("/api/reset", methods=["GET"])
|
| 150 |
-
def reset_state():
|
| 151 |
-
STATE["total_reward"] = 0
|
| 152 |
-
STATE["episode_count"] = 0
|
| 153 |
-
STATE["history"] = []
|
| 154 |
-
STATE["is_running"] = False
|
| 155 |
-
if os.path.exists(HISTORY_FILE):
|
| 156 |
-
os.remove(HISTORY_FILE)
|
| 157 |
-
return jsonify({"status": "reset"})
|
| 158 |
|
| 159 |
-
|
| 160 |
-
app.run(port=5000, threaded=True, debug=True)
|
|
|
|
| 1 |
+
"""
|
| 2 |
FastAPI entry point for Hugging Face Spaces and production deployment.
|
| 3 |
This file simply imports the main FastAPI application to ensure
|
| 4 |
consistent behavior across localhost and HF Spaces deployments.
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
import sys
|
| 8 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Add openenv-course-tmp to path to enable openenv imports
|
| 11 |
+
openenv_path = os.path.join(os.path.dirname(__file__), "openenv-course-tmp")
|
| 12 |
+
if os.path.exists(openenv_path) and openenv_path not in sys.path:
|
| 13 |
+
sys.path.insert(0, openenv_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
from main import app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
__all__ = ["app"]
|
|
|