Spaces:
Sleeping
Sleeping
File size: 13,628 Bytes
dfbf21d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
{
"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": [
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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": [
"<div><iframe src=\"http://127.0.0.1:7865/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|