Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,17 @@ from extensions import mongo, mail
|
|
| 6 |
|
| 7 |
# --- START DATABASE CONNECTION FIX ---
|
| 8 |
# Reads the MONGO_URI from the environment/secrets (set via Hugging Face Secrets).
|
| 9 |
-
# If the secret is set, it uses that value. If not, it falls back to the local placeholder.
|
| 10 |
MONGO_URI_SECRET = os.environ.get('MONGO_URI')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if MONGO_URI_SECRET:
|
| 12 |
# Use the MONGO_URI from the environment (Hugging Face Secret) for deployment
|
| 13 |
class Config:
|
| 14 |
MONGO_URI = MONGO_URI_SECRET
|
|
|
|
|
|
|
| 15 |
app = Flask(__name__)
|
| 16 |
app.config.from_object(Config)
|
| 17 |
else:
|
|
@@ -23,6 +28,7 @@ else:
|
|
| 23 |
# --- END DATABASE CONNECTION FIX ---
|
| 24 |
|
| 25 |
# Initialize extensions
|
|
|
|
| 26 |
mongo.init_app(app)
|
| 27 |
mail.init_app(app)
|
| 28 |
|
|
@@ -42,8 +48,9 @@ from services.evaluation import evaluate_answer # Imported, models loaded here
|
|
| 42 |
@app.route("/")
|
| 43 |
def home():
|
| 44 |
# This line attempts the database connection and was causing the crash
|
|
|
|
| 45 |
tests = list(mongo.db.tests.find({}, {"_id": 0}))
|
| 46 |
return render_template("home.html", tests=tests)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
app.run(debug=True)
|
|
|
|
| 6 |
|
| 7 |
# --- START DATABASE CONNECTION FIX ---
|
| 8 |
# Reads the MONGO_URI from the environment/secrets (set via Hugging Face Secrets).
|
|
|
|
| 9 |
MONGO_URI_SECRET = os.environ.get('MONGO_URI')
|
| 10 |
+
|
| 11 |
+
# Define the database name to be used (ensure this name exists in your Atlas cluster)
|
| 12 |
+
DB_NAME = "subjective_assessment_db" # <--- ASSUMING YOUR DATABASE NAME IS THIS
|
| 13 |
+
|
| 14 |
if MONGO_URI_SECRET:
|
| 15 |
# Use the MONGO_URI from the environment (Hugging Face Secret) for deployment
|
| 16 |
class Config:
|
| 17 |
MONGO_URI = MONGO_URI_SECRET
|
| 18 |
+
# CRITICAL FIX: Explicitly set the database name for the connection object
|
| 19 |
+
MONGO_DBNAME = DB_NAME
|
| 20 |
app = Flask(__name__)
|
| 21 |
app.config.from_object(Config)
|
| 22 |
else:
|
|
|
|
| 28 |
# --- END DATABASE CONNECTION FIX ---
|
| 29 |
|
| 30 |
# Initialize extensions
|
| 31 |
+
# The `mongo` object will now have a correct `.db` attribute if the connection succeeds
|
| 32 |
mongo.init_app(app)
|
| 33 |
mail.init_app(app)
|
| 34 |
|
|
|
|
| 48 |
@app.route("/")
|
| 49 |
def home():
|
| 50 |
# This line attempts the database connection and was causing the crash
|
| 51 |
+
# The fix ensures 'mongo.db' is the correct database object
|
| 52 |
tests = list(mongo.db.tests.find({}, {"_id": 0}))
|
| 53 |
return render_template("home.html", tests=tests)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
app.run(debug=True)
|