Uotpia commited on
Commit
b8c0954
·
verified ·
1 Parent(s): 17fab7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -78
app.py CHANGED
@@ -1,18 +1,15 @@
 
 
1
  import torch
2
-
3
  import spaces
4
  import gradio as gr
5
  import yt_dlp as youtube_dl
 
 
6
  from transformers import pipeline
7
- from transformers.pipelines.audio_utils import ffmpeg_read
8
-
9
- import tempfile
10
- import os
11
 
12
  MODEL_NAME = "openai/whisper-large-v3"
13
  BATCH_SIZE = 8
14
- FILE_LIMIT_MB = 1000
15
- YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
16
 
17
  device = 0 if torch.cuda.is_available() else "cpu"
18
 
@@ -23,86 +20,83 @@ pipe = pipeline(
23
  device=device,
24
  )
25
 
 
 
 
26
  @spaces.GPU
27
- def transcribe(inputs, task):
 
 
 
 
 
 
 
 
 
28
  if inputs is None:
29
- raise gr.Error("No audio file submitted! Please upload or record an audio file before submitting your request.")
30
-
31
- text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
32
- return text
33
-
34
-
35
- def _return_yt_html_embed(yt_url):
36
- video_id = yt_url.split("?v=")[-1]
37
- HTML_str = (
38
- f'<center> <iframe width="500" height="320" src="https://www.youtube.com/embed/{video_id}"> </iframe>'
39
- " </center>"
40
- )
41
- return HTML_str
42
-
43
- def download_yt_audio(yt_url, filename):
44
- info_loader = youtube_dl.YoutubeDL()
45
-
46
- try:
47
- info = info_loader.extract_info(yt_url, download=False)
48
- except youtube_dl.utils.DownloadError as err:
49
- raise gr.Error(str(err))
50
-
51
- file_length = info["duration_string"]
52
- file_h_m_s = file_length.split(":")
53
- file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
54
-
55
- if len(file_h_m_s) == 1:
56
- file_h_m_s.insert(0, 0)
57
- if len(file_h_m_s) == 2:
58
- file_h_m_s.insert(0, 0)
59
- file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
60
-
61
- if file_length_s > YT_LENGTH_LIMIT_S:
62
- yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
63
- file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
64
- raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
65
-
66
- ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
67
-
68
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
69
- try:
70
- ydl.download([yt_url])
71
- except youtube_dl.utils.ExtractorError as err:
72
- raise gr.Error(str(err))
73
-
74
- @spaces.GPU
75
- def yt_transcribe(yt_url, task, max_filesize=75.0):
76
- html_embed_str = _return_yt_html_embed(yt_url)
77
-
78
- with tempfile.TemporaryDirectory() as tmpdirname:
79
- filepath = os.path.join(tmpdirname, "video.mp4")
80
- download_yt_audio(yt_url, filepath)
81
- with open(filepath, "rb") as f:
82
- inputs = f.read()
83
-
84
- inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
85
- inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
86
-
87
- text = pipe(inputs, batch_size=BATCH_SIZE, generate_kwargs={"task": task}, return_timestamps=True)["text"]
88
-
89
- return html_embed_str, text
90
-
91
 
 
 
 
92
  demo = gr.Interface(
93
- fn=transcribe,
94
  inputs=[
95
  gr.Audio(type="filepath"),
96
  gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
97
  ],
98
  outputs=gr.Textbox(lines=3),
99
- title="Whisper Large V3: Transcribe Audio",
100
- description=(
101
- "Transcribe long-form microphone or audio inputs with the click of a button! Demo uses the OpenAI Whisper"
102
- f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to transcribe audio files"
103
- " of arbitrary length."
104
- ),
105
  allow_flagging="never",
106
  )
107
 
108
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
  import torch
 
4
  import spaces
5
  import gradio as gr
6
  import yt_dlp as youtube_dl
7
+ from fastapi import FastAPI, Form, UploadFile, File, HTTPException, Header
8
+ from fastapi.responses import JSONResponse
9
  from transformers import pipeline
 
 
 
 
10
 
11
  MODEL_NAME = "openai/whisper-large-v3"
12
  BATCH_SIZE = 8
 
 
13
 
14
  device = 0 if torch.cuda.is_available() else "cpu"
15
 
 
20
  device=device,
21
  )
22
 
23
+ # ---------------------------------------------------------------------------
24
+ # 核心转录逻辑
25
+ # ---------------------------------------------------------------------------
26
  @spaces.GPU
27
+ def run_transcription(audio_bytes_or_path, task: str):
28
+ return pipe(
29
+ audio_bytes_or_path,
30
+ batch_size=BATCH_SIZE,
31
+ generate_kwargs={"task": task},
32
+ return_timestamps=True,
33
+ )["text"]
34
+
35
+ # Gradio 调用的封装
36
+ def gradio_transcribe(inputs, task):
37
  if inputs is None:
38
+ raise gr.Error("No audio file submitted!")
39
+ return run_transcription(inputs, task)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ # ---------------------------------------------------------------------------
42
+ # 1. 构建 Gradio 界面
43
+ # ---------------------------------------------------------------------------
44
  demo = gr.Interface(
45
+ fn=gradio_transcribe,
46
  inputs=[
47
  gr.Audio(type="filepath"),
48
  gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
49
  ],
50
  outputs=gr.Textbox(lines=3),
51
+ title="Whisper Large V3 API Server",
 
 
 
 
 
52
  allow_flagging="never",
53
  )
54
 
55
+ # ---------------------------------------------------------------------------
56
+ # 2. 构建兼容 OpenAI 格式的 FastAPI 应用
57
+ # ---------------------------------------------------------------------------
58
+ app = FastAPI()
59
+
60
+ async def handle_openai_audio_request(
61
+ file: UploadFile = File(...),
62
+ model: str = Form("whisper-1"),
63
+ response_format: str = Form("json"),
64
+ task: str = "transcribe",
65
+ ):
66
+ try:
67
+ # 读取上传的文件内容
68
+ audio_bytes = await file.read()
69
+
70
+ # 调用推理核心
71
+ text = run_transcription(audio_bytes, task=task)
72
+
73
+ if response_format == "text":
74
+ return text
75
+ else:
76
+ # 默认返回标准的 OpenAI JSON 结构
77
+ return {"text": text}
78
+ except Exception as e:
79
+ raise HTTPException(status_code=500, detail=str(e))
80
+
81
+ # OpenAI 转录接口
82
+ @app.post("/v1/audio/transcriptions")
83
+ async def audio_transcriptions(
84
+ file: UploadFile = File(...),
85
+ model: str = Form("whisper-1"),
86
+ response_format: str = Form("json"),
87
+ ):
88
+ return await handle_openai_audio_request(file, model, response_format, task="transcribe")
89
+
90
+ # OpenAI 翻译接口
91
+ @app.post("/v1/audio/translations")
92
+ async def audio_translations(
93
+ file: UploadFile = File(...),
94
+ model: str = Form("whisper-1"),
95
+ response_format: str = Form("json"),
96
+ ):
97
+ return await handle_openai_audio_request(file, model, response_format, task="translate")
98
+
99
+ # ---------------------------------------------------------------------------
100
+ # 3. 将 Gradio 挂载到 FastAPI 上
101
+ # ---------------------------------------------------------------------------
102
+ app = gr.mount_gradio_app(app, demo, path="/")