jerpint commited on
Commit
d88c550
β€’
1 Parent(s): 397ce12

Add gradio app example (#26)

Browse files

* Put all apps in an `apps` folder
* fix formatting for html separators

buster/__init__.py ADDED
File without changes
buster/apps/gradio_app.ipynb ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "4a6b2b70",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import gradio as gr\n",
11
+ "\n",
12
+ "from buster.chatbot import Chatbot, ChatbotConfig\n",
13
+ "\n",
14
+ "hf_transformers_cfg = ChatbotConfig(\n",
15
+ " documents_file=\"../data/document_embeddings_hf_transformers.tar.gz\",\n",
16
+ " unknown_prompt=\"This doesn't seem to be related to the huggingface library. I am not sure how to answer.\",\n",
17
+ " embedding_model=\"text-embedding-ada-002\",\n",
18
+ " top_k=3,\n",
19
+ " thresh=0.7,\n",
20
+ " max_chars=3000,\n",
21
+ " completion_kwargs={\n",
22
+ " \"engine\": \"text-davinci-003\",\n",
23
+ " \"max_tokens\": 500,\n",
24
+ " },\n",
25
+ " separator=\"<br>\",\n",
26
+ " link_format=\"markdown\",\n",
27
+ " text_after_response=\"I'm a bot πŸ€– trained to answer huggingface πŸ€— transformers questions. My answers aren't always perfect.\",\n",
28
+ " text_before_prompt=\"\"\"You are a slack chatbot assistant answering technical questions about huggingface transformers, a library to train transformers in python.\n",
29
+ " Make sure to format your answers in Markdown format, including code block and snippets.\n",
30
+ " Do not include any links to urls or hyperlinks in your answers.\n",
31
+ "\n",
32
+ " If you do not know the answer to a question, or if it is completely irrelevant to the library usage, simply reply with:\n",
33
+ "\n",
34
+ " 'This doesn't seem to be related to the huggingface library.'\n",
35
+ "\n",
36
+ " For example:\n",
37
+ "\n",
38
+ " What is the meaning of life for huggingface?\n",
39
+ "\n",
40
+ " This doesn't seem to be related to the huggingface library.\n",
41
+ "\n",
42
+ " Now answer the following question:\n",
43
+ " \"\"\",\n",
44
+ ")\n",
45
+ "hf_transformers_chatbot = Chatbot(hf_transformers_cfg)\n",
46
+ "\n",
47
+ "def chat(question, history):\n",
48
+ " history = history or []\n",
49
+ " answer = hf_transformers_chatbot.process_input(question)\n",
50
+ "\n",
51
+ " history.append((question, answer))\n",
52
+ " print(history)\n",
53
+ " return history, history\n",
54
+ "\n",
55
+ "\n",
56
+ "\n",
57
+ "block = gr.Blocks(css=\".gradio-container {background-color: lightgray}\")\n",
58
+ "\n",
59
+ "with block:\n",
60
+ " with gr.Row():\n",
61
+ " gr.Markdown(\"<h3><center>Buster πŸ€–: A Question-Answering Bot for Huggingface πŸ€— Transformers </center></h3>\")\n",
62
+ "\n",
63
+ "\n",
64
+ " chatbot = gr.Chatbot()\n",
65
+ "\n",
66
+ " with gr.Row():\n",
67
+ " message = gr.Textbox(\n",
68
+ " label=\"What's your question?\",\n",
69
+ " placeholder=\"What kind of model should I use for sentiment analysis?\",\n",
70
+ " lines=1,\n",
71
+ " )\n",
72
+ " submit = gr.Button(value=\"Send\", variant=\"secondary\").style(full_width=False)\n",
73
+ "\n",
74
+ " gr.Examples(\n",
75
+ " examples=[\n",
76
+ " \"What kind of models should I use for images and text?\",\n",
77
+ " \"When should I finetune a model vs. training it form scratch?\",\n",
78
+ " \"How can I deploy my trained huggingface model?\",\n",
79
+ " \"Can you give me some python code to quickly finetune a model on my sentiment analysis dataset?\",\n",
80
+ " ],\n",
81
+ " inputs=message,\n",
82
+ " )\n",
83
+ "\n",
84
+ " gr.Markdown(\n",
85
+ " \"\"\"This simple application uses GPT to search the huggingface πŸ€— transformers docs and answer questions.\n",
86
+ " For more info on huggingface transformers view the [full documentation.](https://huggingface.co/docs/transformers/index).\"\"\" \n",
87
+ " )\n",
88
+ "\n",
89
+ "\n",
90
+ " gr.HTML(\n",
91
+ " \"️<center> Created with ❀️ by @jerpint and @hadrienbertrand\"\n",
92
+ " )\n",
93
+ "\n",
94
+ " state = gr.State()\n",
95
+ " agent_state = gr.State()\n",
96
+ "\n",
97
+ " submit.click(chat, inputs=[message, state], outputs=[chatbot, state])\n",
98
+ " message.submit(chat, inputs=[message, state], outputs=[chatbot, state])\n",
99
+ "\n",
100
+ "\n",
101
+ "block.launch(debug=True)"
102
+ ]
103
+ }
104
+ ],
105
+ "metadata": {
106
+ "kernelspec": {
107
+ "display_name": "Python 3 (ipykernel)",
108
+ "language": "python",
109
+ "name": "python3"
110
+ },
111
+ "language_info": {
112
+ "codemirror_mode": {
113
+ "name": "ipython",
114
+ "version": 3
115
+ },
116
+ "file_extension": ".py",
117
+ "mimetype": "text/x-python",
118
+ "name": "python",
119
+ "nbconvert_exporter": "python",
120
+ "pygments_lexer": "ipython3",
121
+ "version": "3.9.12"
122
+ }
123
+ },
124
+ "nbformat": 4,
125
+ "nbformat_minor": 5
126
+ }
app.py β†’ buster/apps/slackbot.py RENAMED
@@ -15,7 +15,7 @@ PYTORCH_CHANNEL = "C04MEK6N882"
15
  HF_TRANSFORMERS_CHANNEL = "C04NJNCJWHE"
16
 
17
  mila_doc_cfg = ChatbotConfig(
18
- documents_file="buster/data/document_embeddings.csv",
19
  unknown_prompt="This doesn't seem to be related to cluster usage.",
20
  embedding_model="text-embedding-ada-002",
21
  top_k=3,
@@ -51,7 +51,7 @@ mila_doc_cfg = ChatbotConfig(
51
  mila_doc_chatbot = Chatbot(mila_doc_cfg)
52
 
53
  orion_cfg = ChatbotConfig(
54
- documents_file="buster/data/document_embeddings_orion.csv",
55
  unknown_prompt="This doesn't seem to be related to the orion library. I am not sure how to answer.",
56
  embedding_model="text-embedding-ada-002",
57
  top_k=3,
@@ -84,7 +84,7 @@ orion_cfg = ChatbotConfig(
84
  orion_chatbot = Chatbot(orion_cfg)
85
 
86
  pytorch_cfg = ChatbotConfig(
87
- documents_file="buster/data/document_embeddings_pytorch.tar.gz",
88
  unknown_prompt="This doesn't seem to be related to the pytorch library. I am not sure how to answer.",
89
  embedding_model="text-embedding-ada-002",
90
  top_k=3,
@@ -117,7 +117,7 @@ pytorch_cfg = ChatbotConfig(
117
  pytorch_chatbot = Chatbot(pytorch_cfg)
118
 
119
  hf_transformers_cfg = ChatbotConfig(
120
- documents_file="buster/data/document_embeddings_hf_transformers.tar.gz",
121
  unknown_prompt="This doesn't seem to be related to the huggingface library. I am not sure how to answer.",
122
  embedding_model="text-embedding-ada-002",
123
  top_k=3,
 
15
  HF_TRANSFORMERS_CHANNEL = "C04NJNCJWHE"
16
 
17
  mila_doc_cfg = ChatbotConfig(
18
+ documents_file="../data/document_embeddings.csv",
19
  unknown_prompt="This doesn't seem to be related to cluster usage.",
20
  embedding_model="text-embedding-ada-002",
21
  top_k=3,
 
51
  mila_doc_chatbot = Chatbot(mila_doc_cfg)
52
 
53
  orion_cfg = ChatbotConfig(
54
+ documents_file="../data/document_embeddings_orion.csv",
55
  unknown_prompt="This doesn't seem to be related to the orion library. I am not sure how to answer.",
56
  embedding_model="text-embedding-ada-002",
57
  top_k=3,
 
84
  orion_chatbot = Chatbot(orion_cfg)
85
 
86
  pytorch_cfg = ChatbotConfig(
87
+ documents_file="../data/document_embeddings_pytorch.tar.gz",
88
  unknown_prompt="This doesn't seem to be related to the pytorch library. I am not sure how to answer.",
89
  embedding_model="text-embedding-ada-002",
90
  top_k=3,
 
117
  pytorch_chatbot = Chatbot(pytorch_cfg)
118
 
119
  hf_transformers_cfg = ChatbotConfig(
120
+ documents_file="../data/document_embeddings_hf_transformers.tar.gz",
121
  unknown_prompt="This doesn't seem to be related to the huggingface library. I am not sure how to answer.",
122
  embedding_model="text-embedding-ada-002",
123
  top_k=3,
buster/chatbot.py CHANGED
@@ -128,12 +128,12 @@ class Chatbot:
128
  names = matched_documents.name.to_list()
129
  similarities = matched_documents.similarity.to_list()
130
 
131
- response += f"{sep}{sep}Here are the sources I used to answer your question:\n"
132
  for url, name, similarity in zip(urls, names, similarities):
133
  if format == "markdown":
134
- response += f"{sep}[{name}]({url}){sep}"
135
  elif format == "slack":
136
- response += f"β€’ <{url}|{name}>, score: {similarity:2.3f}{sep}"
137
  else:
138
  raise ValueError(f"{format} is not a valid URL format.")
139
 
 
128
  names = matched_documents.name.to_list()
129
  similarities = matched_documents.similarity.to_list()
130
 
131
+ response += f"{sep}{sep}Here are the sources I used to answer your question:{sep}"
132
  for url, name, similarity in zip(urls, names, similarities):
133
  if format == "markdown":
134
+ response += f"[{name}]({url}), relevance: {similarity:2.3f}{sep}"
135
  elif format == "slack":
136
+ response += f"β€’ <{url}|{name}>, relevance: {similarity:2.3f}{sep}"
137
  else:
138
  raise ValueError(f"{format} is not a valid URL format.")
139