File size: 2,781 Bytes
c43857d
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: chatbot_retry_undo_like"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio huggingface_hub "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["from huggingface_hub import InferenceClient\n", "import gradio as gr\n", "\n", "client = InferenceClient()\n", "\n", "def respond(\n", "    prompt: str,\n", "    history,\n", "):\n", "    if not history:\n", "        history = [{\"role\": \"system\", \"content\": \"You are a friendly chatbot\"}]\n", "    history.append({\"role\": \"user\", \"content\": prompt})\n", "\n", "    yield history\n", "\n", "    response = {\"role\": \"assistant\", \"content\": \"\"}\n", "    for message in client.chat_completion( # type: ignore\n", "        history,\n", "        temperature=0.95,\n", "        top_p=0.9,\n", "        max_tokens=512,\n", "        stream=True,\n", "        model=\"HuggingFaceH4/zephyr-7b-beta\"\n", "    ):\n", "        response[\"content\"] += message.choices[0].delta.content or \"\"\n", "        yield history + [response]\n", "\n", "\n", "def handle_undo(history, undo_data: gr.UndoData):\n", "    return history[:undo_data.index], history[undo_data.index]['content']\n", "\n", "def handle_retry(history, retry_data: gr.RetryData):\n", "    new_history = history[:retry_data.index]\n", "    previous_prompt = history[retry_data.index]['content']\n", "    yield from respond(previous_prompt, new_history)\n", "\n", "\n", "def handle_like(data: gr.LikeData):\n", "    if data.liked:\n", "        print(\"You upvoted this response: \", data.value)\n", "    else:\n", "        print(\"You downvoted this response: \", data.value)\n", "\n", "\n", "with gr.Blocks() as demo:\n", "    gr.Markdown(\"# Chat with Hugging Face Zephyr 7b \ud83e\udd17\")\n", "    chatbot = gr.Chatbot(\n", "        label=\"Agent\",\n", "        type=\"messages\",\n", "        avatar_images=(\n", "            None,\n", "            \"https://em-content.zobj.net/source/twitter/376/hugging-face_1f917.png\",\n", "        ),\n", "    )\n", "    prompt = gr.Textbox(max_lines=1, label=\"Chat Message\")\n", "    prompt.submit(respond, [prompt, chatbot], [chatbot])\n", "    prompt.submit(lambda: \"\", None, [prompt])\n", "    chatbot.undo(handle_undo, chatbot, [chatbot, prompt])\n", "    chatbot.retry(handle_retry, chatbot, [chatbot])\n", "    chatbot.like(handle_like, None, None)\n", "\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}