gradio_okoshi / app.py
lequa's picture
Rename moji_okoshi.py to app.py
faa6052 verified
import os
from dotenv import load_dotenv
from openai import OpenAI
import gradio as gr
from pydub import AudioSegment
# Load environment variables
load_dotenv()
# Initialize OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Function to transcribe audio using Whisper
def transcribe_audio(file_path):
try:
with open(file_path, "rb") as audio_file:
response = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="ja"
)
return response.text
except Exception as e:
return f"An error occurred: {str(e)}"
# Function to structure transcription
def structure_transcription(transcription):
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "あなたのタスクは、入力された文章をルールに基づいて修正し、指定されたフォーマットに整えることです。文章の意味や意図を変えず、以下のガイドラインを厳守してください。必要に応じて補足説明を追加してくださいが、原文にない情報を追加することは避けてください。"},
{"role": "user", "content": """次の文章を以下のルールに従って書き換えてください:
ですます調をだである調に変更すること。
「総理」を「首相」に置き換えること(ただし「内閣総理大臣」といった熟語はそのままとする)。
「我が国」という表現を「日本」に置き換えること。
「我が党」を話者の所属政党名に置き換えること。
「につきまして」を「について」に置き換えること。
「〜等」を「〜など」と表現すること。
「参ります」を「いく」に置き換えること。
注意: 文章の抜け落ちや内容の改変は行わないこと。"""},
{"role": "user", "content": transcription}
]
)
return response.choices[0].message.content
except Exception as e:
return f"An error occurred while structuring transcription: {str(e)}"
# Function to handle input (either audio file or text)
def process_input(file, text_input):
try:
if file is not None:
# Convert to MP3
audio = AudioSegment.from_file(file)
mp3_path = "temp_output.mp3"
audio.export(mp3_path, format="mp3")
# Transcribe the audio
transcription = transcribe_audio(mp3_path)
# Format the transcription
formatted_transcription = structure_transcription(transcription)
return transcription, formatted_transcription
elif text_input.strip() != "":
# If only text is input, process it directly
formatted_text = structure_transcription(text_input)
return "", formatted_text
else:
return "エラー: 音声ファイルまたはテキストを入力してください", ""
except Exception as e:
return f"An error occurred during processing: {str(e)}", ""
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("## 音声文字起こし&修正アプリ")
gr.Markdown("音声ファイルをアップロードするか、テキストを直接入力してください。")
with gr.Row():
file_input = gr.File(label="音声ファイルをアップロード", file_types=[".wav", ".mp3", ".m4a"])
text_input = gr.Textbox(label="またはテキストを入力", lines=5, placeholder="ここにテキストを入力...")
output_transcription = gr.Textbox(label="文字起こし結果", lines=10)
output_formatted = gr.Textbox(label="修正後のテキスト", lines=10)
submit_button = gr.Button("実行")
submit_button.click(
process_input,
inputs=[file_input, text_input],
outputs=[output_transcription, output_formatted]
)
# Launch the Gradio app with shareable link
demo.launch(share=True)