forked from malywut/gpt_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
77 lines (59 loc) · 2.58 KB
/
run.py
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
from openai import OpenAI
import gradio as gr
import whisper
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
model = whisper.load_model("base")
def transcribe(file):
print(file)
transcription = model.transcribe(file)
return transcription['text']
prompts = {'START': 'Classify the intent of the next input. Is it: WRITE_EMAIL, QUESTION, OTHER? Only answer one word.',
'QUESTION': 'If you can answer the question: ANSWER, if you need more information: MORE, if you cannot answer: OTHER. Only answer one word.',
'ANSWER': 'Now answer the question as a polite assistant',
'MORE': 'Now ask for more information as a polite assistant',
'OTHER': 'Now tell me you cannot answer the question or do the action as a polite assistant',
'WRITE_EMAIL': 'If the subject or recipient or message is missing, answer "MORE". Else if you have all the information answer "ACTION_WRITE_EMAIL | subject:subject, recipient:recipient, message:message". '}
actions = {
'ACTION_WRITE_EMAIL':
"The mail has been sent. Now tell me the action is done in natural language."}
messages = [{"role": "user", "content": prompts['START']}]
def generate_answer(messages):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages)
return (response.choices[0].message.content)
def start(user_input):
messages.append({"role": "user", "content": user_input})
return discussion(messages, 'START')
def discussion(messages, last_step):
answer = generate_answer(messages)
print(answer)
if answer in prompts.keys():
messages.append({"role": "assistant", "content": answer})
messages.append({"role": "user", "content": prompts[answer]})
return discussion(messages, answer)
elif answer.split("|")[0].strip() in actions.keys():
return do_action(answer)
else:
if last_step != 'MORE':
messages = []
last_step = 'END'
return answer
def do_action(answer):
print("Doing action " + answer)
messages.append({"role": "assistant", "content": answer})
action = answer.split("|")[0].strip()
messages.append({"role": "user", "content": actions[action]})
return discussion(messages, answer)
def start_chat(file):
input = transcribe(file)
print(input)
return start(input)
gr.Interface(
theme=gr.themes.Soft(),
fn=start_chat,
live=True,
inputs=gr.Audio(sources="microphone", type="filepath"),
outputs="text").launch()