123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- # import os, sys
- # from typing import List, Any
- from fastapi import Request, APIRouter, UploadFile, File
- from fastapi.responses import FileResponse, PlainTextResponse
- # from fastapi import FastAPI, HTTPException, status
- # from fastapi.middleware.cors import CORSMiddleware
- # import uvicorn
- # from fastapi.exceptions import HTTPException
- # from fastapi.encoders import jsonable_encoder
- # from urllib.parse import urlparse, urljoin
- from pathlib import Path
- from api.openai_scripts_tai_gi.main_script import main
- from api.openai_scripts_chinese.main_script import main as main2
- # from api.openai_scripts_new.main_script import main as main3
- from datetime import datetime
- import random
- import string
- router = APIRouter()
- # router = FastAPI()
- # router.add_middleware(
- # CORSMiddleware,
- # allow_origins=["*"],
- # allow_credentials=True,
- # allow_methods=["*"],
- # allow_headers=["*"],
- # )
- @router.post('/tai_gi')
- async def whisper_auto(file: UploadFile = File()):
- if file == None:
- return {'message': '請上傳檔案'}
- extension = file.filename.split(".")[-1]
- if extension not in ("mp3", "wav", "webm"):
- return PlainTextResponse("Audio must be mp3, wav or webm format!", 400)
- filename = Path(__file__).parent.parent/'speech_audio'/datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.{extension}")
- with open(filename, 'wb') as f:
- f.write(await file.read())
- with open(filename, 'rb') as f:
- raw_transcript, corrected_transcript = main(f)
- # if raw_transcript and corrected_transcript:
- # os.remove(filename)
- # return {'message': corrected_transcript}
- # else:
- # os.remove(filename)
- # return {"message": "Audio processing failed."}
- if raw_transcript and corrected_transcript:
- # os.remove(save_path)
- # return {'message': {"Raw transcript": raw_transcript, "Corrected transcript": corrected_transcript}}
- return {'message': corrected_transcript}
- else:
- # os.remove(save_path)
- return {"message": "Audio processing failed."}
-
-
- @router.post('/chinese')
- async def whisper_auto(file: UploadFile = File()):
- if file == None:
- return {'message': '請上傳檔案'}
- extension = file.filename.split(".")[-1]
- if extension not in ("mp3", "wav", "webm"):
- return PlainTextResponse("Audio must be mp3, wav or webm format!", 400)
- filename = Path(__file__).parent.parent/'speech_audio'/datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.{extension}")
- with open(filename, 'wb') as f:
- f.write(await file.read())
- with open(filename, 'rb') as f:
- raw_transcript, corrected_transcript = main2(f)
- if raw_transcript and corrected_transcript:
- # return {'message': {"Raw transcript": raw_transcript, "Corrected transcript": corrected_transcript}}
- return {'message': corrected_transcript}
- else:
- return {"message": "Audio processing failed."}
-
- # import numpy as np
- # from transformers import pipeline
- # import gradio as gr
- # @router.post('/tai_gi_new')
- # async def whisper_auto(file: UploadFile = File()):
- # if file == None:
- # return {'message': '請上傳檔案'}
- # extension = file.filename.split(".")[-1]
- # if extension not in ("mp3", "wav", "webm"):
- # return PlainTextResponse("Audio must be mp3, wav or webm format!", 400)
- # filename = Path(__file__).parent/'speech_audio'/datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.{extension}")
- # with open(filename, 'wb') as f:
- # f.write(await file.read())
- # # with open(filename, 'rb') as f:
- # # raw_transcript, corrected_transcript = main3(f)
- # # audio_data, sample_rate = librosa.load(filename, sr=None)
- # # 讀取音頻文件
- # audio_input = gr.Audio(type="filepath")
- # raw_transcript, corrected_transcript = main3(audio_input(filename))
- # # if raw_transcript and corrected_transcript:
- # # os.remove(filename)
- # # return {'message': corrected_transcript}
- # # else:
- # # os.remove(filename)
- # # return {"message": "Audio processing failed."}
- # if raw_transcript and corrected_transcript:
- # # os.remove(save_path)
- # return {'message': {"Raw transcript": raw_transcript, "Corrected transcript": corrected_transcript}}
- # else:
- # # os.remove(save_path)
- # return {"message": "Audio processing failed."}
-
- # if __name__ == "__main__":
- # uvicorn.run("whisper:router", reload=False, port=8086, host='cmm.ai', ssl_keyfile="/etc/letsencrypt/live/cmm.ai/privkey.pem", ssl_certfile="/etc/letsencrypt/live/cmm.ai/fullchain.pem")
|