Spaces:
Runtime error
Runtime error
File size: 16,614 Bytes
0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 3059264 8feb591 3059264 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 8feb591 0aa4b61 |
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"# os.system('pip install requests')\n",
"import requests\n",
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
"gpt3_key = \"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\"\n",
"\n",
"def gpt3_question(api_key, prompt):\n",
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" data = {\n",
" \"prompt\": prompt,\n",
" \"max_tokens\": 400,\n",
" \"temperature\": 0.5\n",
" }\n",
" print('sending request')\n",
" response = requests.post(api_endpoint, headers=headers, json=data)\n",
" print(response)\n",
" generated_text = response.json()[\"choices\"][0][\"text\"]\n",
"\n",
" return generated_text\n",
"\n",
"def chatgpt3_question(api_key, prompt):\n",
" url = \"https://api.openai.com/v1/chat/completions\"\n",
" api_key = \"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\"\n",
"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
"\n",
" data = {\n",
" \"model\": \"gpt-3.5-turbo\",\n",
" \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n",
" }\n",
"\n",
" response = requests.post(url, headers=headers, json=data)\n",
" generated_text = response.json()['choices'][0]['message']['content']\n",
"\n",
" return generated_text\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_prompt = None\n",
"history_final = None\n",
"block_predict = False\n",
"block_advice = False\n",
"\n",
"def predict(input, history):\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_final\n",
" global block_predict\n",
"\n",
" if block_predict == False:\n",
" print('@@@', history)\n",
" history_prompt = history2prompt(history, input)\n",
" print('###', history_prompt)\n",
"\n",
" prompt = f\"\"\"\n",
" Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
" Output the first relevant legal question that can result in the highest incrimination for the client (if somebody is hurt, start from fatal injuries), and that can only be answered as Yes or No\n",
" \"\"\"\n",
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
"\n",
" response = list()\n",
" response = [(input, bot_answer)]\n",
" \n",
" history.append(response[0])\n",
" response = history\n",
" history_final = history\n",
"\n",
" # print('#history', history)\n",
" # print('#response', response)\n",
"\n",
" return response, history\n",
"\n",
"def chatbot_foo():\n",
" global history_prompt\n",
" global history_final\n",
" global block_predict\n",
" global block_advice\n",
"\n",
" if block_advice == False and history_prompt is not None:\n",
" prompt = f\"\"\"\n",
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
" \"\"\"\n",
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
"\n",
" history_final.append(('Consult me on the matter:', bot_answer))\n",
"\n",
" block_predict = True\n",
" block_advice = True\n",
" return history_final, history_final\n",
"\n",
"def reset_interface():\n",
" global history_prompt\n",
" global history_final\n",
" global block_predict\n",
" global block_advice\n",
"\n",
" history_prompt = None\n",
" history_final = None\n",
" block_predict = None\n",
" block_advice = None\n",
"\n",
"demo = gr.Blocks()\n",
"with demo:\n",
" gr.Markdown(\n",
" \"\"\"\n",
" <center> \n",
" Chat with Morty by typing in the input box below.\n",
" </center>\n",
" \"\"\"\n",
" )\n",
" state = gr.Variable(value=[]) #beginning\n",
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
" text = gr.Textbox(\n",
" label=\"Talk to your lawyer (press enter to submit)\",\n",
" value=\"The other day it was raining, and while I was driving a hit a stranger with my car.\",\n",
" placeholder=\"reply Yes or No\",\n",
" max_lines=1,\n",
" )\n",
" text.submit(predict, [text, state], [chatbot, state])\n",
" text.submit(lambda x: \"\", text, text)\n",
"\n",
" btn = gr.Button(value=\"submit\")\n",
" btn.click(chatbot_foo, None, [chatbot, state])\n",
"\n",
" btn2 = gr.Button(value=\"reset\")\n",
" btn.click(reset_interface)\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)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"a\n"
]
}
],
"source": [
"print(history_prompt)\n",
"\n",
"if history_prompt is not None:\n",
" print('a')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7862\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7862/\" 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"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"@@@ []\n",
"['The other day it was raining, and while I was driving a hit a stranger with my car.']\n",
"### The other day it was raining, and while I was driving a hit a stranger with my car.\n",
"sending request\n",
"<Response [200]>\n",
"@@@ [('The other day it was raining, and while I was driving a hit a stranger with my car.', '\\nDid the stranger suffer any fatal injuries as a result of the collision?')]\n",
"['The other day it was raining, and while I was driving a hit a stranger with my car.', '\\nDid the stranger suffer any fatal injuries as a result of the collision?', 'yes']\n",
"### The other day it was raining, and while I was driving a hit a stranger with my car.\n",
"\n",
"Did the stranger suffer any fatal injuries as a result of the collision? - yes\n",
"sending request\n",
"<Response [200]>\n",
"sending request\n",
"<Response [200]>\n"
]
}
],
"source": [
"import os\n",
"# os.system('pip install requests')\n",
"import requests\n",
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
"gpt3_key = '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'\n",
"\n",
"def gpt3_question(api_key, prompt):\n",
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
" data = {\n",
" \"prompt\": prompt,\n",
" \"max_tokens\": 400,\n",
" \"temperature\": 0.5\n",
" }\n",
" print('sending request')\n",
" response = requests.post(api_endpoint, headers=headers, json=data)\n",
" print(response)\n",
" generated_text = response.json()[\"choices\"][0][\"text\"]\n",
"\n",
" return generated_text\n",
"\n",
"def chatgpt3_question(api_key, prompt):\n",
" url = \"https://api.openai.com/v1/chat/completions\"\n",
"\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {api_key}\"\n",
" }\n",
"\n",
" data = {\n",
" \"model\": \"gpt-3.5-turbo\",\n",
" \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n",
" }\n",
"\n",
" response = requests.post(url, headers=headers, json=data)\n",
" generated_text = response.json()['choices'][0]['message']['content']\n",
"\n",
" return generated_text\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_prompt = None\n",
"history_final = None\n",
"block_predict = False\n",
"block_advice = False\n",
"\n",
"def predict(input, history, start_var):\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_final\n",
" global block_predict\n",
" global block_advice\n",
"\n",
" if start_var == True:\n",
" history_prompt = None\n",
" history_final = None\n",
" block_predict = False\n",
" block_advice = False\n",
" start_var = False\n",
"\n",
" if block_predict == False:\n",
" print('@@@', history)\n",
" history_prompt = history2prompt(history, input)\n",
" print('###', history_prompt)\n",
"\n",
" prompt = f\"\"\"\n",
" Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
" Output the first relevant legal question that can result in the highest incrimination for the client (if somebody is hurt, start from fatal injuries), and that can only be answered as Yes or No\n",
" \"\"\"\n",
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
"\n",
" response = list()\n",
" response = [(input, bot_answer)]\n",
" \n",
" history.append(response[0])\n",
" response = history\n",
" history_final = history\n",
"\n",
" # print('#history', history)\n",
" # print('#response', response)\n",
"\n",
" return response, history\n",
"\n",
"def chatbot_foo():\n",
" global history_prompt\n",
" global history_final\n",
" global block_predict\n",
" global block_advice\n",
"\n",
" if block_advice == False and history_prompt is not None:\n",
" \n",
" prompt = f\"\"\"\n",
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
" \"\"\"\n",
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
"\n",
" history_final.append(('Consult me on the matter:', bot_answer))\n",
"\n",
" block_predict = True\n",
" block_advice = True\n",
" return history_final, history_final\n",
"\n",
"demo = gr.Blocks()\n",
"with demo:\n",
" gr.Markdown(\n",
" \"\"\"\n",
" <center> \n",
" Chat with your Lawyer\n",
" </center>\n",
" \"\"\"\n",
" )\n",
" state = gr.Variable(value=[]) #beginning\n",
" start_var = gr.Variable(value=True) #beginning\n",
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
" text = gr.Textbox(\n",
" label=\"Talk to your lawyer (press enter to submit)\",\n",
" value=\"The other day it was raining, and while I was driving a hit a stranger with my car.\",\n",
" placeholder=\"reply Yes or No\",\n",
" max_lines=1,\n",
" )\n",
" text.submit(predict, [text, state, start_var], [chatbot, state])\n",
" text.submit(lambda x: \"\", text, text)\n",
"\n",
" btn = gr.Button(value=\"submit\")\n",
" btn.click(chatbot_foo, None, [chatbot, state])\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.9.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|