Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update main.py
Browse files
    	
        main.py
    CHANGED
    
    | @@ -112,13 +112,17 @@ async def ai_personal_advice(request: AIRequest): | |
| 112 | 
             
                        raise HTTPException(status_code=500, detail="Firebase not initialized.")
         | 
| 113 |  | 
| 114 | 
             
                    user_doc = db.collection("user_profiles").document(request.user_id).get()
         | 
|  | |
|  | |
| 115 | 
             
                    if not user_doc.exists:
         | 
| 116 | 
             
                        logging.warning(f"User profile not found for user_id: {request.user_id}")
         | 
| 117 | 
            -
                         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 118 |  | 
| 119 | 
            -
                    data = user_doc.to_dict() or {}
         | 
| 120 | 
            -
                    bios = request.bios if request.bios else data.get("bios", "Chưa có bios.")
         | 
| 121 | 
            -
                    focus_history = data.get("focus_history", [])
         | 
| 122 | 
             
                    focus_text = "\n".join([f"- {f['time_start']}: {f['total_time']} phút" for f in focus_history])
         | 
| 123 |  | 
| 124 | 
             
                    prompt = f"""
         | 
| @@ -129,10 +133,16 @@ async def ai_personal_advice(request: AIRequest): | |
| 129 |  | 
| 130 | 
             
                    Hãy tư vấn cách cải thiện hiệu suất làm việc dựa trên thông tin trên.
         | 
| 131 | 
             
                    """
         | 
|  | |
|  | |
| 132 | 
             
                    input_ids = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
         | 
| 133 | 
             
                    response = ai_model.generate(**input_ids, max_new_tokens=500)
         | 
| 134 | 
             
                    advice = tokenizer.decode(response[0], skip_special_tokens=True)
         | 
| 135 |  | 
|  | |
|  | |
|  | |
|  | |
| 136 | 
             
                    return {"advice": advice}
         | 
| 137 |  | 
| 138 | 
             
                except Exception as e:
         | 
| @@ -141,6 +151,7 @@ async def ai_personal_advice(request: AIRequest): | |
| 141 | 
             
                    logging.error(traceback.format_exc())
         | 
| 142 | 
             
                    raise HTTPException(status_code=500, detail=error_message)
         | 
| 143 |  | 
|  | |
| 144 | 
             
            # Trang chủ
         | 
| 145 | 
             
            @app.get("/")
         | 
| 146 | 
             
            async def home():
         | 
|  | |
| 112 | 
             
                        raise HTTPException(status_code=500, detail="Firebase not initialized.")
         | 
| 113 |  | 
| 114 | 
             
                    user_doc = db.collection("user_profiles").document(request.user_id).get()
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                    # Check if the document exists
         | 
| 117 | 
             
                    if not user_doc.exists:
         | 
| 118 | 
             
                        logging.warning(f"User profile not found for user_id: {request.user_id}")
         | 
| 119 | 
            +
                        bios = "Không có thông tin người dùng."
         | 
| 120 | 
            +
                        focus_history = []
         | 
| 121 | 
            +
                    else:
         | 
| 122 | 
            +
                        data = user_doc.to_dict() or {}
         | 
| 123 | 
            +
                        bios = request.bios if request.bios else data.get("bios", "Chưa có bios.")
         | 
| 124 | 
            +
                        focus_history = data.get("focus_history", [])
         | 
| 125 |  | 
|  | |
|  | |
|  | |
| 126 | 
             
                    focus_text = "\n".join([f"- {f['time_start']}: {f['total_time']} phút" for f in focus_history])
         | 
| 127 |  | 
| 128 | 
             
                    prompt = f"""
         | 
|  | |
| 133 |  | 
| 134 | 
             
                    Hãy tư vấn cách cải thiện hiệu suất làm việc dựa trên thông tin trên.
         | 
| 135 | 
             
                    """
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                    # Truncate prompt if it's too long
         | 
| 138 | 
             
                    input_ids = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
         | 
| 139 | 
             
                    response = ai_model.generate(**input_ids, max_new_tokens=500)
         | 
| 140 | 
             
                    advice = tokenizer.decode(response[0], skip_special_tokens=True)
         | 
| 141 |  | 
| 142 | 
            +
                    # Đảm bảo câu trả lời là một câu hoàn chỉnh
         | 
| 143 | 
            +
                    if not advice.endswith("."):
         | 
| 144 | 
            +
                        advice += "."
         | 
| 145 | 
            +
             | 
| 146 | 
             
                    return {"advice": advice}
         | 
| 147 |  | 
| 148 | 
             
                except Exception as e:
         | 
|  | |
| 151 | 
             
                    logging.error(traceback.format_exc())
         | 
| 152 | 
             
                    raise HTTPException(status_code=500, detail=error_message)
         | 
| 153 |  | 
| 154 | 
            +
             | 
| 155 | 
             
            # Trang chủ
         | 
| 156 | 
             
            @app.get("/")
         | 
| 157 | 
             
            async def home():
         |