Spaces:
Build error
Build error
Kartikeyssj2
commited on
Commit
•
10dd4bf
1
Parent(s):
2f6faa5
Update main.py
Browse files
main.py
CHANGED
@@ -1,173 +1,190 @@
|
|
1 |
-
import
|
2 |
-
import
|
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 |
-
# FastAPI routes
|
76 |
-
@app.get('/')
|
77 |
-
async def root():
|
78 |
-
|
79 |
|
80 |
-
@app.post('/check_post')
|
81 |
-
async def rnc(number):
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
@app.get('/check_get')
|
87 |
-
async def get_rnc():
|
88 |
-
|
89 |
|
90 |
|
91 |
-
@app.post('/fluency_score')
|
92 |
-
async def fluency_scoring(file: UploadFile = File(...)):
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
|
98 |
-
@app.post('/pronunciation_score')
|
99 |
-
async def pronunciation_scoring(file: UploadFile = File(...)):
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
105 |
|
106 |
-
|
107 |
|
108 |
-
|
109 |
|
110 |
-
|
111 |
-
|
112 |
|
113 |
-
|
114 |
|
115 |
-
|
116 |
|
117 |
|
118 |
|
119 |
-
|
120 |
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
|
133 |
-
|
134 |
|
135 |
-
|
136 |
|
137 |
-
|
138 |
|
139 |
-
|
140 |
-
|
141 |
|
142 |
-
|
143 |
|
144 |
-
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
|
155 |
-
|
156 |
-
|
157 |
|
158 |
-
|
159 |
|
160 |
-
|
161 |
-
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
1 |
+
import soundfile as sf
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
@app.post('/fluency_score')
|
5 |
+
async def fluency_scoring(file: UploadFile = File(...)):
|
6 |
+
with sf.SoundFile(file.file, 'r') as sound_file:
|
7 |
+
audio_array = sound_file.read(dtype="float32")
|
8 |
+
sample_rate = sound_file.samplerate
|
9 |
+
|
10 |
+
if sample_rate != 16000:
|
11 |
+
# Resample to 16000 Hz if needed
|
12 |
+
audio_array = librosa.resample(audio_array, sample_rate, 16000)
|
13 |
+
|
14 |
+
print(audio_array)
|
15 |
+
return audio_array[:5].tolist()
|
16 |
+
|
17 |
+
|
18 |
+
# import re
|
19 |
+
# import requests
|
20 |
+
# import pyarrow as pa
|
21 |
+
# import librosa
|
22 |
+
# import torch
|
23 |
+
# from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
|
24 |
+
# from fastapi import FastAPI, File, UploadFile
|
25 |
+
# import warnings
|
26 |
+
# from starlette.formparsers import MultiPartParser
|
27 |
+
# import io
|
28 |
+
# import random
|
29 |
+
# import tempfile
|
30 |
+
# import os
|
31 |
+
# import numba
|
32 |
+
# import soundfile as sf
|
33 |
+
# import asyncio
|
34 |
+
|
35 |
+
# MultiPartParser.max_file_size = 200 * 1024 * 1024
|
36 |
+
|
37 |
+
# # Initialize FastAPI app
|
38 |
+
# app = FastAPI()
|
39 |
+
|
40 |
+
# # Load Wav2Vec2 tokenizer and model
|
41 |
+
# tokenizer = Wav2Vec2Tokenizer.from_pretrained("./models/tokenizer")
|
42 |
+
# model = Wav2Vec2ForCTC.from_pretrained("./models/model")
|
43 |
+
|
44 |
+
|
45 |
+
# # Function to download English word list
|
46 |
+
# def download_word_list():
|
47 |
+
# print("Downloading English word list...")
|
48 |
+
# url = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt"
|
49 |
+
# response = requests.get(url)
|
50 |
+
# words = set(response.text.split())
|
51 |
+
# print("Word list downloaded.")
|
52 |
+
# return words
|
53 |
+
|
54 |
+
# english_words = download_word_list()
|
55 |
+
|
56 |
+
# # Function to count correctly spelled words in text
|
57 |
+
# def count_spelled_words(text, word_list):
|
58 |
+
# print("Counting spelled words...")
|
59 |
+
# # Split the text into words
|
60 |
+
# words = re.findall(r'\b\w+\b', text.lower())
|
61 |
+
|
62 |
+
# correct = sum(1 for word in words if word in word_list)
|
63 |
+
# incorrect = len(words) - correct
|
64 |
+
|
65 |
+
# print("Spelling check complete.")
|
66 |
+
# return incorrect, correct
|
67 |
+
|
68 |
+
# # Function to apply spell check to an item (assuming it's a dictionary)
|
69 |
+
# def apply_spell_check(item, word_list):
|
70 |
+
# print("Applying spell check...")
|
71 |
+
# if isinstance(item, dict):
|
72 |
+
# # This is a single item
|
73 |
+
# text = item['transcription']
|
74 |
+
# incorrect, correct = count_spelled_words(text, word_list)
|
75 |
+
# item['incorrect_words'] = incorrect
|
76 |
+
# item['correct_words'] = correct
|
77 |
+
# print("Spell check applied to single item.")
|
78 |
+
# return item
|
79 |
+
# else:
|
80 |
+
# # This is likely a batch
|
81 |
+
# texts = item['transcription']
|
82 |
+
# results = [count_spelled_words(text, word_list) for text in texts]
|
83 |
|
84 |
+
# incorrect_counts, correct_counts = zip(*results)
|
85 |
|
86 |
+
# item = item.append_column('incorrect_words', pa.array(incorrect_counts))
|
87 |
+
# item = item.append_column('correct_words', pa.array(correct_counts))
|
88 |
|
89 |
+
# print("Spell check applied to batch of items.")
|
90 |
+
# return item
|
91 |
|
92 |
+
# # FastAPI routes
|
93 |
+
# @app.get('/')
|
94 |
+
# async def root():
|
95 |
+
# return "Welcome to the pronunciation scoring API!"
|
96 |
|
97 |
+
# @app.post('/check_post')
|
98 |
+
# async def rnc(number):
|
99 |
+
# return {
|
100 |
+
# "your value:" , number
|
101 |
+
# }
|
102 |
|
103 |
+
# @app.get('/check_get')
|
104 |
+
# async def get_rnc():
|
105 |
+
# return random.randint(0 , 10)
|
106 |
|
107 |
|
108 |
+
# @app.post('/fluency_score')
|
109 |
+
# async def fluency_scoring(file: UploadFile = File(...)):
|
110 |
+
# audio_array, sample_rate = librosa.load(file.file, sr=16000)
|
111 |
+
# print(audio_array)
|
112 |
+
# return audio_array[:5]
|
113 |
|
114 |
|
115 |
+
# @app.post('/pronunciation_score')
|
116 |
+
# async def pronunciation_scoring(file: UploadFile = File(...)):
|
117 |
+
# print("loading the file")
|
118 |
+
# url = "https://speech-processing-6.onrender.com/process_audio"
|
119 |
+
# files = {'file': await file.read()}
|
120 |
|
121 |
+
# print("file loaded")
|
122 |
|
123 |
+
# # print(files)
|
124 |
|
125 |
+
# print("making a POST request on speech processor")
|
126 |
|
127 |
+
# # Make the POST request
|
128 |
+
# response = requests.post(url, files=files)
|
129 |
|
130 |
+
# audio = response.json().get('audio_array')
|
131 |
|
132 |
+
# print("audio:" , audio[:5])
|
133 |
|
134 |
|
135 |
|
136 |
+
# print("length of the audio array:" , len(audio))
|
137 |
|
138 |
+
# print("*" * 100)
|
139 |
|
140 |
+
# # Tokenization
|
141 |
+
# print("Tokenizing audio...")
|
142 |
+
# input_values = tokenizer(
|
143 |
+
# audio,
|
144 |
+
# return_tensors="pt",
|
145 |
+
# padding="max_length",
|
146 |
+
# max_length= 386380,
|
147 |
+
# truncation=True
|
148 |
+
# ).input_values
|
149 |
|
150 |
+
# print(input_values.shape)
|
151 |
|
152 |
+
# print("Tokenization complete. Shape of input_values:", input_values.shape)
|
153 |
|
154 |
+
# return "tokenization successful"
|
155 |
|
156 |
+
# # Perform inference
|
157 |
+
# print("Performing inference with Wav2Vec2 model...")
|
158 |
|
159 |
+
# logits = model(input_values).logits
|
160 |
|
161 |
+
# print("Inference complete. Shape of logits:", logits.shape)
|
162 |
|
163 |
+
# # Get predictions
|
164 |
+
# print("Getting predictions...")
|
165 |
+
# prediction = torch.argmax(logits, dim=-1)
|
166 |
+
# print("Prediction shape:", prediction.shape)
|
167 |
|
168 |
+
# # Decode predictions
|
169 |
+
# print("Decoding predictions...")
|
170 |
+
# transcription = tokenizer.batch_decode(prediction)[0]
|
171 |
|
172 |
+
# # Convert transcription to lowercase
|
173 |
+
# transcription = transcription.lower()
|
174 |
|
175 |
+
# print("Decoded transcription:", transcription)
|
176 |
|
177 |
+
# incorrect, correct = count_spelled_words(transcription, english_words)
|
178 |
+
# print("Spelling check - Incorrect words:", incorrect, ", Correct words:", correct)
|
179 |
|
180 |
+
# # Calculate pronunciation score
|
181 |
+
# fraction = correct / (incorrect + correct)
|
182 |
+
# score = round(fraction * 100, 2)
|
183 |
+
# print("Pronunciation score for", transcription, ":", score)
|
184 |
|
185 |
+
# print("Pronunciation scoring process complete.")
|
186 |
|
187 |
+
# return {
|
188 |
+
# "transcription": transcription,
|
189 |
+
# "pronunciation_score": score
|
190 |
+
# }
|