Spaces:
Runtime error
Runtime error
Upload 38 files
Browse files- app.py +17 -8
- scripts/init_database.py +4 -2
app.py
CHANGED
|
@@ -18,17 +18,21 @@ logging.basicConfig(level=logging.INFO)
|
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
app.config.update(
|
| 22 |
SECRET_KEY=os.environ.get('SECRET_KEY', secrets.token_urlsafe(32)),
|
| 23 |
-
SESSION_COOKIE_SECURE=
|
| 24 |
SESSION_COOKIE_HTTPONLY=True,
|
| 25 |
-
SESSION_COOKIE_SAMESITE='
|
| 26 |
PERMANENT_SESSION_LIFETIME=timedelta(hours=24),
|
| 27 |
SESSION_COOKIE_NAME='tmc_session',
|
| 28 |
-
WTF_CSRF_CHECK_DEFAULT=False
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# CORS with credentials support
|
| 32 |
CORS(app, supports_credentials=True, origins=["https://moderator404-chatbot.hf.space"])
|
| 33 |
csrf = CSRFProtect(app)
|
| 34 |
limiter = Limiter(key_func=get_remote_address, app=app, default_limits=["1000 per hour", "100 per minute"])
|
|
@@ -38,17 +42,16 @@ rag_helper = RAGHelper(use_vector_search=True)
|
|
| 38 |
|
| 39 |
# ---------- Hugging Face Inference Providers (OpenAI-compatible) ----------
|
| 40 |
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 41 |
-
HUGGINGFACE_MODEL = "google/gemma-4-26B-A4B-it:novita"
|
| 42 |
API_BASE_URL = "https://router.huggingface.co/v1"
|
| 43 |
|
| 44 |
-
# Initialize client only if token exists
|
| 45 |
if HF_TOKEN and HF_TOKEN != "dummy":
|
| 46 |
hf_client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 47 |
else:
|
| 48 |
hf_client = None
|
| 49 |
logger.warning("HF_TOKEN not set. Using mock responses only.")
|
| 50 |
|
| 51 |
-
# ---------- Mock response fallback ----------
|
| 52 |
def mock_response(message, rag_context, ticket_context):
|
| 53 |
msg_lower = message.lower()
|
| 54 |
if rag_context:
|
|
@@ -232,7 +235,6 @@ def products():
|
|
| 232 |
|
| 233 |
@app.route('/chat')
|
| 234 |
def chat():
|
| 235 |
-
# Cache busting for static files
|
| 236 |
cache_bust = int(time.time())
|
| 237 |
return render_template('chat.html', configured_model=chatbot.get_configured_model(), cache_bust=cache_bust)
|
| 238 |
|
|
@@ -456,5 +458,12 @@ def get_product_specs(product_name):
|
|
| 456 |
return jsonify({'success': True, 'specifications': f.read()})
|
| 457 |
return jsonify({'success': False, 'error': 'Product not found'}), 404
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
if __name__ == '__main__':
|
| 460 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
|
|
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
app = Flask(__name__)
|
| 21 |
+
|
| 22 |
+
# ------------------------------------------------------------
|
| 23 |
+
# Critical session cookie settings for HTTPS (Hugging Face Spaces)
|
| 24 |
+
# ------------------------------------------------------------
|
| 25 |
app.config.update(
|
| 26 |
SECRET_KEY=os.environ.get('SECRET_KEY', secrets.token_urlsafe(32)),
|
| 27 |
+
SESSION_COOKIE_SECURE=True, # Required for HTTPS
|
| 28 |
SESSION_COOKIE_HTTPONLY=True,
|
| 29 |
+
SESSION_COOKIE_SAMESITE='None', # Allow cross-site (needed for Spaces)
|
| 30 |
PERMANENT_SESSION_LIFETIME=timedelta(hours=24),
|
| 31 |
SESSION_COOKIE_NAME='tmc_session',
|
| 32 |
+
WTF_CSRF_CHECK_DEFAULT=False # Disable global CSRF (we use per‑route exemption)
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# CORS with credentials support – allow your Space domain or '*' for testing
|
| 36 |
CORS(app, supports_credentials=True, origins=["https://moderator404-chatbot.hf.space"])
|
| 37 |
csrf = CSRFProtect(app)
|
| 38 |
limiter = Limiter(key_func=get_remote_address, app=app, default_limits=["1000 per hour", "100 per minute"])
|
|
|
|
| 42 |
|
| 43 |
# ---------- Hugging Face Inference Providers (OpenAI-compatible) ----------
|
| 44 |
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 45 |
+
HUGGINGFACE_MODEL = "google/gemma-4-26B-A4B-it:novita" # or any other supported model
|
| 46 |
API_BASE_URL = "https://router.huggingface.co/v1"
|
| 47 |
|
|
|
|
| 48 |
if HF_TOKEN and HF_TOKEN != "dummy":
|
| 49 |
hf_client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
|
| 50 |
else:
|
| 51 |
hf_client = None
|
| 52 |
logger.warning("HF_TOKEN not set. Using mock responses only.")
|
| 53 |
|
| 54 |
+
# ---------- Mock response fallback (for when HF API is unavailable) ----------
|
| 55 |
def mock_response(message, rag_context, ticket_context):
|
| 56 |
msg_lower = message.lower()
|
| 57 |
if rag_context:
|
|
|
|
| 235 |
|
| 236 |
@app.route('/chat')
|
| 237 |
def chat():
|
|
|
|
| 238 |
cache_bust = int(time.time())
|
| 239 |
return render_template('chat.html', configured_model=chatbot.get_configured_model(), cache_bust=cache_bust)
|
| 240 |
|
|
|
|
| 458 |
return jsonify({'success': True, 'specifications': f.read()})
|
| 459 |
return jsonify({'success': False, 'error': 'Product not found'}), 404
|
| 460 |
|
| 461 |
+
# -------------------------------------------------------------------
|
| 462 |
+
# Debug endpoint (optional)
|
| 463 |
+
# -------------------------------------------------------------------
|
| 464 |
+
@app.route('/api/debug-headers')
|
| 465 |
+
def debug_headers():
|
| 466 |
+
return jsonify(dict(request.headers))
|
| 467 |
+
|
| 468 |
if __name__ == '__main__':
|
| 469 |
app.run(host='0.0.0.0', port=7860, debug=False)
|
scripts/init_database.py
CHANGED
|
@@ -3,12 +3,14 @@ from .database import DatabaseManager
|
|
| 3 |
|
| 4 |
def init_sample_data():
|
| 5 |
db = DatabaseManager()
|
| 6 |
-
|
|
|
|
| 7 |
if admin:
|
| 8 |
with db.get_connection() as conn:
|
| 9 |
conn.execute("UPDATE users SET role = 'admin' WHERE id = ?", (admin,))
|
| 10 |
conn.commit()
|
| 11 |
-
print("Admin created with password 'CHANGE_ME_ADMIN_PASSWORD'
|
|
|
|
| 12 |
db.create_user('customer@example.com', 'John', 'Customer', 'customer123', company='Example Corp')
|
| 13 |
print("Sample data initialized.")
|
| 14 |
|
|
|
|
| 3 |
|
| 4 |
def init_sample_data():
|
| 5 |
db = DatabaseManager()
|
| 6 |
+
# Admin user – change password after first login!
|
| 7 |
+
admin = db.create_user('admin@admin.com', 'Admin', 'User', 'Admin', company='Too Many Cables')
|
| 8 |
if admin:
|
| 9 |
with db.get_connection() as conn:
|
| 10 |
conn.execute("UPDATE users SET role = 'admin' WHERE id = ?", (admin,))
|
| 11 |
conn.commit()
|
| 12 |
+
print("Admin created with password 'CHANGE_ME_ADMIN_PASSWORD'")
|
| 13 |
+
# Sample customer
|
| 14 |
db.create_user('customer@example.com', 'John', 'Customer', 'customer123', company='Example Corp')
|
| 15 |
print("Sample data initialized.")
|
| 16 |
|