AshDavid12 commited on
Commit
6465ad1
·
1 Parent(s): 860f8a3

hf home change

Browse files
Files changed (2) hide show
  1. infer.py +22 -24
  2. requirements.txt +2 -0
infer.py CHANGED
@@ -1,8 +1,10 @@
1
  import torch
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
  import soundfile as sf
4
- from fastapi import FastAPI, File, UploadFile
5
  import uvicorn
 
 
6
  import os
7
  from datetime import datetime
8
 
@@ -30,33 +32,36 @@ model.to(device)
30
  print(f"Model is using device: {device}")
31
 
32
 
33
- @app.post("/transcribe/")
34
- async def transcribe_audio(file: UploadFile = File(...)):
35
- # Print file upload start
36
- print(f"Received audio file: {file.filename}")
37
-
38
- # Save the uploaded file
39
- file_location = f"temp_{file.filename}"
40
  try:
41
- with open(file_location, "wb+") as f:
42
- f.write(await file.read())
43
- print(f"File saved to: {file_location}")
 
 
44
  except Exception as e:
45
- print(f"Error saving the file: {e}")
46
- return {"error": f"Error saving the file: {e}"}
47
 
48
- # Load the audio file and preprocess it
49
  try:
50
- audio_input, _ = sf.read(file_location)
51
- print(f"Audio file {file.filename} successfully read.")
 
 
 
52
 
 
 
53
  inputs = processor(audio_input, return_tensors="pt", sampling_rate=16000)
54
  print(f"Audio file preprocessed for transcription.")
55
  except Exception as e:
56
  print(f"Error processing the audio file: {e}")
57
  return {"error": f"Error processing the audio file: {e}"}
58
 
59
- # Move inputs to the same device as the model
60
  inputs = {key: value.to(device) for key, value in inputs.items()}
61
  print("Inputs moved to the appropriate device.")
62
 
@@ -77,13 +82,6 @@ async def transcribe_audio(file: UploadFile = File(...)):
77
  print(f"Error decoding the transcription: {e}")
78
  return {"error": f"Error decoding the transcription: {e}"}
79
 
80
- # Clean up the temporary file
81
- try:
82
- os.remove(file_location)
83
- print(f"Temporary file {file_location} deleted.")
84
- except Exception as e:
85
- print(f"Error deleting the temporary file: {e}")
86
-
87
  return {"transcription": transcription}
88
 
89
  @app.get("/")
 
1
  import torch
2
  from transformers import WhisperProcessor, WhisperForConditionalGeneration
3
  import soundfile as sf
4
+ from fastapi import FastAPI, File, UploadFile, Form
5
  import uvicorn
6
+ import requests
7
+ import io
8
  import os
9
  from datetime import datetime
10
 
 
32
  print(f"Model is using device: {device}")
33
 
34
 
35
+ @app.post("/transcribe-url/")
36
+ def transcribe_audio_url(audio_url: str = Form(...)):
37
+ # Download the audio file from the provided URL
 
 
 
 
38
  try:
39
+ response = requests.get(audio_url)
40
+ if response.status_code != 200:
41
+ return {"error": f"Failed to download audio from URL. Status code: {response.status_code}"}
42
+ print(f"Successfully downloaded audio from URL: {audio_url}")
43
+ audio_data = io.BytesIO(response.content) # Store audio data in memory
44
  except Exception as e:
45
+ print(f"Error downloading the audio file: {e}")
46
+ return {"error": f"Error downloading the audio file: {e}"}
47
 
48
+ # Process the audio
49
  try:
50
+ audio_input, _ = sf.read(audio_data) # Read the audio from the in-memory BytesIO
51
+ print(f"Audio file from URL successfully read.")
52
+ except Exception as e:
53
+ print(f"Error reading the audio file: {e}")
54
+ return {"error": f"Error reading the audio file: {e}"}
55
 
56
+ # Preprocess the audio for Whisper
57
+ try:
58
  inputs = processor(audio_input, return_tensors="pt", sampling_rate=16000)
59
  print(f"Audio file preprocessed for transcription.")
60
  except Exception as e:
61
  print(f"Error processing the audio file: {e}")
62
  return {"error": f"Error processing the audio file: {e}"}
63
 
64
+ # Move inputs to the appropriate device
65
  inputs = {key: value.to(device) for key, value in inputs.items()}
66
  print("Inputs moved to the appropriate device.")
67
 
 
82
  print(f"Error decoding the transcription: {e}")
83
  return {"error": f"Error decoding the transcription: {e}"}
84
 
 
 
 
 
 
 
 
85
  return {"transcription": transcription}
86
 
87
  @app.get("/")
requirements.txt CHANGED
@@ -3,5 +3,7 @@ uvicorn
3
  torch
4
  whisper
5
  python-multipart
 
 
6
  transformers
7
  soundfile
 
3
  torch
4
  whisper
5
  python-multipart
6
+ requests
7
+ io
8
  transformers
9
  soundfile