encountered weird network error.

This commit is contained in:
Jordan
2025-03-17 06:56:05 -07:00
parent e61fb43ee3
commit 0ba5c4b309
7 changed files with 40 additions and 557 deletions

View File

@ -15,10 +15,13 @@ import {
LanguageServer,
language_matrix_entry,
} from "@/app/i18n/api";
import { Settings } from "@/app/lib/settings";
import { WHISPER_FILES } from "@/app/lib/whisper";
import {
WHISPER_MODEL_SMALL_PATH,
whisperModelExists,
} from "@/app/lib/whisper";
import { initWhisper, WhisperContext } from "whisper.rn";
import { useAudioRecorder, AudioModule, RecordingPresets } from "expo-audio";
import FileSystem from "expo-file-system";
const lasOptions = {
sampleRate: 32000, // default is 44100 but 32000 is adequate for accurate voice recognition
@ -97,22 +100,27 @@ const ConversationThread = ({
console.log("Set cached translator from %s", languageServer.baseUrl);
setCachedTranslator(cachedTranslator);
const settings = await Settings.getDefault();
const whisperFileLabel = (await settings.getWhisperModel()) || "small";
const whisperFile = WHISPER_FILES[whisperFileLabel];
if (!whisperFile) {
throw new Error(`Could not find the whisper file with the label ${whisperFileLabel}`);
try {
if (!(await whisperModelExists())) {
throw new Error(`${WHISPER_MODEL_SMALL_PATH} does not exist`);
}
} catch (err) {
console.error(
`Could not determine if %s exists: %s`,
WHISPER_MODEL_SMALL_PATH,
err
);
throw err;
}
try {
setWhisperContext(
await initWhisper({
filePath: whisperFile.targetPath,
filePath: WHISPER_MODEL_SMALL_PATH,
})
);
} catch (err) {
console.error(err)
console.error(err);
throw err;
}

View File

@ -36,7 +36,7 @@ export function LanguageSelection(props: {
// Replace with your actual async data fetching logic
setTranslator(await CachedTranslator.getDefault());
const languageServer = await LanguageServer.getDefault();
const languages = await languageServer.fetchLanguages(5000);
const languages = await languageServer.fetchLanguages(10000);
setLanguages(languages);
setLanguagesLoaded(true);
} catch (error) {

View File

@ -1,23 +1,11 @@
import React, { useState, useEffect } from "react";
import { View, Text, TextInput, Pressable, StyleSheet } from "react-native";
import {
WHISPER_FILES,
WhisperFile,
download_status_t,
whisper_tag_t,
} from "@/app/lib/whisper";
import { View, Text, TextInput, StyleSheet } from "react-native";
import { Settings } from "@/app/lib/settings";
import { Picker } from "@react-native-picker/picker";
import {
LanguageServer,
language_matrix,
language_matrix_entry,
} from "@/app/i18n/api";
const WHISPER_MODELS = {
small: new WhisperFile("small"),
medium: new WhisperFile("medium"),
large: new WhisperFile("large"),
};
const LIBRETRANSLATE_BASE_URL = "https://translate.argosopentech.com/translate";
@ -33,15 +21,6 @@ const SettingsComponent = () => {
success: boolean;
error?: string;
} | null>(null);
const [whisperModel, setWhisperModel] =
useState<keyof typeof WHISPER_MODELS>("small");
const [whisperFile, setWhisperFile] = useState<WhisperFile | undefined>();
const [downloader, setDownloader] = useState<any>(null);
const [bytesDone, setBytesDone] = useState<number | undefined>();
const [bytesRemaining, setBytesRemaining] = useState<number | undefined>();
const [statusTimeout, setStatusTimeout] = useState<
NodeJS.Timeout | undefined
>();
useEffect(() => {
(async function () {
@ -50,22 +29,9 @@ const SettingsComponent = () => {
setLibretranslateBaseUrl(
(await settings.getLibretranslateBaseUrl()) || LIBRETRANSLATE_BASE_URL
);
setWhisperModel((await settings.getWhisperModel()) || "small");
setWhisperFile(WHISPER_FILES[whisperModel]);
if (!whisperFile) {
throw new Error("Invalid Whisper file!");
}
await whisperFile.syncHfMetadata();
await whisperFile.updateTargetExistence();
await whisperFile.updateTargetHash();
console.log("Does %s exist? part=%s, target=%s", whisperFile.label, whisperFile.does_part_target_exist, whisperFile.does_target_exist)
})();
}, [whisperFile]);
});
const getLanguageOptions = async () => {
const languageServer = await LanguageServer.getDefault();
setLanguageOptions(await languageServer.fetchLanguages());
};
const handleHostLanguageChange = async (lang: string) => {
const settings = await Settings.getDefault();
@ -83,71 +49,17 @@ const SettingsComponent = () => {
const checkLangServerConnection = async (baseUrl: string) => {
try {
// Replace with actual connection check logic
setLangServerConn({ success: true });
const testResult = await fetch(baseUrl, {
method: "HEAD",
});
if (testResult.status !== 200) {
setLangServerConn({ success: true, error: testResult.statusText });
}
} catch (error) {
setLangServerConn({ success: false, error: `${error}` });
}
};
const handleWhisperModelChange = async (model: whisper_tag_t) => {
const settings = await Settings.getDefault();
await settings.setWhisperModel(model);
setWhisperModel(model);
const wFile = WHISPER_FILES[whisperModel];
await wFile.syncHfMetadata();
await wFile.updateTargetExistence();
// await wFile.updateTargetHash();
// setIsWhisperHashValid(wFile.isHashValid);
setWhisperFile(wFile);
};
const doSetDownloadStatus = (arg0: WhisperFile) => {
// console.log("Downloading ....");
setBytesDone(arg0.download_data?.totalBytesWritten);
setBytesRemaining(arg0.download_data?.totalBytesExpectedToWrite);
};
const doOnComplete = async (arg0: WhisperFile) => {
console.log("✅ Download complete.");
setDownloader(undefined);
await arg0.updateTargetExistence();
setWhisperFile(arg0);
await whisperFile?.updateTargetExistence();
};
const doDownload = async () => {
if (!whisperModel) {
throw new Error("Could not start download because whisperModel not set.");
}
console.log("Starting download of %s", whisperModel);
if (!whisperFile) throw new Error("No whisper file");
try {
const resumable = await whisperFile.createDownloadResumable({
onData: doSetDownloadStatus,
onComplete: doOnComplete,
});
setDownloader(resumable);
if (!resumable) throw new Error("Could not construct resumable");
await resumable.resumeAsync();
} catch (error) {
console.error("Failed to download whisper model:", error);
}
};
const doStopDownload = async () => {
downloader.cancelAsync();
setDownloader(null);
};
const doDelete = async () => {
const whisperFile = WHISPER_MODELS[whisperModel];
whisperFile.delete();
await whisperFile.updateTargetExistence();
};
return hostLanguage && libretranslateBaseUrl ? (
<View style={styles.container}>
<Text style={styles.label}>Host Language:</Text>
@ -180,98 +92,6 @@ const SettingsComponent = () => {
Error connecting to {libretranslateBaseUrl}: {langServerConn.error}
</Text>
))}
<Picker
selectedValue={whisperModel}
style={{ height: 50, width: "100%" }}
onValueChange={handleWhisperModelChange}
accessibilityHint="whisper models"
>
{Object.entries(WHISPER_MODELS).map(([key, whisperFile]) => (
<Picker.Item
key={whisperFile.tag}
label={whisperFile.label}
value={key}
/>
))}
</Picker>
<View style={styles.downloadButtonWrapper}>
{/* The target is completely downloaded */}
{!downloader && whisperFile?.does_target_exist && (
<Pressable onPress={doDelete} style={styles.deleteButton}>
<Text style={styles.buttonText}>
DELETE {whisperModel.toUpperCase()}
</Text>
</Pressable>
)}
{/* The target "part" is present and is downloading */}
{downloader && whisperFile?.does_part_target_exist && (
<Pressable
onPress={doStopDownload}
style={styles.pauseDownloadButton}
>
<Text style={styles.buttonText}>STOP DOWNLOAD</Text>
</Pressable>
)}
{/* The target "part" is present and we are NOT downloading */}
{!downloader && whisperFile?.does_part_target_exist && (
<>
<Pressable onPress={doDownload} style={styles.downloadButton}>
<Text style={styles.buttonText}>RESUME DOWNLOAD</Text>
</Pressable>
<Pressable onPress={doDelete} style={styles.deleteButton}>
<Text style={styles.buttonText}>
DELETE {whisperModel.toUpperCase()}
</Text>
</Pressable>
</>
)}
{/* Anything else -- usually if the file has not yet been downloaded */}
{!(
downloader ||
whisperFile?.does_target_exist ||
whisperFile?.does_part_target_exist
) && (
<Pressable onPress={doDownload} style={styles.downloadButton}>
<Text style={styles.buttonText}>
DOWNLOAD {whisperModel.toUpperCase()}
</Text>
</Pressable>
)}
{downloader &&
bytesDone &&
bytesRemaining &&
whisperFile?.does_part_target_exist && (
<View>
{whisperFile && (
<Text>Downloading to {whisperFile.targetPath}</Text>
)}
<Text>
{bytesDone} of {bytesRemaining} (
{(bytesDone / bytesRemaining) * 100} %){" "}
</Text>
</View>
)}
</View>
<View>
<Text>Debug Panel</Text>
<Text>downloader is null? {downloader ? "no" : "yes"}</Text>
<Text>
whisperFile.does_target_exist{" "}
{whisperFile?.does_target_exist ? "yes" : "no"}
</Text>
<Text>
whisperFile.does_part_target_exist{" "}
{whisperFile?.does_part_target_exist ? "yes" : "no"}
</Text>
</View>
</View>
) : (
<View>