Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ 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')
|
@@ -40,37 +42,62 @@ generation_pipeline = pipeline(
|
|
40 |
)
|
41 |
logger.info(f"Generation model loaded successfully: {generation_model_name}")
|
42 |
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
logger.info(f"Classification complete: {classification_result}")
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=
|
70 |
inputs=gr.Textbox(lines=3, label="入力テキスト"),
|
71 |
outputs=gr.Textbox(label="処理結果", lines=8),
|
72 |
-
title="テキスト分類 & 生成デモ",
|
73 |
-
description="
|
74 |
)
|
75 |
|
76 |
# アプリの起動
|
|
|
3 |
from transformers import AutoTokenizer, pipeline
|
4 |
import torch
|
5 |
import logging
|
6 |
+
import asyncio
|
7 |
+
from functools import partial
|
8 |
|
9 |
# ロギング設定
|
10 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
42 |
)
|
43 |
logger.info(f"Generation model loaded successfully: {generation_model_name}")
|
44 |
|
45 |
+
# 非同期で分類を実行する関数
|
46 |
+
async def classify_text_async(prompt):
|
47 |
+
logger.info(f"Running classification for: {prompt[:50]}...")
|
48 |
+
# CPUバウンドな処理を非同期実行するためにループの外で実行
|
49 |
+
loop = asyncio.get_event_loop()
|
50 |
+
classification_result = await loop.run_in_executor(
|
51 |
+
None,
|
52 |
+
lambda: classification_pipeline(prompt)
|
53 |
+
)
|
54 |
logger.info(f"Classification complete: {classification_result}")
|
55 |
+
return classification_result
|
56 |
+
|
57 |
+
# 非同期で生成を実行する関数
|
58 |
+
async def generate_text_async(prompt):
|
59 |
+
logger.info(f"Running text generation for: {prompt[:50]}...")
|
60 |
+
loop = asyncio.get_event_loop()
|
61 |
+
generation_result = await loop.run_in_executor(
|
62 |
+
None,
|
63 |
+
lambda: generation_pipeline(
|
64 |
+
prompt,
|
65 |
+
max_new_tokens=50,
|
66 |
+
do_sample=True,
|
67 |
+
temperature=0.7,
|
68 |
+
num_return_sequences=1
|
69 |
+
)
|
70 |
)
|
71 |
generated_text = generation_result[0]["generated_text"]
|
72 |
logger.info(f"Text generation complete, generated: {len(generated_text)} chars")
|
73 |
+
return generated_text
|
74 |
+
|
75 |
+
# GPUを利用する非同期推論関数
|
76 |
+
@spaces.GPU(duration=120)
|
77 |
+
async def process_text_async(prompt):
|
78 |
+
logger.info(f"Processing input asynchronously: {prompt[:50]}...")
|
79 |
+
|
80 |
+
# 両方のタスクを並行して実行
|
81 |
+
classification_task = classify_text_async(prompt)
|
82 |
+
generation_task = generate_text_async(prompt)
|
83 |
+
|
84 |
+
# 両方のタスクが完了するのを待つ
|
85 |
+
classification_result, generated_text = await asyncio.gather(
|
86 |
+
classification_task,
|
87 |
+
generation_task
|
88 |
+
)
|
89 |
|
90 |
# 結果を組み合わせて返す
|
91 |
combined_result = f"分類結果: {classification_result}\n\n生成されたテキスト: {generated_text}"
|
92 |
return combined_result
|
93 |
|
94 |
+
# Gradioは非同期関数にも対応しているので、そのまま渡す
|
95 |
demo = gr.Interface(
|
96 |
+
fn=process_text_async, # 非同期関数を使用
|
97 |
inputs=gr.Textbox(lines=3, label="入力テキスト"),
|
98 |
outputs=gr.Textbox(label="処理結果", lines=8),
|
99 |
+
title="テキスト分類 & 生成デモ (非同期版)",
|
100 |
+
description="入力テキストに対して分類と生成を非同期で並行実行します。"
|
101 |
)
|
102 |
|
103 |
# アプリの起動
|