Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# 1. テキスト生成モデルの準備(DistilGPT2を使用)
|
5 |
+
# ZeroGPU環境ではGPUが無いのでCPUで動作。DistilGPT2は小型でCPUでも動作可能​:contentReference[oaicite:2]{index=2}。
|
6 |
+
generator = pipeline(
|
7 |
+
"text-generation",
|
8 |
+
model="distilgpt2", # 軽量な事前学習済みモデル
|
9 |
+
return_full_text=False # プロンプト部分を含めず新たに生成したテキストのみ返す
|
10 |
+
)
|
11 |
+
|
12 |
+
# 2. ユーザー入力に対してテキスト生成を行う関数の定義
|
13 |
+
def generate_text(prompt):
|
14 |
+
# max_new_tokensで新規生成する単語数を制限(例: 最大50トークン)
|
15 |
+
result = generator(prompt, max_new_tokens=50)[0]["generated_text"]
|
16 |
+
return result
|
17 |
+
|
18 |
+
# 3. Gradioインターフェイスの構築
|
19 |
+
# テキストボックスを入力と出力に設定し、generate_text関数を呼び出すシンプルなUIを構築
|
20 |
+
demo = gr.Interface(
|
21 |
+
fn=generate_text,
|
22 |
+
inputs=gr.components.Textbox(lines=4, label="入力プロンプト"),
|
23 |
+
outputs=gr.components.Textbox(lines=5, label="生成テキスト"),
|
24 |
+
title="ZeroGPU対応: テキスト生成デモ",
|
25 |
+
description="プロンプトを入力すると、DistilGPT2モデルが続きを生成します。"
|
26 |
+
)
|
27 |
+
|
28 |
+
# 4. アプリの起動(Spaces環境ではlaunch()不要だが、ローカル実行時には必要)
|
29 |
+
if __name__ == "__main__":
|
30 |
+
demo.launch()
|
31 |
+
|