nyasukun commited on
Commit
03de8b6
·
verified ·
1 Parent(s): a2c2bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -24
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
- #model_name = "tiiuae/falcon-7b-instruct"
7
- model_name = "unitary/toxic-bert"
 
8
 
9
- # トークナイザとテキスト生成パイプラインの準備
10
- tokenizer = AutoTokenizer.from_pretrained(model_name)
11
- generator = pipeline(
 
 
 
 
 
 
 
12
  "text-classification",
13
- model=model_name,
14
- tokenizer=tokenizer,
15
- torch_dtype=torch.bfloat16, # モデルをbfloat16精度でロード(メモリ節約)
16
- trust_remote_code=True, # モデルのリポジトリ内の追加コードを信頼して読み込む
17
- device_map="auto" # 利用可能なGPUに自動割り当て(ZeroGPU環境ではA100を使用)
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  )
 
19
 
20
- # GPUを利用する推論関数を定義(ZeroGPUのためデコレータを使用)
21
  @spaces.GPU(duration=120)
22
- def generate_text(prompt):
23
- # プロンプトからテキストを生成し、結果文字列を返す
24
- result = generator(prompt)
25
- #generated = result[0]["generated_text"]
26
- generated = result
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- return generated
 
 
29
 
30
- # Gradioインタフェースの構築(テキスト入力→テキスト出力)
31
  demo = gr.Interface(
32
- fn=generate_text,
33
- inputs=gr.Textbox(lines=3, label="入力プロンプト"),
34
- outputs=gr.Textbox(label="生成されたテキスト"),
35
- title="Falcon-7B-Instruct テキスト生成デモ",
36
- description="プロンプトを入力すると、大規模言語モデルが続きのテキストを生成します。"
37
  )
38
 
39
- # アプリの起動(Spaces上ではこれによりサービスが公開される)
 
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()