Spaces:
Running
Running
陈泽通
commited on
Commit
·
f4ed067
1
Parent(s):
851c20c
Add application file
Browse files- app.py +25 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, SeamlessM4Tv2Model
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# 加载模型和处理器
|
6 |
+
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")
|
7 |
+
model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large")
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def greet(text,src_lang,tgt_lang):
|
13 |
+
# 输入文本
|
14 |
+
text_inputs = processor(text=text, src_lang=src_lang, return_tensors="pt")
|
15 |
+
# 生成翻译的文本
|
16 |
+
output_tokens = model.generate(**text_inputs, tgt_lang=tgt_lang, generate_speech=False)
|
17 |
+
translated_text_from_text = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
|
18 |
+
return translated_text_from_text
|
19 |
+
|
20 |
+
text = gr.Textbox(lines=5,label="输入文本",placeholder="请输入文本")
|
21 |
+
src_lang = gr.Dropdown(label="源语言",choices=["en","zh"])
|
22 |
+
tgt_lang = gr.Dropdown(label="目标语言",choices=["en","zh"])
|
23 |
+
output_text = gr.Textbox(lines=5,label="翻译文本",placeholder="翻译文本")
|
24 |
+
demo=gr.Interface(fn=greet, inputs=[text,src_lang,tgt_lang], outputs=output_text)
|
25 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sentencepiece
|
2 |
+
git+https://github.com/huggingface/transformers.git
|
3 |
+
torchaudio
|
4 |
+
protobuf
|
5 |
+
torch
|
6 |
+
torchvision
|
7 |
+
torchaudio
|