Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -2,14 +2,17 @@ import os | |
| 2 | 
             
            from flask import Flask, jsonify, request, render_template
         | 
| 3 | 
             
            import config  # <--- Imported configuration module
         | 
| 4 | 
             
            from extensions import mongo, mail 
         | 
| 5 | 
            -
            #  | 
|  | |
|  | |
|  | |
| 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 = "assessment_db" #  | 
| 13 |  | 
| 14 | 
             
            if MONGO_URI_SECRET:
         | 
| 15 | 
             
                # Use the MONGO_URI from the environment (Hugging Face Secret) for deployment
         | 
| @@ -42,7 +45,7 @@ app.register_blueprint(test_bp) | |
| 42 | 
             
            from services.feedback_agent import build_feedback_agent
         | 
| 43 | 
             
            app.feedback_agent = build_feedback_agent()
         | 
| 44 |  | 
| 45 | 
            -
            # Import the evaluation logic (models load here)
         | 
| 46 | 
             
            from services.evaluation import evaluate_answer # Imported, models loaded here
         | 
| 47 |  | 
| 48 | 
             
            @app.route("/")
         | 
| @@ -50,10 +53,9 @@ def home(): | |
| 50 | 
             
                # This line uses the explicit client connection (mongo.cx) and database name (DB_NAME)
         | 
| 51 | 
             
                # to avoid the 'AttributeError: NoneType' object has no attribute 'tests' crash.
         | 
| 52 | 
             
                global DB_NAME 
         | 
|  | |
| 53 | 
             
                tests = list(mongo.cx[DB_NAME].tests.find({}, {"_id": 0})) 
         | 
| 54 | 
             
                return render_template("home.html", tests=tests)
         | 
| 55 |  | 
| 56 | 
             
            if __name__ == "__main__":
         | 
| 57 | 
            -
                app.run(debug=True)
         | 
| 58 | 
            -
             | 
| 59 | 
            -
            
         | 
|  | |
| 2 | 
             
            from flask import Flask, jsonify, request, render_template
         | 
| 3 | 
             
            import config  # <--- Imported configuration module
         | 
| 4 | 
             
            from extensions import mongo, mail 
         | 
| 5 | 
            +
            # We need to import urllib for proper URL escaping in case the MONGO_URI needs to be manually cleaned.
         | 
| 6 | 
            +
            from urllib.parse import quote_plus
         | 
| 7 | 
            +
            # --- END IMPORTS ---
         | 
| 8 | 
            +
             | 
| 9 |  | 
| 10 | 
             
            # --- START DATABASE CONNECTION FIX ---
         | 
| 11 | 
             
            # Reads the MONGO_URI from the environment/secrets (set via Hugging Face Secrets).
         | 
| 12 | 
             
            MONGO_URI_SECRET = os.environ.get('MONGO_URI') 
         | 
| 13 |  | 
| 14 | 
             
            # Define the database name to be used (ensure this name exists in your Atlas cluster)
         | 
| 15 | 
            +
            DB_NAME = "assessment_db" # FIXED: Using the confirmed database name from Atlas
         | 
| 16 |  | 
| 17 | 
             
            if MONGO_URI_SECRET:
         | 
| 18 | 
             
                # Use the MONGO_URI from the environment (Hugging Face Secret) for deployment
         | 
|  | |
| 45 | 
             
            from services.feedback_agent import build_feedback_agent
         | 
| 46 | 
             
            app.feedback_agent = build_feedback_agent()
         | 
| 47 |  | 
| 48 | 
            +
            # Import the evaluation logic (models load here, all NLTK/HF caching fixed)
         | 
| 49 | 
             
            from services.evaluation import evaluate_answer # Imported, models loaded here
         | 
| 50 |  | 
| 51 | 
             
            @app.route("/")
         | 
|  | |
| 53 | 
             
                # This line uses the explicit client connection (mongo.cx) and database name (DB_NAME)
         | 
| 54 | 
             
                # to avoid the 'AttributeError: NoneType' object has no attribute 'tests' crash.
         | 
| 55 | 
             
                global DB_NAME 
         | 
| 56 | 
            +
                # Use mongo.cx[DB_NAME] for reliable database access in Flask-PyMongo setups.
         | 
| 57 | 
             
                tests = list(mongo.cx[DB_NAME].tests.find({}, {"_id": 0})) 
         | 
| 58 | 
             
                return render_template("home.html", tests=tests)
         | 
| 59 |  | 
| 60 | 
             
            if __name__ == "__main__":
         | 
| 61 | 
            +
                app.run(debug=True)
         | 
|  | |
|  |