-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
153 lines (122 loc) · 3.96 KB
/
api.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
import webbrowser
from os import getenv
from pathlib import Path
import speech_recognition as sr
from dotenv import load_dotenv
from groq import Groq
from openai import OpenAI
from prompts import prompts
load_dotenv()
groq_client = Groq(api_key=getenv("GROQ"))
openai_client = OpenAI(api_key=getenv("OPENAI"))
def record_until_silence():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Adjusting for ambient noise. Please wait...")
r.adjust_for_ambient_noise(source, duration=1)
print("Speak now. Recording will stop after silence is detected.")
audio = r.listen(source, phrase_time_limit=None, timeout=10)
return audio
def listen(filename) -> str:
audio = record_until_silence()
wav_data = audio.get_wav_data()
with open(f"{filename}.wav", "wb") as wav_file:
wav_file.write(wav_data)
print(f"WAV file saved as '{filename}.wav'")
return filename
def groq_listen(filename, prompt="") -> str:
if not filename:
return
if filename[-4:] != ".wav":
filename += ".wav"
with open(filename, "rb") as file:
transcription = groq_client.audio.transcriptions.create(
file=(filename, file.read()),
model="distil-whisper-large-v3-en",
prompt=prompt,
response_format="text",
language="en",
temperature=0.0,
)
return transcription
def groq_response(
prompt,
temperature: float = 1.0,
max_tokens: int | None = None,
model: str = "llama-3.1-70b-versatile",
stop: list[str] = None,
) -> str:
chat_completion = groq_client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model=model,
temperature=temperature,
max_tokens=max_tokens,
stop=stop,
)
return chat_completion.choices[0].message.content
def openai_speak(text: str):
speech_file_path = Path(__file__).parent / "openai_output.wav"
response = openai_client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
)
response.stream_to_file(speech_file_path) # TODO: update once fully deprecated
"""
[0]: turn explanation into code
[1]: compare code
[2]: give feedback on code
[3]: refined transcript
"""
class API:
def listen(self, recording=None, gr: bool = False) -> str:
if not gr:
listen("input")
transcription = groq_listen("input")
else:
transcription = groq_listen(recording)
refined = groq_response(
prompts[3].format(transcription=transcription), stop=["\n"]
)
refined = refined.strip('"')
return refined
def thinking_to_code(self, text: str, boilerplate: str) -> str:
response = groq_response(prompts[0].format(explanation=text, stub=boilerplate))
response = (
response.strip()
.strip("```python")
.strip("```py")
.strip("```cpp")
.strip("```")
.strip()
)
return response
def evaluate_thinking(
self, code1: str, code2: str, text: str, leetcode: str
) -> str:
response = groq_response(
prompts[2].format(
code1=code1, code2=code2, pseudocode=text, problem=leetcode
),
temperature=0.0,
)
return response
def compare_code(self, code1: str, code2: str, text: str, leetcode: str) -> str:
response = groq_response(
prompts[1].format(
code1=code1, code2=code2, pseudocode=text, problem=leetcode
),
temperature=0.0,
max_tokens=1,
)
return response
def speak(self, text: str) -> None:
openai_speak(text)
def open_problem(self, title: str):
title = title.replace(" ", "-").lower()
webbrowser.open(f"http://leetcode.com/problems/{title}/description/")