hayanaka commited on
Commit
0f8d905
1 Parent(s): e02d949

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ # Pipeline
5
+ # 和訳パイプライン
6
+ translator_ja_to_en = pipeline("translation_ja_to_en", model="Helsinki-NLP/opus-mt-ja-en")
7
+
8
+ # 要約パイプライン(英語)
9
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
+
11
+ # 翻訳パイプラインの初期化(英語から日本語)
12
+ translator_en_to_ja = pipeline("translation_en_to_ja", model="Helsinki-NLP/opus-tatoeba-en-ja")
13
+
14
+ def summarize_and_translate(text):
15
+ # 英訳
16
+ translated_text_to_en = translator_ja_to_en(text, max_length=400)[0]['translation_text']
17
+
18
+ # 要約
19
+ summary_in_en = summarizer(translated_text_to_en, min_length=5, max_length=20)[0]['summary_text']
20
+
21
+ # 和訳
22
+ summary_in_ja = translator_en_to_ja(summary_in_en, max_length=400)[0]['translation_text']
23
+
24
+ # 不要なスペースを除去 → なぜかエラーになるので断念
25
+ # summary_in_ja = re.sub(r'\s+', ' ', summary_in_ja).strip()
26
+
27
+ return summary_in_ja
28
+
29
+ demo = gr.Interface(
30
+ fn=summarize_and_translate,
31
+ inputs=gr.Textbox(lines=5, placeholder="日本語の文章を入力してください"),
32
+ outputs=gr.Textbox(lines=5, label="日本語の要約"),
33
+ title="日本語文書要約ツール",
34
+ description="日本語の文章を入力すると、日本語の要約が表示されます。"
35
+ )
36
+
37
+ demo.launch()