{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "6797ba2e-ef94-42d6-9e64-a1559b507173", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\singl\\anaconda3\\envs\\pshcy\\lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import gradio as gr\n", "from huggingface_hub import InferenceClient\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f50cbd8a-777f-440c-8b7f-8eca9e611e12", "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference\n", "\"\"\"\n", "client = InferenceClient(\"HuggingFaceH4/zephyr-7b-beta\")\n", "\n", "\n", "def respond(\n", " message,\n", " history: list[tuple[str, str]],\n", " system_message,\n", " max_tokens,\n", " temperature,\n", " top_p,\n", "):\n", " messages = [{\"role\": \"system\", \"content\": system_message}]\n", "\n", " for val in history:\n", " if val[0]:\n", " messages.append({\"role\": \"user\", \"content\": val[0]})\n", " if val[1]:\n", " messages.append({\"role\": \"assistant\", \"content\": val[1]})\n", "\n", " messages.append({\"role\": \"user\", \"content\": message})\n", "\n", " response = \"\"\n", "\n", " for message in client.chat_completion(\n", " messages,\n", " max_tokens=max_tokens,\n", " stream=True,\n", " temperature=temperature,\n", " top_p=top_p,\n", " ):\n", " token = message.choices[0].delta.content\n", "\n", " response += token\n", " yield response\n", "\n", "\n", "\"\"\"\n", "For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface\n", "\"\"\"\n", "demo = gr.ChatInterface(\n", " respond,\n", " additional_inputs=[\n", " gr.Textbox(value=\"You are a friendly Chatbot.\", label=\"System message\"),\n", " gr.Slider(minimum=1, maximum=2048, value=512, step=1, label=\"Max new tokens\"),\n", " gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label=\"Temperature\"),\n", " gr.Slider(\n", " minimum=0.1,\n", " maximum=1.0,\n", " value=0.95,\n", " step=0.05,\n", " label=\"Top-p (nucleus sampling)\",\n", " ),\n", " ],\n", ")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python (Psych)", "language": "python", "name": "pshcy" }, "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.10.18" } }, "nbformat": 4, "nbformat_minor": 5 }