Commit cf4c555d authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

create chat send logic

parent 7cca654f
node_modules/ node_modules/
node_modules2/
.expo/ .expo/
dist/ dist/
npm-debug.* npm-debug.*
......
...@@ -3,7 +3,8 @@ import { SafeAreaProvider } from 'react-native-safe-area-context'; ...@@ -3,7 +3,8 @@ import { SafeAreaProvider } from 'react-native-safe-area-context';
import useCachedResources from './hooks/useCachedResources'; import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme'; import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation'; import Navigation from './navigation/index';
import { NativeBaseProvider, Box } from "native-base";
export default function App() { export default function App() {
const isLoadingComplete = useCachedResources(); const isLoadingComplete = useCachedResources();
...@@ -13,10 +14,13 @@ export default function App() { ...@@ -13,10 +14,13 @@ export default function App() {
return null; return null;
} else { } else {
return ( return (
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} /> <NativeBaseProvider>
<StatusBar /> <SafeAreaProvider>
</SafeAreaProvider> <Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
</NativeBaseProvider>
); );
} }
} }
import React, { createContext, useContext, useState, useEffect, useLayoutEffect } from "react"; import React, { createContext, useContext, useState, useEffect, useLayoutEffect } from "react";
import axios from "axios"; import axios from "axios";
import { LOCALHOST } from "./constants"; import { BACKEND_URL } from "./constants";
// import { nexlAxios } from "../../constants/helper"; // 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 { // import {
// IAuthContext, // IAuthContext,
// IAuthRequest, // IAuthRequest,
...@@ -48,28 +49,34 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; ...@@ -48,28 +49,34 @@ import AsyncStorage from "@react-native-async-storage/async-storage";
// const endpoint = getEnvironment().endpoint; // const endpoint = getEnvironment().endpoint;
export const login = async (username, password) => { export const login = async (username, password) => {
const endpoint = false ? 'register' : 'login'; const endpoint = false ? 'register' : 'login';
const payload = { username: username, password: password } 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 } 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; axios.defaults.timeout = 1500;
console.log({payload}) console.log({ payload })
return axios.post('/api/auth/token/obtain/', payload) return axios.post('/api/auth/token/obtain/', payload)
.then(response => { .then(async (response) => {
const { access, refresh, user } = response.data; const { access, refresh, user } = response.data;
axios.defaults.headers.common.Authorization = `Token ${token}`; axios.defaults.headers.common.Authorization = `Token ${access}`;
AsyncStorage.setItem('user_id', user.id)
AsyncStorage.setitem('username',user.username) if (Platform.OS = 'web') {
AsyncStorage.setItem('user_email', user.email) console.log('no storage')
AsyncStorage.setitem('access_token', access) } else {
AsyncStorage.setItem('refresh_token', refresh) 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") 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" // export const BACKEND_URL = "http://192.168.8.103:8000"
\ No newline at end of file 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'; ...@@ -3,6 +3,7 @@ import { Text, View, StyleSheet, Button } from 'react-native';
import { Audio } from 'expo-av'; import { Audio } from 'expo-av';
import { createIconSetFromFontello } from '@expo/vector-icons'; import { createIconSetFromFontello } from '@expo/vector-icons';
import * as FileSystem from 'expo-file-system'; import * as FileSystem from 'expo-file-system';
import { BACKEND_URL } from '../api/constants';
export const AudioRecorder = ({ setDetectedText }) => { export const AudioRecorder = ({ setDetectedText }) => {
...@@ -35,13 +36,8 @@ export const AudioRecorder = ({ setDetectedText }) => { ...@@ -35,13 +36,8 @@ export const AudioRecorder = ({ setDetectedText }) => {
await recording.stopAndUnloadAsync(); await recording.stopAndUnloadAsync();
const uri = recording.getURI(); 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 = { const headers = {
'Content-Type': 'multipart/form-data' 'Content-Type': 'multipart/form-data'
...@@ -49,9 +45,13 @@ export const AudioRecorder = ({ setDetectedText }) => { ...@@ -49,9 +45,13 @@ export const AudioRecorder = ({ setDetectedText }) => {
} }
playSound(uri) 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 => 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 })) .catch(err => console.log({ err }))
......
...@@ -7,6 +7,7 @@ import { FontAwesome } from '@expo/vector-icons'; ...@@ -7,6 +7,7 @@ import { FontAwesome } from '@expo/vector-icons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer, DefaultTheme, DarkTheme } from '@react-navigation/native'; import { NavigationContainer, DefaultTheme, DarkTheme } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { Toast } from 'native-base';
import * as React from 'react'; import * as React from 'react';
import { ColorSchemeName, Pressable } from 'react-native'; import { ColorSchemeName, Pressable } from 'react-native';
import { login } from '../api/Authentication'; import { login } from '../api/Authentication';
...@@ -19,9 +20,10 @@ import NotFoundScreen from '../screens/NotFoundScreen'; ...@@ -19,9 +20,10 @@ import NotFoundScreen from '../screens/NotFoundScreen';
import ChatScreen from '../screens/TabOneScreen'; import ChatScreen from '../screens/TabOneScreen';
import TabTwoScreen from '../screens/TabTwoScreen'; import TabTwoScreen from '../screens/TabTwoScreen';
import { RootStackParamList, RootTabParamList, RootTabScreenProps } from '../types'; import { RootStackParamList, RootTabParamList, RootTabScreenProps } from '../types';
import { ERROR_TOAST_PROPS } from '../util/util';
import LinkingConfiguration from './LinkingConfiguration'; import LinkingConfiguration from './LinkingConfiguration';
export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeName }) { export default function Navigation({ colorScheme }) {
return ( return (
<NavigationContainer <NavigationContainer
linking={LinkingConfiguration} linking={LinkingConfiguration}
...@@ -35,14 +37,17 @@ export default function Navigation({ colorScheme }: { colorScheme: ColorSchemeNa ...@@ -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. * A root stack navigator is often used for displaying modals on top of all other content.
* https://reactnavigation.org/docs/modal * https://reactnavigation.org/docs/modal
*/ */
const Stack = createNativeStackNavigator<RootStackParamList>(); const Stack = createNativeStackNavigator();
function RootNavigator() { function RootNavigator() {
const [isAuthenticated, setIsAutheticated] = React.useState(false) const [isAuthenticated, setIsAutheticated] = React.useState(true)
const onLogin = async (username: String, password: String) => { const onLogin = async (username, password) => {
await login(username, password).then(() => setIsAutheticated(true)).catch(e => console.log(e)) 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 = () => { const onLogout = () => {
...@@ -67,7 +72,7 @@ function RootNavigator() { ...@@ -67,7 +72,7 @@ function RootNavigator() {
* A bottom tab navigator displays tab buttons on the bottom of the display to switch screens. * A bottom tab navigator displays tab buttons on the bottom of the display to switch screens.
* https://reactnavigation.org/docs/bottom-tab-navigator * https://reactnavigation.org/docs/bottom-tab-navigator
*/ */
const BottomTab = createBottomTabNavigator<RootTabParamList>(); const BottomTab = createBottomTabNavigator();
function BottomTabNavigator({ onLogout }) { function BottomTabNavigator({ onLogout }) {
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
...@@ -81,7 +86,7 @@ function BottomTabNavigator({ onLogout }) { ...@@ -81,7 +86,7 @@ function BottomTabNavigator({ onLogout }) {
<BottomTab.Screen <BottomTab.Screen
name="TabOne" name="TabOne"
component={ChatScreen} component={ChatScreen}
options={({ navigation }: RootTabScreenProps<'TabOne'>) => ({ options={({ navigation }) => ({
title: 'Chat', title: 'Chat',
tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />, tabBarIcon: ({ color }) => <TabBarIcon name="code" color={color} />,
headerRight: () => ( headerRight: () => (
...@@ -116,9 +121,6 @@ function BottomTabNavigator({ onLogout }) { ...@@ -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/ * You can explore the built-in icon families and icons on the web at https://icons.expo.fyi/
*/ */
function TabBarIcon(props: { function TabBarIcon(props) {
name: React.ComponentProps<typeof FontAwesome>['name'];
color: string;
}) {
return <FontAwesome size={30} style={{ marginBottom: -3 }} {...props} />; return <FontAwesome size={30} style={{ marginBottom: -3 }} {...props} />;
} }
...@@ -32,11 +32,13 @@ ...@@ -32,11 +32,13 @@
"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", "jwt-decode": "^3.1.2",
"native-base": "^3.4.11",
"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",
"react-native-safe-area-context": "3.3.2", "react-native-safe-area-context": "3.3.2",
"react-native-screens": "~3.10.1", "react-native-screens": "~3.10.1",
"react-native-svg": "12.1.1",
"react-native-web": "0.17.1" "react-native-web": "0.17.1"
}, },
"devDependencies": { "devDependencies": {
......
import React, { useEffect, useState } from 'react' 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 { AudioRecorder } from '../components/AudioRecorder';
import _ from 'lodash' import _ from 'lodash'
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';
import { create, getList } from '../api/api';
export default function ChatScreen({ navigation }) { export default function ChatScreen({ navigation }) {
...@@ -22,25 +23,22 @@ 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: 'dri', to: "esh", message: 'msg3', time: currentTime.setDate(currentTime.getDate() - 3) },
{ from: 'esh', to: "dri", message: 'msg4', time: currentTime.setDate(currentTime.getDate() - 4) }, { from: 'esh', to: "dri", message: 'msg4', time: currentTime.setDate(currentTime.getDate() - 4) },
{ from: 'dri', to: "esh", message: 'msg5', time: currentTime.setDate(currentTime.getDate() - 5) }, { from: 'dri', to: "esh", message: 'msg5', time: currentTime.setDate(currentTime.getDate() - 5) },
] ]
const [detectedText, setDetectedText] = useState("") const [detectedText, setDetectedText] = useState("")
const [chatDetails, setChatDetails] = useState({ from: "esh", to: 'dri' }) const [chatDetails, setChatDetails] = useState({ from_user: 1, to_user: 2, conversation_id: 1 })
const [chats, setChats] = useState(defaultChatData) const [chats, setChats] = useState([])
const [input, setInput] = useState('test')
const [loading, setLoading] = useState(true)
const ws = new WebSocket('ws://localhost:3000/ws') 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(() => {
loadChats()
// useEffect(() => { // startWebsocket()
// startWebsocket() }, [])
// }, [])
const startWebsocket = () => { const startWebsocket = () => {
ws.onopen = () => { ws.onopen = () => {
...@@ -63,6 +61,29 @@ export default function ChatScreen({ navigation }) { ...@@ -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 ( return (
<View style={styles.container}> <View style={styles.container}>
{/* <Text style={styles.title}>Tab One</Text> {/* <Text style={styles.title}>Tab One</Text>
...@@ -75,39 +96,44 @@ export default function ChatScreen({ navigation }) { ...@@ -75,39 +96,44 @@ export default function ChatScreen({ navigation }) {
<View style={{ display: 'flex', flex: 0.9, justifyContent: 'space-between' }}> <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 style={{ alignItems: 'center', justifyContent: 'center', flex: 1}}><Text style={{ fontSize: 50, color: 'white' }}>{detectedText}</Text>
</View> */} </View> */}
<SectionList
inverted={true} {loading ? <ActivityIndicator /> :
sections={sectionChats} <SectionList
keyExtractor={(item, index) => item + index} refreshing={loading}
renderItem={({ item }) => { onRefresh={() => loadChats()}
// console.log({ item }) inverted={true}
return ( sections={chats}
<View style={{ margin: 5, padding: 5 }}><Text keyExtractor={(item, index) => item + index}
style={[{ textAlign: chatDetails.from == item.from ? 'right' : 'left', backgroundColor: 'blue', borderRadius: 5, padding: 5 }, renderItem={({ item }) => {
chatDetails.from == item.from ? { marginLeft: 'auto' } : { marginRight: 'auto' } // console.log({ item })
]} return (
key={item.time}>{item.message}</Text> <View style={{ margin: 5, padding: 5 }}><Text
<Text style={{ textAlign: chatDetails.from == item.from ? 'right' : 'left', color: 'gray' }}>{new Date(item.time).toLocaleTimeString().slice(0, 5)}</Text></View> 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' }
}} ]}
invertStickyHeaders key={item.time}>{item.message}</Text>
renderSectionFooter={({ section: { title } }) => { <Text style={{ textAlign: chatDetails.from_user == item.from_user ? 'right' : 'left', color: 'gray', fontSize: 11 }}>{new Date(item.timestamp).toLocaleTimeString()}</Text></View>
var date = "" )
if (new Date(title).toLocaleDateString() == new Date().toLocaleDateString()) { }}
date = "Today" invertStickyHeaders
} renderSectionFooter={({ section: { title } }) => {
// else if (new Date(title).toLocaleDateString() == new Date().setDate(new Date().getDate() - 1).toLocaleDateString()){ var date = ""
// date = "Yesterday" if (new Date(title).toLocaleDateString() == new Date().toLocaleDateString()) {
// } date = "Today"
else { }
date = new Date(title).toLocaleDateString() // else if (new Date(title).toLocaleDateString() == new Date().setDate(new Date().getDate() - 1).toLocaleDateString()){
} // date = "Yesterday"
// }
else {
date = new Date(title).toLocaleDateString()
}
return ( return (
<Text style={styles.header}>{date}</Text> <Text style={styles.header}>{date}</Text>
) )
}} }}
/> />
}
{/* <FlatList {/* <FlatList
inverted={true} inverted={true}
style={{ alignContent: 'flex-end' }} style={{ alignContent: 'flex-end' }}
...@@ -129,12 +155,24 @@ export default function ChatScreen({ navigation }) { ...@@ -129,12 +155,24 @@ export default function ChatScreen({ navigation }) {
</View> </View>
<View style={{ flex: 0.1, backgroundColor: 'rgb(10,10,10)' }}> <View style={{ flex: 0.1, backgroundColor: 'rgb(10,10,10)' }}>
<View style={{ flexDirection: 'row', display: 'flex', height: '100%' }}> <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.8, backgroundColor: 'rgb(10,10,10)', height: '100%' }}>
<View style={{ flex: 0.2, backgroundColor: 'rgb(10,10,10)', height: '100%' }}><AudioRecorder setDetectedText={setDetectedText} /></View> <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>
</View> </View>
</View> </View>
); );
} }
......
...@@ -3,6 +3,8 @@ import { StyleSheet, TextInput } from 'react-native'; ...@@ -3,6 +3,8 @@ import { StyleSheet, TextInput } from 'react-native';
import EditScreenInfo from '../components/EditScreenInfo'; import EditScreenInfo from '../components/EditScreenInfo';
import { Text, View } from '../components/Themed'; import { Text, View } from '../components/Themed';
import { TouchableOpacity } from 'react-native'; import { TouchableOpacity } from 'react-native';
import { Toast } from 'native-base';
import { ERROR_TOAST_PROPS } from '../util/util';
const LoginForm = ({ onLogin }) => { const LoginForm = ({ onLogin }) => {
...@@ -16,9 +18,9 @@ const LoginForm = ({ onLogin }) => { ...@@ -16,9 +18,9 @@ const LoginForm = ({ onLogin }) => {
<> <>
<TextInput style={styles.input} <TextInput style={styles.input}
defaultValue={username} defaultValue={username}
onChange={(e) => { onChangeText={(e) => {
console.log(username); console.log(e);
setUsername(e.target.value)}} setUsername(e)}}
autoCapitalize="none" autoCapitalize="none"
onSubmitEditing={() => passwordInput.focus()} onSubmitEditing={() => passwordInput.focus()}
autoCorrect={false} autoCorrect={false}
...@@ -29,7 +31,7 @@ const LoginForm = ({ onLogin }) => { ...@@ -29,7 +31,7 @@ const LoginForm = ({ onLogin }) => {
<TextInput style={styles.input} <TextInput style={styles.input}
defaultValue={password} defaultValue={password}
onChange={(e) => setPassword(e.target.value)} onChangeText={(e) => setPassword(e)}
returnKeyType="go" returnKeyType="go"
ref={(input) => passwordInput = input} ref={(input) => passwordInput = input}
placeholder='Password' placeholder='Password'
...@@ -38,9 +40,15 @@ const LoginForm = ({ onLogin }) => { ...@@ -38,9 +40,15 @@ const LoginForm = ({ onLogin }) => {
<TouchableOpacity style={styles.buttonContainer} <TouchableOpacity style={styles.buttonContainer}
// onPress={onButtonPress} // onPress={onButtonPress}
// disabled={!username || !password}
onPress={() => { onPress={() => {
console.log({ username, password }) 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> <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 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(samples): 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(samples) print(samples)
# # print("Audio:",classes[np.argmax(y_val[index])]) # print("Audio:",classes[np.argmax(y_val[index])])
# ipd.Audio(samples, rate=8000) ipd.Audio(samples, rate=8000)
# result = predictSamples(samples) result = predictSamples(samples)
# print("Text:",result) print("Text:",result)
# return result return result
\ No newline at end of file \ No newline at end of file
This diff is collapsed.
...@@ -53,7 +53,7 @@ class ChatSerialier(serializers.ModelSerializer): ...@@ -53,7 +53,7 @@ class ChatSerialier(serializers.ModelSerializer):
'__all__' '__all__'
) )
class ConversationSerializer(serializers.Serializer): class ConversationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Conversation model = Conversation
fields = ( fields = (
......
from http.client import HTTPResponse from http.client import HTTPResponse
from lib2to3.pytree import convert
from pyexpat import model
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
...@@ -12,6 +14,8 @@ import os ...@@ -12,6 +14,8 @@ import os
from rest_framework.parsers import MultiPartParser from rest_framework.parsers import MultiPartParser
from io import BytesIO from io import BytesIO
from datetime import datetime, timedelta
import librosa import librosa
...@@ -20,20 +24,20 @@ from django.core.files.storage import FileSystemStorage ...@@ -20,20 +24,20 @@ from django.core.files.storage import FileSystemStorage
from .models import Chat, Conversation, MlModel from .models import Chat, Conversation, MlModel
from rest_framework_simplejwt.views import TokenObtainPairView 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 from pydub import AudioSegment
import numpy as np import numpy as np
class ObtainTokenPairWithUserView(TokenObtainPairView): class ObtainTokenPairWithUserView(TokenObtainPairView):
permission_classes = (permissions.AllowAny,) permission_classes = (permissions.AllowAny,)
serializer_class = MyTokenObtainPairSerializer serializer_class = MyTokenObtainPairSerializer
class UserViewSet(viewsets.ModelViewSet): class UserViewSet(viewsets.ModelViewSet):
""" """
API endpoint that allows users to be viewed or edited. API endpoint that allows users to be viewed or edited.
...@@ -45,9 +49,8 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -45,9 +49,8 @@ class UserViewSet(viewsets.ModelViewSet):
@action(detail=False) @action(detail=False)
def runFunction(*args, **kwargs): def runFunction(*args, **kwargs):
print('Function ran') print('Function ran')
results = {} # train() CHANGE HERE results = {} # train() CHANGE HERE
print(results) print(results)
return Response({'success': True}) return Response({'success': True})
...@@ -61,6 +64,7 @@ class GroupViewSet(viewsets.ModelViewSet): ...@@ -61,6 +64,7 @@ class GroupViewSet(viewsets.ModelViewSet):
serializer_class = GroupSerializer serializer_class = GroupSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
class MlModelViewSet(viewsets.ViewSet): class MlModelViewSet(viewsets.ViewSet):
queryset = MlModel.objects.all() queryset = MlModel.objects.all()
...@@ -68,6 +72,42 @@ class MlModelViewSet(viewsets.ViewSet): ...@@ -68,6 +72,42 @@ class MlModelViewSet(viewsets.ViewSet):
permission_classes = [permissions.AllowAny] permission_classes = [permissions.AllowAny]
parser_classes = [MultiPartParser] 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) @action(detail=False)
def train(*args, **kwargs): def train(*args, **kwargs):
print('Function ran') print('Function ran')
...@@ -75,7 +115,6 @@ class MlModelViewSet(viewsets.ViewSet): ...@@ -75,7 +115,6 @@ class MlModelViewSet(viewsets.ViewSet):
print(results) print(results)
return Response({'success': True}) return Response({'success': True})
@action(detail=False, methods=["POST"]) @action(detail=False, methods=["POST"])
def detect(self, request, *args, **kwargs): def detect(self, request, *args, **kwargs):
print('Function ran') print('Function ran')
...@@ -83,23 +122,21 @@ class MlModelViewSet(viewsets.ViewSet): ...@@ -83,23 +122,21 @@ class MlModelViewSet(viewsets.ViewSet):
bytesio_object = list(request.data.dict().values())[0].file bytesio_object = list(request.data.dict().values())[0].file
print(bytesio_object) print(bytesio_object)
with open("output.m4a", "wb") as f: with open("output.m4a", "wb") as f:
f.write(bytesio_object.getbuffer()) f.write(bytesio_object.getbuffer())
path = os.path.abspath("output.m4a") path = os.path.abspath("output.m4a")
print(path) print(path)
# convert wav to mp3 # convert wav to mp3
audSeg = AudioSegment.from_file("output.m4a") audSeg = AudioSegment.from_file("output.m4a")
audSeg.export('output.wav', format="wav") audSeg.export('output.wav', format="wav")
samples, sample_rate = librosa.load( samples, sample_rate = librosa.load(
'output.wav', sr=16000) 'output.wav', sr=16000)
samples = librosa.resample(samples, sample_rate, 8000) samples = librosa.resample(samples, sample_rate, 8000)
print('---------------------------------------------------------') print('---------------------------------------------------------')
if(samples.shape[0] > 8000): if(samples.shape[0] > 8000):
print('grateer -------------------------------------') print('grateer -------------------------------------')
samples = samples[-8000:] samples = samples[-8000:]
print(samples.shape) print(samples.shape)
...@@ -109,7 +146,6 @@ class MlModelViewSet(viewsets.ViewSet): ...@@ -109,7 +146,6 @@ class MlModelViewSet(viewsets.ViewSet):
samples = np.concatenate((samples, new_arr)) samples = np.concatenate((samples, new_arr))
print(samples.shape) print(samples.shape)
# audio_file = request.data # audio_file = request.data
# # Using File storage to save file for future converting # # Using File storage to save file for future converting
...@@ -128,27 +164,26 @@ class MlModelViewSet(viewsets.ViewSet): ...@@ -128,27 +164,26 @@ class MlModelViewSet(viewsets.ViewSet):
# # Converting to mp3 here # # Converting to mp3 here
# wma_version = AudioSegment.from_file(path, "wav") # wma_version = AudioSegment.from_file(path, "wav")
# wma_version.export(new_path, format="mp3") # wma_version.export(new_path, format="mp3")
# user_id = self.request.user.id # 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. # # 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 = Track.objects.create(user_id=user_id)
# track.file.name = mp3_filename # track.file.name = mp3_filename
# track.save() # track.save()
results = predict(samples)
results = {} #predict(samples) CHANGE HERE
print(results) print(results)
return Response({'success': True, 'result': results}) return Response({'success': True, 'result': results})
class ChatViewSet(viewsets.ViewSet): class ChatViewSet(viewsets.ModelViewSet):
queryset = Chat.objects.all().order_by('-timestamp') queryset = Chat.objects.all().order_by('-timestamp')
serializer_class = ChatSerialier serializer_class = ChatSerialier
permission_classes = [permissions.IsAuthenticated] 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 serializer_class = ConversationSerializer
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
\ No newline at end of file
...@@ -9,6 +9,8 @@ router = routers.DefaultRouter() ...@@ -9,6 +9,8 @@ router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet) router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet) router.register(r'groups', views.GroupViewSet)
router.register(r'mlmodels', views.MlModelViewSet) 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. # Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API. # 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