import os # os.system('pip install requests') import requests def gpt3_question(prompt): api_endpoint = "https://api.openai.com/v1/engines/text-davinci-003/completions" api_key = "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" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "prompt": prompt, "max_tokens": 400, "temperature": 0.5 } print('sending request') response = requests.post(api_endpoint, headers=headers, json=data) print(response) generated_text = response.json()["choices"][0]["text"] return generated_text def chatgpt3_question(prompt): url = "https://api.openai.com/v1/chat/completions" api_key = "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" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } data = { "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": prompt}] } response = requests.post(url, headers=headers, json=data) generated_text = response.json()['choices'][0]['message']['content'] return generated_text def history2prompt(history, extra): # 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?')] history_ = [item for tup in history for item in tup] history_.append(extra) print(history_) if len(history_) > 1: combinations = [] for i in range(1, len(history_)): if i % 2 == 1: combinations.append([i, i+2]) history_full = list() history_full.append(history_[0]) for range_ in combinations: history_full.append(' - '.join(history_[range_[0]:range_[1]])) return '\n'.join(history_full) else: return history_[0] # gpt3_keywords('The other day it was raining, and while I was driving a hit a stranger with my car.') import subprocess import random import gradio as gr import requests history = None history_prompt = None history_final = None block_predict = False block_advice = False def predict(input, history): #WE CAN PLAY WITH user_input AND bot_answer, as well as history user_input = input # print('##', [x for x in history], input) global history_prompt global history_final global block_predict if block_predict == False: print('@@@', history) history_prompt = history2prompt(history, input) print('###', history_prompt) prompt = f""" Imagine being a criminal lawyer being told the following story with the following circumstances: {history_prompt} 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 """ bot_answer = gpt3_question(prompt) response = list() response = [(input, bot_answer)] history.append(response[0]) response = history history_final = history # print('#history', history) # print('#response', response) return response, history def chatbot_foo(): global history_prompt global history_final global block_predict global block_advice if block_advice == False: prompt = f""" Imagine being an Ohio criminal lawyer being told the following story with the following circumstances: {history_prompt} Tell the client how much does he risk in terms of criminal charges, prison, and cite sources from law books """ bot_answer = gpt3_question(prompt) history_final.append(('Consult me on the matter:', bot_answer)) block_predict = True block_advice = True return history_final, history_final demo = gr.Blocks() with demo: gr.Markdown( """
Chat with Morty by typing in the input box below.
""" ) state = gr.Variable(value=[]) #beginning chatbot = gr.Chatbot(color_map=("#00ff7f", "#00d5ff")) text = gr.Textbox( label="Talk to your lawyer (press enter to submit)", value="The other day it was raining, and while I was driving a hit a stranger with my car.", placeholder="Reply yes or No", max_lines=1, ) text.submit(predict, [text, state], [chatbot, state]) text.submit(lambda x: "", text, text) btn = gr.Button(value="submit") btn.click(chatbot_foo, None, [chatbot, state]) # true_false_radio = gr.Radio(choices=["True", "False"], label="Select True or False") # iface = gr.Interface(fn=my_function, inputs=[text, true_false_radio], outputs=chatbot, live=True, capture_session=True) demo.launch(share=False)