Spaces:
Running
Running
Michelangiolo
commited on
Commit
•
0aa4b61
1
Parent(s):
c5f5fab
changes
Browse files- app.py +7 -11
- gradio_.ipynb +281 -0
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import os
|
2 |
# os.system('pip install requests')
|
3 |
import requests
|
|
|
4 |
|
5 |
-
def gpt3_question(prompt):
|
6 |
api_endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions"
|
7 |
-
api_key = "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"
|
8 |
headers = {
|
9 |
"Content-Type": "application/json",
|
10 |
"Authorization": f"Bearer {api_key}"
|
@@ -21,11 +21,8 @@ def gpt3_question(prompt):
|
|
21 |
|
22 |
return generated_text
|
23 |
|
24 |
-
|
25 |
-
def chatgpt3_question(prompt):
|
26 |
-
|
27 |
url = "https://api.openai.com/v1/chat/completions"
|
28 |
-
api_key = "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"
|
29 |
|
30 |
headers = {
|
31 |
"Content-Type": "application/json",
|
@@ -42,7 +39,6 @@ def chatgpt3_question(prompt):
|
|
42 |
|
43 |
return generated_text
|
44 |
|
45 |
-
|
46 |
def history2prompt(history, extra):
|
47 |
# 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?')]
|
48 |
history_ = [item for tup in history for item in tup]
|
@@ -95,7 +91,7 @@ def predict(input, history):
|
|
95 |
Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}
|
96 |
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
|
97 |
"""
|
98 |
-
bot_answer = gpt3_question(prompt)
|
99 |
|
100 |
response = list()
|
101 |
response = [(input, bot_answer)]
|
@@ -115,13 +111,13 @@ def chatbot_foo():
|
|
115 |
global block_predict
|
116 |
global block_advice
|
117 |
|
118 |
-
if block_advice == False:
|
119 |
|
120 |
prompt = f"""
|
121 |
Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}
|
122 |
Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books
|
123 |
"""
|
124 |
-
bot_answer = gpt3_question(prompt)
|
125 |
|
126 |
history_final.append(('Consult me on the matter:', bot_answer))
|
127 |
|
@@ -143,7 +139,7 @@ with demo:
|
|
143 |
text = gr.Textbox(
|
144 |
label="Talk to your lawyer (press enter to submit)",
|
145 |
value="The other day it was raining, and while I was driving a hit a stranger with my car.",
|
146 |
-
placeholder="
|
147 |
max_lines=1,
|
148 |
)
|
149 |
text.submit(predict, [text, state], [chatbot, state])
|
|
|
1 |
import os
|
2 |
# os.system('pip install requests')
|
3 |
import requests
|
4 |
+
gpt3_key = os.environ['GPT3_API_KEY']
|
5 |
|
6 |
+
def gpt3_question(api_key, prompt):
|
7 |
api_endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions"
|
|
|
8 |
headers = {
|
9 |
"Content-Type": "application/json",
|
10 |
"Authorization": f"Bearer {api_key}"
|
|
|
21 |
|
22 |
return generated_text
|
23 |
|
24 |
+
def chatgpt3_question(api_key, prompt):
|
|
|
|
|
25 |
url = "https://api.openai.com/v1/chat/completions"
|
|
|
26 |
|
27 |
headers = {
|
28 |
"Content-Type": "application/json",
|
|
|
39 |
|
40 |
return generated_text
|
41 |
|
|
|
42 |
def history2prompt(history, extra):
|
43 |
# 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?')]
|
44 |
history_ = [item for tup in history for item in tup]
|
|
|
91 |
Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}
|
92 |
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
|
93 |
"""
|
94 |
+
bot_answer = gpt3_question(gpt3_key, prompt)
|
95 |
|
96 |
response = list()
|
97 |
response = [(input, bot_answer)]
|
|
|
111 |
global block_predict
|
112 |
global block_advice
|
113 |
|
114 |
+
if block_advice == False and history_prompt is not None:
|
115 |
|
116 |
prompt = f"""
|
117 |
Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}
|
118 |
Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books
|
119 |
"""
|
120 |
+
bot_answer = gpt3_question(gpt3_key, prompt)
|
121 |
|
122 |
history_final.append(('Consult me on the matter:', bot_answer))
|
123 |
|
|
|
139 |
text = gr.Textbox(
|
140 |
label="Talk to your lawyer (press enter to submit)",
|
141 |
value="The other day it was raining, and while I was driving a hit a stranger with my car.",
|
142 |
+
placeholder="reply Yes or No",
|
143 |
max_lines=1,
|
144 |
)
|
145 |
text.submit(predict, [text, state], [chatbot, state])
|
gradio_.ipynb
ADDED
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 12,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stderr",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\components.py:4087: UserWarning: The 'color_map' parameter has been deprecated.\n",
|
13 |
+
" warnings.warn(\n"
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"name": "stdout",
|
18 |
+
"output_type": "stream",
|
19 |
+
"text": [
|
20 |
+
"Running on local URL: http://127.0.0.1:7864\n",
|
21 |
+
"\n",
|
22 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"data": {
|
27 |
+
"text/html": [
|
28 |
+
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
29 |
+
],
|
30 |
+
"text/plain": [
|
31 |
+
"<IPython.core.display.HTML object>"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
"metadata": {},
|
35 |
+
"output_type": "display_data"
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"data": {
|
39 |
+
"text/plain": []
|
40 |
+
},
|
41 |
+
"execution_count": 12,
|
42 |
+
"metadata": {},
|
43 |
+
"output_type": "execute_result"
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"name": "stdout",
|
47 |
+
"output_type": "stream",
|
48 |
+
"text": [
|
49 |
+
"@@@ []\n",
|
50 |
+
"['The other day it was raining, and while I was driving a hit a stranger with my car.']\n",
|
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 |
+
]
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"name": "stderr",
|
58 |
+
"output_type": "stream",
|
59 |
+
"text": [
|
60 |
+
"Traceback (most recent call last):\n",
|
61 |
+
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\routes.py\", line 394, in run_predict\n",
|
62 |
+
" output = await app.get_blocks().process_api(\n",
|
63 |
+
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\blocks.py\", line 1075, in process_api\n",
|
64 |
+
" result = await self.call_function(\n",
|
65 |
+
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\gradio\\blocks.py\", line 884, in call_function\n",
|
66 |
+
" prediction = await anyio.to_thread.run_sync(\n",
|
67 |
+
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\anyio\\to_thread.py\", line 31, in run_sync\n",
|
68 |
+
" return await get_asynclib().run_sync_in_worker_thread(\n",
|
69 |
+
" 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",
|
70 |
+
" return await future\n",
|
71 |
+
" File \"c:\\Users\\ardit\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 867, in run\n",
|
72 |
+
" result = context.run(func, *args)\n",
|
73 |
+
" File \"C:\\Users\\ardit\\AppData\\Local\\Temp\\ipykernel_20208\\436873897.py\", line 122, in chatbot_foo\n",
|
74 |
+
" bot_answer = gpt3_question(prompt)\n",
|
75 |
+
"TypeError: gpt3_question() missing 1 required positional argument: 'prompt'\n"
|
76 |
+
]
|
77 |
+
}
|
78 |
+
],
|
79 |
+
"source": [
|
80 |
+
"import os\n",
|
81 |
+
"# os.system('pip install requests')\n",
|
82 |
+
"import requests\n",
|
83 |
+
"# gpt3_key = os.environ['GPT3_API_KEY']\n",
|
84 |
+
"gpt3_key = \"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\"\n",
|
85 |
+
"\n",
|
86 |
+
"def gpt3_question(api_key, prompt):\n",
|
87 |
+
" api_endpoint = \"https://api.openai.com/v1/engines/text-davinci-003/completions\"\n",
|
88 |
+
" headers = {\n",
|
89 |
+
" \"Content-Type\": \"application/json\",\n",
|
90 |
+
" \"Authorization\": f\"Bearer {api_key}\"\n",
|
91 |
+
" }\n",
|
92 |
+
" data = {\n",
|
93 |
+
" \"prompt\": prompt,\n",
|
94 |
+
" \"max_tokens\": 400,\n",
|
95 |
+
" \"temperature\": 0.5\n",
|
96 |
+
" }\n",
|
97 |
+
" print('sending request')\n",
|
98 |
+
" response = requests.post(api_endpoint, headers=headers, json=data)\n",
|
99 |
+
" print(response)\n",
|
100 |
+
" generated_text = response.json()[\"choices\"][0][\"text\"]\n",
|
101 |
+
"\n",
|
102 |
+
" return generated_text\n",
|
103 |
+
"\n",
|
104 |
+
"def chatgpt3_question(api_key, prompt):\n",
|
105 |
+
" url = \"https://api.openai.com/v1/chat/completions\"\n",
|
106 |
+
" api_key = \"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\"\n",
|
107 |
+
"\n",
|
108 |
+
" headers = {\n",
|
109 |
+
" \"Content-Type\": \"application/json\",\n",
|
110 |
+
" \"Authorization\": f\"Bearer {api_key}\"\n",
|
111 |
+
" }\n",
|
112 |
+
"\n",
|
113 |
+
" data = {\n",
|
114 |
+
" \"model\": \"gpt-3.5-turbo\",\n",
|
115 |
+
" \"messages\": [{\"role\": \"user\", \"content\": prompt}]\n",
|
116 |
+
" }\n",
|
117 |
+
"\n",
|
118 |
+
" response = requests.post(url, headers=headers, json=data)\n",
|
119 |
+
" generated_text = response.json()['choices'][0]['message']['content']\n",
|
120 |
+
"\n",
|
121 |
+
" return generated_text\n",
|
122 |
+
"\n",
|
123 |
+
"def history2prompt(history, extra):\n",
|
124 |
+
" # 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",
|
125 |
+
" history_ = [item for tup in history for item in tup]\n",
|
126 |
+
" history_.append(extra)\n",
|
127 |
+
" print(history_)\n",
|
128 |
+
"\n",
|
129 |
+
" if len(history_) > 1:\n",
|
130 |
+
" combinations = []\n",
|
131 |
+
" for i in range(1, len(history_)):\n",
|
132 |
+
" if i % 2 == 1:\n",
|
133 |
+
" combinations.append([i, i+2])\n",
|
134 |
+
"\n",
|
135 |
+
" history_full = list()\n",
|
136 |
+
" history_full.append(history_[0])\n",
|
137 |
+
" for range_ in combinations:\n",
|
138 |
+
" history_full.append(' - '.join(history_[range_[0]:range_[1]]))\n",
|
139 |
+
"\n",
|
140 |
+
" return '\\n'.join(history_full)\n",
|
141 |
+
" else:\n",
|
142 |
+
" return history_[0]\n",
|
143 |
+
"\n",
|
144 |
+
"# gpt3_keywords('The other day it was raining, and while I was driving a hit a stranger with my car.')\n",
|
145 |
+
"\n",
|
146 |
+
"import subprocess\n",
|
147 |
+
"import random\n",
|
148 |
+
"import gradio as gr\n",
|
149 |
+
"import requests\n",
|
150 |
+
"\n",
|
151 |
+
"history = None\n",
|
152 |
+
"history_prompt = None\n",
|
153 |
+
"history_final = None\n",
|
154 |
+
"block_predict = False\n",
|
155 |
+
"block_advice = False\n",
|
156 |
+
"\n",
|
157 |
+
"def predict(input, history):\n",
|
158 |
+
" #WE CAN PLAY WITH user_input AND bot_answer, as well as history\n",
|
159 |
+
" user_input = input\n",
|
160 |
+
"\n",
|
161 |
+
" # print('##', [x for x in history], input)\n",
|
162 |
+
" global history_prompt\n",
|
163 |
+
" global history_final\n",
|
164 |
+
" global block_predict\n",
|
165 |
+
"\n",
|
166 |
+
" if block_predict == False:\n",
|
167 |
+
" print('@@@', history)\n",
|
168 |
+
" history_prompt = history2prompt(history, input)\n",
|
169 |
+
" print('###', history_prompt)\n",
|
170 |
+
"\n",
|
171 |
+
" prompt = f\"\"\"\n",
|
172 |
+
" Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
173 |
+
" 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",
|
174 |
+
" \"\"\"\n",
|
175 |
+
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
|
176 |
+
"\n",
|
177 |
+
" response = list()\n",
|
178 |
+
" response = [(input, bot_answer)]\n",
|
179 |
+
" \n",
|
180 |
+
" history.append(response[0])\n",
|
181 |
+
" response = history\n",
|
182 |
+
" history_final = history\n",
|
183 |
+
"\n",
|
184 |
+
" # print('#history', history)\n",
|
185 |
+
" # print('#response', response)\n",
|
186 |
+
"\n",
|
187 |
+
" return response, history\n",
|
188 |
+
"\n",
|
189 |
+
"def chatbot_foo():\n",
|
190 |
+
" global history_prompt\n",
|
191 |
+
" global history_final\n",
|
192 |
+
" global block_predict\n",
|
193 |
+
" global block_advice\n",
|
194 |
+
"\n",
|
195 |
+
" if block_advice == False and history_prompt is not None:\n",
|
196 |
+
" \n",
|
197 |
+
" prompt = f\"\"\"\n",
|
198 |
+
" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt}\n",
|
199 |
+
" Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books\n",
|
200 |
+
" \"\"\"\n",
|
201 |
+
" bot_answer = gpt3_question(gpt3_key, prompt)\n",
|
202 |
+
"\n",
|
203 |
+
" history_final.append(('Consult me on the matter:', bot_answer))\n",
|
204 |
+
"\n",
|
205 |
+
" block_predict = True\n",
|
206 |
+
" block_advice = True\n",
|
207 |
+
" return history_final, history_final\n",
|
208 |
+
"\n",
|
209 |
+
"demo = gr.Blocks()\n",
|
210 |
+
"with demo:\n",
|
211 |
+
" gr.Markdown(\n",
|
212 |
+
" \"\"\"\n",
|
213 |
+
" <center> \n",
|
214 |
+
" Chat with Morty by typing in the input box below.\n",
|
215 |
+
" </center>\n",
|
216 |
+
" \"\"\"\n",
|
217 |
+
" )\n",
|
218 |
+
" state = gr.Variable(value=[]) #beginning\n",
|
219 |
+
" chatbot = gr.Chatbot(color_map=(\"#00ff7f\", \"#00d5ff\"))\n",
|
220 |
+
" text = gr.Textbox(\n",
|
221 |
+
" label=\"Talk to your lawyer (press enter to submit)\",\n",
|
222 |
+
" value=\"The other day it was raining, and while I was driving a hit a stranger with my car.\",\n",
|
223 |
+
" placeholder=\"reply Yes or No\",\n",
|
224 |
+
" max_lines=1,\n",
|
225 |
+
" )\n",
|
226 |
+
" text.submit(predict, [text, state], [chatbot, state])\n",
|
227 |
+
" text.submit(lambda x: \"\", text, text)\n",
|
228 |
+
"\n",
|
229 |
+
" btn = gr.Button(value=\"submit\")\n",
|
230 |
+
" btn.click(chatbot_foo, None, [chatbot, state])\n",
|
231 |
+
" # true_false_radio = gr.Radio(choices=[\"True\", \"False\"], label=\"Select True or False\")\n",
|
232 |
+
" # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True)\n",
|
233 |
+
"\n",
|
234 |
+
"demo.launch(share=False)"
|
235 |
+
]
|
236 |
+
},
|
237 |
+
{
|
238 |
+
"cell_type": "code",
|
239 |
+
"execution_count": 8,
|
240 |
+
"metadata": {},
|
241 |
+
"outputs": [
|
242 |
+
{
|
243 |
+
"name": "stdout",
|
244 |
+
"output_type": "stream",
|
245 |
+
"text": [
|
246 |
+
"None\n",
|
247 |
+
"a\n"
|
248 |
+
]
|
249 |
+
}
|
250 |
+
],
|
251 |
+
"source": [
|
252 |
+
"print(history_prompt)\n",
|
253 |
+
"\n",
|
254 |
+
"if history_prompt is not None:\n",
|
255 |
+
" print('a')"
|
256 |
+
]
|
257 |
+
}
|
258 |
+
],
|
259 |
+
"metadata": {
|
260 |
+
"kernelspec": {
|
261 |
+
"display_name": "Python 3",
|
262 |
+
"language": "python",
|
263 |
+
"name": "python3"
|
264 |
+
},
|
265 |
+
"language_info": {
|
266 |
+
"codemirror_mode": {
|
267 |
+
"name": "ipython",
|
268 |
+
"version": 3
|
269 |
+
},
|
270 |
+
"file_extension": ".py",
|
271 |
+
"mimetype": "text/x-python",
|
272 |
+
"name": "python",
|
273 |
+
"nbconvert_exporter": "python",
|
274 |
+
"pygments_lexer": "ipython3",
|
275 |
+
"version": "3.9.13"
|
276 |
+
},
|
277 |
+
"orig_nbformat": 4
|
278 |
+
},
|
279 |
+
"nbformat": 4,
|
280 |
+
"nbformat_minor": 2
|
281 |
+
}
|