|
|
import random |
|
|
from functools import partial |
|
|
from typing import List, Tuple |
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
from refresh_bots import BotCache |
|
|
|
|
|
|
|
|
def middchat(bot_cache: BotCache, message: str, history: List[Tuple[str, str]]) -> str: |
|
|
""" |
|
|
Generate a message given a message history and bots |
|
|
|
|
|
Args: |
|
|
bot_cache (BotCache): a bot cache to get bot classes from |
|
|
message (str): the message sent by the user |
|
|
history (List[Tuple[str, str]]): history of messages/responses |
|
|
|
|
|
Returns: |
|
|
str: bot's response |
|
|
""" |
|
|
|
|
|
|
|
|
if message == "update bots" or len(history) <= 1: |
|
|
all_bots = bot_cache.update() |
|
|
else: |
|
|
all_bots = bot_cache.bots |
|
|
|
|
|
|
|
|
bot = None |
|
|
bot_set_id = None |
|
|
for i, (historical_message, _) in enumerate(history): |
|
|
|
|
|
if historical_message.strip() in all_bots: |
|
|
|
|
|
|
|
|
bot = all_bots[historical_message.strip()]() |
|
|
bot_set_id = i |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if bot is None: |
|
|
if message.strip() in all_bots: |
|
|
return f"You're now chatting with {message.strip()}! \ |
|
|
If you aren't sure what to say, type \"examples\"" |
|
|
else: |
|
|
bot_options = "Please choose one of the following bots:\n" |
|
|
bot_options += "\n".join(f"* {bot}" for bot in all_bots.keys()) |
|
|
return bot_options |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
random.seed(457) |
|
|
for historical_message, _ in history[bot_set_id + 1:]: |
|
|
bot.make_reply(historical_message) |
|
|
|
|
|
if message.strip().lower() == "examples": |
|
|
return "Here are some things you might want to say to me:\n" + \ |
|
|
"\n".join(f"* {ex}" for ex in bot.examples()) |
|
|
|
|
|
return bot.make_reply(message) |
|
|
|
|
|
cache = BotCache() |
|
|
chat_partial = partial(middchat, cache) |
|
|
initial_msg = "Welcome to MiddChat! Type anything to get a list of available bots.\nPlease note that if you want to reset your chat, you should refresh this page!\nIf you want to update the bot list, type \"update bots\"" |
|
|
chatbot = gr.Chatbot([(initial_msg, None)]) |
|
|
demo = gr.ChatInterface( |
|
|
fn=chat_partial, |
|
|
chatbot=chatbot, |
|
|
title="MiddChat") |
|
|
|
|
|
demo.launch(share=True) |
|
|
|