lllchenlll commited on
Commit
61308e6
1 Parent(s): 45fab91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -12
app.py CHANGED
@@ -1,15 +1,20 @@
1
  import gradio as gr
2
  import numpy as np
3
  import openai
 
4
 
5
  from sentence_transformers import SentenceTransformer
6
  from langchain.prompts import PromptTemplate
7
  from collections import Counter
8
 
9
 
10
- def process(api, caption, category, asr, ocr):
11
- openai.api_key = api
12
  preference = "兴趣标签"
 
 
 
 
 
13
  example = "例如,给定一个视频,它的\"标题\"为\"长安系最便宜的轿车,4W起很多人都看不上它,但我知道车只是代步工具,又需要什么面子呢" \
14
  "!\",\"类别\"为\"汽车\",\"ocr\"为\"长安系最便宜的一款轿车\",\"asr\"为\"我不否认现在的国产和合资还有一定的差距," \
15
  "但确实是他们让我们5万开了MP V8万开上了轿车,10万开张了ICV15万开张了大七座。\",\"{}\"生成机器人推断出合理的\"{}\"为\"" \
@@ -41,7 +46,7 @@ def process(api, caption, category, asr, ocr):
41
  res += ans.split('、')
42
 
43
  tag_count = Counter(res)
44
- tag_count = sorted(tag_count.items(), key=lambda x: x[1], reverse=True)
45
 
46
  tags_embed = np.load('./tag_data/tags_embed.npy')
47
  tags_dis = np.load('./tag_data/tags_dis.npy')
@@ -65,11 +70,9 @@ def process(api, caption, category, asr, ocr):
65
  if score > 0.9:
66
  final_ans.append(all_tags[j])
67
 
68
- print(final_ans)
69
-
70
  final_ans = Counter(final_ans)
71
  final_ans = sorted(final_ans.items(), key=lambda x: x[1], reverse=True)[:5]
72
- final_ans = [_[0] for _ in final_ans if _[1] > 1]
73
 
74
  return "、".join(final_ans)
75
 
@@ -77,17 +80,118 @@ def process(api, caption, category, asr, ocr):
77
  return 'api error'
78
 
79
 
 
 
 
 
 
80
  with gr.Blocks() as demo:
81
- text_api = gr.Textbox(label='OpenAI API key')
82
- text_caption = gr.Textbox(label='Caption')
83
- text_category = gr.Textbox(label='Category')
84
- text_asr = gr.Textbox(label='ASR')
85
- text_ocr = gr.Textbox(label='OCR')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  text_output = gr.Textbox(value='', label='Output')
88
 
89
  btn = gr.Button(value='Submit')
90
- btn.click(process, inputs=[text_api, text_caption, text_category, text_asr, text_ocr], outputs=[text_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
 
93
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import numpy as np
3
  import openai
4
+ import time
5
 
6
  from sentence_transformers import SentenceTransformer
7
  from langchain.prompts import PromptTemplate
8
  from collections import Counter
9
 
10
 
11
+ def process(caption, category, asr, ocr):
 
12
  preference = "兴趣标签"
13
+ if len(category) == 0:
14
+ category = "空"
15
+ if len(asr) == 0:
16
+ asr = "空"
17
+
18
  example = "例如,给定一个视频,它的\"标题\"为\"长安系最便宜的轿车,4W起很多人都看不上它,但我知道车只是代步工具,又需要什么面子呢" \
19
  "!\",\"类别\"为\"汽车\",\"ocr\"为\"长安系最便宜的一款轿车\",\"asr\"为\"我不否认现在的国产和合资还有一定的差距," \
20
  "但确实是他们让我们5万开了MP V8万开上了轿车,10万开张了ICV15万开张了大七座。\",\"{}\"生成机器人推断出合理的\"{}\"为\"" \
 
46
  res += ans.split('、')
47
 
48
  tag_count = Counter(res)
49
+ tag_count = sorted(tag_count.items(), key=lambda x: x[1], reverse=True)[:10]
50
 
51
  tags_embed = np.load('./tag_data/tags_embed.npy')
52
  tags_dis = np.load('./tag_data/tags_dis.npy')
 
70
  if score > 0.9:
71
  final_ans.append(all_tags[j])
72
 
 
 
73
  final_ans = Counter(final_ans)
74
  final_ans = sorted(final_ans.items(), key=lambda x: x[1], reverse=True)[:5]
75
+ final_ans = [_[0] for _ in final_ans]
76
 
77
  return "、".join(final_ans)
78
 
 
80
  return 'api error'
81
 
82
 
83
+ def connection(api_key):
84
+ openai.api_key = api_key
85
+ time.sleep(5)
86
+
87
+
88
  with gr.Blocks() as demo:
89
+ gr.Markdown("<h3><center>TagGPT</center></h3>")
90
+ gr.Markdown(
91
+ """
92
+ This is a demo to the work [TagGPT: Large Language Models are Zero-shot Multimodal Taggers](https://github.com/TencentARC/TagGPT).<br>
93
+ This space connects TagGPT to provide tagging service based on the tag set (from the Kuaishou data).<br>
94
+ """
95
+ )
96
+ gr.Markdown(
97
+ """
98
+ Step 1: input openai api key (sk-...) and click "connect" button.
99
+ """
100
+ )
101
+
102
+ with gr.Column(variant="panel"):
103
+ with gr.Row(variant="compact"):
104
+ text_api = gr.Textbox(
105
+ label='OpenAI API key',
106
+ placeholder="Paste your OpenAI API key here️",
107
+ type="password"
108
+ ).style(
109
+ container=False,
110
+ )
111
+ btn_api = gr.Button("Connect").style(full_width=False)
112
+
113
+ btn_api.click(connection, [text_api])
114
+
115
+ gr.Markdown(
116
+ """
117
+ Step 2: fall in the four items (i.e., caption, category, ASR, and OCR) in the appropriate input fields.
118
+ Click any item of the "Examples" to quickly see the tagging results.
119
+ """
120
+ )
121
+
122
+ text_caption = gr.Textbox(
123
+ label='标题(Caption)',
124
+ placeholder="Indispensable"
125
+ )
126
+ text_category = gr.Textbox(
127
+ label='类别(Category)',
128
+ placeholder="Dispensable"
129
+ )
130
+ text_asr = gr.Textbox(
131
+ label='ASR',
132
+ placeholder="Dispensable"
133
+ )
134
+ text_ocr = gr.Textbox(
135
+ label='OCR',
136
+ placeholder="Dispensable"
137
+ )
138
 
139
  text_output = gr.Textbox(value='', label='Output')
140
 
141
  btn = gr.Button(value='Submit')
142
+ btn.click(process, inputs=[text_caption, text_category, text_asr, text_ocr], outputs=[text_output])
143
+
144
+ examples = [
145
+ [
146
+ '正确解决iCloud储存空间将满的实用技巧',
147
+ '高新数码',
148
+ '你的iCloud一直提示,iCloud储存空间将满,而且还会把你的iPhone iPad Mac等相关连设备都弹一遍,怎么办,烦死了。其实只要用iPho'
149
+ 'ne打开设置,点击上方的头像栏,然后点击iCloud,选择管理账户储存空间,进入后,把这里不需要云备份按自己的需要进行删除,然后再回'
150
+ '到上一级页面,把这里的iCloud云备份关闭就可以了。',
151
+ 'iCloud储存空间将满实用技巧'
152
+ ],
153
+ [
154
+ '30平米迷你小公寓如何设计?创意设计多出一间房!',
155
+ '房产家居',
156
+ '这是一个30平米的迷你公寓,入户没有鞋柜,没有餐厅,没有书桌��置,咱们可以这样设计,首先吧卫生间墙内推30公分,嵌入鞋柜,'
157
+ '厨房旁边装三联动推拉门,进出舒适,隔绝油烟,把原有沙发该到卧室位置,可以让出一个餐厅位置,沙发前移做一组书柜,内藏隐形'
158
+ '壁床,打开是卧室,收起便是小客厅,对面墙壁,再做一组书柜和书桌,各种需求都满足。',
159
+ '30平迷你公寓怎么设计'
160
+ ],
161
+ [
162
+ '好好钓鱼!千万别算账',
163
+ '生活',
164
+ '一只鱼竿四千多,用它来钓多少鱼才能回本,钓鱼是娱乐而算账却很扎心,这是一根国产标配波爬竿,我的深海标配,主要针对海绵大'
165
+ '型鱼类,比如金枪鱼、gt、鬼头刀等,经济价值最高的算是金枪鱼了,一不小心钓上一条鱼竿就回来了,不小心又钓上一条轮子就回来了'
166
+ '当你发现你的渔具全是渔获换来的,吹起牛来是不是更加有底气。一支鱼竿,一副轮子,又来一套,哈哈。你钓了几套渔具呢?',
167
+ '钓鱼很开心,算账很扎心'
168
+ ],
169
+ [
170
+ '很多朋友再问,为什么蒸出来的花卷葱没变色,今天告诉你',
171
+ '美食',
172
+ '为什么你自己在家做的画卷,蒸出来之后,它的葱总是黄色的呢?而早餐店里面的画卷蒸出来过后它的葱都是绿油油的,那么今天呢,'
173
+ '把方法教给你,那就是我们做花卷的时候呢,再从里面加入少量的泡打粉,拌均匀,这样做出来的画卷呢,蒸熟过后,它的葱也是绿'
174
+ '油油的,大家可以看一下。',
175
+ '为什么你蒸的葱油花卷总会变黄'
176
+ ],
177
+ [
178
+ '豪华七座SUV 比亚迪唐DM-i冠军版带你体验出行新方式',
179
+ '汽车',
180
+ '你听说了吗,2023年冠军版本的唐DM-i已经上市了,上市当天,汉唐总销量已经超过了8000多台,大家觉得这个成绩怎么样,那么,今天'
181
+ '艾琳就带大家全民啊了解一下,2023年冠军版本的唐DM-i,新款的唐DM-i和老款之间的差别并不大,但是我们的新款新增了冰川蓝的颜色'
182
+ '还有啊,你看这个轮毂是不是和我们老款252公里的轮毂一样呢,另外新款多了铝合金底盘,FSD可变主力悬挂,快充也升级到了40千瓦,还'
183
+ '有专门的快充孔,20分钟就可以让他满血复活,2023款唐DM-i一共分为三个配置,尊贵尊荣尊享,不过都是7座,那配置上的差异,艾琳'
184
+ '就把它放在图上了,方便大家对比。想入手的朋友记住了,武汉比亚迪找艾琳,艾琳带你早提车。',
185
+ '豪华标杆SUV,23款唐DM-1冠军版'
186
+ ]
187
+ ]
188
+
189
+ gr.Examples(
190
+ examples,
191
+ [text_caption, text_category, text_asr, text_ocr],
192
+ text_output,
193
+ process
194
+ )
195
 
196
 
197
  if __name__ == "__main__":