Spaces:
Running
Running
| import gradio as gr | |
| import os | |
| import requests | |
| from tqdm import tqdm | |
| def check_and_download_model(): | |
| temp_dir = "/tmp" | |
| model_path = os.path.join(temp_dir, "large-v2.pt") | |
| if os.path.exists(model_path): | |
| return f"モデルは既に存在します: {model_path}" | |
| url = "https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt" | |
| try: | |
| response = requests.get(url, stream=True) | |
| response.raise_for_status() | |
| total_size = int(response.headers.get('content-length', 0)) | |
| with open(model_path, 'wb') as f, tqdm( | |
| desc=model_path, | |
| total=total_size, | |
| unit='iB', | |
| unit_scale=True, | |
| unit_divisor=1024, | |
| ) as pbar: | |
| for data in response.iter_content(chunk_size=1024): | |
| size = f.write(data) | |
| pbar.update(size) | |
| return f"モデルのダウンロードが完了しました: {model_path}" | |
| except Exception as e: | |
| return f"エラーが発生しました: {e}" | |
| def click_test(): | |
| """1から10までのランダムな数値を生成する関数""" | |
| import random | |
| number = random.randint(1, 10) | |
| return f"生成された数値: {number}" | |
| # Gradio インターフェースの作成 | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Whisper モデルチェッカー & ランダム数値ジェネレーター") | |
| gr.Markdown("下のボタンでモデルの確認・ダウンロードと、ランダム数値の生成ができます。") | |
| # 結果表示用のテキストボックス | |
| model_status = gr.Text(label="モデルの状態") | |
| output_text = gr.Text(label="ランダム数値の結果") | |
| with gr.Row(): | |
| # モデルチェック用ボタン | |
| check_btn = gr.Button( | |
| value="モデルを確認する", | |
| variant="secondary", | |
| size="lg" | |
| ) | |
| # ランダム数値生成用ボタン | |
| generate_btn = gr.Button( | |
| value="数値を生成する", | |
| variant="primary", | |
| size="lg" | |
| ) | |
| # ボタンクリック時のイベント設定 | |
| check_btn.click( | |
| fn=check_and_download_model, | |
| outputs=model_status | |
| ) | |
| generate_btn.click( | |
| fn=click_test, | |
| outputs=output_text | |
| ) | |
| # アプリケーションの起動 | |
| if __name__ == "__main__": | |
| demo.launch() |