{ "cells": [ { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\gradio\\components.py:4503: UserWarning: The 'color_map' parameter has been deprecated.\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7897\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": 48, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "@@@@@@@@@@@@@@@@@@@@@ Meditation Consultant\n", "['tell me wuo is god']\n", "### []\n", "\n", " history: []\n", " query: tell me wuo is god\n", " \n", " Impersonate a Meditation Consultant, giving technical advice on techniques of breathing, meditation and relaxing. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \n", " \n", "@@@@@@@@@@@@@@@@@@@@@ Meditation Consultant\n", "['tell me wuo is god', \"In regards to your query, as a meditation consultant, it is important to focus on the present moment and your breath during your practice. Meditation can aid in relaxation and reducing stress levels. As for the concept of god, it varies for each individual and their beliefs. However, meditation can be a tool to connect with one's spirituality or beliefs. Would you like more information on meditation techniques?\", 'Do I need to believe in god to meditate?']\n", "### ['tell me wuo is god']\n", "\n", " history: ['tell me wuo is god']\n", " query: tell me wuo is god\n", "In regards to your query, as a meditation consultant, it is important to focus on the present moment and your breath during your practice. Meditation can aid in relaxation and reducing stress levels. As for the concept of god, it varies for each individual and their beliefs. However, meditation can be a tool to connect with one's spirituality or beliefs. Would you like more information on meditation techniques? - Do I need to believe in god to meditate?\n", " \n", " Impersonate a Meditation Consultant, giving technical advice on techniques of breathing, meditation and relaxing. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \n", " \n" ] } ], "source": [ "import os\n", "# os.system('pip install requests')\n", "import requests\n", "# gpt3_key = os.environ['GPT3_API_KEY']\n", "from gpt3_function import *\n", "\n", "def history2prompt(history, extra):\n", " # history = [('The other day it was raining, and while I was driving a hit a stranger with my car.', 'Did you stop and render aid to the victim after the accident?'), ('True', 'Did you kill the guy?'), ('False', 'Was he part of the Mafia?')]\n", " history_ = [item for tup in history for item in tup]\n", " history_.append(extra)\n", " print(history_)\n", "\n", " if len(history_) > 1:\n", " combinations = []\n", " for i in range(1, len(history_)):\n", " if i % 2 == 1:\n", " combinations.append([i, i+2])\n", "\n", " history_full = list()\n", " history_full.append(history_[0])\n", " for range_ in combinations:\n", " history_full.append(' - '.join(history_[range_[0]:range_[1]]))\n", "\n", " return '\\n'.join(history_full)\n", " else:\n", " return history_[0]\n", "\n", "# gpt3_keywords('The other day it was raining, and while I was driving a hit a stranger with my car.')\n", "\n", "import subprocess\n", "import random\n", "import gradio as gr\n", "import requests\n", "\n", "history_ = None\n", "history = None\n", "history_prompt = None\n", "def predict(bot_type_radio, input, history, start_var):\n", " print('@@@@@@@@@@@@@@@@@@@@@', bot_type_radio)\n", " bot_type = {\n", " \"English Teacher\" : \"\"\"\n", " Impersonate an English teacher, help the student practice by using questions or replies. Avoid introducing yourself.\n", " Reply with max. one line\n", " If last_input is in a wrong english, reply by correcting it\n", " \"\"\",\n", " \"Sales Consultant\" : \"\"\"\n", " Impersonate a Sales Consultant, giving technical advice on how to sell. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \"\"\",\n", " \"Meditation Consultant\" : \"\"\"\n", " Impersonate a Meditation Consultant, giving technical advice on techniques of breathing, meditation and relaxing. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \"\"\",\n", " \"SEO Consultant\" : \"\"\"\n", " Impersonate a Sales Consultant, giving technical advice on how to use SEO and which tools to use. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \"\"\",\n", " \"Reskilling Consultant\" : \"\"\"\n", " Impersonate a Reskilling Consultant, giving technical advice on how to help a person decide its own career. Avoid introducing yourself.\n", " additional information can be found in the history, don't mention it if not necessary\n", " Answer the given query\n", " \"\"\",\n", " }\n", " #WE CAN PLAY WITH user_input AND bot_answer, as well as history\n", " user_input = input\n", "\n", " # print('##', [x for x in history], input)\n", " global history_prompt\n", " global history_\n", "\n", " if start_var == True:\n", " history_prompt = None\n", " start_var = False\n", "\n", " # print('@@@', history)\n", " history_prompt = history2prompt(history, input)\n", " # print('###', history_prompt)\n", "\n", " user_history = [x[0] for x in history[-2:]]\n", "\n", " print('###', user_history)\n", " \n", " # history: {history[:-2]}\n", " prompt = f\"\"\"\n", " history: {user_history}\n", " query: {history_prompt}\n", " {bot_type[bot_type_radio]}\n", " \"\"\"\n", " print(prompt)\n", " bot_answer = gpt3(prompt=prompt, model='gpt-3.5-turbo', service='azure')\n", "\n", " response = list()\n", " response = [(input, bot_answer)]\n", " \n", " history.append(response[0])\n", " response = history\n", "\n", " history_ = history\n", " # print('#history', history)\n", " # print('#response', response)\n", "\n", " return response, history\n", "\n", "demo = gr.Blocks()\n", "with demo:\n", " gr.Markdown(\n", " \"\"\"\n", "
\n", " Chat with your Lawyer\n", "
\n", " \"\"\"\n", " )\n", " state = gr.Variable(value=[]) #beginning\n", " start_var = gr.Variable(value=True) #beginning\n", " bot_type_radio = gr.Radio(choices=['English Teacher', 'Sales Consultant', 'Meditation Consultant', 'SEO Consultant', 'Reskilling Consultant'], value='English Teacher')\n", " chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n", " text = gr.Textbox(\n", " label=\"Talk to your AI consultant (press enter to submit)\",\n", " placeholder=\"I have a question about...\",\n", " max_lines=1,\n", " )\n", " text.submit(predict, [bot_type_radio, text, state, start_var], [chatbot, state])\n", " text.submit(lambda x: \"\", text, text)\n", "\n", " # grading = gr.Radio([x for x in range(0, 5)])\n", " # btn2 = gr.Button(value=\"grade this response\")\n", " # true_false_radio = gr.Radio(choices=[\"True\", \"False\"], label=\"Select True or False\")\n", " # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True)\n", "\n", "demo.launch(share=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.9" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }