88import com .wmods .wppenhacer .xposed .core .components .FMessageWpp ;
99import com .wmods .wppenhacer .xposed .core .devkit .Unobfuscator ;
1010import com .wmods .wppenhacer .xposed .utils .ReflectionUtils ;
11+ import com .wmods .wppenhacer .xposed .utils .ResId ;
12+ import com .wmods .wppenhacer .xposed .utils .Utils ;
1113
1214import org .json .JSONObject ;
1315
@@ -32,7 +34,19 @@ public AudioTranscript(@NonNull ClassLoader classLoader, @NonNull XSharedPrefere
3234 @ Override
3335 public void doHook () throws Throwable {
3436
35- if (!prefs .getBoolean ("assemblyai" , false ) || TextUtils .isEmpty (prefs .getString ("assemblyai_key" , "" )))
37+ if (!prefs .getBoolean ("audio_transcription" , false ))
38+ return ;
39+
40+ String provider = prefs .getString ("transcription_provider" , "assemblyai" );
41+ String apiKey = "" ;
42+
43+ if ("groq" .equals (provider )) {
44+ apiKey = prefs .getString ("groq_api_key" , "" );
45+ } else {
46+ apiKey = prefs .getString ("assemblyai_key" , "" );
47+ }
48+
49+ if (TextUtils .isEmpty (apiKey ))
3650 return ;
3751
3852 var transcribeMethod = Unobfuscator .loadTranscribeMethod (classLoader );
@@ -46,11 +60,23 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
4660 var fmessageObj = fieldFMessage .get (pttTranscriptionRequest );
4761 var fmessage = new FMessageWpp (fmessageObj );
4862 File file = fmessage .getMediaFile ();
63+ if (file == null ) {
64+ Utils .showToast (Utils .getApplication ().getString (ResId .string .download_not_available ), 1 );
65+ return ;
66+ }
4967 var callback = param .args [1 ];
5068 var onComplete = ReflectionUtils .findMethodUsingFilter (callback .getClass (), method -> method .getParameterCount () == 4 );
5169 if (file == null || !file .exists ())
5270 return ;
53- String transcript = runTranscript (file );
71+
72+ // Choose transcription provider based on user preference
73+ String transcript ;
74+ if ("groq" .equals (provider )) {
75+ transcript = transcriptionGroqAI (file );
76+ } else {
77+ transcript = transcriptionAssemblyAI (file );
78+ }
79+
5480 var segments = new ArrayList <>();
5581 var words = transcript .split ("\\ s" );
5682 var totalLength = 0 ;
@@ -65,7 +91,7 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
6591
6692 }
6793
68- private String runTranscript (File fileOpus ) throws Exception {
94+ private String transcriptionAssemblyAI (File fileOpus ) throws Exception {
6995 String apiKey = prefs .getString ("assemblyai_key" , "" );
7096 if (TextUtils .isEmpty (apiKey )) {
7197 return "API key not provided" ;
@@ -139,6 +165,41 @@ private String runTranscript(File fileOpus) throws Exception {
139165 }
140166 }
141167
168+ private String transcriptionGroqAI (File fileAudio ) throws Exception {
169+ String apiKey = prefs .getString ("groq_api_key" , "" );
170+ if (TextUtils .isEmpty (apiKey )) {
171+ return "Groq API key not provided" ;
172+ }
173+
174+ OkHttpClient client = new OkHttpClient ();
175+
176+ // Groq API accepts direct file upload with multipart/form-data
177+ RequestBody requestBody = new okhttp3 .MultipartBody .Builder ()
178+ .setType (okhttp3 .MultipartBody .FORM )
179+ .addFormDataPart ("file" , fileAudio .getName (),
180+ RequestBody .create (fileAudio , MediaType .parse ("audio/ogg" )))
181+ .addFormDataPart ("model" , "whisper-large-v3-turbo" )
182+ .addFormDataPart ("response_format" , "json" )
183+ .addFormDataPart ("temperature" , "0" )
184+ .build ();
185+
186+ Request transcribeRequest = new Request .Builder ()
187+ .url ("https://api.groq.com/openai/v1/audio/transcriptions" )
188+ .addHeader ("Authorization" , "Bearer " + apiKey )
189+ .post (requestBody )
190+ .build ();
191+
192+ try (okhttp3 .Response response = client .newCall (transcribeRequest ).execute ()) {
193+ if (!response .isSuccessful ()) {
194+ return "Failed to transcribe audio: " + response .code () + " - " + response .message ();
195+ }
196+
197+ JSONObject result = new JSONObject (response .body ().string ());
198+ return result .getString ("text" );
199+ }
200+ }
201+
202+
142203 @ NonNull
143204 @ Override
144205 public String getPluginName () {
0 commit comments