File size: 2,071 Bytes
3c4beb7
bdac873
8355740
 
f54cbba
8355740
3c4beb7
 
 
bdac873
 
03c1cd6
bdac873
9b43495
557c7ea
71bdcc5
016c8c1
9b43495
8355740
 
 
 
 
 
 
 
 
 
 
 
 
 
9b43495
219304d
4407c7b
8355740
 
 
 
da5835b
8122e7a
 
 
219304d
4407c7b
8355740
 
 
ea87cb8
8122e7a
f54cbba
 
8355740
219304d
8355740
4407c7b
8355740
 
 
03c1cd6
257d023
016c8c1
 
8355740
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from app.classes.ChatbotGenerate import ChatbotGenerate
from app.classes.ChatbotPipeline import ChatbotPipeline
from app.classes.ChatbotTemplate import ChatbotTemplate
from app.classes.DefaultChatbot import DefaultChatbot

app = FastAPI()

class ChatInput(BaseModel):
    text: str
    memory: list = []


TOPIC="La empresa Smartlog y sus servicios"
# chatbot = Chatbot(config.HF_ENV_MODEL_NAME,topic=TOPIC)
chatbot = ChatbotTemplate(TOPIC)

def clean_and_reset(chatbot: DefaultChatbot):
    if chatbot is not None:
        chatbot.clean_and_reset() #Limpiar y resetear el chatbot
        del chatbot

@app.get("/")
def greet_json():
    return {"Hello": "World!"}

@app.post("/predict")
def predict(input: ChatInput):
    return {"response": f"{input.text}"}

@app.post("/chat-generate")
def chat(input: ChatInput):
    global chatbot  # 🔥 Añadir esta línea
    if not isinstance(chatbot, ChatbotGenerate):  
        clean_and_reset(chatbot)
        chatbot = ChatbotGenerate(TOPIC)

    response = chatbot.chat(input.text)
    return {"response": f"{response}"}

@app.post("/chat-pipeline")
def chat_pipeline(input: ChatInput):
    global chatbot  # 🔥 Añadir esta línea
    if not isinstance(chatbot, ChatbotPipeline):  
        clean_and_reset(chatbot)
        chatbot = ChatbotPipeline(TOPIC)

    response = chatbot.chat(input.text)
    return {"response": f"{response}"}

@app.post("/chat-template")
def chat_template(input: ChatInput):  
    global chatbot  # 🔥 Añadir esta línea
    #Si chatbot_template es instancia de ChatbotTemplate
    if not isinstance(chatbot, ChatbotTemplate):  
        clean_and_reset(chatbot)
        chatbot = ChatbotTemplate(TOPIC)

    response, new_chat_history = chatbot.chat(input.text, input.memory) #TODO: anadir chat_history
    return {"response": f"{response}", "chat_history": new_chat_history}

@app.post("/chat-reset")
def chat_reset(input: ChatInput):  
    response = chatbot.clean_and_reset()     
    return {"response": f"{response} {input}"}