oarab commited on
Commit
daa5eef
·
verified ·
1 Parent(s): dc80376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -25
app.py CHANGED
@@ -2,50 +2,74 @@ import torch
2
  # import gradio as gr
3
  from transformers import pipeline
4
  import numpy as np
5
- import flask
 
 
 
6
 
7
- app = flask.Flask(__name__)
8
 
9
 
10
- device = 0 if torch.cuda.is_available() else "cpu"
 
 
 
 
 
11
 
 
12
  transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english",device=device)
13
 
14
- # def transcribe(stream, new_chunk):
15
- # sr, y = new_chunk
16
- # y = y.astype(np.float32)
17
- # y /= np.max(np.abs(y))
18
 
19
- # if stream is not None:
20
- # stream = np.concatenate([stream, y])
21
- # else:
22
- # stream = y
23
- # return stream, transcriber({"sampling_rate": sr, "raw": stream})["text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  @app.route('/speech-to-text', methods=['POST'])
26
  def speech_to_text():
27
-
28
- audio_data = flask.request.files['audio'].read()
29
-
30
  # Convert audio data to numpy array
31
  y = np.frombuffer(audio_data, dtype=np.float32)
32
-
33
  # Reshape the array if needed (depends on the format of the audio data)
34
- # y = np.reshape(y, (-1,))
35
-
 
36
  # Call the transcribe function
37
  text = transcribe(y)
38
-
39
  # Return the transcription
40
  return flask.jsonify({'text': text})
41
 
42
  def transcribe(new_chunk):
43
 
44
- y = new_chunk
45
  sr = 16000
46
- y = y.astype(np.float32)
47
- y /= np.max(np.abs(y))
48
-
49
- return transcriber({"sampling_rate": sr, "raw": y})["text"]
50
-
51
 
 
 
2
  # import gradio as gr
3
  from transformers import pipeline
4
  import numpy as np
5
+ from flask import Flask, jsonify, request
6
+ from werkzeug.utils import secure_filename
7
+ from flask_cors import CORS
8
+ import os
9
 
 
10
 
11
 
12
+ ALLOWED_EXTENSIONS = set(['xls', 'csv', 'png', 'jpeg', 'jpg','wav'])
13
+ UPLOAD_FOLDER = os.path.abspath(os.path.join(os.path.dirname(__file__), 'Downloads'))
14
+ app = Flask(__name__)
15
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
16
+ app.config['MAX_CONTENT_LENGTH'] = 500 * 1000 * 1000 # 500 MB
17
+ app.config['CORS_HEADER'] = 'application/json'
18
 
19
+ device = 0 if torch.cuda.is_available() else "cpu"
20
  transcriber = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-english",device=device)
21
 
 
 
 
 
22
 
23
+ def allowedFile(filename):
24
+ return '.' in filename and \
25
+ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
26
+
27
+ @app.route('/')
28
+ def home():
29
+ return 'Hello, World!'
30
+
31
+ @app.route('/upload', methods=['POST', 'GET'])
32
+ def fileUpload():
33
+ if request.method == 'POST':
34
+ file = request.files.getlist('files')
35
+ filename = ""
36
+ print(request.files, "....")
37
+ for f in file:
38
+ textz = transcribe(f.read())
39
+ print(textz)
40
+ print(f.filename)
41
+ filename = secure_filename(f.filename)
42
+ print(allowedFile(filename))
43
+ if allowedFile(filename):
44
+ f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
45
+ else:
46
+ return jsonify({'message': 'File type not allowed'}), 400
47
+ return jsonify({"name": filename, "status": "success"})
48
+ else:
49
+ return jsonify({"status": "Upload API GET Request Running"})
50
+
51
+
52
 
53
  @app.route('/speech-to-text', methods=['POST'])
54
  def speech_to_text():
55
+ print(flask.request.data)
56
+ return flask.request.files
57
+
58
  # Convert audio data to numpy array
59
  y = np.frombuffer(audio_data, dtype=np.float32)
 
60
  # Reshape the array if needed (depends on the format of the audio data)
61
+
62
+ # y = np.reshape(y, (-1,))
63
+
64
  # Call the transcribe function
65
  text = transcribe(y)
66
+
67
  # Return the transcription
68
  return flask.jsonify({'text': text})
69
 
70
  def transcribe(new_chunk):
71
 
72
+ y = np.frombuffer(new_chunk, dtype=np.float32)
73
  sr = 16000
 
 
 
 
 
74
 
75
+ return transcriber({"sampling_rate": sr, "raw": y})["text"]