File size: 8,808 Bytes
71f4c16
b4cbbe2
 
71f4c16
 
 
 
ed343f7
71f4c16
ed343f7
71f4c16
 
 
ed343f7
71f4c16
ed343f7
71f4c16
ed343f7
71f4c16
 
 
ed343f7
71f4c16
 
ed343f7
71f4c16
ed343f7
 
71f4c16
 
 
3381d4a
 
71f4c16
 
3381d4a
 
 
 
71f4c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed343f7
71f4c16
 
 
 
 
 
 
 
 
 
ed343f7
71f4c16
 
 
ed343f7
 
71f4c16
 
ed343f7
 
 
 
71f4c16
 
3381d4a
 
71f4c16
3381d4a
71f4c16
ed343f7
3381d4a
ed343f7
3381d4a
 
 
 
ed343f7
3381d4a
71f4c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3381d4a
71f4c16
3381d4a
 
 
 
 
 
 
71f4c16
 
3381d4a
71f4c16
 
3381d4a
 
ed343f7
3381d4a
 
 
71f4c16
 
3381d4a
 
ed343f7
 
3381d4a
ed343f7
3381d4a
ed343f7
 
 
71f4c16
 
 
 
 
 
 
 
ed343f7
 
3381d4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed343f7
71f4c16
 
ed343f7
71f4c16
 
 
 
ed343f7
71f4c16
3381d4a
 
 
71f4c16
 
ed343f7
3381d4a
ed343f7
 
b4cbbe2
ed343f7
71f4c16
ed343f7
71f4c16
b4cbbe2
 
71f4c16
 
 
 
 
 
3381d4a
71f4c16
 
 
3381d4a
71f4c16
 
b4cbbe2
 
ed343f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71f4c16
ed343f7
71f4c16
b4cbbe2
71f4c16
 
 
 
 
 
 
 
 
 
 
 
b4cbbe2
71f4c16
 
f2021d6
71f4c16
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# # #uvicorn app:app --host 0.0.0.0 --port 8000 --reload


# # # from fastapi import FastAPI
# # # from transformers import WhisperProcessor, WhisperForConditionalGeneration
# # # import librosa
# # # import uvicorn

# # # app = FastAPI()

# # # processor = WhisperProcessor.from_pretrained("openai/whisper-small")
# # # model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
# # # model.config.forced_decoder_ids = None

# # # audio_file_path = "output.mp3"

# # # audio_data, _ = librosa.load(audio_file_path, sr=16000)

# # # @app.get("/")
# # # def transcribe_audio():
# # #         input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
        
# # #         predicted_ids = model.generate(input_features)
# # #         transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
        
# # #         return {"transcription": transcription[0]}


# # # if __name__ == "__main__":
# # #     import uvicorn
# # #     uvicorn.run(app, host="0.0.0.0", port=8000)


# # # if __name__=='__main__':
# # #     uvicorn.run('main:app', reload=True)




# # #uvicorn app:app --host 0.0.0.0 --port 8000 --reload
# # #curl -X GET "http://localhost:8000/?text=I%20like%20Apples"
# # #http://localhost:8000/?text=I%20like%20Apples








# # # from fastapi import FastAPI
# # # from transformers import WhisperProcessor, WhisperForConditionalGeneration
# # # import librosa
# # # import uvicorn

# # # app = FastAPI()

# # # # Load model and processor
# # # processor = WhisperProcessor.from_pretrained("openai/whisper-small")
# # # model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
# # # model.config.forced_decoder_ids = None

# # # # Path to your audio file
# # # audio_file_path = "/home/pranjal/Downloads/output.mp3"

# # # # Read the audio file
# # # audio_data, _ = librosa.load(audio_file_path, sr=16000)

# # # @app.get("/")
# # # def transcribe_audio():
# # #         # Process the audio data using the Whisper processor
# # #         input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
        
# # #         # Generate transcription
# # #         predicted_ids = model.generate(input_features)
# # #         transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
        
# # #         return {"transcription": transcription[0]}

# # # if __name__ == "__main__":
# # #     import uvicorn
# # #     uvicorn.run(app, host="0.0.0.0", port=8000)


# # # if __name__=='__app__':
# # #     uvicorn.run('main:app', reload=True)





# # from fastapi import FastAPI, UploadFile, File
# # from transformers import WhisperProcessor, WhisperForConditionalGeneration
# # import librosa
# # from fastapi.responses import HTMLResponse
# # import uvicorn
# # import io

# # app = FastAPI()

# # # Load model and processor
# # processor = WhisperProcessor.from_pretrained("openai/whisper-small")
# # model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
# # model.config.forced_decoder_ids = None

# # @app.get("/")
# # def read_root():
# #     html_form = """
# #     <html>
# #         <body>
# #             <h2>ASR Transcription</h2>
# #             <form action="/transcribe" method="post" enctype="multipart/form-data">
# #                 <label for="audio_file">Upload an audio file (MP3 or WAV):</label>
# #                 <input type="file" id="audio_file" name="audio_file" accept=".mp3, .wav" required><br><br>
# #                 <input type="submit" value="Transcribe">
# #             </form>
# #         </body>
# #     </html>
# #     """
# #     return HTMLResponse(content=html_form, status_code=200)

# # @app.post("/transcribe")
# # async def transcribe_audio(audio_file: UploadFile):
# #     try:
# #         # Read the uploaded audio file
# #         audio_data = await audio_file.read()
        
# #         # Process the audio data using the Whisper processor
# #         audio_data, _ = librosa.load(io.BytesIO(audio_data), sr=16000)
# #         input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
        
# #         # Generate transcription
# #         predicted_ids = model.generate(input_features)
# #         transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
        
# #         return {"transcription": transcription[0]}
# #     except Exception as e:
# #         return {"error": str(e)}

# # if __name__ == "__app__":
# #     uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)






# #uvicorn app:app --host 0.0.0.0 --port 8000 --reload


# from fastapi import FastAPI, UploadFile, File
# from transformers import WhisperProcessor, WhisperForConditionalGeneration
# import librosa
# from fastapi.responses import HTMLResponse
# import uvicorn
# import io

# app = FastAPI()

# # # Load model and processor
# # processor = WhisperProcessor.from_pretrained("openai/whisper-medium")
# # model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-medium")
# # model.config.forced_decoder_ids = None

# import whisper
# model = whisper.load_model("small")


# @app.get("/")
# def read_root():
#     html_form = """
#     <html>
#         <body>
#             <h2>ASR Transcription</h2>
#             <form action="/transcribe" method="post" enctype="multipart/form-data">
#                 <label for="audio_file">Upload an audio file (MP3 or WAV):</label>
#                 <input type="file" id="audio_file" name="audio_file" accept=".mp3, .wav" required><br><br>
#                 <input type="submit" value="Transcribe">
#             </form>
#         </body>
#     </html>
#     """
#     return HTMLResponse(content=html_form, status_code=200)

# @app.post("/transcribe")
# async def transcribe_audio(audio_file: UploadFile):
#     try:
#         # Read the uploaded audio file
#         audio_data = await audio_file.read()
        
#         # Process the audio data using the Whisper processor
#         # audio_data, _ = librosa.load(io.BytesIO(audio_data), sr=16000)
#         # input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
        
#         # # Generate transcription
#         # predicted_ids = model.generate(input_features)
#         # transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
#         result = model.transcribe("/home/pranjal/Downloads/rt.mp3")
        
#         return {"transcription": result['text']}
#     except Exception as e:
#         return {"error": str(e)}

# # if __name__ == "__app__":
# #     uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)

#uvicorn app:app --host 0.0.0.0 --port 8000 --reload

from fastapi import FastAPI, UploadFile, File
from transformers import WhisperProcessor, WhisperForConditionalGeneration
from fastapi.responses import HTMLResponse
import librosa
import io
import re


html_tag_remover = re.compile(r'<[^>]+>')

def remove_tags(text):
  return html_tag_remover.sub('', text)

app = FastAPI()

processor = WhisperProcessor.from_pretrained("openai/whisper-small")
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
model.config.forced_decoder_ids = None

chunk_duration = 30 
overlap_duration = 5 

@app.get("/")
def read_root():
    html_form = """
    <html>
        <body>
            <h2>ASR Transcription</h2>
            <form action="/transcribe" method="post" enctype="multipart/form-data">
                <label for="audio_file">Upload an audio file (MP3 or WAV):</label>
                <input type="file" id="audio_file" name="audio_file" accept=".mp3, .wav" required><br><br>
                <input type="submit" value="Transcribe">
            </form>
        </body>
    </html>
    """
    return HTMLResponse(content=html_form, status_code=200)

@app.post("/transcribe")
async def transcribe_audio(audio_file: UploadFile):
        audio_data = await audio_file.read()
        audio_data, _ = librosa.load(io.BytesIO(audio_data), sr=16000)
        
        transcription = []
        
        start = 0
        while start < len(audio_data):
            end = start + chunk_duration * 16000
            audio_chunk = audio_data[start:end]
            
            input_features = processor(audio_chunk.tolist(), return_tensors="pt").input_features
            predicted_ids = model.generate(input_features, max_length=1000)
            chunk_transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)

            transcription.extend(chunk_transcription) 

            start = end - overlap_duration * 16000
        
        final_transcription = " ".join(transcription)
        final_transcription = remove_tags(final_transcription)

        
        return {"transcription": final_transcription}