Spaces:
Build error
Build error
File size: 8,244 Bytes
c3409a2 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "13fc27bd-8eb2-41a7-9216-cec1b802975f",
"metadata": {},
"outputs": [],
"source": [
"import ollama\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9fb99cdf-44c5-4b2b-aa04-8aee5f07140a",
"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 = safety_checker \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": 3,
"id": "13257d96-ba64-4b58-8077-8827ca1f3921",
"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\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e8a0d538-1b02-4217-b729-733be838ec36",
"metadata": {},
"outputs": [],
"source": [
"def appender(mess, mes_list, user=True):\n",
" if user:\n",
" mes_list.append({'role': 'user', 'content': mess})\n",
" else:\n",
" mes_list.append({'role': 'assistant', 'content': mess})"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "983b960c-5309-47d8-8fe5-346df540fc54",
"metadata": {},
"outputs": [],
"source": [
"def chat(id, mess):\n",
" id = int(id)\n",
" person = people_list[id]\n",
" messag = person.messages\n",
"\n",
" \n",
" if person.safety_checker:\n",
" for word in inappropriate_words:\n",
" if word.lower() in mess.lower(): # case insensitive check\n",
" return \"<SAFETY CHECKER MESSAGE> This message will not be responded to, because safety checker was enabled on your account. Please try a different message.\"\n",
"\n",
" \n",
" appender(mess, messag)\n",
" res = ollama.chat(model=\"llama3.2\", messages=messag)\n",
" appender(res['message']['content'], messag, user=False)\n",
" return res['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e60b3e81-7224-481f-b025-a161ac74eb6b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" 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": [
"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. Only respond to what he told you. Do not make things up!\"}], 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. Only respond to what he told you. Do not make things up!\"}], 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",
"ui.launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c9a6068-880e-4c94-84ec-809cfb3729ce",
"metadata": {},
"outputs": [],
"source": [
"person_list[0].name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1aebd290-e2a5-4702-850c-109b92f027d2",
"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
}
|