bot-consultant / app.py
Michelangiolo's picture
first push
f33a891
import os
# os.system('pip install requests')
import requests
# gpt3_key = os.environ['GPT3_API_KEY']
from gpt3_function import *
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 = None
history_prompt = None
def predict(bot_type_radio, input, history, start_var):
print('@@@@@@@@@@@@@@@@@@@@@', bot_type_radio)
bot_type = {
"English Teacher" : """
Impersonate an English teacher, help the student practice by using questions or replies. Avoid introducing yourself.
Reply with max. one line
If last_input is in a wrong english, reply by correcting it
""",
"Sales Consultant" : """
Impersonate a Sales Consultant, giving technical advice on how to sell. Avoid introducing yourself.
additional information can be found in the history, don't mention it if not necessary
Answer the given query
""",
"Meditation Consultant" : """
Impersonate a Meditation Consultant, giving technical advice on techniques of breathing, meditation and relaxing. Avoid introducing yourself.
additional information can be found in the history, don't mention it if not necessary
Answer the given query
""",
"SEO Consultant" : """
Impersonate a Sales Consultant, giving technical advice on how to use SEO and which tools to use. Avoid introducing yourself.
additional information can be found in the history, don't mention it if not necessary
Answer the given query
""",
"Reskilling Consultant" : """
Impersonate a Reskilling Consultant, giving technical advice on how to help a person decide its own career. Avoid introducing yourself.
additional information can be found in the history, don't mention it if not necessary
Answer the given query
""",
}
#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_
if start_var == True:
history_prompt = None
start_var = False
# print('@@@', history)
history_prompt = history2prompt(history, input)
# print('###', history_prompt)
user_history = [x[0] for x in history[-2:]]
print('###', user_history)
# history: {history[:-2]}
prompt = f"""
history: {user_history}
query: {history_prompt}
{bot_type[bot_type_radio]}
"""
print(prompt)
bot_answer = gpt3(prompt=prompt, model='gpt-3.5-turbo', service='azure')
response = list()
response = [(input, bot_answer)]
history.append(response[0])
response = history
history_ = history
# print('#history', history)
# print('#response', response)
return response, history
demo = gr.Blocks()
with demo:
gr.Markdown(
"""
<center>
Chat with your Lawyer
</center>
"""
)
state = gr.Variable(value=[]) #beginning
start_var = gr.Variable(value=True) #beginning
bot_type_radio = gr.Radio(choices=['English Teacher', 'Sales Consultant', 'Meditation Consultant', 'SEO Consultant', 'Reskilling Consultant'], value='English Teacher')
chatbot = gr.Chatbot(color_map=("#00ff7f", "#00d5ff"))
text = gr.Textbox(
label="Talk to your AI consultant (press enter to submit)",
placeholder="I have a question about...",
max_lines=1,
)
text.submit(predict, [bot_type_radio, text, state, start_var], [chatbot, state])
text.submit(lambda x: "", text, text)
# grading = gr.Radio([x for x in range(0, 5)])
# btn2 = gr.Button(value="grade this response")
# 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)