Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,186 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import hashlib
|
| 3 |
-
import os
|
| 4 |
-
import aiohttp
|
| 5 |
-
import asyncio
|
| 6 |
-
import time
|
| 7 |
-
from langsmith import traceable
|
| 8 |
-
import random
|
| 9 |
-
import discord
|
| 10 |
-
from transformers import pipeline
|
| 11 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
-
import numpy as np
|
| 13 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
-
from pydantic import BaseModel
|
| 15 |
-
from typing import List, Optional
|
| 16 |
-
from tqdm import tqdm
|
| 17 |
-
import re
|
| 18 |
-
import os
|
| 19 |
-
from teapotai import TeapotAI, TeapotAISettings
|
| 20 |
-
import pandas as pd
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
st.set_page_config(page_title="TeapotAI Discord Bot", page_icon=":robot_face:", layout="wide")
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
wiki_data = pd.read_csv("https://docs.google.com/spreadsheets/d/1NNbdQWIfVHq09lMhVSN36_SkGu6XgmKTXgBWPyQcBpk/export?format=csv")
|
| 28 |
-
documents = wiki_data['content'].str.split('\n\n').explode().reset_index(drop=True)
|
| 29 |
-
documents
|
| 30 |
-
|
| 31 |
-
# Intialize Teapot with documents
|
| 32 |
-
teapot_ai = TeapotAI(documents=documents.to_list(), settings=TeapotAISettings(rag_num_results=7))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# I apologize, but I'm not qualified to provide personalized advice or recommendations.
|
| 35 |
-
# Latent caching is a feature of the training process that saves intermediate data and can be used to speed up the training.
|
| 36 |
-
# Yes
|
| 37 |
-
# I'm sorry, but I don't have information on the optimizers.
|
| 38 |
-
# 2
|
| 39 |
-
# The Concepts tab is made up of the following elements: For expert users: Embedding Training To get a general overview of the UI, please read the Quick Start Guide first.
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
async def wrapper(*args, **kwargs):
|
| 44 |
-
start_time = time.time()
|
| 45 |
-
result = await func(*args, **kwargs) # Make it awaitable
|
| 46 |
-
end_time = time.time()
|
| 47 |
-
print(f"{func.__name__} executed in {end_time - start_time:.4f} seconds")
|
| 48 |
-
return result
|
| 49 |
-
return wrapper
|
| 50 |
|
|
|
|
| 51 |
|
| 52 |
-
# @traceable
|
| 53 |
-
@log_time
|
| 54 |
-
async def handle_chat(user_input):
|
| 55 |
-
def run_sync_query():
|
| 56 |
-
return teapot_ai.query(user_input)
|
| 57 |
|
| 58 |
-
output_text = await asyncio.to_thread(run_sync_query)
|
| 59 |
|
| 60 |
-
return output_text
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
st.
|
| 64 |
|
| 65 |
DISCORD_TOKEN = os.environ.get("discord_key")
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
client = discord.Client(intents=intents)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@client.event
|
| 76 |
async def on_ready():
|
| 77 |
print(f'Logged in as {client.user}')
|
| 78 |
|
| 79 |
-
|
| 80 |
@client.event
|
| 81 |
async def on_message(message):
|
| 82 |
-
# Check if the message is from the bot itself to prevent a loop
|
| 83 |
if message.author == client.user:
|
| 84 |
return
|
| 85 |
-
|
| 86 |
-
# Exit the function if the bot is not mentioned
|
| 87 |
if f'<@{client.user.id}>' not in message.content:
|
| 88 |
-
return
|
| 89 |
-
|
| 90 |
-
print(message.content)
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
async with message.channel.typing():
|
| 94 |
-
cleaned_message=message.content.replace(
|
| 95 |
-
response = await
|
| 96 |
-
print(response)
|
| 97 |
sent_message = await message.reply(response)
|
| 98 |
-
|
| 99 |
-
# Create a thread from the sent message
|
| 100 |
if is_debug:
|
| 101 |
-
thread = await sent_message.create_thread(
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
await thread.send(debug_info)
|
| 105 |
|
| 106 |
-
|
| 107 |
@st.cache_resource
|
| 108 |
def initialize():
|
| 109 |
st.session_state["initialized"] = True
|
| 110 |
client.run(DISCORD_TOKEN)
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
initialize()
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import hashlib
|
| 3 |
+
# import os
|
| 4 |
+
# import aiohttp
|
| 5 |
+
# import asyncio
|
| 6 |
+
# import time
|
| 7 |
+
# from langsmith import traceable
|
| 8 |
+
# import random
|
| 9 |
+
# import discord
|
| 10 |
+
# from transformers import pipeline
|
| 11 |
+
# from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
+
# import numpy as np
|
| 13 |
+
# from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
+
# from pydantic import BaseModel
|
| 15 |
+
# from typing import List, Optional
|
| 16 |
+
# from tqdm import tqdm
|
| 17 |
+
# import re
|
| 18 |
+
# import os
|
| 19 |
+
# from teapotai import TeapotAI, TeapotAISettings
|
| 20 |
+
# import pandas as pd
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# st.set_page_config(page_title="TeapotAI Discord Bot", page_icon=":robot_face:", layout="wide")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# wiki_data = pd.read_csv("https://docs.google.com/spreadsheets/d/1NNbdQWIfVHq09lMhVSN36_SkGu6XgmKTXgBWPyQcBpk/export?format=csv")
|
| 28 |
+
# documents = wiki_data['content'].str.split('\n\n').explode().reset_index(drop=True)
|
| 29 |
+
# documents
|
| 30 |
+
|
| 31 |
+
# # Intialize Teapot with documents
|
| 32 |
+
# teapot_ai = TeapotAI(documents=documents.to_list(), settings=TeapotAISettings(rag_num_results=7))
|
| 33 |
+
|
| 34 |
+
# # I apologize, but I'm not qualified to provide personalized advice or recommendations.
|
| 35 |
+
# # Latent caching is a feature of the training process that saves intermediate data and can be used to speed up the training.
|
| 36 |
+
# # Yes
|
| 37 |
+
# # I'm sorry, but I don't have information on the optimizers.
|
| 38 |
+
# # 2
|
| 39 |
+
# # The Concepts tab is made up of the following elements: For expert users: Embedding Training To get a general overview of the UI, please read the Quick Start Guide first.
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# def log_time(func):
|
| 43 |
+
# async def wrapper(*args, **kwargs):
|
| 44 |
+
# start_time = time.time()
|
| 45 |
+
# result = await func(*args, **kwargs) # Make it awaitable
|
| 46 |
+
# end_time = time.time()
|
| 47 |
+
# print(f"{func.__name__} executed in {end_time - start_time:.4f} seconds")
|
| 48 |
+
# return result
|
| 49 |
+
# return wrapper
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# # @traceable
|
| 53 |
+
# @log_time
|
| 54 |
+
# async def handle_chat(user_input):
|
| 55 |
+
# def run_sync_query():
|
| 56 |
+
# return teapot_ai.query(user_input)
|
| 57 |
+
|
| 58 |
+
# output_text = await asyncio.to_thread(run_sync_query)
|
| 59 |
+
|
| 60 |
+
# return output_text
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# st.write("418 I'm a teapot")
|
| 64 |
+
|
| 65 |
+
# DISCORD_TOKEN = os.environ.get("discord_key")
|
| 66 |
+
|
| 67 |
+
# # Create an instance of Intents and enable the required ones
|
| 68 |
+
# intents = discord.Intents.default() # Default intents enable basic functionality
|
| 69 |
+
# intents.messages = True # Enable message-related events
|
| 70 |
+
|
| 71 |
+
# # Create an instance of a client with the intents
|
| 72 |
+
# client = discord.Client(intents=intents)
|
| 73 |
+
|
| 74 |
+
# # Event when the bot has connected to the server
|
| 75 |
+
# @client.event
|
| 76 |
+
# async def on_ready():
|
| 77 |
+
# print(f'Logged in as {client.user}')
|
| 78 |
+
|
| 79 |
+
# # Event when a message is received
|
| 80 |
+
# @client.event
|
| 81 |
+
# async def on_message(message):
|
| 82 |
+
# # Check if the message is from the bot itself to prevent a loop
|
| 83 |
+
# if message.author == client.user:
|
| 84 |
+
# return
|
| 85 |
+
|
| 86 |
+
# # Exit the function if the bot is not mentioned
|
| 87 |
+
# if f'<@{client.user.id}>' not in message.content:
|
| 88 |
+
# return
|
| 89 |
+
|
| 90 |
+
# print(message.content)
|
| 91 |
+
|
| 92 |
+
# is_debug = "debug:" in message.content or "Debug:" in message.content
|
| 93 |
+
# async with message.channel.typing():
|
| 94 |
+
# cleaned_message=message.content.replace("debug:", "").replace("Debug:","").replace(f'<@{client.user.id}>',"")
|
| 95 |
+
# response = await handle_chat(cleaned_message)
|
| 96 |
+
# print(response)
|
| 97 |
+
# sent_message = await message.reply(response)
|
| 98 |
+
|
| 99 |
+
# # Create a thread from the sent message
|
| 100 |
+
# if is_debug:
|
| 101 |
+
# thread = await sent_message.create_thread(name=f"""Debug Thread: '{cleaned_message[:80]}'""", auto_archive_duration=60)
|
| 102 |
+
|
| 103 |
+
# # Send a message in the created thread
|
| 104 |
+
# await thread.send(debug_info)
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# @st.cache_resource
|
| 108 |
+
# def initialize():
|
| 109 |
+
# st.session_state["initialized"] = True
|
| 110 |
+
# client.run(DISCORD_TOKEN)
|
| 111 |
|
| 112 |
+
# return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# initialize()
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
|
|
|
| 117 |
|
|
|
|
| 118 |
|
| 119 |
+
import streamlit as st
|
| 120 |
+
import os
|
| 121 |
+
import aiohttp
|
| 122 |
+
import asyncio
|
| 123 |
+
import discord
|
| 124 |
+
import pandas as pd
|
| 125 |
+
from teapotai import TeapotAI, TeapotAISettings
|
| 126 |
|
| 127 |
+
st.set_page_config(page_title="TeapotAI Discord Bot", page_icon=":robot_face:", layout="wide")
|
| 128 |
|
| 129 |
DISCORD_TOKEN = os.environ.get("discord_key")
|
| 130 |
|
| 131 |
+
# ========= CONFIG =========
|
| 132 |
+
CONFIG = {
|
| 133 |
+
"OneTrainer": TeapotAI(
|
| 134 |
+
documents=pd.read_csv("https://docs.google.com/spreadsheets/d/1NNbdQWIfVHq09lMhVSN36_SkGu6XgmKTXgBWPyQcBpk/export?format=csv").content.str.split('\n\n').explode().reset_index(drop=True).to_list(),
|
| 135 |
+
settings=TeapotAISettings(rag_num_results=7)),
|
| 136 |
+
"Teapot AI": TeapotAI(dataset=[])
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
# ========= DISCORD CLIENT =========
|
| 140 |
+
intents = discord.Intents.default()
|
| 141 |
+
intents.messages = True
|
| 142 |
client = discord.Client(intents=intents)
|
| 143 |
|
| 144 |
+
async def handle_teapot_inference(server_name, user_input):
|
| 145 |
+
teapot_instance = CONFIG.get(server_name, CONFIG["Teapot AI"])
|
| 146 |
+
print(f"Using Teapot instance for server: {server_name}")
|
| 147 |
+
response, debug_info = await teapot_instance.infer(user_input)
|
| 148 |
+
return response, debug_info
|
| 149 |
+
|
| 150 |
+
|
| 151 |
@client.event
|
| 152 |
async def on_ready():
|
| 153 |
print(f'Logged in as {client.user}')
|
| 154 |
|
| 155 |
+
|
| 156 |
@client.event
|
| 157 |
async def on_message(message):
|
|
|
|
| 158 |
if message.author == client.user:
|
| 159 |
return
|
| 160 |
+
|
|
|
|
| 161 |
if f'<@{client.user.id}>' not in message.content:
|
| 162 |
+
return
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
server_name = message.guild.name if message.guild else "Teapot AI"
|
| 165 |
+
server_name ="OneTrainer"
|
| 166 |
async with message.channel.typing():
|
| 167 |
+
cleaned_message = message.content.replace(f'<@{client.user.id}>', "").strip()
|
| 168 |
+
response, debug_info = await handle_teapot_inference(server_name, cleaned_message)
|
|
|
|
| 169 |
sent_message = await message.reply(response)
|
| 170 |
+
|
|
|
|
| 171 |
if is_debug:
|
| 172 |
+
thread = await sent_message.create_thread(
|
| 173 |
+
name=f"Debug Thread: '{cleaned_message[:80]}'",
|
| 174 |
+
auto_archive_duration=60
|
| 175 |
+
)
|
| 176 |
await thread.send(debug_info)
|
| 177 |
|
| 178 |
+
# ========= STREAMLIT =========
|
| 179 |
@st.cache_resource
|
| 180 |
def initialize():
|
| 181 |
st.session_state["initialized"] = True
|
| 182 |
client.run(DISCORD_TOKEN)
|
| 183 |
+
return
|
| 184 |
|
| 185 |
+
st.write("418 I'm a teapot")
|
|
|
|
| 186 |
initialize()
|