Kartikeyssj2 commited on
Commit
4006ad9
1 Parent(s): 5f4c2ef

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -23
main.py CHANGED
@@ -9,6 +9,8 @@ import warnings
9
  from starlette.formparsers import MultiPartParser
10
  import io
11
  import random
 
 
12
 
13
 
14
  MultiPartParser.max_file_size = 200 * 1024 * 1024
@@ -20,15 +22,7 @@ app = FastAPI()
20
  tokenizer = Wav2Vec2Tokenizer.from_pretrained("./models/tokenizer")
21
  model = Wav2Vec2ForCTC.from_pretrained("./models/model")
22
 
23
- from fastapi.middleware.cors import CORSMiddleware
24
 
25
- app.add_middleware(
26
- CORSMiddleware,
27
- allow_origins=["*"], # Allows all origins
28
- allow_credentials=True,
29
- allow_methods=["*"], # Allows all methods
30
- allow_headers=["*"], # Allows all headers
31
- )
32
 
33
  # Function to download English word list
34
  def download_word_list():
@@ -93,22 +87,26 @@ async def get_rnc():
93
  return random.randint(0 , 10)
94
 
95
  @app.post('/pronunciation_scoring')
96
- async def unscripted_root(audio_file: UploadFile):
97
- print("Pronunciation Scoring")
98
-
99
- # Read the UploadFile into memory
100
- contents = await audio_file.read()
101
-
102
- print("Contents:" , contents)
103
-
104
- # Create a BytesIO object from the contents
105
- audio_bytes = io.BytesIO(contents)
106
-
107
- print("audio_bytes:" , audio_bytes)
 
 
 
 
 
 
108
 
109
- # Load the audio file using librosa
110
- audio, sr = librosa.load(audio_bytes)
111
-
112
  # Tokenize audio
113
  print("Tokenizing audio...")
114
  input_values = tokenizer(audio, return_tensors="pt").input_values
 
9
  from starlette.formparsers import MultiPartParser
10
  import io
11
  import random
12
+ import tempfile
13
+ import os
14
 
15
 
16
  MultiPartParser.max_file_size = 200 * 1024 * 1024
 
22
  tokenizer = Wav2Vec2Tokenizer.from_pretrained("./models/tokenizer")
23
  model = Wav2Vec2ForCTC.from_pretrained("./models/model")
24
 
 
25
 
 
 
 
 
 
 
 
26
 
27
  # Function to download English word list
28
  def download_word_list():
 
87
  return random.randint(0 , 10)
88
 
89
  @app.post('/pronunciation_scoring')
90
+ async def upload_audio(file: UploadFile = File(...)):
91
+ # Create a temporary file to store the uploaded audio
92
+ with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as temp_file:
93
+ # Write the uploaded file content to the temporary file
94
+ content = await file.read()
95
+ temp_file.write(content)
96
+ temp_file_path = temp_file.name
97
+
98
+ # Load the audio file using librosa with a fixed sample rate of 16000 Hz
99
+ audio, sr = librosa.load(temp_file_path, sr=16000)
100
+
101
+ # Process the audio data as needed
102
+ # For this example, we'll just return some basic information
103
+ duration = librosa.get_duration(y=audio, sr=sr)
104
+
105
+ print("filename:" , file.filename)
106
+ print("duration:" , duration)
107
+ print("sample_rate:" , sr)
108
 
109
+ os.unlink(temp_file_path)
 
 
110
  # Tokenize audio
111
  print("Tokenizing audio...")
112
  input_values = tokenizer(audio, return_tensors="pt").input_values