Spaces:
Runtime error
Runtime error
Michelangiolo
commited on
Commit
•
8feb591
1
Parent(s):
3059264
changes
Browse files- app.py +12 -3
- gradio_.ipynb +219 -71
app.py
CHANGED
@@ -67,13 +67,13 @@ import random
|
|
67 |
import gradio as gr
|
68 |
import requests
|
69 |
|
70 |
-
history = None
|
71 |
history_prompt = None
|
72 |
history_final = None
|
73 |
block_predict = False
|
74 |
block_advice = False
|
75 |
|
76 |
-
def predict(input, history):
|
77 |
#WE CAN PLAY WITH user_input AND bot_answer, as well as history
|
78 |
user_input = input
|
79 |
|
@@ -81,6 +81,14 @@ def predict(input, history):
|
|
81 |
global history_prompt
|
82 |
global history_final
|
83 |
global block_predict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
if block_predict == False:
|
86 |
print('@@@', history)
|
@@ -135,6 +143,7 @@ with demo:
|
|
135 |
"""
|
136 |
)
|
137 |
state = gr.Variable(value=[]) #beginning
|
|
|
138 |
chatbot = gr.Chatbot(color_map=("#00ff7f", "#00d5ff"))
|
139 |
text = gr.Textbox(
|
140 |
label="Talk to your lawyer (press enter to submit)",
|
@@ -142,7 +151,7 @@ with demo:
|
|
142 |
placeholder="reply Yes or No",
|
143 |
max_lines=1,
|
144 |
)
|
145 |
-
text.submit(predict, [text, state], [chatbot, state])
|
146 |
text.submit(lambda x: "", text, text)
|
147 |
|
148 |
btn = gr.Button(value="submit")
|
|
|
67 |
import gradio as gr
|
68 |
import requests
|
69 |
|
70 |
+
# history = None
|
71 |
history_prompt = None
|
72 |
history_final = None
|
73 |
block_predict = False
|
74 |
block_advice = False
|
75 |
|
76 |
+
def predict(input, history, start_var):
|
77 |
#WE CAN PLAY WITH user_input AND bot_answer, as well as history
|
78 |
user_input = input
|
79 |
|
|
|
81 |
global history_prompt
|
82 |
global history_final
|
83 |
global block_predict
|
84 |
+
global block_advice
|
85 |
+
|
86 |
+
if start_var == True:
|
87 |
+
history_prompt = None
|
88 |
+
history_final = None
|
89 |
+
block_predict = False
|
90 |
+
block_advice = False
|
91 |
+
start_var = False
|
92 |
|
93 |
if block_predict == False:
|
94 |
print('@@@', history)
|
|
|
143 |
"""
|
144 |
)
|
145 |
state = gr.Variable(value=[]) #beginning
|
146 |
+
start_var = gr.Variable(value=True) #beginning
|
147 |
chatbot = gr.Chatbot(color_map=("#00ff7f", "#00d5ff"))
|
148 |
text = gr.Textbox(
|
149 |
label="Talk to your lawyer (press enter to submit)",
|
|
|
151 |
placeholder="reply Yes or No",
|
152 |
max_lines=1,
|
153 |
)
|
154 |
+
text.submit(predict, [text, state, start_var], [chatbot, state])
|
155 |
text.submit(lambda x: "", text, text)
|
156 |
|
157 |
btn = gr.Button(value="submit")
|
gradio_.ipynb
CHANGED
@@ -2,22 +2,211 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"metadata": {},
|
7 |
"outputs": [
|
8 |
{
|
9 |
-
"name": "
|
10 |
"output_type": "stream",
|
11 |
"text": [
|
12 |
-
"
|
13 |
-
"
|
14 |
]
|
15 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
{
|
17 |
"name": "stdout",
|
18 |
"output_type": "stream",
|
19 |
"text": [
|
20 |
-
"Running on local URL: http://127.0.0.1:
|
21 |
"\n",
|
22 |
"To create a public link, set `share=True` in `launch()`.\n"
|
23 |
]
|
@@ -25,7 +214,7 @@
|
|
25 |
{
|
26 |
"data": {
|
27 |
"text/html": [
|
28 |
-
"<div><iframe src=\"http://127.0.0.1:
|
29 |
],
|
30 |
"text/plain": [
|
31 |
"<IPython.core.display.HTML object>"
|
@@ -38,7 +227,7 @@
|
|
38 |
"data": {
|
39 |
"text/plain": []
|
40 |
},
|
41 |
-
"execution_count":
|
42 |
"metadata": {},
|
43 |
"output_type": "execute_result"
|
44 |
},
|
@@ -51,31 +240,16 @@
|
|
51 |
"### The other day it was raining, and while I was driving a hit a stranger with my car.\n",
|
52 |
"sending request\n",
|
53 |
"<Response [200]>\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
"sending request\n",
|
55 |
"<Response [200]>\n"
|
56 |
]
|
57 |
-
},
|
58 |
-
{
|
59 |
-
"name": "stderr",
|
60 |
-
"output_type": "stream",
|
61 |
-
"text": [
|
62 |
-
"Traceback (most recent call last):\n",
|
63 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\routes.py\", line 394, in run_predict\n",
|
64 |
-
" output = await app.get_blocks().process_api(\n",
|
65 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\blocks.py\", line 1075, in process_api\n",
|
66 |
-
" result = await self.call_function(\n",
|
67 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\blocks.py\", line 884, in call_function\n",
|
68 |
-
" prediction = await anyio.to_thread.run_sync(\n",
|
69 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\anyio\\to_thread.py\", line 31, in run_sync\n",
|
70 |
-
" return await get_asynclib().run_sync_in_worker_thread(\n",
|
71 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 937, in run_sync_in_worker_thread\n",
|
72 |
-
" return await future\n",
|
73 |
-
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 867, in run\n",
|
74 |
-
" result = context.run(func, *args)\n",
|
75 |
-
" File \"C:\\Users\\ardit\\AppData\\Local\\Temp\\ipykernel_20208\\3147649169.py\", line 124, in chatbot_foo\n",
|
76 |
-
" history_final.append(('Consult me on the matter:', bot_answer))\n",
|
77 |
-
"AttributeError: 'NoneType' object has no attribute 'append'\n"
|
78 |
-
]
|
79 |
}
|
80 |
],
|
81 |
"source": [
|
@@ -83,7 +257,7 @@
|
|
83 |
"# os.system('pip install requests')\n",
|
84 |
"import requests\n",
|
85 |
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
|
86 |
-
"gpt3_key =
|
87 |
"\n",
|
88 |
"def gpt3_question(api_key, prompt):\n",
|
89 |
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
|
@@ -105,7 +279,6 @@
|
|
105 |
"\n",
|
106 |
"def chatgpt3_question(api_key, prompt):\n",
|
107 |
" url = \"https://api.openai.com/v1/chat/completions\"\n",
|
108 |
-
" api_key = \"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\"\n",
|
109 |
"\n",
|
110 |
" headers = {\n",
|
111 |
" \"Content-Type\": \"application/json\",\n",
|
@@ -150,13 +323,13 @@
|
|
150 |
"import gradio as gr\n",
|
151 |
"import requests\n",
|
152 |
"\n",
|
153 |
-
"history = None\n",
|
154 |
"history_prompt = None\n",
|
155 |
"history_final = None\n",
|
156 |
"block_predict = False\n",
|
157 |
"block_advice = False\n",
|
158 |
"\n",
|
159 |
-
"def predict(input, history):\n",
|
160 |
" #WE CAN PLAY WITH user_input AND bot_answer, as well as history\n",
|
161 |
" user_input = input\n",
|
162 |
"\n",
|
@@ -164,6 +337,14 @@
|
|
164 |
" global history_prompt\n",
|
165 |
" global history_final\n",
|
166 |
" global block_predict\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
"\n",
|
168 |
" if block_predict == False:\n",
|
169 |
" print('@@@', history)\n",
|
@@ -195,6 +376,7 @@
|
|
195 |
" global block_advice\n",
|
196 |
"\n",
|
197 |
" if block_advice == False and history_prompt is not None:\n",
|
|
|
198 |
" prompt = f\"\"\"\n",
|
199 |
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
200 |
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
|
@@ -207,27 +389,17 @@
|
|
207 |
" block_advice = True\n",
|
208 |
" return history_final, history_final\n",
|
209 |
"\n",
|
210 |
-
"def reset_interface():\n",
|
211 |
-
" global history_prompt\n",
|
212 |
-
" global history_final\n",
|
213 |
-
" global block_predict\n",
|
214 |
-
" global block_advice\n",
|
215 |
-
"\n",
|
216 |
-
" history_prompt = None\n",
|
217 |
-
" history_final = None\n",
|
218 |
-
" block_predict = None\n",
|
219 |
-
" block_advice = None\n",
|
220 |
-
"\n",
|
221 |
"demo = gr.Blocks()\n",
|
222 |
"with demo:\n",
|
223 |
" gr.Markdown(\n",
|
224 |
" \"\"\"\n",
|
225 |
" <center> \n",
|
226 |
-
" Chat with
|
227 |
" </center>\n",
|
228 |
" \"\"\"\n",
|
229 |
" )\n",
|
230 |
" state = gr.Variable(value=[]) #beginning\n",
|
|
|
231 |
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
|
232 |
" text = gr.Textbox(\n",
|
233 |
" label=\"Talk to your lawyer (press enter to submit)\",\n",
|
@@ -235,40 +407,16 @@
|
|
235 |
" placeholder=\"reply Yes or No\",\n",
|
236 |
" max_lines=1,\n",
|
237 |
" )\n",
|
238 |
-
" text.submit(predict, [text, state], [chatbot, state])\n",
|
239 |
" text.submit(lambda x: \"\", text, text)\n",
|
240 |
"\n",
|
241 |
" btn = gr.Button(value=\"submit\")\n",
|
242 |
" btn.click(chatbot_foo, None, [chatbot, state])\n",
|
243 |
-
"\n",
|
244 |
-
" btn2 = gr.Button(value=\"reset\")\n",
|
245 |
-
" btn.click(reset_interface)\n",
|
246 |
" # true_false_radio = gr.Radio(choices=[\"True\", \"False\"], label=\"Select True or False\")\n",
|
247 |
" # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True)\n",
|
248 |
"\n",
|
249 |
"demo.launch(share=False)"
|
250 |
]
|
251 |
-
},
|
252 |
-
{
|
253 |
-
"cell_type": "code",
|
254 |
-
"execution_count": 8,
|
255 |
-
"metadata": {},
|
256 |
-
"outputs": [
|
257 |
-
{
|
258 |
-
"name": "stdout",
|
259 |
-
"output_type": "stream",
|
260 |
-
"text": [
|
261 |
-
"None\n",
|
262 |
-
"a\n"
|
263 |
-
]
|
264 |
-
}
|
265 |
-
],
|
266 |
-
"source": [
|
267 |
-
"print(history_prompt)\n",
|
268 |
-
"\n",
|
269 |
-
"if history_prompt is not None:\n",
|
270 |
-
" print('a')"
|
271 |
-
]
|
272 |
}
|
273 |
],
|
274 |
"metadata": {
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import os\n",
|
10 |
+
"# os.system('pip install requests')\n",
|
11 |
+
"import requests\n",
|
12 |
+
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
|
13 |
+
"gpt3_key = \"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\"\n",
|
14 |
+
"\n",
|
15 |
+
"def gpt3_question(api_key, prompt):\n",
|
16 |
+
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
|
17 |
+
" headers = {\n",
|
18 |
+
" \"Content-Type\": \"application/json\",\n",
|
19 |
+
" \"Authorization\": f\"Bearer {api_key}\"\n",
|
20 |
+
" }\n",
|
21 |
+
" data = {\n",
|
22 |
+
" \"prompt\": prompt,\n",
|
23 |
+
" \"max_tokens\": 400,\n",
|
24 |
+
" \"temperature\": 0.5\n",
|
25 |
+
" }\n",
|
26 |
+
" print('sending request')\n",
|
27 |
+
" response = requests.post(api_endpoint, headers=headers, json=data)\n",
|
28 |
+
" print(response)\n",
|
29 |
+
" generated_text = response.json()[\"choices\"][0][\"text\"]\n",
|
30 |
+
"\n",
|
31 |
+
" return generated_text\n",
|
32 |
+
"\n",
|
33 |
+
"def chatgpt3_question(api_key, prompt):\n",
|
34 |
+
" url = \"https://api.openai.com/v1/chat/completions\"\n",
|
35 |
+
" api_key = \"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\"\n",
|
36 |
+
"\n",
|
37 |
+
" headers = {\n",
|
38 |
+
" \"Content-Type\": \"application/json\",\n",
|
39 |
+
" \"Authorization\": f\"Bearer {api_key}\"\n",
|
40 |
+
" }\n",
|
41 |
+
"\n",
|
42 |
+
" data = {\n",
|
43 |
+
" \"model\": \"gpt-3.5-turbo\",\n",
|
44 |
+
" \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n",
|
45 |
+
" }\n",
|
46 |
+
"\n",
|
47 |
+
" response = requests.post(url, headers=headers, json=data)\n",
|
48 |
+
" generated_text = response.json()['choices'][0]['message']['content']\n",
|
49 |
+
"\n",
|
50 |
+
" return generated_text\n",
|
51 |
+
"\n",
|
52 |
+
"def history2prompt(history, extra):\n",
|
53 |
+
" # 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",
|
54 |
+
" history_ = [item for tup in history for item in tup]\n",
|
55 |
+
" history_.append(extra)\n",
|
56 |
+
" print(history_)\n",
|
57 |
+
"\n",
|
58 |
+
" if len(history_) > 1:\n",
|
59 |
+
" combinations = []\n",
|
60 |
+
" for i in range(1, len(history_)):\n",
|
61 |
+
" if i % 2 == 1:\n",
|
62 |
+
" combinations.append([i, i+2])\n",
|
63 |
+
"\n",
|
64 |
+
" history_full = list()\n",
|
65 |
+
" history_full.append(history_[0])\n",
|
66 |
+
" for range_ in combinations:\n",
|
67 |
+
" history_full.append(' - '.join(history_[range_[0]:range_[1]]))\n",
|
68 |
+
"\n",
|
69 |
+
" return '\\n'.join(history_full)\n",
|
70 |
+
" else:\n",
|
71 |
+
" return history_[0]\n",
|
72 |
+
"\n",
|
73 |
+
"# gpt3_keywords('The other day it was raining, and while I was driving a hit a stranger with my car.')\n",
|
74 |
+
"\n",
|
75 |
+
"import subprocess\n",
|
76 |
+
"import random\n",
|
77 |
+
"import gradio as gr\n",
|
78 |
+
"import requests\n",
|
79 |
+
"\n",
|
80 |
+
"history = None\n",
|
81 |
+
"history_prompt = None\n",
|
82 |
+
"history_final = None\n",
|
83 |
+
"block_predict = False\n",
|
84 |
+
"block_advice = False\n",
|
85 |
+
"\n",
|
86 |
+
"def predict(input, history):\n",
|
87 |
+
" #WE CAN PLAY WITH user_input AND bot_answer, as well as history\n",
|
88 |
+
" user_input = input\n",
|
89 |
+
"\n",
|
90 |
+
" # print('##', [x for x in history], input)\n",
|
91 |
+
" global history_prompt\n",
|
92 |
+
" global history_final\n",
|
93 |
+
" global block_predict\n",
|
94 |
+
"\n",
|
95 |
+
" if block_predict == False:\n",
|
96 |
+
" print('@@@', history)\n",
|
97 |
+
" history_prompt = history2prompt(history, input)\n",
|
98 |
+
" print('###', history_prompt)\n",
|
99 |
+
"\n",
|
100 |
+
" prompt = f\"\"\"\n",
|
101 |
+
" Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
102 |
+
" 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",
|
103 |
+
" \"\"\"\n",
|
104 |
+
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
|
105 |
+
"\n",
|
106 |
+
" response = list()\n",
|
107 |
+
" response = [(input, bot_answer)]\n",
|
108 |
+
" \n",
|
109 |
+
" history.append(response[0])\n",
|
110 |
+
" response = history\n",
|
111 |
+
" history_final = history\n",
|
112 |
+
"\n",
|
113 |
+
" # print('#history', history)\n",
|
114 |
+
" # print('#response', response)\n",
|
115 |
+
"\n",
|
116 |
+
" return response, history\n",
|
117 |
+
"\n",
|
118 |
+
"def chatbot_foo():\n",
|
119 |
+
" global history_prompt\n",
|
120 |
+
" global history_final\n",
|
121 |
+
" global block_predict\n",
|
122 |
+
" global block_advice\n",
|
123 |
+
"\n",
|
124 |
+
" if block_advice == False and history_prompt is not None:\n",
|
125 |
+
" prompt = f\"\"\"\n",
|
126 |
+
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
127 |
+
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
|
128 |
+
" \"\"\"\n",
|
129 |
+
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
|
130 |
+
"\n",
|
131 |
+
" history_final.append(('Consult me on the matter:', bot_answer))\n",
|
132 |
+
"\n",
|
133 |
+
" block_predict = True\n",
|
134 |
+
" block_advice = True\n",
|
135 |
+
" return history_final, history_final\n",
|
136 |
+
"\n",
|
137 |
+
"def reset_interface():\n",
|
138 |
+
" global history_prompt\n",
|
139 |
+
" global history_final\n",
|
140 |
+
" global block_predict\n",
|
141 |
+
" global block_advice\n",
|
142 |
+
"\n",
|
143 |
+
" history_prompt = None\n",
|
144 |
+
" history_final = None\n",
|
145 |
+
" block_predict = None\n",
|
146 |
+
" block_advice = None\n",
|
147 |
+
"\n",
|
148 |
+
"demo = gr.Blocks()\n",
|
149 |
+
"with demo:\n",
|
150 |
+
" gr.Markdown(\n",
|
151 |
+
" \"\"\"\n",
|
152 |
+
" <center> \n",
|
153 |
+
" Chat with Morty by typing in the input box below.\n",
|
154 |
+
" </center>\n",
|
155 |
+
" \"\"\"\n",
|
156 |
+
" )\n",
|
157 |
+
" state = gr.Variable(value=[]) #beginning\n",
|
158 |
+
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
|
159 |
+
" text = gr.Textbox(\n",
|
160 |
+
" label=\"Talk to your lawyer (press enter to submit)\",\n",
|
161 |
+
" value=\"The other day it was raining, and while I was driving a hit a stranger with my car.\",\n",
|
162 |
+
" placeholder=\"reply Yes or No\",\n",
|
163 |
+
" max_lines=1,\n",
|
164 |
+
" )\n",
|
165 |
+
" text.submit(predict, [text, state], [chatbot, state])\n",
|
166 |
+
" text.submit(lambda x: \"\", text, text)\n",
|
167 |
+
"\n",
|
168 |
+
" btn = gr.Button(value=\"submit\")\n",
|
169 |
+
" btn.click(chatbot_foo, None, [chatbot, state])\n",
|
170 |
+
"\n",
|
171 |
+
" btn2 = gr.Button(value=\"reset\")\n",
|
172 |
+
" btn.click(reset_interface)\n",
|
173 |
+
" # true_false_radio = gr.Radio(choices=[\"True\", \"False\"], label=\"Select True or False\")\n",
|
174 |
+
" # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True)\n",
|
175 |
+
"\n",
|
176 |
+
"demo.launch(share=False)"
|
177 |
+
]
|
178 |
+
},
|
179 |
+
{
|
180 |
+
"cell_type": "code",
|
181 |
+
"execution_count": 8,
|
182 |
"metadata": {},
|
183 |
"outputs": [
|
184 |
{
|
185 |
+
"name": "stdout",
|
186 |
"output_type": "stream",
|
187 |
"text": [
|
188 |
+
"None\n",
|
189 |
+
"a\n"
|
190 |
]
|
191 |
+
}
|
192 |
+
],
|
193 |
+
"source": [
|
194 |
+
"print(history_prompt)\n",
|
195 |
+
"\n",
|
196 |
+
"if history_prompt is not None:\n",
|
197 |
+
" print('a')"
|
198 |
+
]
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"cell_type": "code",
|
202 |
+
"execution_count": 5,
|
203 |
+
"metadata": {},
|
204 |
+
"outputs": [
|
205 |
{
|
206 |
"name": "stdout",
|
207 |
"output_type": "stream",
|
208 |
"text": [
|
209 |
+
"Running on local URL: http://127.0.0.1:7862\n",
|
210 |
"\n",
|
211 |
"To create a public link, set `share=True` in `launch()`.\n"
|
212 |
]
|
|
|
214 |
{
|
215 |
"data": {
|
216 |
"text/html": [
|
217 |
+
"<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>"
|
218 |
],
|
219 |
"text/plain": [
|
220 |
"<IPython.core.display.HTML object>"
|
|
|
227 |
"data": {
|
228 |
"text/plain": []
|
229 |
},
|
230 |
+
"execution_count": 5,
|
231 |
"metadata": {},
|
232 |
"output_type": "execute_result"
|
233 |
},
|
|
|
240 |
"### The other day it was raining, and while I was driving a hit a stranger with my car.\n",
|
241 |
"sending request\n",
|
242 |
"<Response [200]>\n",
|
243 |
+
"@@@ [('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",
|
244 |
+
"['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",
|
245 |
+
"### The other day it was raining, and while I was driving a hit a stranger with my car.\n",
|
246 |
+
"\n",
|
247 |
+
"Did the stranger suffer any fatal injuries as a result of the collision? - yes\n",
|
248 |
+
"sending request\n",
|
249 |
+
"<Response [200]>\n",
|
250 |
"sending request\n",
|
251 |
"<Response [200]>\n"
|
252 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
}
|
254 |
],
|
255 |
"source": [
|
|
|
257 |
"# os.system('pip install requests')\n",
|
258 |
"import requests\n",
|
259 |
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
|
260 |
+
"gpt3_key = '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'\n",
|
261 |
"\n",
|
262 |
"def gpt3_question(api_key, prompt):\n",
|
263 |
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
|
|
|
279 |
"\n",
|
280 |
"def chatgpt3_question(api_key, prompt):\n",
|
281 |
" url = \"https://api.openai.com/v1/chat/completions\"\n",
|
|
|
282 |
"\n",
|
283 |
" headers = {\n",
|
284 |
" \"Content-Type\": \"application/json\",\n",
|
|
|
323 |
"import gradio as gr\n",
|
324 |
"import requests\n",
|
325 |
"\n",
|
326 |
+
"# history = None\n",
|
327 |
"history_prompt = None\n",
|
328 |
"history_final = None\n",
|
329 |
"block_predict = False\n",
|
330 |
"block_advice = False\n",
|
331 |
"\n",
|
332 |
+
"def predict(input, history, start_var):\n",
|
333 |
" #WE CAN PLAY WITH user_input AND bot_answer, as well as history\n",
|
334 |
" user_input = input\n",
|
335 |
"\n",
|
|
|
337 |
" global history_prompt\n",
|
338 |
" global history_final\n",
|
339 |
" global block_predict\n",
|
340 |
+
" global block_advice\n",
|
341 |
+
"\n",
|
342 |
+
" if start_var == True:\n",
|
343 |
+
" history_prompt = None\n",
|
344 |
+
" history_final = None\n",
|
345 |
+
" block_predict = False\n",
|
346 |
+
" block_advice = False\n",
|
347 |
+
" start_var = False\n",
|
348 |
"\n",
|
349 |
" if block_predict == False:\n",
|
350 |
" print('@@@', history)\n",
|
|
|
376 |
" global block_advice\n",
|
377 |
"\n",
|
378 |
" if block_advice == False and history_prompt is not None:\n",
|
379 |
+
" \n",
|
380 |
" prompt = f\"\"\"\n",
|
381 |
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
382 |
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
|
|
|
389 |
" block_advice = True\n",
|
390 |
" return history_final, history_final\n",
|
391 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
392 |
"demo = gr.Blocks()\n",
|
393 |
"with demo:\n",
|
394 |
" gr.Markdown(\n",
|
395 |
" \"\"\"\n",
|
396 |
" <center> \n",
|
397 |
+
" Chat with your Lawyer\n",
|
398 |
" </center>\n",
|
399 |
" \"\"\"\n",
|
400 |
" )\n",
|
401 |
" state = gr.Variable(value=[]) #beginning\n",
|
402 |
+
" start_var = gr.Variable(value=True) #beginning\n",
|
403 |
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
|
404 |
" text = gr.Textbox(\n",
|
405 |
" label=\"Talk to your lawyer (press enter to submit)\",\n",
|
|
|
407 |
" placeholder=\"reply Yes or No\",\n",
|
408 |
" max_lines=1,\n",
|
409 |
" )\n",
|
410 |
+
" text.submit(predict, [text, state, start_var], [chatbot, state])\n",
|
411 |
" text.submit(lambda x: \"\", text, text)\n",
|
412 |
"\n",
|
413 |
" btn = gr.Button(value=\"submit\")\n",
|
414 |
" btn.click(chatbot_foo, None, [chatbot, state])\n",
|
|
|
|
|
|
|
415 |
" # true_false_radio = gr.Radio(choices=[\"True\", \"False\"], label=\"Select True or False\")\n",
|
416 |
" # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True)\n",
|
417 |
"\n",
|
418 |
"demo.launch(share=False)"
|
419 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
}
|
421 |
],
|
422 |
"metadata": {
|