Browse Source

Oct 03: [FIX] Bug fixed 'voice_to_text'

pull/337/head
Cybrosys Technologies 7 months ago
parent
commit
603ccc0709
  1. 2
      voice_to_text/__manifest__.py
  2. 7
      voice_to_text/doc/RELEASE_NOTES.md
  3. 3
      voice_to_text/models/res_config_settings.py
  4. 6
      voice_to_text/models/voice_recognition.py
  5. 25
      voice_to_text/static/src/js/command_palette.js
  6. 72
      voice_to_text/static/src/js/search_bar_voice.js
  7. 69
      voice_to_text/static/src/js/text_field.js
  8. 4
      voice_to_text/static/src/xml/text_field.xml
  9. 4
      voice_to_text/views/res_config_setting_views.xml

2
voice_to_text/__manifest__.py

@ -51,6 +51,6 @@
],
},
'external_dependencies': {
'python': ['speech_recognition']
'python': ['Speechrecognition','pyaudio']
},
}

7
voice_to_text/doc/RELEASE_NOTES.md

@ -5,3 +5,10 @@
#### ADD
- Initial Commit for - Voice To Text
#### 01.10.2024
#### Version 16.0.1.0.0
##### BUGFIX
- Optimized and added notifications for recording.
- Added required external dependencies

3
voice_to_text/models/res_config_settings.py

@ -26,8 +26,9 @@ class ResConfigSettings(models.TransientModel):
"""Used to add new fields and sets and gets the value for that fields"""
_inherit = 'res.config.settings'
select_fastest_methode = fields.Selection(
select_fastest_method = fields.Selection(
[('chrome', 'Google Chrome(Fastest Way)'),
('all_browser', 'All Browser(May occur buffering)')],
default='all_browser',
string="Select the Method", help="The fastest method",
config_parameter='voice_to_text.select_fastest_method')

6
voice_to_text/models/voice_recognition.py

@ -20,7 +20,7 @@
#
#############################################################################
import speech_recognition as sr
from odoo import models, _
from odoo import api,models, _
class VoiceRecognition(models.Model):
@ -31,9 +31,10 @@ class VoiceRecognition(models.Model):
def get_the_browser(self):
"""Used to retrieve the browser/fastest method tht the user choose."""
methode_browser = self.env['ir.config_parameter'].sudo().get_param(
'voice_to_text.select_fastest_methode')
'voice_to_text.select_fastest_method')
return methode_browser
@api.model
def recognize_speech(self):
"""This is used to recognizes the voice"""
r = sr.Recognizer()
@ -45,6 +46,7 @@ class VoiceRecognition(models.Model):
except sr.UnknownValueError:
return 0
@api.model
def update_field(self, field, model, script, id):
"""This is used to write the voice text into the field"""
if script:

25
voice_to_text/static/src/js/command_palette.js

@ -1,24 +1,41 @@
/** @odoo-module **/
import {CommandPalette} from "@web/core/commands/command_palette";
import {patch} from "@web/core/utils/patch";
import {useService} from "@web/core/utils/hooks";
import rpc from 'web.rpc';
patch(CommandPalette.prototype, '@web/core/commands/command_palette', {
setup() {
this._super.apply();
this.notification = useService("notification");
},
//This function is used to recognize the voice
async recordVoice(event) {
if (location.href.includes('http:')) {
const closeFun = this.notification.add(
"Recording....",
{
title: "Recording",
type: "success",
sticky: true
},
);
setTimeout(() => closeFun(), 15000)
var response = await rpc.query({
model: 'voice.recognition',
method: 'recognize_speech',
args: [,],
args: [],
})
if (response) {
this.state.searchValue = response
}
else{
this.state.searchValue = "Your voice could not be recognizing......"
} else {
this.notification.add(
"Couldn't recognize the voice, please try again.",
{
title: "Recording",
type: "info",
},
)
}
}
},

72
voice_to_text/static/src/js/search_bar_voice.js

@ -3,43 +3,71 @@
import {SearchBar} from "@web/search/search_bar/search_bar";
import {patch} from "@web/core/utils/patch";
import rpc from 'web.rpc';
var microphone = false
import {useService} from "@web/core/utils/hooks";
const {useEffect, useState} = owl;
patch(SearchBar.prototype, '@web/search/search_bar/search_bar', {
setup() {
this._super(...arguments);
this.microphone = false;
this.data = useState({
response: false
});
this.notification = useService("notification");
useEffect(() => {
this.onSearchInput();
}, () => [this.data.response]);
},
//This function is used to recognize the voice in the search bar
async recordVoiceBar(event) {
this.microphone = true
// Function to recognize the voice in the search bar
async recordVoiceBar() {
this.microphone = true;
if (location.href.includes('http:')) {
var response = await rpc.query({
const closeFun = this.notification.add(
"Recording....",
{
title: "Recording",
type: "success",
sticky: true
},
);
setTimeout(() => closeFun(), 15000)
try {
const response = await rpc.query({
model: 'voice.recognition',
method: 'recognize_speech',
args: [,],
})
if (response){
this.response = response
args: [],
});
this.data.response = response || false;
if (!response) {
this.notification.add(
"Couldn't recognize the voice, please try again.",
{
title: "Recording",
type: "info",
},
)
}
else{
this.response= "False"
var w_response = confirm("can't recognize try again....")
} catch (error) {
console.error("RPC error: ", error);
}
}
},
onSearchInput(ev) {
if (this.microphone == true){
if(this.response != "False"){
ev.target.value = this.response;
}
else{
ev.target.value = "Your Voice can't recognize please try again.";
}
let query = ev?.target?.value?.trim();
if (this.microphone && this.data.response) {
query = this.data.response;
this.microphone = false;
}
const query = ev.target.value
if (query.trim()) {
if (query) {
this.computeState({query, expanded: [], focusedIndex: 0, subItems: []});
} else if (this.items.length) {
this.resetState();
}
}
})
});

69
voice_to_text/static/src/js/text_field.js

@ -2,10 +2,13 @@
import {TextField} from "@web/views/fields/text/text_field";
import {patch} from "@web/core/utils/patch";
import {useService} from "@web/core/utils/hooks";
import rpc from 'web.rpc';
patch(TextField.prototype, '@web/views/fields/text/text_field', {
setup() {
this._super.apply();
this.notification = useService("notification");
},
// This function is used to recognize voice on the text fields
async recordText(ev) {
@ -14,50 +17,72 @@ patch(TextField.prototype,'@web/views/fields/text/text_field',{
model: 'voice.recognition',
method: 'get_the_browser',
args: [,],
}).then(function (data) {
if (data =='chrome'){
}).then((data) => {
console.log(this.notification);
const closeFun = this.notification.add(
"Recording....",
{
title: "Recording",
type: "success",
sticky: true
},
);
setTimeout(() => closeFun(), 15000)
if (data === 'chrome') {
let final_transcript = "";
let interim_transcript = ""; // Define this variable for interim results
if ("webkitSpeechRecognition" in window) {
let speechRecognition = new webkitSpeechRecognition();
if (speechRecognition) {
speechRecognition.continuous = true;
navigator.mediaDevices.getUserMedia({
audio: true}).then(
speechRecognition.start())
navigator.mediaDevices.getUserMedia({audio: true}).then(() => {
speechRecognition.start();
});
speechRecognition.onresult = (e) => {
for (let i = e.resultIndex; i < e.results.length; ++i) {
if (e.results[i].isFinal) {
final_transcript += e.results[i][0].transcript;
} else {
interim_consttranscript += event.results[i][0].transcript;
interim_transcript += e.results[i][0].transcript;
}
}
if (final_transcript) {
var field = this.__owl__.bdom.parentEl.attributes.name.nodeValue
var model = this.props.record.resModel
var browser = rpc.query({
const field = this.__owl__.bdom.parentEl.attributes.name.nodeValue;
const model = this.props.record.resModel;
const id = this.env.model.__bm_load_params__.res_id;
console.log(id)
console.log(final_transcript)
rpc.query({
model: 'voice.recognition',
method: 'update_field',
args: [,field,model,final_transcript],
})
}
args: [field, model, final_transcript,id],
}).then(() => {
this.env.searchModel._notify();
});
}
};
}
}
}
else if(data=='all_browser') {
var field = self.__owl__.bdom.parentEl.attributes.name.nodeValue
var model = self.props.record.resModel
var id = self.env.model.__bm_load_params__.res_id
var browser = rpc.query({
else if (data === 'all_browser') {
const field = this.__owl__.bdom.parentEl.attributes.name.nodeValue;
const model = this.props.record.resModel;
const id = this.env.model.__bm_load_params__.res_id;
rpc.query({
model: 'voice.recognition',
method: 'update_field',
args: [,field,model,false,id],
})
args: [field, model, false, id],
}).then(() => {
this.env.searchModel._notify();
});
}
})
});
}
})

4
voice_to_text/static/src/xml/text_field.xml

@ -4,11 +4,11 @@
<t t-name="voiceText" t-inherit="web.TextField" t-inherit-mode="extension"
owl="1">
<xpath expr="." position="inside">
<div style="margin-left: 150px;margin-top: -55px;">
<div>
<button class="o_Composer_button o_Composer_toolButton btn btn-light fa fa-microphone border-0 rounded-pill mx-1"
id="record_voice" title="Voice" aria-label="Voice"
type="button"
t-on-click="recordText">
t-on-click="recordText" t-ref="voice_record_button">
</button>
</div>
</xpath>

4
voice_to_text/views/res_config_setting_views.xml

@ -15,14 +15,14 @@
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_right_pane">
<label for="select_fastest_methode"
<label for="select_fastest_method"
string="Choose your way"/>
<div class="text-muted">
Depending on the browser the speech to text
conversion speed may change.Choose Your
browser.
</div>
<field name='select_fastest_methode'
<field name='select_fastest_method'
class="w-auto ps-3 fw-bold"
widget="radio"/>
</div>

Loading…
Cancel
Save