Skip to content

Commit c49f6a0

Browse files
authored
Add files via upload
0 parents  commit c49f6a0

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

main.py

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#Se importa la librería Whisper
2+
import whisper
3+
import openai
4+
import os
5+
from dotenv import load_dotenv
6+
import pprint
7+
import pyaudio
8+
import wave
9+
from pydub import AudioSegment
10+
11+
def grabar_audio(num):
12+
# set parameters for recording
13+
FORMAT = pyaudio.paInt16
14+
CHANNELS = 1
15+
RATE = 44100
16+
CHUNK = 1024
17+
RECORD_SECONDS = 10
18+
WAVE_OUTPUT_FILENAME = "output.wav"
19+
MP3_OUTPUT_FILENAME = f"output{num}.mp3"
20+
21+
# initialize PyAudio
22+
audio = pyaudio.PyAudio()
23+
24+
# start recording
25+
stream = audio.open(format=FORMAT, channels=CHANNELS,
26+
rate=RATE, input=True,
27+
frames_per_buffer=CHUNK)
28+
29+
frames = []
30+
31+
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
32+
data = stream.read(CHUNK)
33+
frames.append(data)
34+
35+
# stop recording
36+
stream.stop_stream()
37+
stream.close()
38+
audio.terminate()
39+
40+
# save the recorded audio to a WAV file
41+
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
42+
wf.setnchannels(CHANNELS)
43+
wf.setsampwidth(audio.get_sample_size(FORMAT))
44+
wf.setframerate(RATE)
45+
wf.writeframes(b''.join(frames))
46+
wf.close()
47+
48+
# convert the WAV file to MP3
49+
sound = AudioSegment.from_wav(WAVE_OUTPUT_FILENAME)
50+
sound.export(MP3_OUTPUT_FILENAME, format="mp3")
51+
52+
53+
54+
55+
def update_chat(message, role, content):
56+
message.append({"role": role, "content": content})
57+
return message
58+
59+
def get_chatgpt_response(messages):
60+
response = openai.ChatCompletion.create(
61+
model='gpt-3.5-turbo',
62+
messages=messages
63+
)
64+
65+
return response['choices'][0]['message']['content']
66+
67+
# Se carga el modelo a utilizar
68+
model = whisper.load_model("base")
69+
70+
load_dotenv()
71+
openai.api_key = os.getenv("OPENAI_API_KEY")
72+
'''
73+
messages=[{"role": "system", "content": "You are an english evaluator. You will create an question to evaluate the english skill of the user. After receiving the answer of the user, you will evaluate his grammar, coherence and vocabulary, along with providing feedback and how to improve. The answer was spoken and transcribed with Whisper. The user did not write it, he spoke it."},
74+
{"role": "assistant", "content": "I understand. I will evaluate the answer of the user."},
75+
{"role": "assistant", "content": "Hey, are you ready to begin the test? Once you say yes, I will provide the question that you will need to answer."},
76+
{"role": "user", "content": "yes"},
77+
{"role": "assistant", "content": "What are you currently studying and why?"}]
78+
79+
'''
80+
81+
messages= [{"role": "system", "content": "You are an english evaluator. We will have a conversation about a topic. Start asking an initial question to talk with me. Keep the conversation going for 3 more questions in total, but ask only one question after i answer, and so on. After that, evaluate my grammar, coherence and vocabulary, each in a scale from 1 to 100. After finishing the conversations, only respond with the 3 scores on the areas previously mentioned, i don't want feedback, only the scores stored in a json. "},
82+
{"role": "assistant", "content": "I understand, after 5 questions I will only show the results in coherence, vocabulary and grammar in a JSON and that is the only thing I will return to the user."},
83+
{"role": "assistant", "content": "Hey! What are you currently studying and why'"}]
84+
85+
go = True
86+
control = 0
87+
preguntas = 0
88+
first = True
89+
90+
pprint.pprint(messages[-1]["content"])
91+
92+
while go:
93+
preguntas += 1
94+
if not first:
95+
pprint.pprint(messages[-1]["content"])
96+
97+
user_input = input()
98+
# grabar_audio(0)
99+
# user_input = model.transcribe("output0.mp3")["text"]
100+
messages = update_chat(messages, "user", user_input)
101+
model_response = get_chatgpt_response(messages)
102+
messages = update_chat(messages, "assistant", model_response)
103+
first = False
104+
if preguntas == 5:
105+
break
106+
107+
print("Holaaa")
108+
messages = update_chat(messages, "assistant",
109+
"The conversation has finished. Based on the answers I gave you, generate a JSON with 6 fields: Grammar, Coherence, Vocabulary, Feedback, Recommendations and English_Level. The first three fields must be evaluated in a scale from 1 to 100, the feedback must be a paragraph of my overall performance and the English_Level field must be either A1, A2, B1, B2, C1 or C2. The Recommendations field must be an array of 3 specific recommendations that the user could have done to improve his phrasing referring to what he said, in this recommendations mention specific words or sentences that could have been changed..")
110+
model_response = get_chatgpt_response(messages)
111+
112+
print(model_response)

0 commit comments

Comments
 (0)