Laura Biester commited on
Commit ·
be3de43
1
Parent(s): 0272534
Update setup
Browse files- app.py +23 -40
- refresh_bots.py +56 -0
- refresh_files.py +0 -20
app.py
CHANGED
|
@@ -1,49 +1,30 @@
|
|
| 1 |
import random
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
from typing import Dict
|
| 6 |
-
from refresh_files import refresh_files
|
| 7 |
-
from refresh_files import REPO_PATH as BOT_PATH
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
class BotCache:
|
| 11 |
-
def __init__(self):
|
| 12 |
-
self.bots = self._load_bots()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
self.bots = self._load_bots()
|
| 16 |
-
return self.bots
|
| 17 |
-
|
| 18 |
-
@staticmethod
|
| 19 |
-
def _load_bots() -> Dict[str, type]:
|
| 20 |
-
refresh_files()
|
| 21 |
-
python_files = [f[:-3] for f in os.listdir(BOT_PATH)
|
| 22 |
-
if f.endswith(".py")]
|
| 23 |
-
all_classes = []
|
| 24 |
-
for py in python_files:
|
| 25 |
-
mod = __import__('.'.join([BOT_PATH, py]), fromlist=[py])
|
| 26 |
-
classes = [getattr(mod, x) for x in dir(mod)
|
| 27 |
-
if isinstance(getattr(mod, x), type)]
|
| 28 |
-
all_classes.extend(classes)
|
| 29 |
-
|
| 30 |
-
bots = {}
|
| 31 |
-
for cls in all_classes:
|
| 32 |
-
bots[cls.get_name()] = cls
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
# get the bot options
|
| 42 |
# for new chats, fetch new bots
|
| 43 |
if len(history) == 0:
|
| 44 |
-
all_bots =
|
| 45 |
else:
|
| 46 |
-
all_bots =
|
| 47 |
|
| 48 |
# check to see if the bot was set in the message history
|
| 49 |
bot = None
|
|
@@ -62,7 +43,8 @@ def middchat(message, history):
|
|
| 62 |
# If it doesn't, tell the user about the options again.
|
| 63 |
if bot is None:
|
| 64 |
if message.strip() in all_bots:
|
| 65 |
-
return f"You're now chatting with {message.strip()}!
|
|
|
|
| 66 |
else:
|
| 67 |
bot_options = "Please choose one of the following bots:\n"
|
| 68 |
bot_options += "\n".join(f"* {bot}" for bot in all_bots.keys())
|
|
@@ -84,13 +66,14 @@ def middchat(message, history):
|
|
| 84 |
|
| 85 |
return bot.make_reply(message)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
demo = gr.ChatInterface(
|
| 90 |
fn=middchat,
|
| 91 |
-
chatbot=
|
| 92 |
title="MiddChat",
|
| 93 |
clear_btn="Reset chat history and talk to another bot",
|
| 94 |
retry_btn=None,
|
| 95 |
undo_btn=None)
|
| 96 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
import random
|
| 2 |
+
from functools import partial
|
| 3 |
+
from typing import List, Tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from refresh_bots import BotCache
|
| 8 |
|
| 9 |
|
| 10 |
+
def middchat(bot_cache: BotCache, message: str, history: List[Tuple[str, str]]) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Generate a message given a message history and bots
|
| 13 |
|
| 14 |
+
Args:
|
| 15 |
+
bot_cache (BotCache): a bot cache to get bot classes from
|
| 16 |
+
message (str): the message sent by the user
|
| 17 |
+
history (List[Tuple[str, str]]): history of messages/responses
|
| 18 |
|
| 19 |
+
Returns:
|
| 20 |
+
str: bot's response
|
| 21 |
+
"""
|
| 22 |
# get the bot options
|
| 23 |
# for new chats, fetch new bots
|
| 24 |
if len(history) == 0:
|
| 25 |
+
all_bots = bot_cache.update()
|
| 26 |
else:
|
| 27 |
+
all_bots = bot_cache.bots
|
| 28 |
|
| 29 |
# check to see if the bot was set in the message history
|
| 30 |
bot = None
|
|
|
|
| 43 |
# If it doesn't, tell the user about the options again.
|
| 44 |
if bot is None:
|
| 45 |
if message.strip() in all_bots:
|
| 46 |
+
return f"You're now chatting with {message.strip()}! \
|
| 47 |
+
If you aren't sure what to say, type \"examples\""
|
| 48 |
else:
|
| 49 |
bot_options = "Please choose one of the following bots:\n"
|
| 50 |
bot_options += "\n".join(f"* {bot}" for bot in all_bots.keys())
|
|
|
|
| 66 |
|
| 67 |
return bot.make_reply(message)
|
| 68 |
|
| 69 |
+
cache = BotCache()
|
| 70 |
+
chat_partial = partial(middchat, cache)
|
| 71 |
+
chatbot = gr.Chatbot([("Welcome to MiddChat! " + chat_partial("", []), None)])
|
| 72 |
demo = gr.ChatInterface(
|
| 73 |
fn=middchat,
|
| 74 |
+
chatbot=chatbot,
|
| 75 |
title="MiddChat",
|
| 76 |
clear_btn="Reset chat history and talk to another bot",
|
| 77 |
retry_btn=None,
|
| 78 |
undo_btn=None)
|
| 79 |
+
demo.launch(share=True)
|
refresh_bots.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import os
|
| 3 |
+
from typing import Dict
|
| 4 |
+
|
| 5 |
+
import git
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
REPO_PATH = "student_bots"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class BotCache:
|
| 12 |
+
"""
|
| 13 |
+
Stores and updates bot modules from a git repo
|
| 14 |
+
"""
|
| 15 |
+
def __init__(self):
|
| 16 |
+
self.bots = self._load_bots()
|
| 17 |
+
|
| 18 |
+
def update(self) -> Dict[str, type]:
|
| 19 |
+
"""
|
| 20 |
+
Update and return bot dictionary
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Dict[str, type]: maps bot names to bot classes
|
| 24 |
+
"""
|
| 25 |
+
self.bots = self._load_bots()
|
| 26 |
+
return self.bots
|
| 27 |
+
|
| 28 |
+
@staticmethod
|
| 29 |
+
def _load_bots() -> Dict[str, type]:
|
| 30 |
+
BotCache._refresh_files()
|
| 31 |
+
python_files = [f[:-3] for f in os.listdir(REPO_PATH)
|
| 32 |
+
if f.endswith(".py")]
|
| 33 |
+
all_classes = []
|
| 34 |
+
for py in python_files:
|
| 35 |
+
mod = __import__('.'.join([REPO_PATH, py]), fromlist=[py])
|
| 36 |
+
classes = [getattr(mod, x) for x in dir(mod)
|
| 37 |
+
if isinstance(getattr(mod, x), type)]
|
| 38 |
+
all_classes.extend(classes)
|
| 39 |
+
|
| 40 |
+
bots = {}
|
| 41 |
+
for cls in all_classes:
|
| 42 |
+
bots[cls.get_name()] = cls
|
| 43 |
+
|
| 44 |
+
return bots
|
| 45 |
+
|
| 46 |
+
@staticmethod
|
| 47 |
+
def _refresh_files():
|
| 48 |
+
if os.path.exists(REPO_PATH):
|
| 49 |
+
repo = git.Repo(REPO_PATH)
|
| 50 |
+
origin = repo.remotes.origin
|
| 51 |
+
origin.pull()
|
| 52 |
+
else:
|
| 53 |
+
pat_token = os.environ["GITHUB_PAT_TOKEN"]
|
| 54 |
+
username = os.environ["GITHUB_USERNAME"]
|
| 55 |
+
repo = os.environ["GITHUB_REPO"]
|
| 56 |
+
git.Repo.clone_from(f"https://{pat_token}@github.com/{username}/{repo}.git", REPO_PATH)
|
refresh_files.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import git
|
| 3 |
-
|
| 4 |
-
REPO_PATH = "student_bots"
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def refresh_files():
|
| 8 |
-
if os.path.exists(REPO_PATH):
|
| 9 |
-
repo = git.Repo(REPO_PATH)
|
| 10 |
-
origin = repo.remotes.origin
|
| 11 |
-
origin.pull()
|
| 12 |
-
else:
|
| 13 |
-
pat_token = os.environ["GITHUB_PAT_TOKEN"]
|
| 14 |
-
username = os.environ["GITHUB_USERNAME"]
|
| 15 |
-
repo = os.environ["GITHUB_REPO"]
|
| 16 |
-
git.Repo.clone_from(f"https://{pat_token}@github.com/{username}/{repo}.git", REPO_PATH)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if __name__ == "__main__":
|
| 20 |
-
refresh_files()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|