kouta728 commited on
Commit
ae5184d
·
verified ·
1 Parent(s): c927e64

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +70 -8
  2. __pycache__/app.cpython-311.pyc +0 -0
  3. app.py +266 -0
  4. requirements.txt +3 -0
README.md CHANGED
@@ -1,13 +1,75 @@
1
  ---
2
- title: Plamo 3 Translate Api
3
- emoji: 🔥
4
- colorFrom: pink
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 6.31.0
8
- python_version: '3.12'
9
  app_file: app.py
10
- pinned: false
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: PLaMo 3 Translate API
3
+ emoji: 🌐
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: "6.31.0"
 
8
  app_file: app.py
9
+ python_version: "3.12"
10
+ short_description: PLaMo 3 Translate 2B (PFN) REST & MCP API on ZeroGPU
11
+ startup_duration_timeout: 30m
12
  ---
13
 
14
+ # 🌐 PLaMo 3 Translate API
15
+
16
+ [PLaMo 3 Translate](https://huggingface.co/pfnet/plamo-3-translate-2b)(PFN 製・53言語対応の翻訳特化 LLM)を
17
+ ZeroGPU(RTX PRO 6000 Blackwell 48GB)上で動かし、**REST API / MCP サーバ**として叩けるようにした Space です。
18
+
19
+ Built with PLaMo.
20
+
21
+ ## Endpoints
22
+
23
+ | Endpoint | Description |
24
+ |---|---|
25
+ | `POST /gradio_api/call/translate` | 1テキスト翻訳 |
26
+ | `POST /gradio_api/call/translate_batch` | JSON 配列の一括翻訳 |
27
+ | `https://<space>.hf.space/gradio/mcp` | MCP サーバ(tools: `translate`, `translate_batch`) |
28
+
29
+ ### curl
30
+
31
+ ```bash
32
+ curl -s -X POST "https://kouta728-plamo-3-translate-api.hf.space/gradio_api/call/translate" \
33
+ -H "Content-Type: application/json" \
34
+ -d '{"data": ["会議は来週の月曜日に変更されました。", "en", "", "", "", "", "", 512]}'
35
+ # => {"event_id": "<id>"}
36
+ curl -s "https://kouta728-plamo-3-translate-api.hf.space/gradio_api/call/translate/<id>"
37
+ # => {"data": ["The meeting has been rescheduled for next Monday."]}
38
+ ```
39
+
40
+ `/translate` の `data` は順に:
41
+ `text, target_lang, source_lang, context, glossary, form, history, max_new_tokens`
42
+
43
+ - `target_lang`: `en`, `ja`, `zh-hans`, ...(53言語コード)
44
+ - `source_lang`: 空欄で自動判定
45
+ - `context`: 文脈(例: 周辺テキスト)
46
+ - `glossary`: `[{"source": "管理画面", "target": "dashboard"}]` 形式の JSON
47
+ - `form`: `plain` / `polite`(`target_lang="ja"` のときのみ)
48
+ - `history`: 過去の `{"source": ..., "translation": ...}` ペアの JSON(段落またぎで訳語を統一)
49
+ - `max_new_tokens`: 最大 4096
50
+
51
+ ### Python (gradio_client)
52
+
53
+ ```python
54
+ from gradio_client import Client
55
+
56
+ client = Client("https://kouta728-plamo-3-translate-api.hf.space")
57
+ print(client.predict("こんにちは、お元気ですか?", "en", api_name="/translate"))
58
+ # Hello, how are you?
59
+ ```
60
+
61
+ ## Model
62
+
63
+ - Default: `pfnet/plamo-3-translate-2b` (BF16, 約5.8GB)
64
+ - Change via env var `MODEL_REPO` (e.g. `pfnet/plamo-3-translate-0.8b` / `-8b`; 31B は ZeroGPU `xlarge` が必要)
65
+
66
+ ## Runtime notes
67
+
68
+ - ZeroGPU 上で動くため、長時間アクセスがないと Space はスリープします(復帰時はコールドスタートで数分かかることがあります)。
69
+ 定期的に ping する keep-alive の Scheduled Job を設定済みです(`hf jobs scheduled ps` で確認)。
70
+ - 翻訳リクエストは呼び出し側アカウントの ZeroGPU 日次クォータ(無料アカウント 5分/日)を消費します。Space 作成者(このアカウント)は無料です。
71
+
72
+ ## License
73
+
74
+ Model: [PLaMo Community License](https://plamo.preferredai.jp/info/plamo-community-license-en)
75
+ (個人・非商用利用は無料。商用利用は売上10億円以下なら登録フォームで無料)
__pycache__/app.cpython-311.pyc ADDED
Binary file (14.8 kB). View file
 
app.py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """PLaMo 3 Translate API — Gradio Space on ZeroGPU.
2
+
3
+ Exposed endpoints (Gradio auto-generates a REST API + MCP server):
4
+ POST /gradio_api/call/translate -> single text translation
5
+ POST /gradio_api/call/translate_batch -> JSON list of texts
6
+ MCP: https://<space>.hf.space/gradio/mcp
7
+ """
8
+
9
+ import json
10
+ import os
11
+
12
+ import spaces # noqa: F401 (MUST be imported before torch on ZeroGPU)
13
+ import torch
14
+ import gradio as gr
15
+ from transformers import AutoModelForCausalLM, AutoTokenizer
16
+
17
+ MODEL_REPO = os.environ.get("MODEL_REPO", "pfnet/plamo-3-translate-2b")
18
+ MAX_NEW_TOKENS_CAP = 4096
19
+
20
+ LANGUAGES = [
21
+ "ja", "en", "zh-hans", "zh-hant", "ko", "ar", "az", "be", "bg", "bn",
22
+ "ca", "ceb", "de", "es", "et", "eu", "fa", "fr", "gu", "he", "hi", "hr",
23
+ "hu", "hy", "id", "it", "jv", "ka", "kk", "km", "lt", "lv", "ml", "mr",
24
+ "ms", "my", "ne", "nl", "pa", "pl", "ru", "si", "sk", "sl", "sr", "sw",
25
+ "ta", "te", "th", "tl", "ur", "uz", "vi",
26
+ ]
27
+
28
+ print(f"[boot] loading model: {MODEL_REPO}", flush=True)
29
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, trust_remote_code=True)
30
+ model = AutoModelForCausalLM.from_pretrained(
31
+ MODEL_REPO,
32
+ trust_remote_code=True,
33
+ dtype=torch.bfloat16,
34
+ ).to("cuda")
35
+ model.eval()
36
+ print("[boot] model ready", flush=True)
37
+
38
+
39
+ def _translate_one(
40
+ text: str,
41
+ target_lang: str,
42
+ source_lang: str,
43
+ context: str,
44
+ glossary_json: str,
45
+ form: str,
46
+ history_json: str,
47
+ max_new_tokens: int,
48
+ ) -> str:
49
+ messages = []
50
+ if history_json.strip():
51
+ for pair in json.loads(history_json):
52
+ messages.append({"role": "user", "content": pair["source"]})
53
+ messages.append({"role": "assistant", "content": pair["translation"]})
54
+ messages.append({"role": "user", "content": text})
55
+
56
+ template_kwargs = {
57
+ "target_lang": target_lang,
58
+ "add_generation_prompt": True,
59
+ "return_dict": True,
60
+ "return_tensors": "pt",
61
+ }
62
+ if source_lang.strip():
63
+ template_kwargs["source_lang"] = source_lang.strip()
64
+ if context.strip():
65
+ template_kwargs["context"] = context
66
+ if form.strip():
67
+ template_kwargs["form"] = form.strip()
68
+ if glossary_json.strip():
69
+ template_kwargs["glossary"] = json.loads(glossary_json)
70
+
71
+ inputs = tokenizer.apply_chat_template(messages, **template_kwargs).to(model.device)
72
+ prompt_len = inputs["input_ids"].shape[1]
73
+ max_new_tokens = max(16, min(int(max_new_tokens), MAX_NEW_TOKENS_CAP))
74
+
75
+ with torch.inference_mode():
76
+ output = model.generate(**inputs, do_sample=False, max_new_tokens=max_new_tokens)
77
+ return tokenizer.decode(output[0, prompt_len:], skip_special_tokens=True).strip()
78
+
79
+
80
+ @spaces.GPU(duration=120)
81
+ def translate(
82
+ text: str,
83
+ target_lang: str = "en",
84
+ source_lang: str = "",
85
+ context: str = "",
86
+ glossary: str = "",
87
+ form: str = "",
88
+ history: str = "",
89
+ max_new_tokens: int = 2048,
90
+ ) -> str:
91
+ """Translate `text` into `target_lang` with PLaMo 3 Translate (PFN).
92
+
93
+ Args:
94
+ text: Source text to translate.
95
+ target_lang: Target language code (e.g. "en", "ja", "zh-hans"). One of 53 supported codes.
96
+ source_lang: Source language code. Empty = auto-detect from the text.
97
+ context: Optional reference context (surrounding text / situation) to disambiguate the translation.
98
+ glossary: Optional JSON list of {"source": ..., "target": ...} term pairs, e.g.
99
+ [{"source": "管理画面", "target": "dashboard"}].
100
+ form: Optional Japanese output style: "plain" or "polite" (only valid with target_lang="ja").
101
+ history: Optional JSON list of previous {"source": ..., "translation": ...} pairs used as
102
+ translation history (keeps terminology/style consistent across paragraphs).
103
+ max_new_tokens: Generation limit for the translation (capped at 4096).
104
+
105
+ Returns:
106
+ The translated text.
107
+ """
108
+ return _translate_one(
109
+ text, target_lang, source_lang, context, glossary, form, history, max_new_tokens
110
+ )
111
+
112
+
113
+ @spaces.GPU(duration=300)
114
+ def translate_batch(
115
+ texts_json: str,
116
+ target_lang: str = "en",
117
+ source_lang: str = "",
118
+ context: str = "",
119
+ glossary: str = "",
120
+ form: str = "",
121
+ max_new_tokens: int = 2048,
122
+ ) -> str:
123
+ """Translate a JSON array of texts into `target_lang` with PLaMo 3 Translate (PFN).
124
+
125
+ Args:
126
+ texts_json: JSON array of source strings, e.g. ["こんにちは。", "お元気ですか?"].
127
+ target_lang: Target language code (e.g. "en", "ja").
128
+ source_lang: Source language code. Empty = auto-detect.
129
+ context: Optional shared reference context applied to every text.
130
+ glossary: Optional JSON list of {"source": ..., "target": ...} term pairs applied to every text.
131
+ form: Optional Japanese output style: "plain" or "polite" (only with target_lang="ja").
132
+ max_new_tokens: Per-text generation limit (capped at 4096).
133
+
134
+ Returns:
135
+ JSON array of translated strings, in the same order as the input.
136
+ """
137
+ texts = json.loads(texts_json)
138
+ if not isinstance(texts, list) or not all(isinstance(t, str) for t in texts):
139
+ raise ValueError("texts_json must be a JSON array of strings")
140
+ results = [
141
+ _translate_one(t, target_lang, source_lang, context, glossary, form, "", max_new_tokens)
142
+ for t in texts
143
+ ]
144
+ return json.dumps(results, ensure_ascii=False)
145
+
146
+
147
+ API_DOC = """## REST API
148
+
149
+ Base URL: `https://kouta728-plamo-3-translate-api.hf.space`
150
+
151
+ ```bash
152
+ # 1) POST the request
153
+ curl -s -X POST "https://kouta728-plamo-3-translate-api.hf.space/gradio_api/call/translate" \\
154
+ -H "Content-Type: application/json" \\
155
+ -d '{"data": ["会議は来週の月曜日に変更されました。", "en", "", "", "", "", "", 512]}'
156
+ # => {"event_id": "<id>"}
157
+
158
+ # 2) GET the result
159
+ curl -s "https://kouta728-plamo-3-translate-api.hf.space/gradio_api/call/translate/<id>"
160
+ # => {"data": ["The meeting has been rescheduled for next Monday."]}
161
+ ```
162
+
163
+ `data` order: `text, target_lang, source_lang, context, glossary, form, history, max_new_tokens`
164
+
165
+ Batch endpoint `/gradio_api/call/translate_batch` takes
166
+ `texts_json, target_lang, source_lang, context, glossary, form, max_new_tokens`.
167
+
168
+ ## Python client
169
+
170
+ ```python
171
+ from gradio_client import Client
172
+
173
+ client = Client("https://kouta728-plamo-3-translate-api.hf.space")
174
+ print(client.predict("こんにちは、お元気ですか?", "en", api_name="/translate"))
175
+ # Hello, how are you?
176
+ ```
177
+
178
+ ## MCP
179
+
180
+ The Space is also an MCP server: `https://kouta728-plamo-3-translate-api.hf.space/gradio/mcp`
181
+ (tools: `translate`, `translate_batch`).
182
+
183
+ ## Notes
184
+
185
+ - GPU runs on ZeroGPU (RTX PRO 6000 Blackwell, 48 GB). The Space sleeps after long inactivity;
186
+ the first request after a nap includes a cold start.
187
+ - Model: `pfnet/plamo-3-translate-2b` (BF16, 53 languages). Switch size via the `MODEL_REPO`
188
+ env var (e.g. `pfnet/plamo-3-translate-8b`; 31B needs ZeroGPU `xlarge`).
189
+ - License: [PLaMo Community License](https://plamo.preferredai.jp/info/plamo-community-license-en).
190
+ Built with PLaMo.
191
+ """
192
+
193
+ with gr.Blocks(title="PLaMo 3 Translate API") as demo:
194
+ gr.Markdown("# 🌐 PLaMo 3 Translate API\nPFN の翻訳特化モデル **PLaMo 3 Translate** を ZeroGPU 上で動かす REST / MCP API です(53言語対応)。")
195
+
196
+ with gr.Tab("Translate"):
197
+ with gr.Row():
198
+ with gr.Column():
199
+ text = gr.Textbox(label="Source text", lines=6, placeholder="翻訳したいテキスト")
200
+ target = gr.Dropdown(choices=LANGUAGES, value="en", label="target_lang")
201
+ source = gr.Textbox(label="source_lang(空欄で自動判定)", placeholder="例: ja")
202
+ ctx = gr.Textbox(label="context(任意: 文脈・状況の説明)", lines=2)
203
+ glos = gr.Textbox(
204
+ label='glossary(任意: JSON 例 [{"source":"管理画面","target":"dashboard"}])',
205
+ lines=2,
206
+ )
207
+ form = gr.Dropdown(choices=["", "plain", "polite"], value="", label='form(ja のみ: plain / polite)')
208
+ hist = gr.Textbox(
209
+ label='history(任意: JSON 例 [{"source":"...","translation":"..."}])', lines=2
210
+ )
211
+ mnt = gr.Slider(16, MAX_NEW_TOKENS_CAP, value=2048, step=16, label="max_new_tokens")
212
+ btn = gr.Button("Translate", variant="primary")
213
+ with gr.Column():
214
+ out = gr.Textbox(label="Translation", lines=12)
215
+ btn.click(
216
+ translate,
217
+ [text, target, source, ctx, glos, form, hist, mnt],
218
+ out,
219
+ api_name="/translate",
220
+ )
221
+ text.submit(
222
+ translate,
223
+ [text, target, source, ctx, glos, form, hist, mnt],
224
+ out,
225
+ api_name=None,
226
+ )
227
+ gr.Examples(
228
+ examples=[
229
+ ["会議は来週の月曜日に変更されました。", "en", "", "", "", "", "", 512],
230
+ ["Hello, how are you?", "ja", "", "", "", "polite", "", 512],
231
+ ["かみを切りました。", "en", "", "工作で折り紙を作っています。", "", "", "", 512],
232
+ [
233
+ "プロジェクトの管理画面を開いてください。",
234
+ "en",
235
+ "ja",
236
+ "",
237
+ '[{"source": "管理画面", "target": "dashboard"}, {"source": "プロジェクト", "target": "project"}]',
238
+ "",
239
+ "",
240
+ 512,
241
+ ],
242
+ ],
243
+ inputs=[text, target, source, ctx, glos, form, hist, mnt],
244
+ )
245
+
246
+ with gr.Tab("Batch (API)"):
247
+ b_in = gr.Textbox(label="texts_json", lines=4, value='["こんにちは。", "お元気ですか?"]')
248
+ b_target = gr.Dropdown(choices=LANGUAGES, value="en", label="target_lang")
249
+ b_source = gr.Textbox(label="source_lang(空欄��自動判定)", value="")
250
+ b_ctx = gr.Textbox(label="context(任意)", value="")
251
+ b_glos = gr.Textbox(label="glossary(任意)", value="")
252
+ b_form = gr.Dropdown(choices=["", "plain", "polite"], value="", label="form(ja のみ)")
253
+ b_mnt = gr.Slider(16, MAX_NEW_TOKENS_CAP, value=2048, step=16, label="max_new_tokens")
254
+ b_btn = gr.Button("Translate batch", variant="primary")
255
+ b_out = gr.Textbox(label="結果 (JSON 配列)", lines=8)
256
+ b_btn.click(
257
+ translate_batch,
258
+ [b_in, b_target, b_source, b_ctx, b_glos, b_form, b_mnt],
259
+ b_out,
260
+ api_name="/translate_batch",
261
+ )
262
+
263
+ with gr.Tab("API"):
264
+ gr.Markdown(API_DOC)
265
+
266
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==5.22.2
2
+ accelerate
3
+ numba