Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,39 +2,77 @@ import spaces
|
|
2 |
import gradio as gr
|
3 |
from transformers import AutoTokenizer, pipeline
|
4 |
import torch
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
"text-classification",
|
13 |
-
model=
|
14 |
-
tokenizer=
|
15 |
-
torch_dtype=torch.bfloat16,
|
16 |
-
trust_remote_code=True,
|
17 |
-
device_map="auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
)
|
|
|
19 |
|
20 |
-
# GPU
|
21 |
@spaces.GPU(duration=120)
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
# Gradio
|
31 |
demo = gr.Interface(
|
32 |
-
fn=
|
33 |
-
inputs=gr.Textbox(lines=3, label="
|
34 |
-
outputs=gr.Textbox(label="
|
35 |
-
title="
|
36 |
-
description="
|
37 |
)
|
38 |
|
39 |
-
#
|
|
|
40 |
demo.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import AutoTokenizer, pipeline
|
4 |
import torch
|
5 |
+
import logging
|
6 |
|
7 |
+
# ロギング設定
|
8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
|
11 |
+
# モデル定義
|
12 |
+
classification_model_name = "unitary/toxic-bert"
|
13 |
+
generation_model_name = "distilgpt2" # 軽量なテキスト生成モデル
|
14 |
+
|
15 |
+
logger.info("Starting model loading...")
|
16 |
+
|
17 |
+
# 分類モデルのロード
|
18 |
+
logger.info(f"Loading classification model: {classification_model_name}")
|
19 |
+
classification_tokenizer = AutoTokenizer.from_pretrained(classification_model_name)
|
20 |
+
classification_pipeline = pipeline(
|
21 |
"text-classification",
|
22 |
+
model=classification_model_name,
|
23 |
+
tokenizer=classification_tokenizer,
|
24 |
+
torch_dtype=torch.bfloat16,
|
25 |
+
trust_remote_code=True,
|
26 |
+
device_map="auto"
|
27 |
+
)
|
28 |
+
logger.info(f"Classification model loaded successfully: {classification_model_name}")
|
29 |
+
|
30 |
+
# 生成モデルのロード
|
31 |
+
logger.info(f"Loading generation model: {generation_model_name}")
|
32 |
+
generation_tokenizer = AutoTokenizer.from_pretrained(generation_model_name)
|
33 |
+
generation_pipeline = pipeline(
|
34 |
+
"text-generation",
|
35 |
+
model=generation_model_name,
|
36 |
+
tokenizer=generation_tokenizer,
|
37 |
+
torch_dtype=torch.bfloat16,
|
38 |
+
trust_remote_code=True,
|
39 |
+
device_map="auto"
|
40 |
)
|
41 |
+
logger.info(f"Generation model loaded successfully: {generation_model_name}")
|
42 |
|
43 |
+
# GPUを利用する推論関数(両方のモデルを使用)
|
44 |
@spaces.GPU(duration=120)
|
45 |
+
def process_text(prompt):
|
46 |
+
logger.info(f"Processing input: {prompt[:50]}...")
|
47 |
+
|
48 |
+
# 分類モデルで処理
|
49 |
+
classification_result = classification_pipeline(prompt)
|
50 |
+
logger.info(f"Classification complete: {classification_result}")
|
51 |
+
|
52 |
+
# 生成モデルで処理
|
53 |
+
generation_result = generation_pipeline(
|
54 |
+
prompt,
|
55 |
+
max_new_tokens=50,
|
56 |
+
do_sample=True,
|
57 |
+
temperature=0.7,
|
58 |
+
num_return_sequences=1
|
59 |
+
)
|
60 |
+
generated_text = generation_result[0]["generated_text"]
|
61 |
+
logger.info(f"Text generation complete, generated: {len(generated_text)} chars")
|
62 |
|
63 |
+
# 結果を組み合わせて返す
|
64 |
+
combined_result = f"分類結果: {classification_result}\n\n生成されたテキスト: {generated_text}"
|
65 |
+
return combined_result
|
66 |
|
67 |
+
# Gradioインタフェース
|
68 |
demo = gr.Interface(
|
69 |
+
fn=process_text,
|
70 |
+
inputs=gr.Textbox(lines=3, label="入力テキスト"),
|
71 |
+
outputs=gr.Textbox(label="処理結果", lines=8),
|
72 |
+
title="テキスト分類 & 生成デモ",
|
73 |
+
description="入力テキストに対して分類と生成の両方を実行します。"
|
74 |
)
|
75 |
|
76 |
+
# アプリの起動
|
77 |
+
logger.info("Starting application...")
|
78 |
demo.launch()
|