-
Notifications
You must be signed in to change notification settings - Fork 0
/
laso.py
executable file
·116 lines (84 loc) · 3 KB
/
laso.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
#!/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
import base64
import pyperclip
# 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
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
fileName = "screencapture.png"
async def main() -> None:
os.system(f"screencapture -i {fileName}")
giveCommand()
command = await transcribe()
print("")
print("Command: " + command)
os.remove("command.wav")
# Define the modification you want to make
modification_prompt = f"I'm writing a detox test. Please provide code to perform the following task: {command}.\n\n Include the lines of code necessary to perform the task. ONLY include code in your response with no codeblock formatting."
print("")
print("Working on that...")
base64_image = encode_image(fileName)
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": modification_prompt
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
},
],
model="gpt-4-vision-preview"
)
print("")
print("Working out the details...")
print(chat_completion.choices[0].message.content)
pyperclip.copy(chat_completion.choices[0].message.content)
# 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
print("")
print("✨ Code copied to your keyboard! ✨")
print("")
asyncio.run(main())