Spaces:
Running
Running
Add thumbs up (#22)
Browse files* add like button; log response to mongodb
* remove DEV from space title
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title: TowardsAI π€ Buster
|
| 3 |
emoji: π€
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: blue
|
|
|
|
| 1 |
---
|
| 2 |
+
title: TowardsAI π€ Buster
|
| 3 |
emoji: π€
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: blue
|
app.py
CHANGED
|
@@ -35,6 +35,24 @@ AVAILABLE_SOURCES = [
|
|
| 35 |
]
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def format_sources(matched_documents: pd.DataFrame) -> str:
|
| 39 |
if len(matched_documents) == 0:
|
| 40 |
return ""
|
|
@@ -132,6 +150,9 @@ with demo:
|
|
| 132 |
gr.Markdown(
|
| 133 |
"<h3><center>Toward's AI x Buster π€: A Question-Answering Bot for anything AI-related</center></h3>"
|
| 134 |
)
|
|
|
|
|
|
|
|
|
|
| 135 |
source_selection = gr.Dropdown(
|
| 136 |
choices=AVAILABLE_SOURCES_UI,
|
| 137 |
label="Select Sources",
|
|
@@ -159,15 +180,16 @@ with demo:
|
|
| 159 |
"\n\n### Powered by [Buster π€](www.github.com/jerpint/buster)"
|
| 160 |
)
|
| 161 |
|
| 162 |
-
|
| 163 |
|
| 164 |
submit.click(user, [question, chatbot], [question, chatbot], queue=False).then(
|
| 165 |
-
get_answer, inputs=[chatbot, source_selection], outputs=[chatbot,
|
| 166 |
-
).then(add_sources, inputs=[chatbot,
|
| 167 |
question.submit(user, [question, chatbot], [question, chatbot], queue=False).then(
|
| 168 |
-
get_answer, inputs=[chatbot, source_selection], outputs=[chatbot,
|
| 169 |
-
).then(add_sources, inputs=[chatbot,
|
| 170 |
|
|
|
|
| 171 |
|
| 172 |
demo.queue(concurrency_count=16)
|
| 173 |
demo.launch(debug=True, share=False)
|
|
|
|
| 35 |
]
|
| 36 |
|
| 37 |
|
| 38 |
+
def log_likes(completion: Completion, like_data: gr.LikeData):
|
| 39 |
+
# make it a str so json-parsable
|
| 40 |
+
|
| 41 |
+
collection = "liked_data-test"
|
| 42 |
+
|
| 43 |
+
completion_json = completion.to_json(
|
| 44 |
+
columns_to_ignore=["embedding", "similarity", "similarity_to_answer"]
|
| 45 |
+
)
|
| 46 |
+
completion_json["liked"] = like_data.liked
|
| 47 |
+
logger.info(f"User reported {like_data.liked=}")
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
cfg.mongo_db[collection].insert_one(completion_json)
|
| 51 |
+
logger.info("")
|
| 52 |
+
except:
|
| 53 |
+
logger.info("Something went wrong logging")
|
| 54 |
+
|
| 55 |
+
|
| 56 |
def format_sources(matched_documents: pd.DataFrame) -> str:
|
| 57 |
if len(matched_documents) == 0:
|
| 58 |
return ""
|
|
|
|
| 150 |
gr.Markdown(
|
| 151 |
"<h3><center>Toward's AI x Buster π€: A Question-Answering Bot for anything AI-related</center></h3>"
|
| 152 |
)
|
| 153 |
+
|
| 154 |
+
latest_completion = gr.State()
|
| 155 |
+
|
| 156 |
source_selection = gr.Dropdown(
|
| 157 |
choices=AVAILABLE_SOURCES_UI,
|
| 158 |
label="Select Sources",
|
|
|
|
| 180 |
"\n\n### Powered by [Buster π€](www.github.com/jerpint/buster)"
|
| 181 |
)
|
| 182 |
|
| 183 |
+
completion = gr.State()
|
| 184 |
|
| 185 |
submit.click(user, [question, chatbot], [question, chatbot], queue=False).then(
|
| 186 |
+
get_answer, inputs=[chatbot, source_selection], outputs=[chatbot, completion]
|
| 187 |
+
).then(add_sources, inputs=[chatbot, completion], outputs=[chatbot])
|
| 188 |
question.submit(user, [question, chatbot], [question, chatbot], queue=False).then(
|
| 189 |
+
get_answer, inputs=[chatbot, source_selection], outputs=[chatbot, completion]
|
| 190 |
+
).then(add_sources, inputs=[chatbot, completion], outputs=[chatbot])
|
| 191 |
|
| 192 |
+
chatbot.like(log_likes, completion)
|
| 193 |
|
| 194 |
demo.queue(concurrency_count=16)
|
| 195 |
demo.launch(debug=True, share=False)
|
cfg.py
CHANGED
|
@@ -9,6 +9,12 @@ from buster.retriever import DeepLakeRetriever, Retriever
|
|
| 9 |
from buster.tokenizers import GPTTokenizer
|
| 10 |
from buster.validators import QuestionAnswerValidator, Validator
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
logging.basicConfig(level=logging.INFO)
|
| 14 |
|
|
|
|
| 9 |
from buster.tokenizers import GPTTokenizer
|
| 10 |
from buster.validators import QuestionAnswerValidator, Validator
|
| 11 |
|
| 12 |
+
from utils import init_mongo_db
|
| 13 |
+
|
| 14 |
+
MONGODB_URI = os.getenv("MONGODB_URI")
|
| 15 |
+
mongo_db = init_mongo_db(uri=MONGODB_URI, db_name="towardsai-buster")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
|
utils.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pymongo.mongo_client import MongoClient
|
| 2 |
+
from pymongo.server_api import ServerApi
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def init_mongo_db(uri: str, db_name: str):
|
| 6 |
+
"""Initialize the mongodb database."""
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
assert uri is not None, "No URI passed"
|
| 10 |
+
client = MongoClient(uri, server_api=ServerApi("1"))
|
| 11 |
+
database = client[db_name]
|
| 12 |
+
print("Connected to MongoDB")
|
| 13 |
+
return database
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print("Something went wrong connecting to mongodb")
|
| 16 |
+
return
|