-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
159 lines (120 loc) · 4.64 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import openai
import config
from gtts import gTTS
import os
import random
import requests
# from pydub import AudioSegment
from datetime import datetime
import openpyxl
from openpyxl import Workbook
from datetime import datetime
openai.api_key = config.OPENAI_API_KEY
# To generate AI response to prompt
def generate_response(prompt):
# URL if using informed AI
url = 'http://35.203.35.125:6000/generate_response_informed'
# URL if using uninformed AI
# url = "http://35.203.35.125:5000/generate_response"
data = {"prompt":prompt}
response = requests.post(url, json=data)
return response.json()
# To convert text to speech
def generate_audio(text):
tts = gTTS(text, lang='en-us')
tts.save('audio.mp3')
with open('audio.mp3', 'rb') as f:
audio_content = f.read()
os.remove('audio.mp3')
return audio_content
# To transcribe speech to text
def transcribe(audio):
audio_file = open(audio, "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file,language = "en", response_format="json")
return transcript.text
# To save logs of the QuickTalker
def update_log(time, latency, action, prompt, resp1, resp2):
# Load the existing workbook or create a new one
filename = 'logs/quicktalker.xlsx'
workbook = openpyxl.load_workbook(filename)
# Select the active sheet or create a new one
sheet = workbook.active
# Find the next available row to add the entry
# row = sheet.max_row + 1
# Find the next available row by searching for the first empty row below the header
row = 2 # Start from row 2 (assuming row 1 is the header)
while sheet.cell(row=row, column=1).value is not None:
row += 1
# Update the Date and Time columns with the current date and time
now = datetime.now()
sheet.cell(row=row, column=1, value=now.date())
# Update the Time, Time Taken, Prompt, Response #1, Response #2 columns
sheet.cell(row=row, column=2, value=time)
sheet.cell(row=row, column=3, value=latency + ' s')
sheet.cell(row=row, column=4, value=action)
sheet.cell(row=row, column=5, value=prompt)
sheet.cell(row=row, column=6, value=resp1)
sheet.cell(row=row, column=7, value=resp2)
# Save the workbook
workbook.save(filename)
# To update header for current training session
def update_header():
with open("trainer_prompts.txt", "r") as file:
header = file.readlines()[0].strip()
filename = 'logs/trainer.xlsx'
workbook = openpyxl.load_workbook(filename)
sheet = workbook.active
row = 2 # Start from row 2 (assuming row 1 is the header)
while sheet.cell(row=row, column=1).value is not None:
row += 1
sheet.cell(row=row, column=1, value=header)
workbook.save(filename)
# To generate a sentence for the trainer
def generate_sentence(lineNum):
with open("trainer_prompts.txt", "r") as file:
lines = file.readlines()
return lines[lineNum].strip()
# To save logs of the Trainer
def update_trainer_log(time, latency, prompt):
# Load the existing workbook or create a new one
filename = 'logs/trainer.xlsx'
workbook = openpyxl.load_workbook(filename)
# Select the active sheet or create a new one
sheet = workbook.active
# Find the next available row to add the entry
# row = sheet.max_row + 1
# Find the next available row by searching for the first empty row below the header
row = 3 # Start from row 3 (assuming row 1 is the header)
while sheet.cell(row=row, column=1).value is not None:
row += 1
# Update the Date and Time columns with the current date and time
now = datetime.now()
sheet.cell(row=row, column=1, value=now.date())
# Update the Time, Latency, Prompt columns
sheet.cell(row=row, column=2, value=time)
sheet.cell(row=row, column=3, value=latency + ' s')
sheet.cell(row=row, column=4, value=prompt)
# Save the workbook
workbook.save(filename)
# To assess the user's response to trainer
def response_score(r1, r2):
prompt, resp = r1[:-1].split(), r2.split()
if r1[-1] == '.':
prompt.append('.')
else:
prompt.append('?')
wordScore = 1 # how many tries it took to get the word right
# user gets 5 tries, anything beyond that results in a score of 0
# each extra try costs 0.20
scoreSum = 0 # the sum of scores for each word
i = j = 0
while i < len(prompt) and j < len(resp):
if prompt[i] == resp[j]:
i+=1
j+=1
scoreSum += max(wordScore, 0)
wordScore = 1
else:
j+=1
wordScore -= 0.20
return round(scoreSum / len(prompt) * 100, 2)