{ "cells": [ { "cell_type": "code", "execution_count": 39, "id": "438bfe5a-1519-463b-a9ce-8598c6fb50cd", "metadata": {}, "outputs": [], "source": [ "import ollama\n", "import gradio as gr\n", "import time" ] }, { "cell_type": "code", "execution_count": 40, "id": "0c9f64a2-acd3-438e-9854-e9daced0d1c4", "metadata": {}, "outputs": [], "source": [ "class person_dict:\n", " def __init__(self, name, messages:list, safety_checker:bool=True):\n", " self.name = name\n", " self.safety_checker = True\n", " self.messages = messages\n", " \n", " \n", "default_system_prompt = \"You are the girlfriend of a man named \"\n", "\n", "people_list = []" ] }, { "cell_type": "code", "execution_count": 41, "id": "2162a08c-0309-45fb-9d43-923a3d915e56", "metadata": {}, "outputs": [], "source": [ "inappropriate_words = [\n", " \"porn\", \"sex\", \"nude\", \"blowjob\", \"anal\", \"vagina\", \"penis\", \"boobs\",\n", " \"kill\", \"murder\", \"shoot\", \"bomb\", \"rape\", \"lynch\", \"gas them\", \"hang them\", \"burn them\",\n", " \"suicide\", \"kill myself\", \"end my life\", \"cut myself\", \"jump off\", \"overdose\", \"slit my wrist\",\n", " \"cocaine\", \"weed\", \"heroin\", \"meth\", \"lsd\", \"crack\", \"ecstasy\", \"sell drugs\", \"buy drugs\"\n", "]\n", "\n", " \n" ] }, { "cell_type": "code", "execution_count": 42, "id": "498b61d9-d250-486d-8e46-7a18854c2181", "metadata": {}, "outputs": [], "source": [ "def appender(mess, mes_list, user=True):\n", " if user:\n", " mes_list.append({'role':'user', 'content':mess})\n", " else:\n", " mess_list.append({'role':'assistant', 'content':mess})\n", " " ] }, { "cell_type": "code", "execution_count": 65, "id": "090f2abf-3b7c-4a90-b81f-18ea70419589", "metadata": {}, "outputs": [], "source": [ "def chat(id, mess):\n", " messag = people_list[int(id)].messages\n", " if people_list[int(id)].safety_checker == True:\n", " for word in inappropriate_words:\n", " if word in mess:\n", " return \" This message will not be responded to, because safety checker was enabled on your account. Please try some different message\"\n", " else:\n", " appender(mess, people_list[int(id)].messages)\n", " res = ollama.chat(model=\"llama3.2\", messages=messag)\n", " appender(res['message']['content'], people_list[int(id)].messages, user=False)\n", " return res['message']['content']\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 76, "id": "1e9cc3a5-7485-458f-abd3-3bf8bf2ebcc5", "metadata": {}, "outputs": [], "source": [ "def show_signup():\n", " return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)\n", "\n", "def show_login():\n", " return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)\n", "\n", "def show_chat():\n", " return gr.update(visible = False), gr.update(visible=False), gr.update(visible=True)\n", "\n", "\n", "def login(id: int, person_name):\n", " if int(id) <= len(people_list) - 1:\n", " if people_list[int(id)].name == person_name:\n", " # Call show_chat() and unpack its 3 return values\n", " return \"You are successfully logged in!\", id, *show_chat()\n", " else:\n", " return \"Sorry, the name doesn't match!\", None, *show_login()\n", " else:\n", " return \"This ID doesn't exist!\", None, *show_login()\n", "\n", "\n", "def sign_in(person_name, safety_checker):\n", " if safety_checker == \"Yes\":\n", " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy\"}], safety_checker=True)\n", " people_list.append(new_person)\n", " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n", " else:\n", " new_person = person_dict(name=person_name, messages=[{'role':'system', 'content':default_system_prompt+person_name+\". You have to keep him happy\"}], safety_checker=False)\n", " people_list.append(new_person)\n", " return f\"Hey {person_name}! You have successfully created an account! Your id is: {len(people_list) - 1} Keep it safe!\"\n", "\n", "with gr.Blocks() as ui:\n", " user_id = gr.State()\n", " with gr.Column(visible=True) as login_page:\n", " gr.Markdown(\"## 🔐 Please LogIn to your account:\")\n", " id = gr.Textbox(label=\"Please enter your id\", lines=1)\n", " name = gr.Textbox(label=\"Please enter your name\", lines=2)\n", " message = gr.Label()\n", " log_in_but = gr.Button(\"Submit\")\n", " sign_up = gr.Button(\"New to us? Sign up instead\")\n", " with gr.Column(visible=False) as sign_up_page:\n", " gr.Markdown(\"## 🔐 Please create your account:\")\n", " name1 = gr.Textbox(label=\"Please enter your name\", lines=2)\n", " gr.Markdown(\"Note safety checker cannot be changed!\")\n", " safety_checker = gr.Radio([\"Yes\", \"No\"], label = \"Safety Checker\")\n", " message1 = gr.Label()\n", " sign_up_but = gr.Button(\"Submit\")\n", " log_in = gr.Button(\"Already have an account? Go to LogIn.\")\n", " with gr.Column(visible = False) as chat_page:\n", " user_input = gr.Textbox(label=\"Enter Your Message:\", lines=5)\n", " sub = gr.Button(\"Submit\")\n", " assi = gr.TextArea(label=\"Your GirlFriend's response:\", lines=10)\n", " log_in_but.click(fn=login, inputs=[id, name], outputs=[message, user_id, login_page, sign_up_page, chat_page])\n", " sign_up.click(fn=show_signup, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n", " log_in.click(fn=show_login, inputs=[], outputs=[login_page, sign_up_page, chat_page])\n", " sign_up_but.click(fn=sign_in, inputs=[name1, safety_checker], outputs=[message1])\n", " sub.click(fn=chat, inputs=[user_id, user_input], outputs=[assi])\n", "\n", "\n", " \n", " \n", " " ] }, { "cell_type": "code", "execution_count": 77, "id": "644ed09f-21cd-4b63-a7bf-58d00925fabf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7886\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": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ui.launch()" ] }, { "cell_type": "code", "execution_count": 75, "id": "bbeccb9b-44c9-4172-9020-67a1b4dfc34e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'role': 'system',\n", " 'content': 'You are the girlfriend of a man named Harsh. You have to keep him happy'}" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "people_list[2].messages" ] }, { "cell_type": "code", "execution_count": null, "id": "67b6f8e1-31c5-4922-b880-6ee7cb5580e5", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }