Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -17,12 +17,12 @@ load_dotenv()
|
|
17 |
# Initialize FastAPI app
|
18 |
app = FastAPI()
|
19 |
|
20 |
-
# HuggingFace credentials from environment variables
|
21 |
# HuggingFace credentials from environment variables
|
22 |
EMAIL = "[email protected]"
|
23 |
PASSWORD = "Allahisgreatest17"
|
24 |
ASSISTANT_ID = "682e0c1f5f0c3d952a27498e"
|
25 |
|
|
|
26 |
# Cookie directory path
|
27 |
COOKIE_PATH_DIR = "./cookies/"
|
28 |
|
@@ -36,17 +36,22 @@ class PromptRequest(BaseModel):
|
|
36 |
# Ensure cookie directory exists
|
37 |
def ensure_cookie_directory():
|
38 |
if not os.path.exists(COOKIE_PATH_DIR):
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Lifespan handler for startup and shutdown
|
43 |
@asynccontextmanager
|
44 |
async def lifespan(app: FastAPI):
|
45 |
-
# Startup: Initialize chatbot
|
46 |
global chatbot
|
47 |
try:
|
48 |
# Validate credentials
|
49 |
if not EMAIL or not PASSWORD or not ASSISTANT_ID:
|
|
|
|
|
50 |
raise ValueError("HuggingFace credentials or assistant ID not provided in environment variables")
|
51 |
|
52 |
# Ensure cookie directory exists
|
@@ -54,17 +59,24 @@ async def lifespan(app: FastAPI):
|
|
54 |
|
55 |
# Log in to HuggingFace
|
56 |
sign = Login(EMAIL, PASSWORD)
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# Initialize chatbot
|
61 |
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
62 |
-
# Start a new conversation
|
63 |
chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True)
|
64 |
logger.info("Chatbot initialized with assistant ID: %s", ASSISTANT_ID)
|
|
|
65 |
except Exception as e:
|
66 |
logger.error("Failed to initialize chatbot: %s", str(e))
|
67 |
-
raise HTTPException(status_code=500, detail="Failed to initialize chatbot")
|
68 |
|
69 |
yield # Application runs here
|
70 |
|
@@ -79,6 +91,7 @@ app.lifespan = lifespan
|
|
79 |
async def ask(prompt_request: PromptRequest):
|
80 |
try:
|
81 |
if not chatbot:
|
|
|
82 |
raise HTTPException(status_code=500, detail="Chatbot not initialized")
|
83 |
|
84 |
# Send prompt to chatbot
|
|
|
17 |
# Initialize FastAPI app
|
18 |
app = FastAPI()
|
19 |
|
|
|
20 |
# HuggingFace credentials from environment variables
|
21 |
EMAIL = "[email protected]"
|
22 |
PASSWORD = "Allahisgreatest17"
|
23 |
ASSISTANT_ID = "682e0c1f5f0c3d952a27498e"
|
24 |
|
25 |
+
|
26 |
# Cookie directory path
|
27 |
COOKIE_PATH_DIR = "./cookies/"
|
28 |
|
|
|
36 |
# Ensure cookie directory exists
|
37 |
def ensure_cookie_directory():
|
38 |
if not os.path.exists(COOKIE_PATH_DIR):
|
39 |
+
try:
|
40 |
+
os.makedirs(COOKIE_PATH_DIR)
|
41 |
+
logger.info("Created cookie directory: %s", COOKIE_PATH_DIR)
|
42 |
+
except Exception as e:
|
43 |
+
logger.error("Failed to create cookie directory: %s", str(e))
|
44 |
+
raise
|
45 |
|
46 |
# Lifespan handler for startup and shutdown
|
47 |
@asynccontextmanager
|
48 |
async def lifespan(app: FastAPI):
|
|
|
49 |
global chatbot
|
50 |
try:
|
51 |
# Validate credentials
|
52 |
if not EMAIL or not PASSWORD or not ASSISTANT_ID:
|
53 |
+
logger.error("Missing environment variables: EMAIL=%s, PASSWORD=%s, ASSISTANT_ID=%s",
|
54 |
+
EMAIL, "****" if PASSWORD else None, ASSISTANT_ID)
|
55 |
raise ValueError("HuggingFace credentials or assistant ID not provided in environment variables")
|
56 |
|
57 |
# Ensure cookie directory exists
|
|
|
59 |
|
60 |
# Log in to HuggingFace
|
61 |
sign = Login(EMAIL, PASSWORD)
|
62 |
+
try:
|
63 |
+
cookies = sign.login(cookie_path_dir=COOKIE_PATH_DIR, save_cookies=True)
|
64 |
+
logger.info("Successfully logged in to HuggingFace with cookie_path_dir")
|
65 |
+
except TypeError as e:
|
66 |
+
logger.warning("cookie_path_dir not supported, trying default cookie storage: %s", str(e))
|
67 |
+
cookies = sign.login(save_cookies=True) # Fallback to default cookie storage
|
68 |
+
except Exception as e:
|
69 |
+
logger.error("Login failed: %s", str(e))
|
70 |
+
raise
|
71 |
|
72 |
# Initialize chatbot
|
73 |
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
|
|
74 |
chatbot.new_conversation(assistant=ASSISTANT_ID, switch_to=True)
|
75 |
logger.info("Chatbot initialized with assistant ID: %s", ASSISTANT_ID)
|
76 |
+
|
77 |
except Exception as e:
|
78 |
logger.error("Failed to initialize chatbot: %s", str(e))
|
79 |
+
raise HTTPException(status_code=500, detail=f"Failed to initialize chatbot: {str(e)}")
|
80 |
|
81 |
yield # Application runs here
|
82 |
|
|
|
91 |
async def ask(prompt_request: PromptRequest):
|
92 |
try:
|
93 |
if not chatbot:
|
94 |
+
logger.error("Chatbot not initialized")
|
95 |
raise HTTPException(status_code=500, detail="Chatbot not initialized")
|
96 |
|
97 |
# Send prompt to chatbot
|