-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPTViewModel.kt
292 lines (261 loc) · 11.3 KB
/
ChatGPTViewModel.kt
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.example.talkandexecute
import android.app.Application
import android.content.Context
import android.media.MediaRecorder
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.example.talkandexecute.classification.AudioClassificationHelper
import com.example.talkandexecute.model.AudioToText
import com.example.talkandexecute.model.GeneratedAnswer
import com.example.talkandexecute.model.SpeechState
import com.example.talkandexecute.recorder.Recorder
import com.example.talkandexecute.whisperengine.IWhisperEngine
import com.example.talkandexecute.whisperengine.WhisperEngine
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.logging.HttpLoggingInterceptor
import org.json.JSONArray
import org.json.JSONObject
import org.tensorflow.lite.support.label.Category
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
class ChatGPTViewModel(application: Application) : AndroidViewModel(application) {
var speechState by mutableStateOf(SpeechState())
private var mediaRecorder: MediaRecorder = MediaRecorder()
private var isRecording: Boolean = false
private var numberOfBackgroundLabel = 0
private val outputFile = File(application.filesDir, RECORDING_FILE)
private val outputFileWav = File(application.filesDir, RECORDING_FILE_WAV)
private val audioClassificationListener = object : AudioClassificationListener {
override fun onResult(results: List<Category>, inferenceTime: Long) {
Log.v("speech_result", "$results $inferenceTime")
if (results.isNotEmpty()) {
if (results[0].index == 7) {
numberOfBackgroundLabel = 0
// startListening()
} else if (results[0].index == 0) {
numberOfBackgroundLabel++
}
} else {
numberOfBackgroundLabel++
}
if (isRecording) {
// Log.v("speech_number", "$numberOfBackgroundLabel")
if (numberOfBackgroundLabel > 10) {
numberOfBackgroundLabel = 0
// stopListening()
}
}
}
override fun onError(error: String) {
Log.v("speech_result", error)
}
}
private val whisperEngine: IWhisperEngine = WhisperEngine(application)
private val recorder: Recorder = Recorder(application)
private val audioClassificationHelper =
AudioClassificationHelper(context = application, listener = audioClassificationListener)
init {
audioClassificationHelper.initClassifier()
whisperEngine.initialize(MODEL_PATH, getFilePath(VOCAB_PATH, application), false)
recorder.setFilePath(getFilePath(RECORDING_FILE_WAV, application) )
}
fun startRecordingWav() {
recorder.start()
}
fun stopRecordingWav() {
recorder.stop()
try {
viewModelScope.launch(Dispatchers.Default) {
// Offline speech to text
val transcribedText = whisperEngine.transcribeFile(outputFileWav.absolutePath)
speechState = try {
speechState.copy(speechResult = transcribedText)
} catch (e: IOException) {
// There was an error
speechState.copy(speechResult = "API Error: ${e.message}")
}
// Send text to ChatGPT
speechState = try {
val returnedText = createChatCompletion(transcribedText)
resetRecording()
speechState.copy(palmResult = returnedText)
} catch (e: IOException) {
// There was an error
resetRecording()
speechState.copy(palmResult = "API Error: ${e.message}")
}
}
} catch (e: RuntimeException) {
Log.e(TAG, e.toString())
// Handle the exception -> state machine is not in a valid state
resetRecording()
} catch (e: IllegalStateException) {
Log.e(TAG, e.toString())
resetRecording()
}
}
fun startListening() {
// Log.v("speech_start", "start")
if (!isRecording) {
isRecording = true
numberOfBackgroundLabel = 0
// Log.v("speech_start", "true")
try {
mediaRecorder.apply {
// Initialization.
setAudioSource(MediaRecorder.AudioSource.MIC)
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
// path to recording, if you want to hear it.
setOutputFile(outputFile.absolutePath)
}
mediaRecorder.prepare()
mediaRecorder.start()
} catch (e: IllegalStateException) {
Log.e(TAG, e.toString())
// Handle the exception -> MediaRecorder is not in the initialized state
resetRecording()
} catch (e: IOException) {
Log.e(TAG, e.toString())
// Handle the exception -> failed to prepare MediaRecorder
resetRecording()
}
}
}
fun stopListening() {
// Log.v("speech_stop", "stop")
if (isRecording) {
// Log.v("speech_stop", "true")
try {
mediaRecorder.stop()
mediaRecorder.reset()
isRecording = false
viewModelScope.launch(Dispatchers.Default) {
val transcribedText = transcribeAudio(outputFile)
speechState = try {
speechState.copy(speechResult = transcribedText)
} catch (e: IOException) {
// There was an error
speechState.copy(speechResult = "API Error: ${e.message}")
}
speechState = try {
val returnedText = createChatCompletion(transcribedText)
resetRecording()
speechState.copy(palmResult = returnedText)
} catch (e: IOException) {
// There was an error
resetRecording()
speechState.copy(palmResult = "API Error: ${e.message}")
}
}
} catch (e: RuntimeException) {
Log.e(TAG, e.toString())
// Handle the exception -> state machine is not in a valid state
resetRecording()
} catch (e: IllegalStateException) {
Log.e(TAG, e.toString())
resetRecording()
}
}
}
private fun resetRecording() {
mediaRecorder.reset()
isRecording = false
numberOfBackgroundLabel = 0
}
private var loggingInterceptor =
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private val client = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.connectTimeout(30, TimeUnit.SECONDS) // Set connection timeout to 30 seconds
.readTimeout(30, TimeUnit.SECONDS) // Set read timeout to 30 seconds
.build()
private fun transcribeAudio(audioFile: File): String {
val audioRequestBody = audioFile.asRequestBody("audio/*".toMediaType())
val formBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", audioFile.name, audioRequestBody)
.addFormDataPart("model", "whisper-1")
.addFormDataPart("language", "en")
.build()
val request = Request.Builder()
.url("https://api.openai.com/v1/audio/transcriptions")
.header("Authorization", "Bearer $API_KEY")
.post(formBody)
val gson = Gson()
return client.newCall(request.build()).execute().use { response ->
val audioCompletionResponse =
gson.fromJson(response.body?.string() ?: "", AudioToText::class.java)
audioCompletionResponse.text
}
}
private fun createChatCompletion(prompt: String): String {
val mediaType = "application/json; charset=utf-8".toMediaType()
val completeString = "I say $prompt. As an assistant how can you help me?\n" +
"Pick one from the options below if it is related to volume and write only the two words:\n" +
"volume up\n" +
"volume down\n" +
"if it is not related to volume answer the below two words: \n" +
"volume stable"
val messagesArray = JSONArray()
messagesArray.put(
JSONObject().put("role", "system")
.put("content", "You are a helpful assistant inside a car.")
)
// messagesArray.put(JSONObject().put("role", "user").put("content", "Who won the world series in 2020?"))
// messagesArray.put(JSONObject().put("role", "assistant").put("content", "The Los Angeles Dodgers won the World Series in 2020."))
messagesArray.put(JSONObject().put("role", "user").put("content", completeString))
val json = JSONObject()
.put("model", "gpt-3.5-turbo") // new gpt-3.5-turbo-1106
.put("messages", messagesArray)
val requestBody = json.toString().toRequestBody(mediaType)
val request = Request.Builder()
.url("https://api.openai.com/v1/chat/completions")
.header("Authorization", "Bearer $API_KEY")
.post(requestBody)
.build()
val gson = Gson()
client.newCall(request).execute().use { response ->
val chatCompletionResponse =
gson.fromJson(response.body?.string() ?: "", GeneratedAnswer::class.java)
return chatCompletionResponse.choices?.get(0)?.message?.content.toString()
}
}
override fun onCleared() {
super.onCleared()
mediaRecorder.release()
audioClassificationHelper.stopAudioClassification()
}
companion object {
private const val MODEL_PATH = "whisper_tiny_english_14.tflite"
private const val VOCAB_PATH = "filters_vocab_en.bin"
private const val RECORDING_FILE = "recording.mp3"
private const val RECORDING_FILE_WAV = "recording.wav"
}
// Returns file path for vocab .bin file
private fun getFilePath(assetName: String, context: Context): String? {
val outfile = File(context.filesDir, assetName)
if (!outfile.exists()) {
Log.d(TAG, "File not found - " + outfile.absolutePath)
}
Log.d(TAG, "Returned asset path: " + outfile.absolutePath)
return outfile.absolutePath
}
}
interface AudioClassificationListener {
fun onError(error: String)
fun onResult(results: List<Category>, inferenceTime: Long)
}