-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (60 loc) · 2.23 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
from flask import Flask, request, jsonify, render_template
#from flask_cors import CORS
from main import generate, transcribe
import os
# Create a Flask application
app = Flask(__name__)
@app.route("/")
def home():
return 'Hello, HOME'
# Define a route for "/api/hello" that returns "Hello, World!"
@app.route('/api/hello')
def hello():
return 'Hello, World!'
@app.route('/api/generate', methods=['POST'])
def custom():
try:
# Get JSON data from the request
data = request.get_json()
filepath = os.path.join('uploads', data["filename"])
if 'prompt' in data and data["filename"]:
param_value = data['prompt']
result = generate(param_value, filepath)
return jsonify({"result": result})
else:
return jsonify({"error": "Missing 'prompt' in JSON request"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 400
@app.route('/api/upload', methods=['POST'])
def upload_file():
# Check if a file is present in the request
if 'audioFile' not in request.files:
return jsonify(error='No file part'), 400
file = request.files['audioFile']
# If no file is selected
if file.filename == '':
return jsonify(error='No selected file'), 400
# Save and process the file
filepath = os.path.join('uploads', file.filename)
file.save(filepath)
# Return a JSON response
return jsonify(body='Uploaded successfully')
@app.route('/api/transcribe', methods=['POST'])
def handle_transcribe():
# Get filename from request body
filename = request.get_data(as_text=True)
# Validate the filename
if not filename:
return jsonify(error="Filename is empty"), 400
# Locate the file and transcribe
try:
filepath = os.path.join('uploads', filename)
transcript = transcribe(filepath) # Your existing transcription function
except Exception as e:
# Handle potential errors like FileNotFound or issues in the transcription function
return jsonify(error=str(e)), 500
# Return the transcription result
return jsonify(result=transcript)
if __name__ == '__main__':
# Run the Flask application
app.run(debug=True)