Spaces:
Runtime error
Runtime error
File size: 5,429 Bytes
8ce55a3 | 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 | import * as React from 'react';
import { Text, View, StyleSheet, Button,TouchableHighlight,ActivityIndicator,Alert,Image} from 'react-native';
import { Audio } from 'expo-av';
import axios from 'axios';
import { Feather } from '@expo/vector-icons';
import { EvilIcons } from '@expo/vector-icons';
import { FontAwesome } from '@expo/vector-icons';
export default function App() {
const [recording, setRecording] = React.useState();
const [recognition,setRecognition] = React.useState();
const [caesarson,setCaesarOn] = React.useState(false);
const [recognizing,setRecognizing] = React.useState(false);
async function turncaesaron(){
setRecognizing(true)
const response = await axios.get("https://palondomus-caesarai.hf.space")
if (response.data === "Welcome to CaesarAI's API's and CaesarAINL."){
console.log(response.data)
setCaesarOn(true)
setRecognizing(false)
}
}
const toCapitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
async function startRecording() {
try {
if (caesarson === true){
console.log('Requesting permissions..');
await Audio.requestPermissionsAsync();
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true,
});
console.log('Starting recording..');
const recording = new Audio.Recording();
await recording.prepareToRecordAsync(Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY);
await recording.startAsync();
setRecording(recording);
console.log('Recording started');}
else{
Alert.alert("Turn CaesarON")
}
} catch (err) {
console.error('Failed to start recording', err);
}
}
async function stopRecording() {
// utitlity function to convert BLOB to BASE64
const blobToBase64 = (blob) => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((resolve) => {
reader.onloadend = () => {
resolve(reader.result);
};
});
};
console.log('Stopping recording..');
setRecording(undefined);
await recording.stopAndUnloadAsync();
const uri = recording.getURI();
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
reject(new TypeError("Network request failed"));
};
xhr.responseType = "blob";
xhr.open("GET", uri, true);
xhr.send(null);
});
const audioBase64 = await blobToBase64(blob);
setRecognizing(true)
const response = await axios.post("https://palondomus-caesarai.hf.space/caesarsr",{"audio_data":audioBase64})
//console.log(response.data)
setRecognition(response.data.message)
setRecognizing(false)
//console.log('Recording stopped and stored at', uri);
blob.close()
}
return (
<View
style={[
styles.container,
{
// Try setting `flexDirection` to `"row"`.
flexDirection: 'column',
},
]}>
<View style={{position:"absolute",width:100,height:100}} >
<Image style={{position:"absolute",width:"100%",height:"100%",top:30}} source={require("./CaesarAILogo.png")}></Image>
</View>
<View style={{flex: 1}} >
<View style={{display:"flex",flexDirection:"row",justifyContent:"flex-end",alignItems:"flex-end"}}>
<TouchableHighlight style={{position:"relative",top:30,right:20}} onPress={turncaesaron}>
<Feather name="power" size={34} style={{color:caesarson === false ? "red":"blue"}}/>
</TouchableHighlight>
<TouchableHighlight style={{position:"relative",top:30,right:0}} >
<EvilIcons name="navicon" size={44} color="red" />
</TouchableHighlight>
</View>
</View>
<View style={{flex: 2}} >
<View style={{display:"flex",flexDirection:"row",justifyContent:"center",alignItems:"center"}}>
<View style={{position:"relative",top:30}}>
{ recognition !== undefined &&
<Text style={{fontSize:30}}>{toCapitalize(recognition+".")}</Text>
}
{recognizing === true && <ActivityIndicator style={{marginTop:30}} size={55} /> }
</View>
</View>
</View>
<View style={{flex: 3}} >
<View style={{display:"flex",flexDirection:"row",justifyContent:"center",alignItems:"center"}}>
<TouchableHighlight style={{position:"relative",top:70}} onPress={recording ? stopRecording : startRecording}>
<FontAwesome name="microphone" size={94} style={{color:recording ? 'blue' : 'black'}} />
</TouchableHighlight>
</View>
</View>
</View>
);
}
/* <Button
title={recording ? 'Stop Recording' : 'Start Recording'}
onPress={recording ? stopRecording : startRecording}
/> */
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
},
});
/*
<View style={{"flex":""}}>
<Text>{recognition}</Text>
<TouchableHighlight onPress={turncaesaron}>
<Feather name="power" size={24} style={{color:caesarson === false ? "red":"blue"}}/>
</TouchableHighlight>
<Button
title={recording ? 'Stop Recording' : 'Start Recording'}
onPress={recording ? stopRecording : startRecording}
/>
</View> */ |