Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,27 +1,23 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
import firebase_admin
|
3 |
from firebase_admin import credentials, firestore
|
4 |
from transformers import pipeline
|
5 |
from pydantic import BaseModel
|
6 |
-
from typing import List, Optional
|
7 |
-
from huggingface_hub import login
|
8 |
-
from fastapi.responses import JSONResponse
|
9 |
-
|
10 |
-
|
11 |
import os
|
12 |
-
|
13 |
-
if not os.path.exists("firebase_config.json"):
|
14 |
-
raise FileNotFoundError("Không tìm thấy file firebase_config.json")
|
15 |
-
|
16 |
|
17 |
# Load Firebase
|
18 |
cred = credentials.Certificate("firebase_config.json")
|
19 |
firebase_admin.initialize_app(cred)
|
20 |
db = firestore.client()
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
24 |
|
|
|
25 |
ai_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
26 |
|
27 |
app = FastAPI()
|
@@ -57,11 +53,6 @@ async def add_focus_history(request: FocusHistoryRequest):
|
|
57 |
user_ref.set({"focus_history": focus_history}, merge=True)
|
58 |
return {"message": "Thêm lịch sử focus thành công"}
|
59 |
|
60 |
-
@app.route('/')
|
61 |
-
def home():
|
62 |
-
return jsonify({"message": "Welcome to the Recommendation API!"})
|
63 |
-
|
64 |
-
|
65 |
# API lấy dữ liệu người dùng
|
66 |
@app.get("/get_user_data")
|
67 |
async def get_user_data(user_id: str):
|
@@ -80,6 +71,7 @@ async def ai_personal_advice(request: AIRequest):
|
|
80 |
bios = data.get("bios", "Chưa có bios.")
|
81 |
focus_history = data.get("focus_history", [])
|
82 |
focus_text = "\n".join([f"- {f['time_start']}: {f['total_time']} phút" for f in focus_history])
|
|
|
83 |
prompt = f"""
|
84 |
Thông tin người dùng:
|
85 |
- Bios: {bios}
|
@@ -88,5 +80,11 @@ async def ai_personal_advice(request: AIRequest):
|
|
88 |
|
89 |
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.
|
90 |
"""
|
|
|
91 |
response = ai_model(prompt, max_length=300)
|
92 |
return {"advice": response[0]["generated_text"]}
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
import firebase_admin
|
4 |
from firebase_admin import credentials, firestore
|
5 |
from transformers import pipeline
|
6 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
7 |
import os
|
8 |
+
from huggingface_hub import login
|
|
|
|
|
|
|
9 |
|
10 |
# Load Firebase
|
11 |
cred = credentials.Certificate("firebase_config.json")
|
12 |
firebase_admin.initialize_app(cred)
|
13 |
db = firestore.client()
|
14 |
|
15 |
+
# Đăng nhập vào Hugging Face (nếu cần)
|
16 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
+
if HF_TOKEN:
|
18 |
+
login(HF_TOKEN)
|
19 |
|
20 |
+
# Load AI Model
|
21 |
ai_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
22 |
|
23 |
app = FastAPI()
|
|
|
53 |
user_ref.set({"focus_history": focus_history}, merge=True)
|
54 |
return {"message": "Thêm lịch sử focus thành công"}
|
55 |
|
|
|
|
|
|
|
|
|
|
|
56 |
# API lấy dữ liệu người dùng
|
57 |
@app.get("/get_user_data")
|
58 |
async def get_user_data(user_id: str):
|
|
|
71 |
bios = data.get("bios", "Chưa có bios.")
|
72 |
focus_history = data.get("focus_history", [])
|
73 |
focus_text = "\n".join([f"- {f['time_start']}: {f['total_time']} phút" for f in focus_history])
|
74 |
+
|
75 |
prompt = f"""
|
76 |
Thông tin người dùng:
|
77 |
- Bios: {bios}
|
|
|
80 |
|
81 |
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.
|
82 |
"""
|
83 |
+
|
84 |
response = ai_model(prompt, max_length=300)
|
85 |
return {"advice": response[0]["generated_text"]}
|
86 |
+
|
87 |
+
# Trang chủ
|
88 |
+
@app.get("/")
|
89 |
+
async def home():
|
90 |
+
return JSONResponse(content={"message": "Welcome to the Recommendation API!"})
|