Spaces:
Runtime error
Runtime error
Jiang Xiaolan
commited on
Commit
·
73e28bf
1
Parent(s):
b18b636
update
Browse files- app.py +67 -79
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,92 +1,80 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from typing import Optional, List, Dict, Any
|
| 6 |
-
|
| 7 |
-
import torch
|
| 8 |
import gradio as gr
|
| 9 |
-
from transformers import pipeline
|
| 10 |
-
from transformers.pipelines.audio_utils import ffmpeg_read
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
BATCH_SIZE = 4
|
| 16 |
-
CHUNK_LENGTH_S = 15
|
| 17 |
-
FILE_LIMIT_MB = 1000
|
| 18 |
-
TOKEN = os.environ.get('HF_TOKEN', None)
|
| 19 |
-
# device setting
|
| 20 |
-
if torch.cuda.is_available():
|
| 21 |
-
torch_dtype = torch.bfloat16
|
| 22 |
-
device = "cuda:0"
|
| 23 |
-
else:
|
| 24 |
-
torch_dtype = torch.float32
|
| 25 |
-
device = "cpu"
|
| 26 |
-
# define the pipeline
|
| 27 |
-
pipe = pipeline(
|
| 28 |
-
model=MODEL_NAME,
|
| 29 |
-
chunk_length_s=CHUNK_LENGTH_S,
|
| 30 |
-
batch_size=BATCH_SIZE,
|
| 31 |
-
torch_dtype=torch_dtype,
|
| 32 |
-
device=device,
|
| 33 |
-
trust_remote_code=True,
|
| 34 |
-
token=TOKEN,
|
| 35 |
-
)
|
| 36 |
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"task": "transcribe",
|
| 42 |
-
"length_penalty": 0,
|
| 43 |
-
"num_beams": 2,
|
| 44 |
-
}
|
| 45 |
-
if prompt:
|
| 46 |
-
generate_kwargs['prompt_ids'] = pipe.tokenizer.get_prompt_ids(prompt, return_tensors='pt').to(device)
|
| 47 |
-
prediction = pipe(inputs, return_timestamps=True, generate_kwargs=generate_kwargs)
|
| 48 |
-
text = "".join([c['text'] for c in prediction['chunks']])
|
| 49 |
-
return text
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def transcribe(inputs: str):
|
| 53 |
-
if inputs is None:
|
| 54 |
-
raise gr.Error("音声ファイルが送信されていません!リクエストを送信する前に、音声ファイルをアップロードまたは録音してください。")
|
| 55 |
-
with open(inputs, "rb") as f:
|
| 56 |
-
inputs = f.read()
|
| 57 |
-
inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
|
| 58 |
-
inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}
|
| 59 |
-
return get_prediction(inputs)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
file_transcribe = gr.Interface(
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
)
|
| 87 |
|
| 88 |
-
with demo:
|
| 89 |
-
|
| 90 |
|
| 91 |
-
demo.queue(max_size=10)
|
| 92 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
+
import sys
|
| 4 |
+
import subprocess
|
|
|
|
|
|
|
|
|
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
+
logging.basicConfig(level=logging.ERROR)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
+
def clone_repo():
|
| 13 |
+
# 从环境变量中获取 GitHub Token
|
| 14 |
+
github_token = os.getenv('GH_TOKEN')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
if github_token is None:
|
| 17 |
+
logger.error("GitHub token is not set. Please set the GH_TOKEN secret in your Space settings.")
|
| 18 |
+
return False
|
| 19 |
+
|
| 20 |
+
# 使用 GitHub Token 进行身份验证并克隆仓库
|
| 21 |
+
clone_command = f'git clone https://{github_token}@github.com/mamba-ai/invoice_agent.git'
|
| 22 |
+
repo_dir = 'invoice_agent'
|
| 23 |
+
if os.path.exists(repo_dir):
|
| 24 |
+
logger.warning("Repository already exists.")
|
| 25 |
+
# 将仓库路径添加到 Python 模块搜索路径中
|
| 26 |
+
# logger.warning(f"Adding {os.path.abspath(repo_dir)} to sys.path")
|
| 27 |
+
# sys.path.append(os.path.abspath(repo_dir))
|
| 28 |
+
return True
|
| 29 |
+
else:
|
| 30 |
+
logger.info("Cloning repository...")
|
| 31 |
+
result = subprocess.run(clone_command, shell=True, capture_output=True, text=True)
|
| 32 |
+
|
| 33 |
+
if result.returncode == 0:
|
| 34 |
+
logger.warning("Repository cloned successfully.")
|
| 35 |
+
repo_dir = 'invoice_agent'
|
| 36 |
+
|
| 37 |
+
# 将仓库路径添加到 Python 模块搜索路径中
|
| 38 |
+
sys.path.append(os.path.abspath(repo_dir))
|
| 39 |
+
logger.warning(f"Adding {os.path.abspath(repo_dir)} to sys.path")
|
| 40 |
+
return True
|
| 41 |
+
else:
|
| 42 |
+
logger.error(f"Failed to clone repository: {result.stderr}")
|
| 43 |
+
return False
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
if clone_repo():
|
| 47 |
+
# 克隆成功后导入模块
|
| 48 |
+
import transcribe_agent.agent as ta
|
| 49 |
+
demo = gr.Blocks()
|
| 50 |
+
mf_transcribe = gr.Interface(
|
| 51 |
+
fn=ta.transcribe,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Audio(sources=["microphone"], type="filepath"),
|
| 54 |
+
],
|
| 55 |
+
outputs=["text"],
|
| 56 |
+
# layout="horizontal",
|
| 57 |
+
theme="huggingface",
|
| 58 |
+
title=f"オーディオをMambaVoice-v1で文字起こしする",
|
| 59 |
+
description=f"ボタンをクリックするだけで、長時間のマイク入力やオーディオ入力を文字起こしできます!デモではMambaVoice-v1モデルを使用しており、任意の長さの音声ファイルを文字起こしすることができます。",
|
| 60 |
+
allow_flagging="never",
|
| 61 |
+
)
|
| 62 |
|
| 63 |
+
file_transcribe = gr.Interface(
|
| 64 |
+
fn=ta.transcribe,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Audio(sources=["upload"], type="filepath", label="Audio file"),
|
| 67 |
+
],
|
| 68 |
+
outputs=["text"],
|
| 69 |
+
# layout="horizontal",
|
| 70 |
+
theme="huggingface",
|
| 71 |
+
title=f"オーディオをMambaVoice-v1で文字起こしする",
|
| 72 |
+
description=f"ボタンをクリックするだけで、長時間のマイク入力やオーディオ入力を文字起こしできます!デモではMambaVoice-v1モデルを使用しており、任意の長さの音声ファイルを文字起こしすることができます。",
|
| 73 |
+
allow_flagging="never",
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
with demo:
|
| 77 |
+
gr.TabbedInterface([mf_transcribe, file_transcribe], ["Microphone", "Audio file"])
|
| 78 |
|
| 79 |
+
demo.queue(max_size=10)
|
| 80 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -2,4 +2,6 @@ git+https://github.com/huggingface/transformers
|
|
| 2 |
torch
|
| 3 |
yt-dlp
|
| 4 |
punctuators==0.0.5
|
| 5 |
-
stable-ts==2.16.0
|
|
|
|
|
|
|
|
|
| 2 |
torch
|
| 3 |
yt-dlp
|
| 4 |
punctuators==0.0.5
|
| 5 |
+
stable-ts==2.16.0
|
| 6 |
+
torchaudio
|
| 7 |
+
pydub
|