Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,17 @@ logger = logging.getLogger(__name__)
|
|
18 |
load_dotenv()
|
19 |
|
20 |
app = Flask(__name__)
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Store active sessions
|
24 |
sessions = {}
|
@@ -34,7 +44,8 @@ def index():
|
|
34 |
# Generate a unique session ID if one doesn't exist
|
35 |
if "session_id" not in session:
|
36 |
session["session_id"] = str(uuid.uuid4())
|
37 |
-
|
|
|
38 |
return render_template("index.html")
|
39 |
|
40 |
def is_rate_limited(session_id):
|
@@ -64,11 +75,18 @@ def chat():
|
|
64 |
"""Handle chat requests"""
|
65 |
data = request.json
|
66 |
message = data.get("message", "")
|
67 |
-
session_id = session.get("session_id")
|
68 |
|
|
|
|
|
|
|
|
|
|
|
69 |
if not session_id:
|
|
|
70 |
return jsonify({"error": "No valid session"}), 400
|
71 |
|
|
|
|
|
72 |
# Check rate limiting
|
73 |
if is_rate_limited(session_id):
|
74 |
return jsonify({
|
@@ -137,17 +155,9 @@ if __name__ == "__main__":
|
|
137 |
os.makedirs("templates", exist_ok=True)
|
138 |
|
139 |
# Priority: 1. Command line argument, 2. Environment variable, 3. Default (8080)
|
140 |
-
|
141 |
-
|
142 |
-
#logger.info(f"Starting GitHub Navigator on port {port}")
|
143 |
-
|
144 |
-
# Run the app
|
145 |
-
#app.run(host="0.0.0.0", port=port, debug=True)
|
146 |
-
|
147 |
-
|
148 |
-
port = args.port if args.port else int(os.environ.get("PORT", 7860))
|
149 |
|
150 |
logger.info(f"Starting GitHub Navigator on port {port}")
|
151 |
|
152 |
-
# Run the app
|
153 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
18 |
load_dotenv()
|
19 |
|
20 |
app = Flask(__name__)
|
21 |
+
# More explicit session configuration
|
22 |
+
# We set SESSION_COOKIE_SECURE=False because Hugging Face Spaces handles HTTPS termination externally.
|
23 |
+
# If you were running HTTPS directly in Flask, you'd set this to True.
|
24 |
+
app.config.update(
|
25 |
+
SECRET_KEY=os.environ.get("SECRET_KEY", os.urandom(24).hex()), # Ensure SECRET_KEY is loaded
|
26 |
+
SESSION_COOKIE_HTTPONLY=True, # Prevent client-side JS access to the cookie
|
27 |
+
SESSION_COOKIE_SAMESITE='Lax', # Recommended setting for CSRF protection
|
28 |
+
SESSION_COOKIE_SECURE=False, # Set to False as HTTPS is handled externally by HF
|
29 |
+
)
|
30 |
+
|
31 |
+
app.secret_key = app.config['SECRET_KEY'] # Make sure app.secret_key is set from config
|
32 |
|
33 |
# Store active sessions
|
34 |
sessions = {}
|
|
|
44 |
# Generate a unique session ID if one doesn't exist
|
45 |
if "session_id" not in session:
|
46 |
session["session_id"] = str(uuid.uuid4())
|
47 |
+
logger.info(f"New session created: {session['session_id']}") # Add logging
|
48 |
+
|
49 |
return render_template("index.html")
|
50 |
|
51 |
def is_rate_limited(session_id):
|
|
|
75 |
"""Handle chat requests"""
|
76 |
data = request.json
|
77 |
message = data.get("message", "")
|
|
|
78 |
|
79 |
+
# Add logging to see the session state
|
80 |
+
logger.info(f"Chat request received. Current session keys: {list(session.keys())}")
|
81 |
+
|
82 |
+
session_id = session.get("session_id")
|
83 |
+
|
84 |
if not session_id:
|
85 |
+
logger.error("No valid session ID found in session object.") # Add error logging
|
86 |
return jsonify({"error": "No valid session"}), 400
|
87 |
|
88 |
+
logger.info(f"Valid session ID found: {session_id}") # Add success logging
|
89 |
+
|
90 |
# Check rate limiting
|
91 |
if is_rate_limited(session_id):
|
92 |
return jsonify({
|
|
|
155 |
os.makedirs("templates", exist_ok=True)
|
156 |
|
157 |
# Priority: 1. Command line argument, 2. Environment variable, 3. Default (8080)
|
158 |
+
port = args.port if args.port else int(os.environ.get("PORT", 8080))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
logger.info(f"Starting GitHub Navigator on port {port}")
|
161 |
|
162 |
+
# Run the app
|
163 |
+
app.run(host="0.0.0.0", port=port, debug=True)
|