-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.py
executable file
·100 lines (75 loc) · 2.62 KB
/
mastermind.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
#!/usr/bin/env python3
from openai import AsyncOpenAI, audio
import os
from scipy.io.wavfile import write
from dotenv import load_dotenv
import sounddevice as sd
import asyncio
# pull this from my .env file
# Load the .env file
load_dotenv()
client = AsyncOpenAI(
# This is the default and can be omitted
api_key=os.getenv("OPENAI_API_KEY"),
)
def giveCommand():
fs = 44100 # Sample rate
seconds = 15 # Duration of recording
myRecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
print("listening...")
# sd.wait() # Wait until recording is finished
input("Press Enter to stop recording")
sd.stop()
write('command.wav', fs, myRecording) # Save as WAV file
print("done")
async def transcribe():
audio_file= open("command.wav", "rb")
transcript = await client.audio.transcriptions.create(model="whisper-1", file=audio_file)
return transcript.text
async def main() -> None:
fileName = input("Enter the file: ")
giveCommand()
command = await transcribe()
print("")
print("Command: " + command)
os.remove("command.wav")
# Read the React Native file into a string
with open(fileName, 'r') as file:
file_content = file.read()
# Define the modification you want to make
modification_prompt = f"{file_content}\n\n# Now, modify the above code by with the following request: {command}.\n\n Include the whole file. ONLY include code in your response."
print("")
print("Working on that...")
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": "When writing code, please do not use inline styles for React Native. Instead, use the StyleSheet API.",
},
{
"role": "user",
"content": modification_prompt,
}
],
model="gpt-3.5-turbo"
)
print("")
print("Working out the details...")
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": chat_completion.choices[0].message.content + "\n\n Strip everything from this response and only return the code. Strip extra characters like ``` or ```jsx",
}
],
model="gpt-3.5-turbo"
)
newCode = chat_completion.choices[0].message.content
# Open the file in write mode
with open(fileName, 'w') as file:
# Write the new text to the file
file.write(newCode)
print("")
print("✨ File has been updated with the new code ✨")
print("")
asyncio.run(main())