Advertisement
Chessmaster1251

Untitled

May 17th, 2022
636
141 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from flask import Flask, render_template, redirect
  2. from vosk import Model, KaldiRecognizer
  3. import wave
  4. import re
  5. import json
  6. import time
  7.  
  8. app = Flask(__name__)
  9. app.secret_key = 'secret key'
  10.  
  11.  
  12. @app.route('/', methods=['POST', 'GET'])
  13. def start():
  14.     return render_template('root.html')
  15.  
  16.  
  17.  
  18. @app.route('/translate', methods=['POST'])
  19. def to_text():
  20.  
  21.     output = 'LOVV66_-blago-white-Говорят-чо.wav'
  22.  
  23.     model = Model("vosk-model-small-ru-0.22")
  24.     wf = wave.open(output, "rb")
  25.     wf_fr = wf.getframerate() * wf.getnchannels()
  26.     rec = KaldiRecognizer(model, wf_fr)
  27.     to_return = ''
  28.     last = False
  29.     read_block_size = wf.getnframes()
  30.     while True:
  31.         data = wf.readframes(read_block_size)
  32.         if len(data) == 0:
  33.             break
  34.  
  35.         if rec.AcceptWaveform(data):
  36.             res = json.loads(rec.Result())
  37.  
  38.             if res['text'] != '':
  39.                 to_return = to_return + " " + res['text']
  40.                 if read_block_size < 200000:
  41.                     print(res['text'] + " \n")
  42.  
  43.                 last = False
  44.             elif not last:
  45.                 to_return += '\n'
  46.                 last = True
  47.  
  48.     res = json.loads(rec.FinalResult())
  49.     to_return = to_return + " " + res['text']
  50.  
  51.     return '\n'.join(line.strip() for line in re.findall(r'.{1,150}(?:\s+|$)', to_return))
  52.  
Advertisement
RAW Paste Data Copied
Advertisement