initios commited on
Commit
6b454fa
1 Parent(s): efd2df9

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +2 -8
  2. demo.py +43 -0
  3. flagged/log.csv +2 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Ja Zh
3
- emoji: 🐠
4
- colorFrom: gray
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 3.39.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: ja_zh
3
+ app_file: demo.py
 
 
4
  sdk: gradio
5
  sdk_version: 3.39.0
 
 
6
  ---
 
 
demo.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from transformers import pipeline
4
+ import re
5
+
6
+ pipe = pipeline(model="larryvrh/mt5-translation-ja_zh", device=0)
7
+
8
+ def translate_sentence(sentence):
9
+ return pipe(f'<-ja2zh-> {sentence}')[0]['translation_text']
10
+
11
+
12
+ def translate_paragraph(paragraph):
13
+ sentences = []
14
+ cursor = 0
15
+ for i, c in enumerate(paragraph):
16
+ if c == '。':
17
+ sentences.append(paragraph[cursor:i + 1])
18
+ cursor = i + 1
19
+ if paragraph[-1] != '。':
20
+ sentences.append(paragraph[cursor:])
21
+ return ''.join(translate_sentence(s) for s in sentences)
22
+
23
+
24
+ def translate_article(article):
25
+ paragraphs = re.split(r'([\r\n]+)', article)
26
+ for i, p in enumerate(paragraphs):
27
+ if len(p.strip()) == 0:
28
+ continue
29
+ paragraphs[i] = translate_paragraph(p)
30
+ return ''.join(paragraphs)
31
+
32
+ article = '''文は、「主語・修飾語・述語」の語順で構成される。修飾語は被修飾語の前に位置する。また、名詞の格を示すためには、語順や語尾を変化させるのでなく、文法的な機能を示す機能語(助詞)を後ろに付け加える(膠着させる)。これらのことから、言語類型論上は、語順の点ではSOV型の言語に、形態の点では膠着語に分類される(「文法」の節参照)。
33
+
34
+ ##語彙は、古来の大和言葉(和語)のほか、漢語(字音語)、外来語、および、それらの混ざった混種語に分けられる。字音語(漢字の音読みに由来する語の意、一般に「漢語」と称する)は現代の語彙の一部分を占めている。また、「絵/画(ゑ)」など、もともと音であるが和語と認識されているものもある。さらに近代以降には西洋由来の語を中心とする外来語が増大している(「語種」の節参照)。'''
35
+
36
+
37
+ print(translate_article(article))
38
+
39
+ def question_answer(question):
40
+ return translate_article(question)
41
+
42
+ gr.Interface(fn=question_answer, inputs=["text"], outputs=["text"]).launch( share=True)
43
+
flagged/log.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ context,question,output 0,output 1,flag,username,timestamp
2
+ eeeeeeee,eee,,,,,2023-07-29 17:31:47.569633