Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from openai import OpenAI
|
4 |
+
from io import BytesIO
|
5 |
+
from pydub import AudioSegment
|
6 |
+
import imageio_ffmpeg
|
7 |
+
|
8 |
+
# imageio-ffmpegからffmpegの実行可能ファイルのパスを取得
|
9 |
+
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
10 |
+
|
11 |
+
# pydubで使用するffmpegのパスを設定
|
12 |
+
AudioSegment.converter = ffmpeg_path
|
13 |
+
AudioSegment.ffprobe = ffmpeg_path
|
14 |
+
|
15 |
+
# OpenAIクライアントの初期化
|
16 |
+
client = OpenAI()
|
17 |
+
|
18 |
+
def process_audio(audio_file, output_format):
|
19 |
+
# 音声ファイルを読み込む
|
20 |
+
audio_data = audio_file.read()
|
21 |
+
file_type = audio_file.name.split('.')[-1]
|
22 |
+
|
23 |
+
audio = AudioSegment.from_file(BytesIO(audio_data), format=file_type)
|
24 |
+
if file_type != "mp3":
|
25 |
+
mp3_buffer = BytesIO()
|
26 |
+
audio.export(mp3_buffer, format="mp3")
|
27 |
+
mp3_buffer.seek(0)
|
28 |
+
audio_data = mp3_buffer
|
29 |
+
|
30 |
+
# OpenAIを使用してテキストに変換
|
31 |
+
transcript = client.audio.transcriptions.create(
|
32 |
+
model="whisper-1",
|
33 |
+
file=BytesIO(audio_data)
|
34 |
+
)
|
35 |
+
|
36 |
+
if output_format == "テキスト":
|
37 |
+
return transcript.text
|
38 |
+
elif output_format == "Docx":
|
39 |
+
from docx import Document
|
40 |
+
doc = Document()
|
41 |
+
doc.add_paragraph(transcript.text)
|
42 |
+
docx_buffer = BytesIO()
|
43 |
+
doc.save(docx_buffer)
|
44 |
+
docx_buffer.seek(0)
|
45 |
+
return docx_buffer
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn=process_audio,
|
49 |
+
inputs=[
|
50 |
+
gr.inputs.Audio(type="file", label="音声ファイルをアップロード"),
|
51 |
+
gr.inputs.Radio(choices=["テキスト", "Docx"], label="出力フォーマットを選択")
|
52 |
+
],
|
53 |
+
outputs=[
|
54 |
+
gr.outputs.Textbox(label="テキスト出力") if output_format == "テキスト" else gr.outputs.File(label="Word文書をダウンロード")
|
55 |
+
],
|
56 |
+
title="音声ファイルをテキストに変換",
|
57 |
+
description="このツールは音声ファイルをテキストに変換します。出力形式としてテキストまたはWord文書を選択できます。"
|
58 |
+
)
|
59 |
+
|
60 |
+
iface.launch()
|