Pranjal12345 commited on
Commit
172d443
·
1 Parent(s): 3c9ba70

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +1 -29
main.py CHANGED
@@ -1,26 +1,5 @@
1
  #uvicorn app:app --host 0.0.0.0 --port 8000 --reload
2
 
3
- # from fastapi import FastAPI
4
- # from transformers import pipeline
5
-
6
- # pipe = pipeline("automatic-speech-recognition", model="Pranjal12345/whisper-small-ne-pranjal")
7
-
8
- # audio_path = "/home/pranjal/Downloads/chinese_audio.mp3"
9
-
10
- # with open("/home/pranjal/Downloads/chinese_audio.mp3", "rb") as audio_file:
11
- # audio_data = audio_file.read()
12
-
13
- # app = FastAPI()
14
-
15
- # @app.get("/")
16
- # def hello():
17
- # output = pipe(input)
18
- # return {"Output": output}
19
-
20
-
21
-
22
-
23
-
24
 
25
  from fastapi import FastAPI
26
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
@@ -28,26 +7,19 @@ import librosa
28
 
29
  app = FastAPI()
30
 
31
- # Load model and processor
32
  processor = WhisperProcessor.from_pretrained("openai/whisper-small")
33
  model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
34
  model.config.forced_decoder_ids = None
35
 
36
- # Path to your audio file
37
  audio_file_path = "output.mp3"
38
 
39
- # Read the audio file
40
  audio_data, _ = librosa.load(audio_file_path, sr=16000)
41
 
42
  @app.get("/")
43
  def transcribe_audio():
44
- # Process the audio data using the Whisper processor
45
  input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
46
 
47
- # Generate transcription
48
  predicted_ids = model.generate(input_features)
49
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
50
 
51
- return {"transcription": transcription[0]}
52
-
53
-
 
1
  #uvicorn app:app --host 0.0.0.0 --port 8000 --reload
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  from fastapi import FastAPI
5
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
 
7
 
8
  app = FastAPI()
9
 
 
10
  processor = WhisperProcessor.from_pretrained("openai/whisper-small")
11
  model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
12
  model.config.forced_decoder_ids = None
13
 
 
14
  audio_file_path = "output.mp3"
15
 
 
16
  audio_data, _ = librosa.load(audio_file_path, sr=16000)
17
 
18
  @app.get("/")
19
  def transcribe_audio():
 
20
  input_features = processor(audio_data.tolist(), return_tensors="pt").input_features
21
 
 
22
  predicted_ids = model.generate(input_features)
23
  transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
24
 
25
+ return {"transcription": transcription[0]}