{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7864\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "\n", "# Define some pre-written templates\n", "templates = {\n", " \"Friendly Chatbot\": \"You are a helpful, friendly chatbot that engages in light-hearted conversations.\",\n", " \"Technical Assistant\": \"You are a technical assistant specialized in answering questions related to Python programming.\",\n", " \"Nutrition Advisor\": \"You provide evidence-based advice on nutrition and healthy eating habits.\",\n", "}\n", "\n", "# Chatbot logic: Takes system instructions and user query, returns a response\n", "def chatbot_response(system_instructions, user_query):\n", " if \"friendly\" in system_instructions.lower():\n", " return f\"Friendly Chatbot says: Hi there! 😊 How can I assist you today?\"\n", " elif \"technical\" in system_instructions.lower():\n", " return f\"Technical Assistant says: Sure! Here's some information on Python: {user_query}\"\n", " elif \"nutrition\" in system_instructions.lower():\n", " return f\"Nutrition Advisor says: Here's some advice about healthy eating: {user_query}\"\n", " else:\n", " return f\"Custom Chatbot says: {user_query}\"\n", "\n", "# Function to update the interface when a selection is made from the dropdown\n", "def update_interface(template_name, custom_instructions):\n", " if template_name == \"Custom Instructions\":\n", " return gr.update(visible=True), gr.update(visible(False))\n", " else:\n", " template_content = templates.get(template_name, \"\")\n", " return gr.update(visible=False), gr.update(visible=True, value=template_content)\n", "\n", "# Chatbot conversation function\n", "def chatbot_conversation(system_instructions, chat_history, user_query):\n", " response = chatbot_response(system_instructions, user_query)\n", " chat_history.append((user_query, response))\n", " return chat_history, \"\"\n", "\n", "# Build the Gradio interface\n", "with gr.Blocks() as demo:\n", " \n", " # Add the title and description\n", " gr.Markdown(\"# **SRF Innovation Labs - AI Chatbot Use Case Explorer**\")\n", " gr.Markdown(\"\"\"\n", " Welcome to the SRF Innovation Labs AI Chatbot Use Case Explorer! \n", " This tool allows you to experiment with different system prompts, \n", " giving you control over how the chatbot behaves. You can either use pre-defined templates or write your own custom instructions.\n", " \n", " Additionally, the chatbot has access to a vector database where it can look up and retrieve learnings for various queries. \n", " This makes it an excellent platform for exploring potential AI use cases in real-time.\n", " \"\"\")\n", "\n", " # Section to select system instructions from dropdown\n", " gr.Markdown(\"## **Chatbot Setup**\")\n", "\n", " # Dropdown for selecting a pre-written template or custom instructions\n", " template_name = gr.Dropdown(choices=[\"Custom Instructions\"] + list(templates.keys()), label=\"Choose Instructions\", value=\"Friendly Chatbot\")\n", " \n", " # Textbox for custom chatbot instructions (only shown when \"Custom Instructions\" is selected)\n", " custom_instructions = gr.Textbox(label=\"Custom Instructions\", visible=False, placeholder=\"Write your own instructions here...\")\n", " \n", " # Output field to display the selected pre-written template (not shown when Custom Instructions is selected)\n", " template_display = gr.Textbox(label=\"Template Content\", interactive=False, visible=True)\n", " \n", " # Section for chat interface\n", " gr.Markdown(\"## **Chatbot Interaction**\")\n", "\n", " # Chatbot interface with chat history\n", " chatbot = gr.Chatbot(label=\"Chatbot Conversation\")\n", " user_query = gr.Textbox(label=\"Your Query\", placeholder=\"Ask a question or say something to the chatbot...\")\n", "\n", " # Button to submit the query\n", " submit_button = gr.Button(\"Send\")\n", "\n", " # Update logic to control the display based on the dropdown selection\n", " template_name.change(fn=update_interface, \n", " inputs=[template_name, custom_instructions], \n", " outputs=[custom_instructions, template_display])\n", "\n", " # Chatbot interaction logic\n", " submit_button.click(fn=chatbot_conversation, \n", " inputs=[custom_instructions if template_name == \"Custom Instructions\" else template_display, chatbot, user_query], \n", " outputs=[chatbot, user_query])\n", "\n", "# Launch the app\n", "demo.launch()\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7865\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "\n", "# Define some pre-written templates for Tab 1\n", "templates = {\n", " \"Friendly Chatbot\": \"You are a helpful, friendly chatbot that engages in light-hearted conversations.\",\n", " \"Technical Assistant\": \"You are a technical assistant specialized in answering questions related to Python programming.\",\n", " \"Nutrition Advisor\": \"You provide evidence-based advice on nutrition and healthy eating habits.\",\n", "}\n", "\n", "# Define some agentic workflows for Tab 2\n", "agentic_workflows = {\n", " \"Blog Post Generator\": \"This agent is designed to help generate a blog post based on user input.\",\n", " \"Document Summarizer\": \"This agent summarizes long documents by extracting key points.\",\n", " \"Task Manager\": \"This agent helps you organize tasks and provides step-by-step guidance.\"\n", "}\n", "\n", "# Chatbot logic for custom instructions or pre-written templates\n", "def chatbot_response(system_instructions, user_query):\n", " if \"friendly\" in system_instructions.lower():\n", " return f\"Friendly Chatbot says: Hi there! 😊 How can I assist you today?\"\n", " elif \"technical\" in system_instructions.lower():\n", " return f\"Technical Assistant says: Sure! Here's some information on Python: {user_query}\"\n", " elif \"nutrition\" in system_instructions.lower():\n", " return f\"Nutrition Advisor says: Here's some advice about healthy eating: {user_query}\"\n", " else:\n", " return f\"Custom Chatbot says: {user_query}\"\n", "\n", "# Chatbot conversation function\n", "def chatbot_conversation(system_instructions, chat_history, user_query):\n", " response = chatbot_response(system_instructions, user_query)\n", " chat_history.append((user_query, response))\n", " return chat_history, \"\"\n", "\n", "# Chatbot conversation for predefined agentic workflows\n", "def agentic_chatbot_conversation(workflow_instructions, chat_history, user_query):\n", " response = f\"Agent Workflow ({workflow_instructions}) says: {user_query}\"\n", " chat_history.append((user_query, response))\n", " return chat_history, \"\"\n", "\n", "# Function to update the interface when a selection is made from the dropdown (Tab 1)\n", "def update_interface(template_name, custom_instructions):\n", " if template_name == \"Custom Instructions\":\n", " return gr.update(visible=True), gr.update(visible=False)\n", " else:\n", " template_content = templates.get(template_name, \"\")\n", " return gr.update(visible=False), gr.update(visible=True, value=template_content)\n", "\n", "# Build the Gradio interface with Tabs\n", "with gr.Blocks() as demo:\n", " \n", " # Add Tabs\n", " with gr.Tabs():\n", " \n", " # Tab 1: Custom Instructions or Pre-Written Templates\n", " with gr.Tab(\"Custom Instructions Chatbot\"):\n", " gr.Markdown(\"# **SRF Innovation Labs - AI Chatbot Use Case Explorer**\")\n", " gr.Markdown(\"\"\"\n", " This tool allows you to experiment with different system prompts, \n", " giving you control over how the chatbot behaves. You can either use pre-defined templates or write your own custom instructions.\n", " \"\"\")\n", "\n", " # Section to select system instructions from dropdown\n", " gr.Markdown(\"## **Chatbot Setup**\")\n", " template_name = gr.Dropdown(choices=[\"Custom Instructions\"] + list(templates.keys()), label=\"Choose Instructions\", value=\"Friendly Chatbot\")\n", " custom_instructions = gr.Textbox(label=\"Custom Instructions\", visible=False, placeholder=\"Write your own instructions here...\")\n", " template_display = gr.Textbox(label=\"Template Content\", interactive=False, visible=True)\n", "\n", " # Chatbot interface\n", " gr.Markdown(\"## **Chatbot Interaction**\")\n", " chatbot = gr.Chatbot(label=\"Chatbot Conversation\")\n", " user_query = gr.Textbox(label=\"Your Query\", placeholder=\"Ask a question or say something to the chatbot...\")\n", " submit_button = gr.Button(\"Send\")\n", "\n", " # Update logic for Tab 1\n", " template_name.change(fn=update_interface, inputs=[template_name, custom_instructions], outputs=[custom_instructions, template_display])\n", " submit_button.click(fn=chatbot_conversation, inputs=[custom_instructions if template_name == \"Custom Instructions\" else template_display, chatbot, user_query], outputs=[chatbot, user_query])\n", "\n", " # Tab 2: Predefined Agentic Workflows\n", " with gr.Tab(\"Agentic Workflow Chatbots\"):\n", " gr.Markdown(\"# **Agentic Workflow Explorer**\")\n", " gr.Markdown(\"\"\"\n", " This tab allows you to experiment with different agentic workflows that are predefined. \n", " Each workflow executes a specific task, such as generating blog posts, summarizing documents, or managing tasks.\n", " \"\"\")\n", "\n", " # Dropdown for selecting agentic workflows\n", " workflow_name = gr.Dropdown(choices=list(agentic_workflows.keys()), label=\"Choose Agent Workflow\", value=\"Blog Post Generator\")\n", " workflow_display = gr.Textbox(label=\"Workflow Description\", interactive=False, visible=True)\n", " workflow_chatbot = gr.Chatbot(label=\"Agent Workflow Conversation\")\n", " workflow_user_query = gr.Textbox(label=\"Your Query\", placeholder=\"Ask the agent to perform a task...\")\n", " workflow_submit_button = gr.Button(\"Send\")\n", "\n", " # Chatbot interaction for agentic workflows\n", " workflow_submit_button.click(fn=agentic_chatbot_conversation, inputs=[workflow_name, workflow_chatbot, workflow_user_query], outputs=[workflow_chatbot, workflow_user_query])\n", "\n", "# Launch the app\n", "demo.launch()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "srf_chatbot_v2", "language": "python", "name": "srf_chatbot_v2" }, "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.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }