Spaces:
Sleeping
Sleeping
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 | |
def greet_json(): | |
return {"Hello": "World!"} | |
def predict(input: ChatInput): | |
return {"response": f"{input.text}"} | |
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}"} | |
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}"} | |
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} | |
def chat_reset(input: ChatInput): | |
response = chatbot.clean_and_reset() | |
return {"response": f"{response} {input}"} | |