Commit de86a677 authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

tts part

parent b733b112
import React, { createContext, useContext, useState, useEffect, useLayoutEffect } from "react";
import { nexlAxios } from "../../constants/helper";
import AsyncStorage from "@react-native-community/async-storage";
import {
IAuthContext,
IAuthRequest,
STORAGE_BEARER_TOKEN,
UNINITIALISED,
USER_EMAIL,
} from "./IAuthContext";
import { Loading } from "./state/Loading";
import { getEnvironment } from "../../constants/variables";
import { AxiosError, AxiosResponse } from "axios";
import { rollbar } from "./ErrorBoundary";
const AuthContext = createContext({
isLoggedIn: false,
token: "",
email: "",
error: "",
loading: false,
initialised: false,
login: UNINITIALISED,
logout: UNINITIALISED,
});
export const useAuthContext = () => useContext(AuthContext);
export const AuthConsumer = AuthContext.Consumer;
// interface IProps {
// children: any;
// }
// interface ILoginData {
// error: string;
// user: {
// email: string;
// id: string;
// first_name: string;
// last_name: string;
// };
// }
const endpoint = getEnvironment().endpoint;
export const AuthProvider= ({ children }) => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [token, setToken] = useState("");
const [email, setEmail] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [initialised, setInitialised] = useState(false);
const login = async ({ email, password, rememberMe }) => {
setLoading(true);
nexlAxios(false)
.post(`${endpoint}/accounts/sign_in.json`, {
source: "iOS app",
account: {
email,
password,
remember_me: rememberMe ? "1" : "0",
},
})
.then(async (response) => {
setLoading(false);
const user = response.data.user;
user.email && setIsLoggedIn(true);
const token = response.headers["authorization"];
setToken(token);
await AsyncStorage.setItem(USER_EMAIL, email);
setEmail(email);
rollbar.setPerson(user.id, `${user.first_name} ${user.last_name}`, user.email);
})
.catch((result) => {
setLoading(false);
setError(
result?.response?.data?.error || result?.message || "An unknown error has occurred.",
);
});
};
const loginWithBearerToken = async (bearer) => {
if (bearer) {
setLoading(true);
await nexlAxios(false, bearer)
.post(`${endpoint}/accounts/sign_in.json`, undefined, {
headers: {
Authorization: bearer,
},
})
.then((response) => {
setLoading(false);
const user = response.data.user;
user.email && setIsLoggedIn(true);
rollbar.setPerson(user.id, `${user.first_name} ${user.last_name}`, user.email);
})
.catch(() => {
setLoading(false);
setToken("");
});
}
};
const logout = async () => {
const token = await AsyncStorage.getItem(STORAGE_BEARER_TOKEN);
token && (await nexlAxios(false, token).post(`${endpoint}/sign_out.json`));
setToken("");
setIsLoggedIn(false);
await AsyncStorage.removeItem(STORAGE_BEARER_TOKEN);
rollbar.clearPerson();
};
// look if token / email exist in local storage on initialising the app
// if token exists, try log in with token
useLayoutEffect(() => {
let isMounted = true;
const initialise = async () => {
const bearerToken = await AsyncStorage.getItem(STORAGE_BEARER_TOKEN);
setToken(bearerToken || "");
const userEmail = await AsyncStorage.getItem(USER_EMAIL);
setEmail(userEmail || "");
bearerToken && (await loginWithBearerToken(bearerToken));
setInitialised(true);
};
if (isMounted) {
initialise();
}
return () => {
isMounted = false;
};
}, []);
// update token to local storage on token change
useEffect(() => {
AsyncStorage.setItem(STORAGE_BEARER_TOKEN, token);
}, [token]);
return (
<AuthContext.Provider
value={{
login,
isLoggedIn,
logout,
token,
email: email.toLowerCase(),
loading,
error,
initialised,
}}
>
{initialised ? children : <Loading copy="Logging in..." />}
</AuthContext.Provider>
);
};
\ No newline at end of file
import React from 'react'
import { Text, View, StyleSheet, Button } from 'react-native';
import { Audio } from 'expo-av';
import { createIconSetFromFontello } from '@expo/vector-icons';
import * as FileSystem from 'expo-file-system';
export const AudioRecorder = ({ setDetectedText }) => {
const [recording, setRecording] = React.useState();
const [sound, setSound] = React.useState();
async function startRecording() {
try {
console.log('Requesting permissions..');
await Audio.requestPermissionsAsync();
await Audio.setAudioModeAsync({
allowsRecordingIOS: true,
playsInSilentModeIOS: true,
});
console.log('Starting recording..');
const { recording } = await Audio.Recording.createAsync(
Audio.RECORDING_OPTIONS_PRESET_HIGH_QUALITY
);
setRecording(recording);
console.log('Recording started');
} catch (err) {
console.error('Failed to start recording', err);
}
}
async function stopRecording() {
console.log('Stopping recording..');
setRecording(undefined);
await recording.stopAndUnloadAsync();
const uri = recording.getURI();
console.log('Recording stopped and stored at', uri);
console.log(recording)
const headers = {
'Content-Type': 'multipart/form-data'
// 'Content-Type': 'application/x-www-form-urlencoded',
}
playSound(uri)
FileSystem.uploadAsync("https://1ec4-112-134-210-155.ap.ngrok.io/mlmodels/detect/", uri, { headers: headers, uploadType: FileSystem.FileSystemUploadType.MULTIPART })
.then(data => JSON.parse(data.body))
.then(data => { console.log({ result: data }); setDetectedText(data.result); setTimeout(() => setDetectedText(''), 1000) })
.catch(err => console.log({ err }))
}
async function playSound(uri) {
console.log('Loading Sound');
// const { sound } = await Audio.Sound.createAsync(
// require(uri)
// );
// setSound(sound);
// console.log('Playing Sound');
// await sound.playAsync();
}
return (
<Button
style={{ height: '100%', padding: 5 }}
title={recording ? 'Stop Recording' : 'Start Recording'}
onPress={recording ? stopRecording : startRecording}
/>
);
}
\ No newline at end of file
...@@ -12,6 +12,7 @@ import { ColorSchemeName, Pressable } from 'react-native'; ...@@ -12,6 +12,7 @@ import { ColorSchemeName, Pressable } from 'react-native';
import Colors from '../constants/Colors'; import Colors from '../constants/Colors';
import useColorScheme from '../hooks/useColorScheme'; import useColorScheme from '../hooks/useColorScheme';
import { LoginScreen } from '../screens/loginScreen';
import ModalScreen from '../screens/ModalScreen'; import ModalScreen from '../screens/ModalScreen';
import NotFoundScreen from '../screens/NotFoundScreen'; import NotFoundScreen from '../screens/NotFoundScreen';
import TabOneScreen from '../screens/TabOneScreen'; import TabOneScreen from '../screens/TabOneScreen';
...@@ -36,10 +37,28 @@ export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeNa ...@@ -36,10 +37,28 @@ export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeNa
const Stack = createNativeStackNavigator<RootStackParamList>(); const Stack = createNativeStackNavigator<RootStackParamList>();
function RootNavigator() { function RootNavigator() {
const [isAuthenticated, setIsAutheticated] = React.useState(false)
const onLogin = () => {
setIsAutheticated(true)
}
const onLogout = () => {
setIsAutheticated(false)
}
return ( return (
<Stack.Navigator> <Stack.Navigator >
<Stack.Screen name="Root" component={BottomTabNavigator} options={{ headerShown: false }} /> {isAuthenticated ? <Stack.Screen name="Root" options={{ headerShown: false }}>{(props) => <BottomTabNavigator {...props} onLogout={onLogout}/>}</Stack.Screen> :
<Stack.Screen name='Login'>{(props) => <LoginScreen {...props} onLogin={onLogin} />}</Stack.Screen>}
<Stack.Screen name="NotFound" component={NotFoundScreen} options={{ title: 'Oops!' }} /> <Stack.Screen name="NotFound" component={NotFoundScreen} options={{ title: 'Oops!' }} />
<Stack.Group screenOptions={{ presentation: 'modal' }}> <Stack.Group screenOptions={{ presentation: 'modal' }}>
<Stack.Screen name="Modal" component={ModalScreen} /> <Stack.Screen name="Modal" component={ModalScreen} />
</Stack.Group> </Stack.Group>
...@@ -53,7 +72,7 @@ function RootNavigator() { ...@@ -53,7 +72,7 @@ function RootNavigator() {
*/ */
const BottomTab = createBottomTabNavigator<RootTabParamList>(); const BottomTab = createBottomTabNavigator<RootTabParamList>();
function BottomTabNavigator() { function BottomTabNavigator({onLogout}) {
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
return ( return (
...@@ -66,11 +85,12 @@ function BottomTabNavigator() { ...@@ -66,11 +85,12 @@ function BottomTabNavigator() {
name="TabOne" name="TabOne"
component={TabOneScreen} component={TabOneScreen}
options={({ navigation }: RootTabScreenProps<'TabOne'>) => ({ options={({ navigation }: RootTabScreenProps<'TabOne'>) => ({
title: 'Tab One', title: 'Chat',
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />, tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
headerRight: () => ( headerRight: () => (
<Pressable <Pressable
onPress={() => navigation.navigate('Modal')} // onPress={() => navigation.navigate('Modal')}
onPress={onLogout}
style={({ pressed }) => ({ style={({ pressed }) => ({
opacity: pressed ? 0.5 : 1, opacity: pressed ? 0.5 : 1,
})}> })}>
......
...@@ -18,14 +18,19 @@ ...@@ -18,14 +18,19 @@
"@react-navigation/bottom-tabs": "^6.0.5", "@react-navigation/bottom-tabs": "^6.0.5",
"@react-navigation/native": "^6.0.2", "@react-navigation/native": "^6.0.2",
"@react-navigation/native-stack": "^6.1.0", "@react-navigation/native-stack": "^6.1.0",
"axios": "^0.27.2",
"expo": "~44.0.0", "expo": "~44.0.0",
"expo-asset": "~8.4.4", "expo-asset": "~8.4.4",
"expo-av": "~10.2.0",
"expo-constants": "~13.0.0", "expo-constants": "~13.0.0",
"expo-file-system": "~13.1.4",
"expo-font": "~10.0.4", "expo-font": "~10.0.4",
"expo-linking": "~3.0.0", "expo-linking": "~3.0.0",
"expo-media-library": "~14.0.0",
"expo-splash-screen": "~0.14.0", "expo-splash-screen": "~0.14.0",
"expo-status-bar": "~1.2.0", "expo-status-bar": "~1.2.0",
"expo-web-browser": "~10.1.0", "expo-web-browser": "~10.1.0",
"jwt-decode": "^3.1.2",
"react": "17.0.1", "react": "17.0.1",
"react-dom": "17.0.1", "react-dom": "17.0.1",
"react-native": "0.64.3", "react-native": "0.64.3",
...@@ -37,8 +42,8 @@ ...@@ -37,8 +42,8 @@
"@babel/core": "^7.12.9", "@babel/core": "^7.12.9",
"@types/react": "~17.0.21", "@types/react": "~17.0.21",
"@types/react-native": "~0.64.12", "@types/react-native": "~0.64.12",
"jest-expo": "~44.0.1",
"jest": "^26.6.3", "jest": "^26.6.3",
"jest-expo": "~44.0.1",
"react-test-renderer": "17.0.1", "react-test-renderer": "17.0.1",
"typescript": "~4.3.5" "typescript": "~4.3.5"
}, },
......
import React, { useState } from 'react'
import { StyleSheet, TextInput } from 'react-native'; import { StyleSheet, TextInput } from 'react-native';
import { AudioRecorder } from '../components/AudioRecorder';
import EditScreenInfo from '../components/EditScreenInfo'; import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed'; import { Text, View } from '../components/Themed';
import { RootTabScreenProps } from '../types'; import { RootTabScreenProps } from '../types';
export default function TabOneScreen({ navigation }: RootTabScreenProps<'TabOne'>) { export default function TabOneScreen({ navigation }: RootTabScreenProps<'TabOne'>) {
const [detectedText, setDetectedText] = useState("")
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.title}>Tab One</Text> {/* <Text style={styles.title}>Tab One</Text>
<View style={{height: 70, width: 100, backgroundColor: ''}}><TextInput style={{height: 100, width: 1000, }} onChange={() => console.log('change')}></TextInput></View> <View style={{height: 70, width: 100, backgroundColor: ''}}><TextInput style={{borderColor: 'white', borderWidth: 1, alignSelf: }} onChange={() => console.log('change')}></TextInput></View>
<View style={styles.separator} lightColor="#000" darkColor="rgba(255,0,255,0.1)" /> <View style={styles.separator} lightColor="#000" darkColor="rgba(255,0,255,0.1)" />
<EditScreenInfo path="/screens/TabOneScreen.tsx" /> <EditScreenInfo path="/screens/TabOneScreen.tsx" /> */}
<View style={{ flex: 1, width: '100%' }}>
<View style={{ display: 'flex', flex: 0.9, backgroundColor: 'green' }}>
<View style={{ alignItems: 'center', justifyContent: 'center', height: '100%'}}><Text style={{fontSize: 50, color: 'white'}}>{detectedText}</Text></View>
</View>
<View style={{ flex: 0.1, backgroundColor: 'rgb(10,10,10)'}}>
<View style={{ flexDirection: 'row', display: 'flex', height: '100%' }}>
<View style={{ flex: 0.8, backgroundColor: 'rgb(10,10,10)', height: '100%' }}></View>
<View style={{ flex: 0.2, backgroundColor: 'rgb(10,10,10)', height:'100%'}}><AudioRecorder setDetectedText={setDetectedText} /></View>
</View>
</View>
</View>
</View> </View>
); );
} }
......
...@@ -6,9 +6,7 @@ import { Text, View } from '../components/Themed'; ...@@ -6,9 +6,7 @@ import { Text, View } from '../components/Themed';
export default function TabTwoScreen() { export default function TabTwoScreen() {
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.title}>Tab Two</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<EditScreenInfo path="/screens/TabTwoScreen.tsx" />
</View> </View>
); );
} }
......
import React from 'react'
import { StyleSheet, TextInput} from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { TouchableOpacity } from 'react-native';
const LoginForm = ({onLogin}) => {
var passwordInput
return (
<>
<TextInput style={styles.input}
autoCapitalize="none"
// onSubmitEditing={() => passwordInput.focus()}
autoCorrect={false}
keyboardType='email-address'
returnKeyType="next"
placeholder='Email'
placeholderTextColor='rgba(225,225,225,0.7)' />
<TextInput style={styles.input}
returnKeyType="go"
ref={(input) => passwordInput = input}
placeholder='Password'
placeholderTextColor='rgba(225,225,225,0.7)'
secureTextEntry />
<TouchableOpacity style={styles.buttonContainer}
// onPress={onButtonPress}
onPress={onLogin}
>
<Text style={styles.buttonText}>LOGIN</Text>
</TouchableOpacity>
</>
// define your styles
)
}
export const LoginScreen = ({onLogin}) => {
return (
<View style={styles.container}>
<View style={styles.loginContainer}>
{/* r<Image resizeMode="contain" style={styles.logo} source={require('../../components/images/logo-dark-bg.png')} /> */}
</View>
<View style={styles.formContainer}>
<LoginForm onLogin={onLogin}/>
</View>
</View>
);
}
// define your styles
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#2c3e50',
},
loginContainer: {
alignItems: 'center',
flexGrow: 1,
justifyContent: 'center'
},
logo: {
position: 'absolute',
width: 300,
height: 100
},
container: {
padding: 20
},
input: {
height: 40,
backgroundColor: 'rgba(225,225,225,0.2)',
marginBottom: 10,
padding: 10,
color: '#fff'
},
buttonContainer: {
backgroundColor: '#2980b6',
paddingVertical: 15
},
buttonText: {
color: '#fff',
textAlign: 'center',
fontWeight: '700'
}
})
\ No newline at end of file
...@@ -16,6 +16,7 @@ declare global { ...@@ -16,6 +16,7 @@ declare global {
export type RootStackParamList = { export type RootStackParamList = {
Root: NavigatorScreenParams<RootTabParamList> | undefined; Root: NavigatorScreenParams<RootTabParamList> | undefined;
Modal: undefined; Modal: undefined;
Login: undefined;
NotFound: undefined; NotFound: undefined;
}; };
......
This diff is collapsed.
Main objective:- Is to ensure that the passengers who are using public staff transport (Bus) are safe with covid and they are given the service according the government health regulations. Main objective:- Is to ensure that the passengers who are using public staff transport (Bus) are safe with covid and they are given the service according the government health regulations.
Main Research questions: How to deliver a safe public transport during the pandemic? Main Research questions: How to deliver a safe public transport during the pandemic?
How to Ensure Passenger's health is safe? How to Ensure Passenger's health is safe?
How to build trust and motivate passengers to use Public Transport? How to build trust and motivate passengers to use Public Transport?
Individual research question Individual research question
IT18145380 - How to select the perfect seat based on passenger attributes? IT18145380 - How to select the perfect seat based on passenger attributes?
IT19012384 - How to manage real time tracking with the real time data? IT19012384 - How to manage real time tracking with the real time data?
How to do the payments through QR code with the public transportation? How to do the payments through QR code with the public transportation?
IT18094664 - Interfaces to perform in-vehicle tasks while driving? IT18094664 - Interfaces to perform in-vehicle tasks while driving?
voice or manual interfaces? voice or manual interfaces?
IT19153728 - How to confirm the arrival of the relevant passenger and calculate the monthly fare based on the arrival of the passenger? IT19153728 - How to confirm the arrival of the relevant passenger and calculate the monthly fare based on the arrival of the passenger?
Individual Objectives, Individual Objectives,
IT18145380 - Provide a comfortable ride for the passenger IT18145380 - Provide a comfortable ride for the passenger
IT19012384 - To be able to use the time effectively with the help of route tracking. IT19012384 - To be able to use the time effectively with the help of route tracking.
To decrease the use of paper and cash usage by using QR payment method. To decrease the use of paper and cash usage by using QR payment method.
To facilitate daily activities in transportation. To facilitate daily activities in transportation.
IT18094664 - The main objective of this proposed research is to develop an application for the prevention of driver-related accidents and to make driver and passenger communication more efficient and convenient. IT18094664 - The main objective of this proposed research is to develop an application for the prevention of driver-related accidents and to make driver and passenger communication more efficient and convenient.
IT19153728 - The main objective of this research is marking the passenger's arrival and identify the passenger. IT19153728 - The main objective of this research is marking the passenger's arrival and identify the passenger.
backend/data/ backend/data/
*.pyc *.pyc
\ No newline at end of file
""" """
ASGI config for backend project. ASGI config for backend project.
It exposes the ASGI callable as a module-level variable named ``application``. It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/ https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
""" """
import os import os
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
application = get_asgi_application() application = get_asgi_application()
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from django.apps import AppConfig from django.apps import AppConfig
class CmsConfig(AppConfig): class CmsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
name = 'backend.cms' name = 'backend.cms'
# Generated by Django 4.0.4 on 2022-04-25 19:51 # Generated by Django 4.0.4 on 2022-04-25 19:51
from django.db import migrations, models from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
initial = True initial = True
dependencies = [ dependencies = [
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='MlModel', name='MlModel',
fields=[ fields=[
('id', models.AutoField(primary_key=True, serialize=False)), ('id', models.AutoField(primary_key=True, serialize=False)),
('description', models.TextField(blank=True)), ('description', models.TextField(blank=True)),
('timestamp', models.DateTimeField(auto_now=True)), ('timestamp', models.DateTimeField(auto_now=True)),
('details', models.JSONField()), ('details', models.JSONField()),
], ],
), ),
] ]
import pickle import pickle
from keras.models import load_model from keras.models import load_model
import numpy as np import numpy as np
import IPython.display as ipd import IPython.display as ipd
import random import random
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import LabelEncoder
def predict(): def predict(samples):
model=load_model(r'./best_model_final.hdf5') model=load_model(r'./best_model_final.hdf5')
f1 = open('all_label.txt', 'rb') f1 = open('all_label.txt', 'rb')
all_label = pickle.load(f1) all_label = pickle.load(f1)
print('loaded labels') print('loaded labels')
f2 = open('all_waves_file.txt', 'rb') f2 = open('all_waves_file.txt', 'rb')
all_wave = pickle.load(f2) all_wave = pickle.load(f2)
print('loaded waves') print('loaded waves')
le = LabelEncoder() le = LabelEncoder()
y = le.fit_transform(all_label) y = le.fit_transform(all_label)
classes = list(le.classes_) classes = list(le.classes_)
train_data_file = open("train_data_file.txt", 'rb') train_data_file = open("train_data_file.txt", 'rb')
[x_tr, x_val, y_tr, y_val] = np.load(train_data_file, allow_pickle=True) [x_tr, x_val, y_tr, y_val] = np.load(train_data_file, allow_pickle=True)
train_data_file.close() train_data_file.close()
def predictSamples(audio): def predictSamples(audio):
prob=model.predict(audio.reshape(1,8000,1)) prob=model.predict(audio.reshape(1,8000,1))
index=np.argmax(prob[0]) index=np.argmax(prob[0])
return classes[index] return classes[index]
index=random.randint(0,len(x_val)-1) # index=random.randint(0,len(x_val)-1)
samples=x_val[index].ravel() # samples=x_val[index].ravel()
print("Audio:",classes[np.argmax(y_val[index])]) print(samples)
ipd.Audio(samples, rate=8000) # print("Audio:",classes[np.argmax(y_val[index])])
ipd.Audio(samples, rate=8000)
print("Text:",predictSamples(samples))
\ No newline at end of file result = predictSamples(samples)
print("Text:",result)
return result
\ No newline at end of file
This diff is collapsed.
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class MlModel(models.Model): class MlModel(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
description = models.TextField(blank=True) description = models.TextField(blank=True)
timestamp = models.DateTimeField(blank=True, auto_now=True) timestamp = models.DateTimeField(blank=True, auto_now=True)
details = models.JSONField() details = models.JSONField()
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from .models import MlModel from .models import MlModel
from rest_framework import serializers from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer): class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ['url', 'username', 'email', 'groups'] fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer): class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = Group model = Group
fields = ['url', 'name'] fields = ['url', 'name']
class MlModelSerializer(serializers.ModelSerializer): class MlModelSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = MlModel model = MlModel
fields = ( fields = (
'__all__' '__all__'
) )
\ No newline at end of file
from django.test import TestCase from django.test import TestCase
# Create your tests here. # Create your tests here.
from http.client import HTTPResponse from http.client import HTTPResponse
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework import permissions from rest_framework import permissions
from backend.cms.serializers import MlModelSerializer from backend.cms.serializers import MlModelSerializer
from backend.cms.serializers import UserSerializer, GroupSerializer from backend.cms.serializers import UserSerializer, GroupSerializer
from rest_framework.decorators import action from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
import mimetypes
from .models import MlModel import os
from rest_framework.parsers import MultiPartParser
from .model.train import train from io import BytesIO
from .model.predict import predict import librosa
class UserViewSet(viewsets.ModelViewSet): from django.core.files.storage import FileSystemStorage
"""
API endpoint that allows users to be viewed or edited. from .models import MlModel
"""
queryset = User.objects.all().order_by('-date_joined') from .model.train import train
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated] from .model.predict import predict
@action(detail=False)
def runFunction(*args, **kwargs): from pydub import AudioSegment
import numpy as np
print('Function ran')
results = train() class UserViewSet(viewsets.ModelViewSet):
print(results) """
API endpoint that allows users to be viewed or edited.
return Response({'success': True}) """
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated]
"""
API endpoint that allows groups to be viewed or edited. @action(detail=False)
""" def runFunction(*args, **kwargs):
queryset = Group.objects.all()
serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated] print('Function ran')
results = train()
class MlModelViewSet(viewsets.ViewSet): print(results)
queryset = MlModel.objects.all() return Response({'success': True})
serializer_class = MlModelSerializer
permission_classes = [permissions.IsAuthenticated]
class GroupViewSet(viewsets.ModelViewSet):
@action(detail=False) """
def train(*args, **kwargs): API endpoint that allows groups to be viewed or edited.
print('Function ran') """
results = train() queryset = Group.objects.all()
print(results) serializer_class = GroupSerializer
return Response({'success': True}) permission_classes = [permissions.IsAuthenticated]
class MlModelViewSet(viewsets.ViewSet):
@action(detail=False)
def predict(*args, **kwargs): queryset = MlModel.objects.all()
print('Function ran') serializer_class = MlModelSerializer
results = predict() permission_classes = [permissions.AllowAny]
print(results) parser_classes = [MultiPartParser]
return Response({'success': True})
@action(detail=False)
def train(*args, **kwargs):
print('Function ran')
results = train()
print(results)
return Response({'success': True})
@action(detail=False, methods=["POST"])
def detect(self, request, *args, **kwargs):
print('Function ran')
print(request.headers)
bytesio_object = list(request.data.dict().values())[0].file
print(bytesio_object)
with open("output.m4a", "wb") as f:
f.write(bytesio_object.getbuffer())
# convert wav to mp3
audSeg = AudioSegment.from_file("output.m4a")
audSeg.export('output.wav', format="wav")
samples, sample_rate = librosa.load(
'output.wav', sr=16000)
samples = librosa.resample(samples, sample_rate, 8000)
print('---------------------------------------------------------')
if(samples.shape[0] > 8000):
print('grateer -------------------------------------')
samples = samples[-8000:]
print(samples.shape)
else:
len = 8000 - samples.shape[0]
new_arr = np.zeroes(len, )
samples = np.concatenate((samples, new_arr))
print(samples.shape)
# audio_file = request.data
# # Using File storage to save file for future converting
# fs = FileSystemStorage()
# file_name = fs.save(audio_file.name, audio_file)
# audio_file_url = fs.url(file_name)
# # Preparing paths for convertion
# upstream = os.path.dirname(os.path.dirname(os.path.dirname(
# os.path.abspath(__file__))))
# path = os.path.join(upstream, 'media', audio_file.name)
# mp3_filename = '.'.join([audio_file.name.split('.')[0], 'mp3'])
# new_path = os.path.join(
# upstream, 'media', mp3_filename)
# # Converting to mp3 here
# wma_version = AudioSegment.from_file(path, "wav")
# wma_version.export(new_path, format="mp3")
# user_id = self.request.user.id
# # I was trying to create a Track instance, the mp3 get's saved but it is not being saved using location specified in models.
# track = Track.objects.create(user_id=user_id)
# track.file.name = mp3_filename
# track.save()
results = predict(samples)
print(results)
return Response({'success': True, 'result': results})
""" """
Django settings for backend project. Django settings for backend project.
Generated by 'django-admin startproject' using Django 4.0.4. Generated by 'django-admin startproject' using Django 4.0.4.
For more information on this file, see For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/ https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/ https://docs.djangoproject.com/en/4.0/ref/settings/
""" """
from pathlib import Path from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. import datetime
BASE_DIR = Path(__file__).resolve().parent.parent
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# Quick-start development settings - unsuitable for production
# SECURITY WARNING: keep the secret key used in production secret! # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
SECRET_KEY = 'django-insecure-54-4$#$7yzk9*+myixyk2fu(-u0sg(wtf8a$ets11yeqs5w0ly'
# SECURITY WARNING: keep the secret key used in production secret!
# SECURITY WARNING: don't run with debug turned on in production! SECRET_KEY = 'django-insecure-54-4$#$7yzk9*+myixyk2fu(-u0sg(wtf8a$ets11yeqs5w0ly'
DEBUG = True
# SECURITY WARNING: don't run with debug turned on in production!
ALLOWED_HOSTS = [] DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [ # Application definition
'django.contrib.admin',
'django.contrib.auth', INSTALLED_APPS = [
'django.contrib.contenttypes', 'django.contrib.admin',
'django.contrib.sessions', 'django.contrib.auth',
'django.contrib.messages', 'django.contrib.contenttypes',
'django.contrib.staticfiles', 'django.contrib.sessions',
'rest_framework', 'django.contrib.messages',
'backend.cms' 'django.contrib.staticfiles',
] 'rest_framework',
'backend.cms'
MIDDLEWARE = [ ]
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', MIDDLEWARE = [
'django.middleware.common.CommonMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.common.CommonMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
] 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
ROOT_URLCONF = 'backend.urls' ]
TEMPLATES = [ ROOT_URLCONF = 'backend.urls'
{
'BACKEND': 'django.template.backends.django.DjangoTemplates', TEMPLATES = [
'DIRS': [], {
'APP_DIRS': True, 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': { 'DIRS': [],
'context_processors': [ 'APP_DIRS': True,
'django.template.context_processors.debug', 'OPTIONS': {
'django.template.context_processors.request', 'context_processors': [
'django.contrib.auth.context_processors.auth', 'django.template.context_processors.debug',
'django.contrib.messages.context_processors.messages', 'django.template.context_processors.request',
], 'django.contrib.auth.context_processors.auth',
}, 'django.contrib.messages.context_processors.messages',
}, ],
] },
},
WSGI_APPLICATION = 'backend.wsgi.application' ]
WSGI_APPLICATION = 'backend.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
# Database
DATABASES = { # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
'default': {
'ENGINE': 'django.db.backends.sqlite3', DATABASES = {
'NAME': BASE_DIR / 'db.sqlite3', 'default': {
} 'ENGINE': 'django.db.backends.sqlite3',
} 'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
# Password validation
AUTH_PASSWORD_VALIDATORS = [ # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', AUTH_PASSWORD_VALIDATORS = [
}, {
{ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', },
}, {
{ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', },
}, {
{ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', },
}, {
] 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
# Internationalization
LANGUAGE_CODE = 'en-us' # https://docs.djangoproject.com/en/4.0/topics/i18n/
TIME_ZONE = 'UTC' LANGUAGE_CODE = 'en-us'
USE_I18N = True TIME_ZONE = 'UTC'
USE_TZ = True USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
# Static files (CSS, JavaScript, Images)
STATIC_URL = 'static/' # https://docs.djangoproject.com/en/4.0/howto/static-files/
# Default primary key field type STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
REST_FRAMEWORK = { DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10 REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': datetime.timedelta(minutes=14*24*60),
'REFRESH_TOKEN_LIFETIME': datetime.timedelta(days=30),
'ROTATE_REFRESH_TOKENS': True,
'BLACKLIST_AFTER_ROTATION': True,
'ALGORITHM': 'HS256',
'SIGNING_KEY': SECRET_KEY,
'VERIFYING_KEY': None,
'AUTH_HEADER_TYPES': ('Bearer',),
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
'TOKEN_TYPE_CLAIM': 'token_type',
'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_LIFETIME': datetime.timedelta(minutes=5),
'SLIDING_TOKEN_REFRESH_LIFETIME': datetime.timedelta(days=1),
} }
\ No newline at end of file
from django.urls import include, path from django.urls import include, path, re_path
from rest_framework import routers from rest_framework import routers
from backend.cms import views from backend.cms import views
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet) router = routers.DefaultRouter()
router.register(r'groups', views.GroupViewSet) router.register(r'users', views.UserViewSet)
router.register(r'mlmodels', views.MlModelViewSet) router.register(r'groups', views.GroupViewSet)
router.register(r'mlmodels', views.MlModelViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # Wire up our API using automatic URL routing.
urlpatterns = [ # Additionally, we include login URLs for the browsable API.
path('', include(router.urls)), urlpatterns = [
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
re_path(r'^api/auth/token/obtain/$', TokenObtainPairView.as_view()),
re_path(r'^api/auth/token/refresh/$', TokenRefreshView.as_view())
] ]
\ No newline at end of file
""" """
WSGI config for backend project. WSGI config for backend project.
It exposes the WSGI callable as a module-level variable named ``application``. It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/ https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
""" """
import os import os
from django.core.wsgi import get_wsgi_application from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
application = get_wsgi_application() application = get_wsgi_application()
#!/usr/bin/env python #!/usr/bin/env python
"""Django's command-line utility for administrative tasks.""" """Django's command-line utility for administrative tasks."""
import os import os
import sys import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:
raise ImportError( raise ImportError(
"Couldn't import Django. Are you sure it's installed and " "Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you " "available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?" "forget to activate a virtual environment?"
) from exc ) from exc
execute_from_command_line(sys.argv) execute_from_command_line(sys.argv)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment