randydev commited on
Commit
3b52020
1 Parent(s): 01f2fab

Update whisper.py

Browse files
Files changed (1) hide show
  1. whisper.py +15 -28
whisper.py CHANGED
@@ -3,49 +3,36 @@ import requests
3
  from fastapi import APIRouter
4
  from pydantic import BaseModel
5
  from dotenv import load_dotenv
 
 
 
6
  from models import *
7
 
8
- class WhisperX(BaseModel):
9
- filename: str
10
-
11
  router = APIRouter()
12
 
13
  load_dotenv()
14
  HUGGING_TOKEN = os.environ["HUGGING_TOKEN"]
15
 
16
- async def whsiper_to_text(filename):
17
- if not os.path.isfile(filename):
18
- print(f"Error: File {filename} does not exist.")
19
- return None
20
-
21
  API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
22
  headers = {"Authorization": f"Bearer {HUGGING_TOKEN}"}
23
-
24
- with open(filename, "rb") as f:
25
- files = {'file': f}
26
- response = requests.post(API_URL, headers=headers, files=files)
27
-
28
  if response.status_code != 200:
29
  print(f"Error status {response.status_code}")
30
  return None
31
  return response.json()
32
 
33
- @router.post("/akeno/whsiper", response_model=SuccessResponse, responses={422: {"model": SuccessResponse}})
34
- async def whsiper_new(payload: WhisperX):
35
  try:
36
- response_data = await whsiper_to_text(payload.filename)
37
  if response_data is None:
38
- return SuccessResponse(
39
- status="False",
40
- randydev={"error": f"Failed to whisper the filename '{payload.filename}'"}
41
- )
42
 
43
- return SuccessResponse(
44
- status="True",
45
- randydev={"message": response_data.get("text")}
46
- )
47
- except Exception as e:
48
- return SuccessResponse(
49
- status="False",
50
- randydev={"error": f"An error occurred: {str(e)}"}
51
  )
 
 
 
3
  from fastapi import APIRouter
4
  from pydantic import BaseModel
5
  from dotenv import load_dotenv
6
+ from fastapi import APIRouter, UploadFile, File, HTTPException
7
+ from fastapi.responses import JSONResponse
8
+ from dotenv import load_dotenv
9
  from models import *
10
 
 
 
 
11
  router = APIRouter()
12
 
13
  load_dotenv()
14
  HUGGING_TOKEN = os.environ["HUGGING_TOKEN"]
15
 
16
+ async def whsiper_to_text(file: UploadFile):
 
 
 
 
17
  API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
18
  headers = {"Authorization": f"Bearer {HUGGING_TOKEN}"}
19
+ contents = await file.read()
20
+ response = requests.post(API_URL, headers=headers, data=contents)
 
 
 
21
  if response.status_code != 200:
22
  print(f"Error status {response.status_code}")
23
  return None
24
  return response.json()
25
 
26
+ @router.post("akeno/whsiper")
27
+ async def whsiper_new(file: UploadFile = File(...)):
28
  try:
29
+ response_data = await whsiper_to_text(file)
30
  if response_data is None:
31
+ raise HTTPException(status_code=400, detail="Failed to process the file")
 
 
 
32
 
33
+ return JSONResponse(
34
+ status_code=200,
35
+ content={"status": "True", "message": response_data.get("text")}
 
 
 
 
 
36
  )
37
+ except Exception as e:
38
+ raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")