Skip to content

Commit

Permalink
added usage calculation for Whisper
Browse files Browse the repository at this point in the history
  • Loading branch information
DevEmperor committed Oct 3, 2024
1 parent 93344ed commit 652b5de
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,12 @@ private void query(String query) throws JSONException, IOException {
fc.setCustomKey("settings", sp.getAll().toString());
fc.setUserId(sp.getString("net.devemperor.wristassist.userid", "null"));
fc.recordException(e);
fc.sendUnsentReports();

runOnUiThread(() -> {
if (sp.getBoolean("net.devemperor.wristassist.vibrate", true)) {
vibrator.vibrate(VibrationEffect.createWaveform(new long[]{50, 50, 50, 50, 50}, new int[]{-1, 0, -1, 0, -1}, -1));
}

e.printStackTrace();
if (Objects.requireNonNull(e.getMessage()).contains("SocketTimeoutException")) {
errorTv.setText(R.string.wristassist_timeout);
} else if (e.getMessage().contains("API key")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ public void run() {
fc.setCustomKey("settings", sp.getAll().toString());
fc.setUserId(sp.getString("net.devemperor.wristassist.userid", "null"));
fc.recordException(e);
fc.sendUnsentReports();

e.printStackTrace();
runOnUiThread(() -> {
imagePb.setVisibility(View.GONE);
errorTv.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -260,9 +258,7 @@ public void saveImage(View view) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException ignored) { }
timer.cancel();

Intent data = new Intent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.theokanning.openai.service.OpenAiService;

import net.devemperor.wristassist.R;
import net.devemperor.wristassist.database.UsageDatabaseHelper;
import net.devemperor.wristassist.util.WristAssistUtil;

import java.io.File;
import java.io.IOException;
Expand All @@ -55,6 +57,7 @@ public class InputWhisperActivity extends AppCompatActivity {
ImageView errorIv2;

SharedPreferences sp;
UsageDatabaseHelper usageDatabaseHelper;
MediaRecorder recorder;
Runnable recordTimeRunnable;
Handler recordTimeHandler;
Expand All @@ -76,6 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
errorIv2 = findViewById(R.id.activity_input_whisper_error_iv2);

sp = getSharedPreferences("net.devemperor.wristassist", MODE_PRIVATE);
usageDatabaseHelper = new UsageDatabaseHelper(this);

recordTimeHandler = new Handler(Looper.getMainLooper());
recordTimeRunnable = new Runnable() {
Expand Down Expand Up @@ -221,7 +225,7 @@ private void startWhisperApiRequest() {
.build();
TranscriptionResult result = service.createTranscription(request, new File(getCacheDir(), "whisper_input_audio.mp3"));

// TODO: add usage to db
usageDatabaseHelper.edit("whisper-1", Math.round(result.getDuration()), WristAssistUtil.calcCostWhisper(result.getDuration()));

Intent data = new Intent();
data.putExtra("net.devemperor.wristassist.input.content", result.getText());
Expand All @@ -234,7 +238,6 @@ private void startWhisperApiRequest() {
fc.setCustomKey("settings", sp.getAll().toString());
fc.setUserId(sp.getString("net.devemperor.wristassist.userid", "null"));
fc.recordException(e);
fc.sendUnsentReports();

showError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public View getView (int position, View convertView, @NonNull ViewGroup parent)
if (dataProvider.getModelName().startsWith("gpt")) {
tokensTv.setText(context.getString(R.string.wristassist_token_usage,
String.format(Locale.getDefault(), "%,d", dataProvider.getTokens())));
} else {
} else if (dataProvider.getModelName().startsWith("dall-e")) {
tokensTv.setText(context.getString(R.string.wristassist_images_count,
String.format(Locale.getDefault(), "%,d", dataProvider.getTokens())));
} else if (dataProvider.getModelName().startsWith("whisper")) {
tokensTv.setText(context.getString(R.string.wristassist_whisper_count,
dataProvider.getTokens() / 60, dataProvider.getTokens() % 60));
}

TextView costTv = listItem.findViewById(R.id.item_usage_cost_tv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public static double calcCostImage(String model, String quality, String size) {
return 0;
}

public static double calcCostWhisper(double duration) {
return Math.round(duration) * 0.0001;
}

public static String translate(Context context, String origin) {
switch (origin) {
case "gpt-4o-mini":
Expand All @@ -91,6 +95,8 @@ public static String translate(Context context, String origin) {
return "DALL-E 3";
case "dall-e-2":
return "DALL-E 2";
case "whisper-1":
return "Whisper";
case "hd":
return "HD";
case "standard":
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@
<string name="wristassist_define_global_system_prompt">Lege eine globale System Anweisung fest</string>
<string name="changelog_md_30">### Version 3.1.0 \n#### Globale System Anweisungen \nDu kannst jetzt in den Einstellungen eine globale Systemrolle festlegen, die die KI in jedem Chat verwenden wird. \n</string>
<string name="changelog_md_31">### Version 3.1.1 \n#### Neues GPT-4 Omni Mini Modell \nOpenAI hat ihr neues Modell GPT-4o mini veröffentlicht, das ihr kostengünstigstes kleines Modell ist – intelligenter und billiger als GPT-3.5 Turbo und hat auch Vision-Fähigkeiten. Das Modell hat einen Kontext von 128K und eine Wissensgrenze bis Oktober 2023. Du kannst in den Einstellungen zu diesem Modell wechseln, das 60% günstiger ist als GPT-3.5 Turbo. \n</string>
<string name="wristassist_whisper_count" formatted="false">Transkriptionszeit:\n%1$d min, %2$d s</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@
<string name="wristassist_define_global_system_prompt">Define global system prompt</string>
<string name="changelog_md_30">### Version 3.1.0 \n#### Global system prompts \nYou can now set a global system role in the settings, that the AI will use in every chat. \n</string>
<string name="changelog_md_31">### Version 3.1.1 \n#### New GPT-4 Omni mini model \nOpenAI released their new model GPT-4o mini, which is their most cost-efficient small model that\'s smarter and cheaper than GPT-3.5 Turbo, and has vision capabilities. The model has 128K context and an October 2023 knowledge cutoff. You can switch to this model, which is 60% cheaper than GPT-3.5 Turbo, in the settings. \n</string>
<string name="wristassist_whisper_count" formatted="false">Transcription time:\n%1$d min, %2$d s</string>
</resources>

0 comments on commit 652b5de

Please sign in to comment.