abdullahalioo commited on
Commit
2f3bde3
·
verified ·
1 Parent(s): d5471e9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +10 -27
main.py CHANGED
@@ -3,17 +3,12 @@ from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from hugchat import hugchat
5
  from hugchat.login import Login
6
- from dotenv import load_dotenv
7
- from contextlib import asynccontextmanager
8
  import logging
9
 
10
  # Set up logging
11
  logging.basicConfig(level=logging.INFO)
12
  logger = logging.getLogger(__name__)
13
 
14
- # Load environment variables
15
- load_dotenv()
16
-
17
  # Initialize FastAPI app
18
  app = FastAPI()
19
 
@@ -22,10 +17,6 @@ EMAIL = "[email protected]"
22
  PASSWORD = "Allahisgreatest17"
23
  ASSISTANT_ID = "682e0c1f5f0c3d952a27498e"
24
 
25
-
26
- # Cookie directory path
27
- COOKIE_PATH_DIR = "./cookies/"
28
-
29
  # Global chatbot instance
30
  chatbot = None
31
 
@@ -33,17 +24,9 @@ chatbot = None
33
  class PromptRequest(BaseModel):
34
  prompt: str
35
 
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
@@ -54,17 +37,12 @@ async def lifespan(app: FastAPI):
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
58
- ensure_cookie_directory()
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
@@ -104,3 +82,8 @@ async def ask(prompt_request: PromptRequest):
104
  logger.error("Error processing request: %s", str(e))
105
  raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
106
 
 
 
 
 
 
 
3
  from pydantic import BaseModel
4
  from hugchat import hugchat
5
  from hugchat.login import Login
 
 
6
  import logging
7
 
8
  # Set up logging
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
 
 
 
12
  # Initialize FastAPI app
13
  app = FastAPI()
14
 
 
17
  PASSWORD = "Allahisgreatest17"
18
  ASSISTANT_ID = "682e0c1f5f0c3d952a27498e"
19
 
 
 
 
 
20
  # Global chatbot instance
21
  chatbot = None
22
 
 
24
  class PromptRequest(BaseModel):
25
  prompt: str
26
 
 
 
 
 
 
 
 
 
 
 
27
  # Lifespan handler for startup and shutdown
28
+ from contextlib import asynccontextmanager
29
+
30
  @asynccontextmanager
31
  async def lifespan(app: FastAPI):
32
  global chatbot
 
37
  EMAIL, "****" if PASSWORD else None, ASSISTANT_ID)
38
  raise ValueError("HuggingFace credentials or assistant ID not provided in environment variables")
39
 
 
 
 
40
  # Log in to HuggingFace
41
  sign = Login(EMAIL, PASSWORD)
42
  try:
43
+ # Try login without cookie_path_dir to avoid file system issues
44
+ cookies = sign.login(save_cookies=False)
45
+ logger.info("Successfully logged in to HuggingFace without saving cookies")
 
 
46
  except Exception as e:
47
  logger.error("Login failed: %s", str(e))
48
  raise
 
82
  logger.error("Error processing request: %s", str(e))
83
  raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
84
 
85
+ # Health check endpoint
86
+ @app.get("/health")
87
+ async def health():
88
+ return {"status": "healthy", "chatbot_initialized": chatbot is not None}
89
+