Skip to content

Commit

Permalink
add ability to fill product details based on audio input
Browse files Browse the repository at this point in the history
  • Loading branch information
nullsploit01 committed Jan 27, 2024
1 parent 665fca1 commit 8428fdb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
8 changes: 2 additions & 6 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ class Controller {
throw new Error()
}

let product = await productService.extractProductInformation(files[0])
if (!product) {
product = ''
}
res.json({ product: JSON.parse(product) })
const product = await productService.extractProductInformation(files[0])
res.json(product)
} catch (error) {
console.log(JSON.stringify(error))
next(error)
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/services/product/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ class ProductService {

extractProductInformation = async (audioFile: Express.Multer.File) => {
const transcript = await openAiClient.whisper(audioFile)
const message = `Create a json object interface { name?: string price?: number description?: string image?: string | ArrayBuffer color?: string brand?: string model?: string category?: string subCategory?: string inventory?: number } of the transcript: ${transcript}`

const message = `Create a json object interface {
name?: string
price?: number
description?: string
image?: string | ArrayBuffer
color?:
string brand?: string
model?: string
category?: string
subCategory?: string
inventory?: number } for the transcript: ${transcript} only for keys provided in transcript`

const response = await openAiClient.chatCompletions(message)

if (!response || !response.content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const EditProductImageModal: FC = () => {
const reader = new FileReader()

reader.onload = () => {
productService.uploadProductIMages(image).then((response) => {
productService.uploadProductImages(image).then((response) => {
const product = {
...productToEdit,
color: response.data.find((res) =>
Expand Down
38 changes: 29 additions & 9 deletions ui/src/components/organisms/edit-product-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ import EditProductImageModal from './components/edit-product-image-modal'
import { Box, Button, Card, CardMedia, TextField, Tooltip, Typography } from '@mui/material'
import Grid from '@mui/material/Unstable_Grid2'
import { Form, Formik } from 'formik'
import { Fragment } from 'react'
import { Fragment, useEffect, useState } from 'react'
import { AudioRecorder } from 'react-audio-voice-recorder'

const EditProductModal = () => {
const { productToEdit, closeEditProductModal, updateProduct, openProductImageEditModal } =
useProducts()

const [_productState, setProductState] = useState<IProduct>({} as IProduct)

useEffect(() => {
if (productToEdit) {
setProductState(productToEdit)
}
}, [productToEdit])

const onSubmit = (product: IProduct) => {
updateProduct(product)
closeEditProductModal()
}

const addAudioElement = (blob: Blob) => {
const data = new FormData()
data.append('file', blob)
productService.uploadVoiceInput(data)
const addAudioElement = async (blob: Blob) => {
const formData = new FormData()
formData.append('file', blob)
const { data } = await productService.uploadVoiceInput(formData)
setProductState({ ...productToEdit, ...data })
}

return (
Expand All @@ -32,11 +41,11 @@ const EditProductModal = () => {
<EditProductImageModal />
<Box flexGrow={1}>
<Typography mb={2} variant="h5">
Edit {productToEdit?.name}
Edit {_productState?.name}
</Typography>
<Grid container spacing={2}>
<Formik enableReinitialize={true} initialValues={productToEdit} onSubmit={onSubmit}>
{({ values, dirty, handleChange }) => (
<Formik enableReinitialize={true} initialValues={_productState} onSubmit={onSubmit}>
{({ values, handleChange }) => (
<Form>
<Box sx={{ mr: 3, mb: 2, ml: 2 }}>
<Grid container spacing={2}>
Expand Down Expand Up @@ -75,6 +84,7 @@ const EditProductModal = () => {
}}
>
<TextField
InputLabelProps={{ shrink: Boolean(values.name) }}
fullWidth
color="secondary"
sx={{ mx: 1, mb: 2 }}
Expand All @@ -84,6 +94,9 @@ const EditProductModal = () => {
label="Name"
/>
<TextField
InputLabelProps={{
shrink: Boolean(values.price !== undefined && values.price !== null)
}}
fullWidth
color="secondary"
sx={{ mx: 1, mb: 2 }}
Expand All @@ -101,6 +114,7 @@ const EditProductModal = () => {
<Grid xs={12}>
<Box sx={{ maxWidth: 'inherit', mx: 1, mb: 1 }}>
<TextField
InputLabelProps={{ shrink: Boolean(values.description) }}
color="secondary"
fullWidth
multiline
Expand All @@ -113,6 +127,7 @@ const EditProductModal = () => {
</Grid>
<Grid xs={12}>
<TextField
InputLabelProps={{ shrink: Boolean(values.brand) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
name="brand"
Expand All @@ -121,6 +136,7 @@ const EditProductModal = () => {
value={values.brand}
/>
<TextField
InputLabelProps={{ shrink: Boolean(values.model) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
name="model"
Expand All @@ -131,6 +147,7 @@ const EditProductModal = () => {
</Grid>
<Grid xs={12}>
<TextField
InputLabelProps={{ shrink: Boolean(values.category) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
name="category"
Expand All @@ -139,6 +156,7 @@ const EditProductModal = () => {
value={values.category}
/>
<TextField
InputLabelProps={{ shrink: Boolean(values.subCategory) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
name="subCategory"
Expand All @@ -149,6 +167,7 @@ const EditProductModal = () => {
</Grid>
<Grid xs={12}>
<TextField
InputLabelProps={{ shrink: Boolean(values.color) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
name="color"
Expand All @@ -158,6 +177,7 @@ const EditProductModal = () => {
/>

<TextField
InputLabelProps={{ shrink: Boolean(values.inventory) }}
color="secondary"
sx={{ mx: 1, mb: 1 }}
type="number"
Expand All @@ -172,7 +192,7 @@ const EditProductModal = () => {
<Button onClick={closeEditProductModal} sx={{ mx: 3 }} color="secondary">
Cancel
</Button>
<Button disabled={!dirty} type="submit" color="secondary" variant="contained">
<Button type="submit" color="secondary" variant="contained">
Save
</Button>
<AudioRecorder
Expand Down
2 changes: 1 addition & 1 deletion ui/src/providers/product-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const ProductContextProvider: FC<IProductContextProvider> = ({ children }
setLoading(true)

productService
.uploadProductIMages(images)
.uploadProductImages(images)
.then((response) => {
Array.from(images).map((image) => {
const reader = new FileReader()
Expand Down
8 changes: 4 additions & 4 deletions ui/src/services/product/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { axiosClient } from 'src/clients/axios'
import { IProductImageColor } from 'src/models/product'
import { IProduct, IProductImageColor } from 'src/models/product'

class ProductService {
uploadProductIMages(images: FileList) {
uploadProductImages = async (images: FileList) => {
return axiosClient.post<IProductImageColor[]>('/images', images)
}

uploadVoiceInput(audio: any) {
return axiosClient.post('/audio', audio)
uploadVoiceInput = async (audio: any) => {
return axiosClient.post<IProduct>('/audio', audio)
}
}

Expand Down

0 comments on commit 8428fdb

Please sign in to comment.