abdullahalioo commited on
Commit
808ec17
·
verified ·
1 Parent(s): 49158eb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -12
main.py CHANGED
@@ -1,10 +1,15 @@
1
- from fastapi import FastAPI, Query
 
2
  from hugchat import hugchat
3
  from hugchat.login import Login
4
  import os
5
 
6
  app = FastAPI()
7
 
 
 
 
 
8
  # Global variable to store the chatbot instance
9
  chatbot = None
10
 
@@ -38,33 +43,32 @@ def setup_chatbot(email, password, cookie_path, assistant_id):
38
  async def startup_event():
39
  global chatbot
40
  # Credentials and configuration
41
- EMAIL = os.getenv("EMAIL")
42
- PASSWD = os.getenv("PASSWD")
43
  COOKIE_PATH_DIR = "./cookies/"
44
- ASSISTANT_ID = "682e0c1f5f0c3d952a27498e" # Replace with your actual assistant ID
45
 
46
  chatbot = setup_chatbot(EMAIL, PASSWD, COOKIE_PATH_DIR, ASSISTANT_ID)
47
 
48
  @app.post("/generate")
49
- async def generate_response(
50
- prompt: str = Query(..., description="The prompt for the AI")
51
- ):
52
  """
53
- Generates a response from the AI based on the provided prompt.
54
 
55
  Args:
56
- prompt (str): The user's input prompt for the AI.
57
 
58
  Returns:
59
  dict: A dictionary containing the AI's response or an error message.
60
  """
61
  global chatbot
62
  if chatbot is None:
63
- return {"error": "Chatbot not initialized. Please try again later."}
64
 
65
  try:
66
  # Generate response (non-streaming for simplicity in API context)
67
- response = chatbot.chat(prompt, stream=False)
68
  return {"response": response}
69
  except Exception as e:
70
- return {"error": f"Failed to generate response: {str(e)}"}
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
  from hugchat import hugchat
4
  from hugchat.login import Login
5
  import os
6
 
7
  app = FastAPI()
8
 
9
+ # Pydantic model for request body
10
+ class QuestionRequest(BaseModel):
11
+ question: str
12
+
13
  # Global variable to store the chatbot instance
14
  chatbot = None
15
 
 
43
  async def startup_event():
44
  global chatbot
45
  # Credentials and configuration
46
+ EMAIL = os.getenv("HUGCHAT_EMAIL", "[email protected]")
47
+ PASSWD = os.getenv("HUGCHAT_PASSWD", "Allahisgreatest17")
48
  COOKIE_PATH_DIR = "./cookies/"
49
+ ASSISTANT_ID = "66017fca58d60bd7d5c5c26c" # Replace with your actual assistant ID
50
 
51
  chatbot = setup_chatbot(EMAIL, PASSWD, COOKIE_PATH_DIR, ASSISTANT_ID)
52
 
53
  @app.post("/generate")
54
+ async def generate_response(request: QuestionRequest):
 
 
55
  """
56
+ Generates a response from the AI based on the provided question.
57
 
58
  Args:
59
+ request (QuestionRequest): JSON body containing the question.
60
 
61
  Returns:
62
  dict: A dictionary containing the AI's response or an error message.
63
  """
64
  global chatbot
65
  if chatbot is None:
66
+ raise HTTPException(status_code=500, detail="Chatbot not initialized. Please try again later.")
67
 
68
  try:
69
  # Generate response (non-streaming for simplicity in API context)
70
+ response = chatbot.chat(request.question, stream=False)
71
  return {"response": response}
72
  except Exception as e:
73
+ raise HTTPException(status_code=500, detail=f"Failed to generate response: {str(e)}")
74
+