Commit cf4c555d authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

create chat send logic

parent 7cca654f
node_modules/
node_modules2/
.expo/
dist/
npm-debug.*
......
......@@ -3,7 +3,8 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation';
import Navigation from './navigation/index';
import { NativeBaseProvider, Box } from "native-base";
export default function App() {
const isLoadingComplete = useCachedResources();
......@@ -13,10 +14,13 @@ export default function App() {
return null;
} else {
return (
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
<NativeBaseProvider>
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
</NativeBaseProvider>
);
}
}
import React, { createContext, useContext, useState, useEffect, useLayoutEffect } from "react";
import axios from "axios";
import { LOCALHOST } from "./constants";
import { BACKEND_URL } from "./constants";
// import { nexlAxios } from "../../constants/helper";
import AsyncStorage from "@react-native-async-storage/async-storage";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Platform } from 'react-native'
// import {
// IAuthContext,
// IAuthRequest,
......@@ -48,28 +49,34 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
// const endpoint = getEnvironment().endpoint;
export const login = async (username, password) => {
const endpoint = false ? 'register' : 'login';
const payload = { username: username, password: password }
const headers = new Headers({ "Content-Type": 'application./json','mode': 'no-cors', 'Access-Control-Allow-Headers': '*' })
const headers = new Headers({ "Content-Type": 'application./json', 'mode': 'no-cors', 'Access-Control-Allow-Headers': '*' })
const options = { method: 'POST', data: payload, headers }
axios.defaults.baseURL = 'http://127.0.0.1:8000';
axios.defaults.baseURL = BACKEND_URL;
axios.defaults.timeout = 1500;
console.log({payload})
console.log({ payload })
return axios.post('/api/auth/token/obtain/', payload)
.then(response => {
.then(async (response) => {
const { access, refresh, user } = response.data;
axios.defaults.headers.common.Authorization = `Token ${token}`;
AsyncStorage.setItem('user_id', user.id)
AsyncStorage.setitem('username',user.username)
AsyncStorage.setItem('user_email', user.email)
AsyncStorage.setitem('access_token', access)
AsyncStorage.setItem('refresh_token', refresh)
axios.defaults.headers.common.Authorization = `Token ${access}`;
if (Platform.OS = 'web') {
console.log('no storage')
} else {
await AsyncStorage.setItem('user_id', user.id)
await AsyncStorage.setitem('username', user.username)
// AsyncStorage.setItem('user_email', user.email)
// AsyncStorage.setitem('access_token', access)
// AsyncStorage.setItem('refresh_token', refresh)
}
console.log("login successful")
......
import { BACKEND_URL } from "./constants"
const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjYxMzU2NDA5LCJpYXQiOjE2NjAxNDY4MDksImp0aSI6ImFhMTljMmY2ZDNkMzRiNDdhZmZmM2FjMzVjNzI4MWJhIiwidXNlcl9pZCI6MX0.IVzibo_Rf2xzoT1J5o1L3zwu3mco6ODcNPC-7imu3Lo"
const headers = new Headers({ authorization: `Bearer ${token}`, 'Content-Type': 'application/json' })
export const create = (resource, values) => {
try {
return fetch(`${BACKEND_URL}/${resource}/`, { method: 'POST', body: JSON.stringify(values), headers })
}
catch (e) {
console.log(e)
}
}
export const getList = (resource) => {
return fetch(`${BACKEND_URL}/${resource}/`, { method: 'GET', headers: headers })
}
\ No newline at end of file
export const LOCALHOST = "http://127.0.0.1:8000"
\ No newline at end of file
// export const BACKEND_URL = "http://192.168.8.103:8000"
export const BACKEND_URL = "https://675d-123-231-127-199.in.ngrok.io"
\ No newline at end of file
......@@ -3,6 +3,7 @@ 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';
import { BACKEND_URL } from '../api/constants';
export const AudioRecorder = ({ setDetectedText }) => {
......@@ -35,13 +36,8 @@ export const AudioRecorder = ({ setDetectedText }) => {
await recording.stopAndUnloadAsync();
const uri = recording.getURI();
console.log('Recording stopped and stored at', uri);
console.log(recording)
console.log('Recording stopped and stored at', uri)
//console.log(recording)
const headers = {
'Content-Type': 'multipart/form-data'
......@@ -49,9 +45,13 @@ export const AudioRecorder = ({ setDetectedText }) => {
}
playSound(uri)
FileSystem.uploadAsync("https://ebf4-2401-dd00-10-20-4ca2-920b-4150-8178.in.ngrok.io/mlmodels/detect/", uri, { headers: headers, uploadType: FileSystem.FileSystemUploadType.MULTIPART })
FileSystem.uploadAsync(`${BACKEND_URL}/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) })
.then(data => {
// console.log({ result: data });
setDetectedText(data.result);
// setTimeout(() => setDetectedText(''), 1000)
})
.catch(err => console.log({ err }))
......
......@@ -7,6 +7,7 @@ import { FontAwesome } from '@expo/vector-icons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer, DefaultTheme, DarkTheme } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Toast } from 'native-base';
import * as React from 'react';
import { ColorSchemeName, Pressable } from 'react-native';
import { login } from '../api/Authentication';
......@@ -19,9 +20,10 @@ import NotFoundScreen from '../screens/NotFoundScreen';
import ChatScreen from '../screens/TabOneScreen';
import TabTwoScreen from '../screens/TabTwoScreen';
import { RootStackParamList, RootTabParamList, RootTabScreenProps } from '../types';
import { ERROR_TOAST_PROPS } from '../util/util';
import LinkingConfiguration from './LinkingConfiguration';
export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeName }) {
export default function Navigation({ colorScheme }) {
return (
<NavigationContainer
linking={LinkingConfiguration}
......@@ -35,14 +37,17 @@ export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeNa
* A root stack navigator is often used for displaying modals on top of all other content.
* https://reactnavigation.org/docs/modal
*/
const Stack = createNativeStackNavigator<RootStackParamList>();
const Stack = createNativeStackNavigator();
function RootNavigator() {
const [isAuthenticated, setIsAutheticated] = React.useState(false)
const [isAuthenticated, setIsAutheticated] = React.useState(true)
const onLogin = async (username: String, password: String) => {
await login(username, password).then(() => setIsAutheticated(true)).catch(e => console.log(e))
const onLogin = async (username, password) => {
await login(username, password).then(() => setIsAutheticated(true)).catch(e => {
console.log(e)
Toast.show({ title: 'Error logging in! Try again', ...ERROR_TOAST_PROPS })
})
}
const onLogout = () => {
......@@ -67,7 +72,7 @@ function RootNavigator() {
* A bottom tab navigator displays tab buttons on the bottom of the display to switch screens.
* https://reactnavigation.org/docs/bottom-tab-navigator
*/
const BottomTab = createBottomTabNavigator<RootTabParamList>();
const BottomTab = createBottomTabNavigator();
function BottomTabNavigator({ onLogout }) {
const colorScheme = useColorScheme();
......@@ -81,7 +86,7 @@ function BottomTabNavigator({ onLogout }) {
<BottomTab.Screen
name="TabOne"
component={ChatScreen}
options={({ navigation }: RootTabScreenProps<'TabOne'>) => ({
options={({ navigation }) => ({
title: 'Chat',
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
headerRight: () => (
......@@ -116,9 +121,6 @@ function BottomTabNavigator({ onLogout }) {
/**
* You can explore the built-in icon families and icons on the web at https://icons.expo.fyi/
*/
function TabBarIcon(props: {
name: React.ComponentProps<typeof FontAwesome>['name'];
color: string;
}) {
function TabBarIcon(props) {
return <FontAwesome size={30} style={{ marginBottom: -3 }} {...props} />;
}
......@@ -32,11 +32,13 @@
"expo-status-bar": "~1.2.0",
"expo-web-browser": "~10.1.0",
"jwt-decode": "^3.1.2",
"native-base": "^3.4.11",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-safe-area-context": "3.3.2",
"react-native-screens": "~3.10.1",
"react-native-svg": "12.1.1",
"react-native-web": "0.17.1"
},
"devDependencies": {
......
import React, { useEffect, useState } from 'react'
import { FlatList, StyleSheet, TextInput, SectionList } from 'react-native';
import { FlatList, StyleSheet, TextInput, SectionList, Button, ActivityIndicator } from 'react-native';
import { AudioRecorder } from '../components/AudioRecorder';
import _ from 'lodash'
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { RootTabScreenProps } from '../types';
import { create, getList } from '../api/api';
export default function ChatScreen({ navigation }) {
......@@ -22,25 +23,22 @@ export default function ChatScreen({ navigation }) {
{ from: 'dri', to: "esh", message: 'msg3', time: currentTime.setDate(currentTime.getDate() - 3) },
{ from: 'esh', to: "dri", message: 'msg4', time: currentTime.setDate(currentTime.getDate() - 4) },
{ from: 'dri', to: "esh", message: 'msg5', time: currentTime.setDate(currentTime.getDate() - 5) },
]
const [detectedText, setDetectedText] = useState("")
const [chatDetails, setChatDetails] = useState({ from: "esh", to: 'dri' })
const [chats, setChats] = useState(defaultChatData)
const [chatDetails, setChatDetails] = useState({ from_user: 1, to_user: 2, conversation_id: 1 })
const [chats, setChats] = useState([])
const [input, setInput] = useState('test')
const [loading, setLoading] = useState(true)
const ws = new WebSocket('ws://localhost:3000/ws')
const sections = [...new Set(chats.map(chat => new Date(chat.time).setHours(0, 0, 0, 0)))];
const sectionChats = sections.map(section => ({ title: section, data: chats.filter(chat => new Date(chat.time).setHours(0, 0, 0, 0) == section) }))
// useEffect(() => {
// startWebsocket()
// }, [])
useEffect(() => {
loadChats()
// startWebsocket()
}, [])
const startWebsocket = () => {
ws.onopen = () => {
......@@ -63,6 +61,29 @@ export default function ChatScreen({ navigation }) {
}
const loadChats = async () => {
await getList('chats').then(res => {
return res.json()
}).then(res => {
// console.log(res)
const chats = res.results
const sections = [...new Set(chats.map(chat => new Date(chat.timestamp).setHours(0, 0, 0, 0)))];
const sectionChats = sections.map(section => ({ title: section, data: chats.filter(chat => new Date(chat.timestamp).setHours(0, 0, 0, 0) == section) }))
setChats(sectionChats)
})
setLoading(false)
}
const onSendPress = () => {
create('chats', { message: input, from_user: chatDetails.from_user, to_user: chatDetails.to_user, conversation: chatDetails.conversation_id }).then(response => {
// console.log(response)
})
setLoading(true)
setInput('')
loadChats()
}
return (
<View style={styles.container}>
{/* <Text style={styles.title}>Tab One</Text>
......@@ -75,39 +96,44 @@ export default function ChatScreen({ navigation }) {
<View style={{ display: 'flex', flex: 0.9, justifyContent: 'space-between' }}>
{/* <View style={{ alignItems: 'center', justifyContent: 'center', flex: 1}}><Text style={{ fontSize: 50, color: 'white' }}>{detectedText}</Text>
</View> */}
<SectionList
inverted={true}
sections={sectionChats}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => {
// console.log({ item })
return (
<View style={{ margin: 5, padding: 5 }}><Text
style={[{ textAlign: chatDetails.from == item.from ? 'right' : 'left', backgroundColor: 'blue', borderRadius: 5, padding: 5 },
chatDetails.from == item.from ? { marginLeft: 'auto' } : { marginRight: 'auto' }
]}
key={item.time}>{item.message}</Text>
<Text style={{ textAlign: chatDetails.from == item.from ? 'right' : 'left', color: 'gray' }}>{new Date(item.time).toLocaleTimeString().slice(0, 5)}</Text></View>
)
}}
invertStickyHeaders
renderSectionFooter={({ section: { title } }) => {
var date = ""
if (new Date(title).toLocaleDateString() == new Date().toLocaleDateString()) {
date = "Today"
}
// else if (new Date(title).toLocaleDateString() == new Date().setDate(new Date().getDate() - 1).toLocaleDateString()){
// date = "Yesterday"
// }
else {
date = new Date(title).toLocaleDateString()
}
{loading ? <ActivityIndicator /> :
<SectionList
refreshing={loading}
onRefresh={() => loadChats()}
inverted={true}
sections={chats}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => {
// console.log({ item })
return (
<View style={{ margin: 5, padding: 5 }}><Text
style={[{ textAlign: chatDetails.from_user == item.from_user ? 'right' : 'left', backgroundColor: chatDetails.from_user == item.from_user ? 'blue' : 'green', borderRadius: 5, padding: 5 },
chatDetails.from_user == item.from_user ? { marginLeft: 'auto' } : { marginRight: 'auto' }
]}
key={item.time}>{item.message}</Text>
<Text style={{ textAlign: chatDetails.from_user == item.from_user ? 'right' : 'left', color: 'gray', fontSize: 11 }}>{new Date(item.timestamp).toLocaleTimeString()}</Text></View>
)
}}
invertStickyHeaders
renderSectionFooter={({ section: { title } }) => {
var date = ""
if (new Date(title).toLocaleDateString() == new Date().toLocaleDateString()) {
date = "Today"
}
// else if (new Date(title).toLocaleDateString() == new Date().setDate(new Date().getDate() - 1).toLocaleDateString()){
// date = "Yesterday"
// }
else {
date = new Date(title).toLocaleDateString()
}
return (
<Text style={styles.header}>{date}</Text>
)
}}
/>
}}
/>
}
{/* <FlatList
inverted={true}
style={{ alignContent: 'flex-end' }}
......@@ -129,12 +155,24 @@ export default function ChatScreen({ navigation }) {
</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 style={{ flex: 0.8, backgroundColor: 'rgb(10,10,10)', height: '100%' }}>
<TextInput
style={{ borderWidth: 2, borderColor: 'gray', color: 'white', padding: 5, borderRadius: 5 }}
defaultValue={input}
onChange={(e) => setInput(e.target.value)}></TextInput>
</View>
{input ?
<View style={{ flex: 0.2, backgroundColor: 'rgb(10,10,10)', height: '100%' }}>
<Button style={{ width: '100%', backgroundColor: 'rgb(10,10,10)', height: '100%' }} title='Send' onPress={onSendPress} />
</View>
:
<View style={{ flex: 0.2, backgroundColor: 'rgb(10,10,10)', height: '100%' }}><AudioRecorder setDetectedText={setInput} /></View>
}
</View>
</View>
</View>
</View>
);
}
......
......@@ -3,6 +3,8 @@ import { StyleSheet, TextInput } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed';
import { TouchableOpacity } from 'react-native';
import { Toast } from 'native-base';
import { ERROR_TOAST_PROPS } from '../util/util';
const LoginForm = ({ onLogin }) => {
......@@ -16,9 +18,9 @@ const LoginForm = ({ onLogin }) => {
<>
<TextInput style={styles.input}
defaultValue={username}
onChange={(e) => {
console.log(username);
setUsername(e.target.value)}}
onChangeText={(e) => {
console.log(e);
setUsername(e)}}
autoCapitalize="none"
onSubmitEditing={() => passwordInput.focus()}
autoCorrect={false}
......@@ -29,7 +31,7 @@ const LoginForm = ({ onLogin }) => {
<TextInput style={styles.input}
defaultValue={password}
onChange={(e) => setPassword(e.target.value)}
onChangeText={(e) => setPassword(e)}
returnKeyType="go"
ref={(input) => passwordInput = input}
placeholder='Password'
......@@ -38,9 +40,15 @@ const LoginForm = ({ onLogin }) => {
<TouchableOpacity style={styles.buttonContainer}
// onPress={onButtonPress}
// disabled={!username || !password}
onPress={() => {
console.log({ username, password })
onLogin(username, password)
if(!username || !password){
Toast.show({title: 'Please fill in all the fields!', ...ERROR_TOAST_PROPS})
}else{
onLogin(username, password)
}
}}
>
<Text style={styles.buttonText}>LOGIN</Text>
......
export const TOAST_PROPS = {placement : 'top'}
export const ERROR_TOAST_PROPS = {...TOAST_PROPS, style:{backgroundColor: 'red'}}
\ No newline at end of file
This diff is collapsed.
# Generated by Django 4.1 on 2022-08-10 13:05
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('cms', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Conversation',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('timestamp', models.DateTimeField(auto_now=True)),
('from_user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='from_chat', to=settings.AUTH_USER_MODEL)),
('to_user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='to_chat', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Chat',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('message', models.TextField()),
('timestamp', models.DateTimeField(auto_now=True)),
('conversation', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='cms.conversation')),
('from_user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='from_convo', to=settings.AUTH_USER_MODEL)),
('to_user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='to_convo', to=settings.AUTH_USER_MODEL)),
],
),
]
# import pickle
# from keras.models import load_model
import pickle
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.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# def predict(samples):
# model=load_model(r'./best_model_final.hdf5')
def predict(samples):
model=load_model(r'./best_model_final.hdf5')
# f1 = open('all_label.txt', 'rb')
# all_label = pickle.load(f1)
# print('loaded labels')
f1 = open('all_label.txt', 'rb')
all_label = pickle.load(f1)
print('loaded labels')
# # f2 = open('all_waves_file.txt', 'rb')
# # all_wave = pickle.load(f2)
# # print('loaded waves')
# f2 = open('all_waves_file.txt', 'rb')
# all_wave = pickle.load(f2)
# print('loaded waves')
# le = LabelEncoder()
# y = le.fit_transform(all_label)
# classes = list(le.classes_)
le = LabelEncoder()
y = le.fit_transform(all_label)
classes = list(le.classes_)
# # 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)
# # train_data_file.close()
# 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)
# train_data_file.close()
# def predictSamples(audio):
# prob=model.predict(audio.reshape(1,8000,1))
# index=np.argmax(prob[0])
# return classes[index]
def predictSamples(audio):
prob=model.predict(audio.reshape(1,8000,1))
index=np.argmax(prob[0])
return classes[index]
# # index=random.randint(0,len(x_val)-1)
# # samples=x_val[index].ravel()
# print(samples)
# # print("Audio:",classes[np.argmax(y_val[index])])
# ipd.Audio(samples, rate=8000)
# index=random.randint(0,len(x_val)-1)
# samples=x_val[index].ravel()
print(samples)
# print("Audio:",classes[np.argmax(y_val[index])])
ipd.Audio(samples, rate=8000)
# result = predictSamples(samples)
result = predictSamples(samples)
# print("Text:",result)
print("Text:",result)
# return result
\ No newline at end of file
return result
\ No newline at end of file
This diff is collapsed.
......@@ -53,7 +53,7 @@ class ChatSerialier(serializers.ModelSerializer):
'__all__'
)
class ConversationSerializer(serializers.Serializer):
class ConversationSerializer(serializers.ModelSerializer):
class Meta:
model = Conversation
fields = (
......
from http.client import HTTPResponse
from lib2to3.pytree import convert
from pyexpat import model
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
......@@ -12,6 +14,8 @@ import os
from rest_framework.parsers import MultiPartParser
from io import BytesIO
from datetime import datetime, timedelta
import librosa
......@@ -20,20 +24,20 @@ from django.core.files.storage import FileSystemStorage
from .models import Chat, Conversation, MlModel
from rest_framework_simplejwt.views import TokenObtainPairView
# from .model.train import train
from .model.train import train
# from .model.predict import predict
from .model.predict import predict
from pydub import AudioSegment
import numpy as np
class ObtainTokenPairWithUserView(TokenObtainPairView):
permission_classes = (permissions.AllowAny,)
serializer_class = MyTokenObtainPairSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
......@@ -45,9 +49,8 @@ class UserViewSet(viewsets.ModelViewSet):
@action(detail=False)
def runFunction(*args, **kwargs):
print('Function ran')
results = {} # train() CHANGE HERE
results = {} # train() CHANGE HERE
print(results)
return Response({'success': True})
......@@ -61,6 +64,7 @@ class GroupViewSet(viewsets.ModelViewSet):
serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated]
class MlModelViewSet(viewsets.ViewSet):
queryset = MlModel.objects.all()
......@@ -68,6 +72,42 @@ class MlModelViewSet(viewsets.ViewSet):
permission_classes = [permissions.AllowAny]
parser_classes = [MultiPartParser]
@action(detail=False)
def runAction(*args, **kwargs):
admin = User.objects.get(username='admin')
user2 = User.objects.get(username='user2')
convo = Conversation(from_user=admin, to_user=user2)
convo.save()
now = datetime.now()
chats = [{'from': 1, 'to': 2, 'message': 'msg1', 'time': now},
{'from': 1, 'to': 2, 'message': 'msg2',
'time': now + timedelta(minutes=1)},
{'from': 2, 'to': 1, 'message': 'msg3',
'time': now + timedelta(minutes=2)},
{'from': 1, 'to': 2, 'message': 'msg4',
'time': now + timedelta(minutes=3)},
{'from': 2, 'to': 1, 'message': 'msg5',
'time': now + timedelta(minutes=4)},
{'from': 1, 'to': 2, 'message': 'msg11', 'time': now},
{'from': 1, 'to': 2, 'message': 'msg22',
'time': now + timedelta(days=1)},
{'from': 2, 'to': 1, 'message': 'msg33',
'time': now + timedelta(days=2)},
{'from': 1, 'to': 2, 'message': 'msg44',
'time': now + timedelta(days=3)},
{'from': 2, 'to': 1, 'message': 'msg55',
'time': now + timedelta(days=4)},
]
for chat in chats:
object = Chat(
conversation=convo, from_user_id=chat['from'], to_user_id=chat['to'], messsage=chat['message'])
object.save()
@action(detail=False)
def train(*args, **kwargs):
print('Function ran')
......@@ -75,7 +115,6 @@ class MlModelViewSet(viewsets.ViewSet):
print(results)
return Response({'success': True})
@action(detail=False, methods=["POST"])
def detect(self, request, *args, **kwargs):
print('Function ran')
......@@ -83,23 +122,21 @@ class MlModelViewSet(viewsets.ViewSet):
bytesio_object = list(request.data.dict().values())[0].file
print(bytesio_object)
with open("output.m4a", "wb") as f:
f.write(bytesio_object.getbuffer())
path = os.path.abspath("output.m4a")
print(path)
# convert wav to mp3
# 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)
'output.wav', sr=16000)
samples = librosa.resample(samples, sample_rate, 8000)
print('---------------------------------------------------------')
if(samples.shape[0] > 8000):
if(samples.shape[0] > 8000):
print('grateer -------------------------------------')
samples = samples[-8000:]
print(samples.shape)
......@@ -109,7 +146,6 @@ class MlModelViewSet(viewsets.ViewSet):
samples = np.concatenate((samples, new_arr))
print(samples.shape)
# audio_file = request.data
# # Using File storage to save file for future converting
......@@ -128,27 +164,26 @@ class MlModelViewSet(viewsets.ViewSet):
# # 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) CHANGE HERE
results = predict(samples)
print(results)
return Response({'success': True, 'result': results})
class ChatViewSet(viewsets.ViewSet):
class ChatViewSet(viewsets.ModelViewSet):
queryset = Chat.objects.all().order_by('-timestamp')
serializer_class = ChatSerialier
permission_classes = [permissions.IsAuthenticated]
class ConversationViewSet(viewsets.ViewSet):
queryset = Conversation.objects.all
class ConversationViewSet(viewsets.ModelViewSet):
queryset = Conversation.objects.all().order_by('id')
serializer_class = ConversationSerializer
permission_classes = [permissions.IsAuthenticated]
\ No newline at end of file
permission_classes = [permissions.IsAuthenticated]
......@@ -9,6 +9,8 @@ router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'mlmodels', views.MlModelViewSet)
router.register(r'chats', views.ChatViewSet)
router.register(r'conversations', views.ConversationViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
......
No preview for this file type
No preview for this file type
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