Spaces:
Running
Running
new
Browse files- app.py +182 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""【3種用】gradio_whisper_音声入力.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1MgpRKQUjqD5PFRRMte8UZd99YBR969Cw
|
8 |
+
"""
|
9 |
+
|
10 |
+
# !pip install gradio==4.0.2
|
11 |
+
!pip install gradio
|
12 |
+
!pip install openai
|
13 |
+
|
14 |
+
pip show gradio
|
15 |
+
|
16 |
+
import gradio as gr
|
17 |
+
from openai import OpenAI
|
18 |
+
import os
|
19 |
+
import time
|
20 |
+
|
21 |
+
audio_format = [".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm"
|
22 |
+
,".MP3", ".MP4", ".MPEG", ".MPGA", ".M4A", ".WAV", ".WEBM"]
|
23 |
+
|
24 |
+
lang_code = {'Japanese': "ja", 'English': "en"}
|
25 |
+
mode_code = {'文字': "transcribe", '翻訳': "translate"}
|
26 |
+
|
27 |
+
max_file_size = 25 * 1024 * 1024
|
28 |
+
|
29 |
+
def set_state(openai_key, lang, mode, state):
|
30 |
+
|
31 |
+
state["openai_key"]= openai_key
|
32 |
+
state["lang"] = lang_code[lang]
|
33 |
+
state["mode"] = mode_code[mode[0:2]]
|
34 |
+
|
35 |
+
return state
|
36 |
+
|
37 |
+
def create_textfile(voice_msg, up_file ,state):
|
38 |
+
|
39 |
+
# OpenAIキーチェック
|
40 |
+
if state["openai_key"] == "":
|
41 |
+
|
42 |
+
err_msg = "OpenAIキーを入力してください。(設定タブ)"
|
43 |
+
|
44 |
+
return voice_msg, up_file, "", None, err_msg
|
45 |
+
|
46 |
+
# ファイル入力チェック
|
47 |
+
if up_file is None and voice_msg is None:
|
48 |
+
|
49 |
+
err_msg = "マイクかファイルで音声を入力してください。"
|
50 |
+
|
51 |
+
return None, None, "", None, err_msg
|
52 |
+
|
53 |
+
# アップロードの場合ファイルチェック
|
54 |
+
if voice_msg is None:
|
55 |
+
|
56 |
+
# ファイルパスなど取得
|
57 |
+
file_name = os.path.splitext(os.path.basename(up_file.name))[0]
|
58 |
+
root, ext = os.path.splitext(up_file.name)
|
59 |
+
|
60 |
+
if ext not in audio_format:
|
61 |
+
|
62 |
+
# ファイル形式チェック
|
63 |
+
err_msg = "指定した形式のファイルをアップしてください。(mp3, mp4, mpeg, mpga, m4a, wav, webm)"
|
64 |
+
|
65 |
+
return None, None, "", None, err_msg
|
66 |
+
|
67 |
+
file_size = os.path.getsize(up_file.name)
|
68 |
+
|
69 |
+
if file_size >= max_file_size:
|
70 |
+
|
71 |
+
# ファイルサイズチェック
|
72 |
+
err_msg = "ファイルが大きすぎます。25MB未満にして下さい。"
|
73 |
+
|
74 |
+
return None, up_file.name, "", None, err_msg
|
75 |
+
|
76 |
+
# 音声ファイルセット
|
77 |
+
if voice_msg:
|
78 |
+
audio_file= open(voice_msg, "rb")
|
79 |
+
|
80 |
+
else:
|
81 |
+
audio_file= open(up_file.name, "rb")
|
82 |
+
|
83 |
+
try:
|
84 |
+
|
85 |
+
# whisperで変換処理
|
86 |
+
os.environ["OPENAI_API_KEY"] = state["openai_key"]
|
87 |
+
|
88 |
+
# クライアント新規作成
|
89 |
+
client = OpenAI()
|
90 |
+
|
91 |
+
# client作成後は消す
|
92 |
+
os.environ["OPENAI_API_KEY"] = ""
|
93 |
+
|
94 |
+
if state["mode"] == "transcribe":
|
95 |
+
|
96 |
+
# 文字起こし実行
|
97 |
+
trans_text = client.audio.transcriptions.create(model="whisper-1", file=audio_file ,language = state["lang"], response_format="text")
|
98 |
+
|
99 |
+
else:
|
100 |
+
|
101 |
+
# 翻訳文字起こし実行
|
102 |
+
trans_text = client.audio.translations.create(model="whisper-1", file=audio_file, response_format="text")
|
103 |
+
|
104 |
+
# ファイル名設定
|
105 |
+
if voice_msg:
|
106 |
+
trans_file = trans_text[:8] + "_whisper.txt"
|
107 |
+
else:
|
108 |
+
trans_file = file_name + "_whisper.txt"
|
109 |
+
|
110 |
+
with open(trans_file, mode="w") as f:
|
111 |
+
|
112 |
+
# テキストに書き出す
|
113 |
+
f.write(trans_text)
|
114 |
+
|
115 |
+
return None, None, trans_text, trans_file, ""
|
116 |
+
|
117 |
+
except Exception as e:
|
118 |
+
|
119 |
+
return voice_msg, None, "", None, e
|
120 |
+
|
121 |
+
def wait_mic_process():
|
122 |
+
|
123 |
+
# 録音後処理に時間がかかるので待つ
|
124 |
+
time.sleep(1)
|
125 |
+
|
126 |
+
return gr.update(interactive=True)
|
127 |
+
|
128 |
+
with gr.Blocks() as demo:
|
129 |
+
|
130 |
+
title = "<h2>Whisperデモアプリ【基本版】</h2>"
|
131 |
+
message = "<h3>最初に[設定]タブからOpenAIキーを入力してください。"
|
132 |
+
message += "</h3>"
|
133 |
+
|
134 |
+
gr.Markdown(title + message)
|
135 |
+
|
136 |
+
# セッションの宣言
|
137 |
+
state = gr.State({
|
138 |
+
"openai_key" : "",
|
139 |
+
"lang": "",
|
140 |
+
"mode" : "",
|
141 |
+
})
|
142 |
+
|
143 |
+
with gr.Tab("whisperを利用する") as maintab:
|
144 |
+
|
145 |
+
# 各コンポーネント定義
|
146 |
+
voice_msg=gr.components.Audio(sources="microphone",type="filepath", label="音声入力")
|
147 |
+
up_file = gr.File(file_types=[".mp3", ".mp4", ".mpeg", ".mpga", ".m4a", ".wav", ".webm"], label="音声ファイルアップロード", type="filepath")
|
148 |
+
|
149 |
+
# ボタン類
|
150 |
+
with gr.Row():
|
151 |
+
btn = gr.Button("文字に起こす")
|
152 |
+
clear = gr.ClearButton([voice_msg, up_file], value="クリア")
|
153 |
+
|
154 |
+
# 出力
|
155 |
+
sys_msg = gr.Text(label="システムメッセージ")
|
156 |
+
text = gr.TextArea(label="文字起こし内容")
|
157 |
+
file = gr.File(label="出力テキストファイル")
|
158 |
+
|
159 |
+
# 送信ボタンクリック時の処理
|
160 |
+
btn.click(create_textfile, inputs=[voice_msg, up_file, state], outputs=[voice_msg, up_file, text, file, sys_msg], queue=True)
|
161 |
+
|
162 |
+
# 音声録音後処理に時間がかかるのでボタンを利用不可にする
|
163 |
+
voice_msg.start_recording(lambda:gr.update(interactive=False), None, btn)
|
164 |
+
voice_msg.stop_recording(wait_mic_process, None, btn)
|
165 |
+
|
166 |
+
with gr.Tab("設定") as settab:
|
167 |
+
openai_key = gr.Textbox(label="OpenAI API Key", interactive = True)
|
168 |
+
lang = gr.Dropdown(label="Language", choices=["Japanese", "English"], value = "Japanese", interactive = True)
|
169 |
+
mode = gr.Dropdown(label="Mode", choices=["文字起こし", "翻訳+文字起こし"], value = "文字起こし", interactive = True)
|
170 |
+
|
171 |
+
# 設定変更時
|
172 |
+
maintab.select(set_state, [openai_key, lang, mode, state], state)
|
173 |
+
|
174 |
+
with gr.Tab("利用上の注意"):
|
175 |
+
|
176 |
+
gr.Markdown("・マイクの音声とファイル入力の両方がある場合はマイクが優先されます。")
|
177 |
+
gr.Markdown("・文字に起こせるファイル形式は mp3, mp4, mpeg, mpga, m4a, wav, webm のみです。")
|
178 |
+
gr.Markdown("・翻訳は日本語の音声を英語のテキストに変えます。英語の音声→日本語はできません。")
|
179 |
+
gr.Markdown("※翻訳機能はマイク入力の精度が低いです。ファイルでの入力をおすすめします。")
|
180 |
+
|
181 |
+
demo.queue()
|
182 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# gradio==4.19.0
|
2 |
+
openai==1.12.0
|