File size: 3,211 Bytes
808ec17
 
d7f32ed
 
74a7b28
d7f32ed
49158eb
29331bd
808ec17
 
 
 
49158eb
 
1cb9532
49158eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03991d8
49158eb
 
 
 
 
d5e95f4
 
49158eb
e0c20b6
664b22f
49158eb
cc3751a
49158eb
808ec17
49158eb
808ec17
49158eb
 
808ec17
49158eb
 
 
 
 
 
808ec17
49158eb
 
cbb83bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49158eb
808ec17
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from hugchat import hugchat
from hugchat.login import Login
import os

app = FastAPI()

# Pydantic model for request body
class QuestionRequest(BaseModel):
    question: str

# Global variable to store the chatbot instance
chatbot = None

def setup_chatbot(email, password, cookie_path, assistant_id):
    """
    Sets up the Hugging Face chatbot with login and conversation.
    
    Args:
        email (str): User email for login
        password (str): User password for login
        cookie_path (str): Directory to store cookies
        assistant_id (str): ID of the assistant to use
    
    Returns:
        hugchat.ChatBot: Configured chatbot instance
    """
    try:
        # Create cookie directory if it doesn't exist
        os.makedirs(cookie_path, exist_ok=True)
        
        sign = Login(email, password)
        cookies = sign.login(cookie_dir_path=cookie_path, save_cookies=True)
        chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
        chatbot.new_conversation(assistant=assistant_id, switch_to=True)
        return chatbot
    except Exception as e:
        raise Exception(f"Failed to set up chatbot: {e}")

# Initialize chatbot at startup
@app.on_event("startup")
async def startup_event():
    global chatbot
    # Credentials and configuration
    EMAIL = os.getenv("EMAIL")
    PASSWD = os.getenv("PASSWD")
    COOKIE_PATH_DIR = "./cookies/"
    ASSISTANT_ID = "682e0c1f5f0c3d952a27498e"  # Replace with your actual assistant ID
    
    chatbot = setup_chatbot(EMAIL, PASSWD, COOKIE_PATH_DIR, ASSISTANT_ID)

@app.post("/generate")
async def generate_response(request: QuestionRequest):
    """
    Generates a response from the AI based on the provided question.
    
    Args:
        request (QuestionRequest): JSON body containing the question.
    
    Returns:
        dict: A dictionary containing the AI's response or an error message.
    """
    global chatbot
    if chatbot is None:
        raise HTTPException(status_code=500, detail="Chatbot not initialized. Please try again later.")
    
    try:
        # Generate response (non-streaming for simplicity)
        response_data = chatbot.chat(request.question, stream=False)
        
        # Extract the actual response text
        # The response may be a dictionary; check for 'gen' or other keys
        if isinstance(response_data, dict):
            # Assuming 'gen' contains the response text (list of strings)
            response_text = "".join(response_data.get("gen", [])) if response_data.get("gen") else "Here's what we can do: Let's discuss your vision!"
            if not response_text:
                # Fallback to a default premium response if 'gen' is empty
                response_text = f"Welcome, valued client! How can Abdullah Ali and our premium team bring your vision to life with a custom website or AI chatbot?"
        else:
            response_text = response_data  # Direct string response (if hugchat returns string)
        
        return {"response": response_text}
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Failed to generate response: {str(e)}")