xxiani commited on
Commit
7f1b8f0
β€’
1 Parent(s): a5fe68a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -1,40 +1,48 @@
1
  import os
2
- import shutil
3
- import tempfile
4
- from fastapi import FastAPI, UploadFile, File, HTTPException
5
  from gradio_client import Client
 
 
 
6
 
7
- app = FastAPI()
8
 
9
  def process_input(input_text):
10
  # 倄理函数
11
  result = "Processed: " + input_text
12
  return result
13
 
14
- @app.post('/')
15
- async def api(audio: UploadFile = File(...)):
16
- try:
17
- if not audio.filename.endswith('.wav'):
18
- raise HTTPException(status_code=400, detail="Only WAV files are supported")
19
-
 
 
 
 
20
  # Create a temporary directory
21
  temp_dir = tempfile.mkdtemp()
22
 
23
- # Save the uploaded audio file to the temporary directory
24
- destination_path = os.path.join(temp_dir, audio.filename)
25
- with open(destination_path, 'wb') as dest_file:
26
- shutil.copyfileobj(audio.file, dest_file)
27
 
28
  client = Client("https://xxiani-speechbrain-asr-wav2vec2-transformer-aishells.hf.space/")
29
  result = client.predict(
30
- destination_path, # str (filepath or URL to file) in 'Input' Audio component
31
- api_name="/predict"
 
32
  )
33
-
34
  os.remove(destination_path)
35
  os.rmdir(temp_dir)
36
-
37
- return {'result': result}
38
- except Exception as e:
39
- return {'error': str(e)}
40
 
 
 
 
1
  import os
2
+ from flask import Flask, request, jsonify
 
 
3
  from gradio_client import Client
4
+ import tempfile
5
+
6
+
7
 
8
+ app = Flask(__name__)
9
 
10
  def process_input(input_text):
11
  # 倄理函数
12
  result = "Processed: " + input_text
13
  return result
14
 
15
+ @app.route('/', methods=["POST"])
16
+ @require_api_key
17
+ def api():
18
+ # try:
19
+ if 'audio' not in request.files:
20
+ return jsonify({"error": "No audio file provided"})
21
+
22
+ file = request.files['audio']
23
+ filename = request.form.get('filename')
24
+
25
  # Create a temporary directory
26
  temp_dir = tempfile.mkdtemp()
27
 
28
+ # Save the audio file to the temporary directory with the specified filename and .wav extension
29
+ destination_path = os.path.join(temp_dir, f"{filename}.wav")
30
+ file.save(destination_path)
31
+ print(destination_path)
32
 
33
  client = Client("https://xxiani-speechbrain-asr-wav2vec2-transformer-aishells.hf.space/")
34
  result = client.predict(
35
+
36
+ destination_path, # str (filepath or URL to file) in 'Input' Audio component
37
+ api_name="/predict"
38
  )
39
+
40
  os.remove(destination_path)
41
  os.rmdir(temp_dir)
42
+ return jsonify({'result': result})
43
+
44
+
45
+
46
 
47
+ if __name__ == '__main__':
48
+ app.run(host='0.0.0.0', port=app.config.get("PORT", 23456), debug=app.config.get("DEBUG", False))