ch4rly-api / app /app.py
ikasplay
memory com list
03c1cd6
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}"}