Spaces:
Build error
Build error
Upload 8 files
Browse files- Dockerfile +11 -13
- README_hf.md +43 -0
- app_docker.py +114 -0
- requirements_hf.txt +11 -0
Dockerfile
CHANGED
|
@@ -1,27 +1,25 @@
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
-
# システムの
|
| 4 |
-
RUN apt-get update && \
|
| 5 |
-
apt-get install -y --no-install-recommends \
|
| 6 |
build-essential \
|
| 7 |
curl \
|
|
|
|
| 8 |
git \
|
| 9 |
-
&&
|
| 10 |
-
&& rm -rf /var/lib/apt/lists/* \
|
| 11 |
-
|| echo "Warning: Some packages may not have installed correctly"
|
| 12 |
|
| 13 |
-
# 作業ディレクトリ
|
| 14 |
WORKDIR /app
|
| 15 |
|
| 16 |
# Pythonの依存関係をインストール
|
| 17 |
-
COPY
|
| 18 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
|
| 20 |
-
# アプリケーション
|
| 21 |
COPY . .
|
| 22 |
|
| 23 |
-
# ポート
|
| 24 |
-
EXPOSE
|
| 25 |
|
| 26 |
-
# アプリケーション
|
| 27 |
-
CMD ["python", "
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
+
# システムパッケージの更新とインストール
|
| 4 |
+
RUN apt-get update && apt-get install -y \
|
|
|
|
| 5 |
build-essential \
|
| 6 |
curl \
|
| 7 |
+
software-properties-common \
|
| 8 |
git \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# 作業ディレクトリの設定
|
| 12 |
WORKDIR /app
|
| 13 |
|
| 14 |
# Pythonの依存関係をインストール
|
| 15 |
+
COPY requirements_hf.txt requirements.txt
|
| 16 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
|
| 18 |
+
# アプリケーションファイルをコピー
|
| 19 |
COPY . .
|
| 20 |
|
| 21 |
+
# ポートの公開
|
| 22 |
+
EXPOSE 7860
|
| 23 |
|
| 24 |
+
# アプリケーションの起動
|
| 25 |
+
CMD ["python", "app_docker.py"]
|
README_hf.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: 年代・性別推定システム
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
app_port: 7860
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# 年代・性別推定システム
|
| 13 |
+
|
| 14 |
+
日本語テキストから年代と性別を推定するAIシステムです。BERTベースのモデルを使用して、入力されたテキストの特徴から年代(10代〜60代)と性別(男性・女性)を確率で予測します。
|
| 15 |
+
|
| 16 |
+
## 機能
|
| 17 |
+
|
| 18 |
+
- **年代推定**: 10代、20代、30代、40代、50代、60代の6つの年代を確率で予測
|
| 19 |
+
- **性別推定**: 男性・女性を確率で予測
|
| 20 |
+
- **リアルタイム予測**: Webアプリケーションでリアルタイムに予測結果を表示
|
| 21 |
+
|
| 22 |
+
## 技術仕様
|
| 23 |
+
|
| 24 |
+
- **ベースモデル**: cl-tohoku/bert-large-japanese
|
| 25 |
+
- **フレームワーク**: PyTorch, Transformers, Gradio
|
| 26 |
+
- **デプロイ**: Docker on Hugging Face Spaces
|
| 27 |
+
|
| 28 |
+
## 使用方法
|
| 29 |
+
|
| 30 |
+
1. テキストボックスに日本語のテキストを入力
|
| 31 |
+
2. 「推測実行」ボタンをクリック
|
| 32 |
+
3. 年代と性別の確率が表示されます
|
| 33 |
+
|
| 34 |
+
## モデル詳細
|
| 35 |
+
|
| 36 |
+
- **年代モデル**: 各年代を独立した二値分類器として学習
|
| 37 |
+
- **性別モデル**: 2クラス分類(男性・女性)
|
| 38 |
+
- **学習データ**: 日本語テキストデータセット
|
| 39 |
+
- **精度**: 年代推定 約79%、性別推定 約70%
|
| 40 |
+
|
| 41 |
+
## ライセンス
|
| 42 |
+
|
| 43 |
+
MIT License
|
app_docker.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import gzip
|
| 5 |
+
import shutil
|
| 6 |
+
from predictor import load_models, predict_text
|
| 7 |
+
|
| 8 |
+
def extract_compressed_models():
|
| 9 |
+
"""圧縮されたモデルファイルを展開"""
|
| 10 |
+
try:
|
| 11 |
+
print("=== 圧縮モデルファイルの展開 ===")
|
| 12 |
+
|
| 13 |
+
# 圧縮ファイルを展開
|
| 14 |
+
compressed_files = [
|
| 15 |
+
("bert_age_model.bin.gz", "bert_age_model.bin"),
|
| 16 |
+
("bert_gender_model.bin.gz", "bert_gender_model.bin")
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
for compressed_file, extracted_file in compressed_files:
|
| 20 |
+
if os.path.exists(compressed_file) and not os.path.exists(extracted_file):
|
| 21 |
+
print(f"📦 {compressed_file} を展開中...")
|
| 22 |
+
with gzip.open(compressed_file, 'rb') as f_in:
|
| 23 |
+
with open(extracted_file, 'wb') as f_out:
|
| 24 |
+
shutil.copyfileobj(f_in, f_out)
|
| 25 |
+
print(f"✅ {extracted_file} の展開が完了しました")
|
| 26 |
+
elif os.path.exists(extracted_file):
|
| 27 |
+
print(f"✅ {extracted_file} は既に存在します")
|
| 28 |
+
else:
|
| 29 |
+
print(f"❌ {compressed_file} が見つかりません")
|
| 30 |
+
|
| 31 |
+
return True
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"❌ 展開エラー: {e}")
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
def predict_age_gender(text):
|
| 38 |
+
"""年代・性別予測関数"""
|
| 39 |
+
if not text.strip():
|
| 40 |
+
return "テキストを入力してください。", "", ""
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
result = predict_text(text)
|
| 44 |
+
|
| 45 |
+
# 年代予測結果を整形
|
| 46 |
+
age_results = []
|
| 47 |
+
for age, percentage in result['age_percentages'].items():
|
| 48 |
+
age_results.append(f"{age}: {percentage}%")
|
| 49 |
+
age_text = "\n".join(age_results)
|
| 50 |
+
|
| 51 |
+
# 性別予測結果を整形
|
| 52 |
+
gender_results = []
|
| 53 |
+
for gender, percentage in result['gender_percentages'].items():
|
| 54 |
+
gender_results.append(f"{gender}: {percentage}%")
|
| 55 |
+
gender_text = "\n".join(gender_results)
|
| 56 |
+
|
| 57 |
+
# 最も高い確率の年代を特定
|
| 58 |
+
max_age = max(result['age_percentages'].items(), key=lambda x: x[1])
|
| 59 |
+
max_gender = max(result['gender_percentages'].items(), key=lambda x: x[1])
|
| 60 |
+
|
| 61 |
+
summary = f"推定結果: {max_age[0]} ({max_age[1]}%), {max_gender[0]} ({max_gender[1]}%)"
|
| 62 |
+
|
| 63 |
+
return summary, age_text, gender_text
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return f"エラーが発生しました: {str(e)}", "", ""
|
| 67 |
+
|
| 68 |
+
# モデルの展開と読み込み
|
| 69 |
+
print("=== アプリケーション初期化 ===")
|
| 70 |
+
|
| 71 |
+
# 圧縮ファイルを展開
|
| 72 |
+
if extract_compressed_models():
|
| 73 |
+
# モデルの読み込み
|
| 74 |
+
print("=== モデル読み込み開始 ===")
|
| 75 |
+
try:
|
| 76 |
+
load_models()
|
| 77 |
+
print("✅ モデルの読み込みが完了しました")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"❌ モデルの読み込みに失敗しました: {e}")
|
| 80 |
+
print("⚠️ モデルファイルが存在しない可能性があります")
|
| 81 |
+
else:
|
| 82 |
+
print("❌ モデルの展開に失敗しました")
|
| 83 |
+
|
| 84 |
+
# Gradioインターフェース
|
| 85 |
+
interface = gr.Interface(
|
| 86 |
+
fn=predict_age_gender,
|
| 87 |
+
inputs=gr.Textbox(
|
| 88 |
+
label="日本語テキストを入力してください",
|
| 89 |
+
placeholder="例: 今日はとても良い天気ですね。友達と一緒に散歩をしました。",
|
| 90 |
+
lines=3
|
| 91 |
+
),
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Textbox(label="推定結果サマリー"),
|
| 94 |
+
gr.Textbox(label="年代予測詳細"),
|
| 95 |
+
gr.Textbox(label="性別予測詳細")
|
| 96 |
+
],
|
| 97 |
+
title="🧠 年代・性別推定システム",
|
| 98 |
+
description="日本語テキストから年代と性別を推定するAIシステムです。",
|
| 99 |
+
examples=[
|
| 100 |
+
"今日はとても良い天気ですね。",
|
| 101 |
+
"友達と一緒に散歩をしました。",
|
| 102 |
+
"新しいスマートフォンを買いました。",
|
| 103 |
+
"仕事が忙しくて疲れました。"
|
| 104 |
+
],
|
| 105 |
+
theme=gr.themes.Soft()
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# アプリケーション起動
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
interface.launch(
|
| 111 |
+
server_name="0.0.0.0",
|
| 112 |
+
server_port=7860,
|
| 113 |
+
share=False # Hugging Face SpacesではFalse
|
| 114 |
+
)
|
requirements_hf.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
torch>=1.9.0
|
| 3 |
+
transformers>=4.21.0
|
| 4 |
+
pandas>=1.3.0
|
| 5 |
+
scikit-learn>=1.0.0
|
| 6 |
+
numpy>=1.21.0
|
| 7 |
+
fugashi>=1.2.0
|
| 8 |
+
ipadic>=1.0.0
|
| 9 |
+
sudachipy>=0.6.0
|
| 10 |
+
sudachidict-core>=20240101
|
| 11 |
+
neologdn>=0.0.0
|