generated from deepgram-starters/project-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.py
77 lines (56 loc) · 1.91 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import json
from flask import Flask, jsonify, request, abort, make_response
from deepgram import Deepgram
import os
from dotenv import load_dotenv
from flask_cors import CORS
load_dotenv()
app = Flask(__name__, static_folder="./static", static_url_path="/")
@app.route("/", methods=["GET"])
def index():
return app.send_static_file("index.html")
deepgram = Deepgram(os.environ.get("DEEPGRAM_API_KEY"))
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
@app.route("/api", methods=["POST"])
async def transcribe():
form = request.form
files = request.files
url = form.get("url")
features = form.get("features")
model = form.get("model")
version = form.get("version")
tier = form.get("tier")
dgFeatures = json.loads(features)
dgRequest = None
try:
if url and url.startswith("https://res.cloudinary.com/deepgram"):
dgRequest = {"url": url}
if "file" in files:
file = files.get("file")
dgRequest = {"mimetype": file.mimetype, "buffer": file.stream.read()}
dgFeatures["model"] = model
if version:
dgFeatures["version"] = version
if model == "whisper":
dgFeatures["tier"] = tier
if not dgRequest:
raise Exception(
"Error: You need to choose a file to transcribe your own audio."
)
transcription = await deepgram.transcription.prerecorded(dgRequest, dgFeatures)
return jsonify(
{
"model": model,
"version": version,
"tier": tier,
"dgFeatures": dgFeatures,
"transcription": transcription,
}
)
except Exception as error:
return json_abort(error)
def json_abort(message):
print(message)
return abort(make_response(jsonify(err=str(message)), 500))
if __name__ == "__main__":
app.run(debug=True)