xusong28 commited on
Commit
0a105e7
1 Parent(s): 2444544
Files changed (2) hide show
  1. app.py +25 -36
  2. app2.py +52 -0
app.py CHANGED
@@ -1,51 +1,40 @@
1
  # coding=utf-8
2
  # author: xusong <xusong28@jd.com>
3
- # time: 2022/8/23 12:58
4
 
5
  import gradio as gr
6
 
7
- import torch
8
- import modeling_kplug_s2s_patch
9
- from transformers import BertTokenizer, BartModel, BartForConditionalGeneration
10
 
 
 
11
 
12
 
13
- # 改成 huggingface-model自动模型
14
- model_dir = "models/ft_cepsum_jiadian/"
15
- model = BartForConditionalGeneration.from_pretrained(model_dir) # cnn指的是cnn daily mail
16
- tokenizer = BertTokenizer.from_pretrained(model_dir)
17
 
18
 
19
- def generate(text):
20
- inputs = tokenizer([text], max_length=512, return_tensors="pt")
21
- summary_ids = model.generate(inputs["input_ids"][:, 1:], num_beams=4, min_length=20, max_length=100)
22
- summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
23
- return summary[0]
24
 
 
 
 
 
 
 
25
 
26
- def greet(name):
27
- return "Hello " + name + "!!"
28
 
 
 
 
 
 
29
 
30
- #TODO:
31
- # 1. 下拉框,选择类目。 gr.Radio(['服饰','箱包', '鞋靴']
32
- # 2. 支持NER、LM、Corrector
33
- # beam seach参数
34
- # promp参数
35
 
36
- iface = gr.Interface(
37
- fn=generate,
38
- inputs=gr.inputs.Textbox(
39
- label="text",
40
- default="美的对开门风冷无霜家用智能电冰箱波光金纤薄机身高颜值助力保鲜,美的家居风,尺寸说明:"
41
- "M以上的距离尤其是左右两侧距离必须保证。关于尺寸的更多问题可,LED冷光源,纤薄机身,风冷"
42
- "无霜,智能操控,远程调温,节能静音,照亮你的视野,535L大容量,系统散热和使用的便利性,"
43
- "建议左右两侧、顶部和背部需要预留10C,电源线和调平脚等。冰箱放置时为保证,菜谱推荐,半开"
44
- "门俯视图,全开门俯视图,预留参考图"),
45
- outputs=gr.Textbox(
46
- label="Summarization(文本摘要)",
47
- ),
48
- title="Abstractive Summarization (电商文本摘要)",
49
- description="电商领域文本摘要"
50
- )
51
- iface.launch()
 
1
  # coding=utf-8
2
  # author: xusong <xusong28@jd.com>
3
+ # time: 2022/8/23 16:06
4
 
5
  import gradio as gr
6
 
 
 
 
7
 
8
+ def generate_text(title, context):
9
+ return f"Title:{title}\nContext:{context}\n..."
10
 
11
 
12
+ def generate_mutimodal(title, context, img):
13
+ return f"Title:{title}\nContext:{context}\n...{img}"
 
 
14
 
15
 
16
+ with gr.Blocks() as demo:
17
+ # 顶部文字
18
+ gr.Markdown("# 多模态可控XX生成")
 
 
19
 
20
+ # 多个tab
21
+ with gr.Tabs():
22
+ with gr.TabItem("新闻输入"):
23
+ title = gr.Textbox(label="标题", lines=1, placeholder="请输入标题")
24
+ context = gr.Textbox(label="正文", lines=2, placeholder="请输入正文")
25
+ text_button = gr.Button("生成")
26
 
27
+ text_output = gr.Textbox(label="生成内容", lines=5, placeholder="生成列表")
 
28
 
29
+ with gr.TabItem("图文输入"):
30
+ title = gr.Textbox(label="标题", lines=1, placeholder="请输入标题")
31
+ context = gr.Textbox(label="正文", lines=2, placeholder="请输入正文")
32
+ img = gr.Image(shape=(200, 200), label="请上传图片")
33
+ img_button = gr.Button("生成")
34
 
35
+ img_output = gr.Textbox(label="生成内容", lines=5, placeholder="生成列表")
 
 
 
 
36
 
37
+ text_button.click(fn=generate_text, inputs=[title, context], outputs=text_output)
38
+ img_button.click(fn=generate_mutimodal, inputs=[title, context, img], outputs=img_output)
39
+
40
+ demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
app2.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # author: xusong <xusong28@jd.com>
3
+ # time: 2022/8/23 12:58
4
+
5
+ import gradio as gr
6
+
7
+ import torch
8
+ import modeling_kplug_s2s_patch
9
+ from transformers import BertTokenizer, BartModel, BartForConditionalGeneration
10
+
11
+
12
+ # 改成 huggingface-model自动模型
13
+ model_dir = "models/ft_cepsum_jiadian/"
14
+ model = BartForConditionalGeneration.from_pretrained(model_dir) # cnn指的是cnn daily mail
15
+ tokenizer = BertTokenizer.from_pretrained(model_dir)
16
+
17
+
18
+ def generate(text):
19
+ inputs = tokenizer([text], max_length=512, return_tensors="pt")
20
+ summary_ids = model.generate(inputs["input_ids"][:, 1:], num_beams=4, min_length=20, max_length=100)
21
+ summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
22
+ return summary[0]
23
+
24
+
25
+ def greet(name):
26
+ return "Hello " + name + "!!"
27
+
28
+
29
+ #TODO:
30
+ # 1. 下拉框,选择类目。 gr.Radio(['服饰','箱包', '鞋靴']
31
+ # 2. 支持NER、LM、Corrector
32
+ # beam seach参数
33
+ # promp参数
34
+
35
+ iface = gr.Interface(
36
+ fn=generate,
37
+ inputs=gr.inputs.Textbox(
38
+ label="商品信息(product info)",
39
+ default="美的对开门风冷无霜家用智能电冰箱波光金纤薄机身高颜值助力保鲜,美的家居风,尺寸说明:"
40
+ "M以上的距离尤其是左右两侧距离必须保证。关于尺寸的更多问题可,LED冷光源,纤薄机身,风冷"
41
+ "无霜,智能操控,远程调温,节能静音,照亮你的视野,535L大容量,系统散热和使用的便利性,"
42
+ "建议左右两侧、顶部和背部需要预留10C,电源线和调平脚等。冰箱放置时为保证,菜谱推荐,半开"
43
+ "门俯视图,全开门俯视图,预留参考图"),
44
+ outputs=gr.Textbox(
45
+ label="文本摘要(Summarization)",
46
+ ),
47
+ title="电商文本摘要(Abstractive Summarization)",
48
+ description='电商领域文本摘要, 基于KPLUG预训练语言模型,'
49
+ '<a href=""> K-PLUG: Knowledge-injected Pre-trained Language Model for Natural Language Understanding'
50
+ ' and Generation in E-Commerce (Findings of EMNLP 2021) </a>。'
51
+ )
52
+ iface.launch()