Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity
|
| 4 |
from routes.auth import auth_bp
|
|
@@ -8,6 +8,7 @@ import tempfile
|
|
| 8 |
import os
|
| 9 |
import subprocess
|
| 10 |
import json
|
|
|
|
| 11 |
from pymongo import MongoClient
|
| 12 |
from routes.reviews import reviews_bp
|
| 13 |
from flask_limiter import Limiter
|
|
@@ -256,6 +257,55 @@ def submit_review():
|
|
| 256 |
|
| 257 |
except Exception as e:
|
| 258 |
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
if __name__ == '__main__':
|
| 261 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response, stream_with_context
|
| 2 |
from flask_cors import CORS
|
| 3 |
from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity
|
| 4 |
from routes.auth import auth_bp
|
|
|
|
| 8 |
import os
|
| 9 |
import subprocess
|
| 10 |
import json
|
| 11 |
+
import time
|
| 12 |
from pymongo import MongoClient
|
| 13 |
from routes.reviews import reviews_bp
|
| 14 |
from flask_limiter import Limiter
|
|
|
|
| 257 |
|
| 258 |
except Exception as e:
|
| 259 |
return jsonify({"error": str(e)}), 500
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
@app.route("/api/enhance-stream", methods=["POST"])
|
| 263 |
+
@jwt_required()
|
| 264 |
+
def enhance_stream():
|
| 265 |
+
data = request.get_json()
|
| 266 |
+
code = data.get("code", "")
|
| 267 |
+
language = data.get("language", "python")
|
| 268 |
+
|
| 269 |
+
if not code.strip():
|
| 270 |
+
return jsonify({"error": "No code"}), 400
|
| 271 |
+
|
| 272 |
+
def generate():
|
| 273 |
+
try:
|
| 274 |
+
# 1️⃣ starting
|
| 275 |
+
yield json.dumps({
|
| 276 |
+
"type": "progress",
|
| 277 |
+
"progress": 5
|
| 278 |
+
}) + "\n"
|
| 279 |
+
|
| 280 |
+
time.sleep(0.5)
|
| 281 |
+
|
| 282 |
+
# 2️⃣ preprocessing
|
| 283 |
+
yield json.dumps({
|
| 284 |
+
"type": "progress",
|
| 285 |
+
"progress": 20
|
| 286 |
+
}) + "\n"
|
| 287 |
+
|
| 288 |
+
# 3️⃣ heavy AI call
|
| 289 |
+
result = enhance_code(code, language)
|
| 290 |
+
|
| 291 |
+
# 4️⃣ done
|
| 292 |
+
yield json.dumps({
|
| 293 |
+
"type": "result",
|
| 294 |
+
"data": result
|
| 295 |
+
}) + "\n"
|
| 296 |
+
|
| 297 |
+
except Exception as e:
|
| 298 |
+
yield json.dumps({
|
| 299 |
+
"type": "error",
|
| 300 |
+
"message": str(e)
|
| 301 |
+
}) + "\n"
|
| 302 |
+
|
| 303 |
+
return Response(
|
| 304 |
+
stream_with_context(generate()),
|
| 305 |
+
mimetype="text/plain"
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
|
| 309 |
|
| 310 |
if __name__ == '__main__':
|
| 311 |
app.run(host="0.0.0.0", port=5000, debug=True)
|