{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "4a6b2b70", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import gradio as gr\n", "\n", "from buster.chatbot import Chatbot, ChatbotConfig\n", "\n", "hf_transformers_cfg = ChatbotConfig(\n", " documents_file=\"../data/document_embeddings_huggingface.tar.gz\",\n", " unknown_prompt=\"This doesn't seem to be related to the huggingface library. I am not sure how to answer.\",\n", " embedding_model=\"text-embedding-ada-002\",\n", " top_k=3,\n", " thresh=0.7,\n", " max_words=3000,\n", " completion_kwargs={\n", " \"engine\": \"text-davinci-003\",\n", " \"max_tokens\": 500,\n", " },\n", " separator=\"
\",\n", " link_format=\"markdown\",\n", " text_after_response=\"I'm a bot 🤖 trained to answer huggingface 🤗 transformers questions. My answers aren't always perfect.\",\n", " text_before_prompt=\"\"\"You are a slack chatbot assistant answering technical questions about huggingface transformers, a library to train transformers in python.\n", "Make sure to format your answers in Markdown format, including code block and snippets.\n", "Do not include any links to urls or hyperlinks in your answers.\n", "\n", "If you do not know the answer to a question, or if it is completely irrelevant to the library usage, simply reply with:\n", "\n", "'This doesn't seem to be related to the huggingface library.'\n", "\n", "For example:\n", "\n", "What is the meaning of life for huggingface?\n", "\n", "This doesn't seem to be related to the huggingface library.\n", "\n", "Now answer the following question:\n", "\"\"\",\n", ")\n", "hf_transformers_chatbot = Chatbot(hf_transformers_cfg)\n", "\n", "def chat(question, history):\n", " history = history or []\n", " \n", " answer = hf_transformers_chatbot.process_input(question)\n", " \n", " # formatting hack for code blocks to render properly every time\n", " answer = answer.replace(\"```\", \"\\n```\\n\")\n", "\n", " history.append((question, answer))\n", " return history, history\n", "\n", "\n", "\n", "block = gr.Blocks(css=\".gradio-container {background-color: lightgray}\")\n", "\n", "with block:\n", " with gr.Row():\n", " gr.Markdown(\"

Buster 🤖: A Question-Answering Bot for Huggingface 🤗 Transformers

\")\n", "\n", "\n", " chatbot = gr.Chatbot()\n", "\n", " with gr.Row():\n", " message = gr.Textbox(\n", " label=\"What's your question?\",\n", " placeholder=\"What kind of model should I use for sentiment analysis?\",\n", " lines=1,\n", " )\n", " submit = gr.Button(value=\"Send\", variant=\"secondary\").style(full_width=False)\n", "\n", " gr.Examples(\n", " examples=[\n", " \"What kind of models should I use for images and text?\",\n", " \"When should I finetune a model vs. training it form scratch?\",\n", " \"How can I deploy my trained huggingface model?\",\n", " \"Can you give me some python code to quickly finetune a model on my sentiment analysis dataset?\",\n", " ],\n", " inputs=message,\n", " )\n", "\n", " gr.Markdown(\n", " \"\"\"This simple application uses GPT to search the huggingface 🤗 transformers docs and answer questions.\n", " For more info on huggingface transformers view the [full documentation.](https://huggingface.co/docs/transformers/index).\"\"\" \n", " )\n", "\n", "\n", " gr.HTML(\n", " \"️
Created with ❤️ by @jerpint and @hadrienbertrand\"\n", " )\n", "\n", " state = gr.State()\n", " agent_state = gr.State()\n", "\n", " submit.click(chat, inputs=[message, state], outputs=[chatbot, state])\n", " message.submit(chat, inputs=[message, state], outputs=[chatbot, state])\n", "\n", "\n", "block.launch(debug=True)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12 (main, Apr 5 2022, 01:52:34) \n[Clang 12.0.0 ]" }, "vscode": { "interpreter": { "hash": "5abee959af829406add33dbeab4b81a0c8afd11a2faef151b217e9aebad2d8c1" } } }, "nbformat": 4, "nbformat_minor": 5 }