lvyb
commited on
Commit
•
c3a1883
1
Parent(s):
6f66685
xixi
Browse files- app.py +34 -0
- tabs/__init__.py +0 -0
- tabs/audit.py +31 -0
- tabs/chatbot.py +23 -0
- tabs/helloworld.py +15 -0
- tabs/ocr.py +64 -0
- tabs/openai.py +49 -0
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from tabs.audit import audit_tab
|
4 |
+
from tabs.ocr import ocr_tab
|
5 |
+
from tabs.helloworld import hello_world_tab
|
6 |
+
from tabs.chatbot import chatbot_tab
|
7 |
+
from tabs.openai import openai_tab
|
8 |
+
|
9 |
+
gr.close_all()
|
10 |
+
|
11 |
+
css_string = """
|
12 |
+
#ddd1233 .user {
|
13 |
+
min-width: 100px;
|
14 |
+
max-width: 500px;
|
15 |
+
width: auto;
|
16 |
+
}
|
17 |
+
#ddd1233 .bot {
|
18 |
+
min-width: 100px;
|
19 |
+
max-width: 500px;
|
20 |
+
width: auto;
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
|
24 |
+
with gr.Blocks(css=css_string) as demo:
|
25 |
+
gr.Markdown("""
|
26 |
+
# gradio分享呀
|
27 |
+
""")
|
28 |
+
hello_world_tab()
|
29 |
+
audit_tab()
|
30 |
+
ocr_tab()
|
31 |
+
chatbot_tab()
|
32 |
+
openai_tab()
|
33 |
+
|
34 |
+
demo.launch()
|
tabs/__init__.py
ADDED
File without changes
|
tabs/audit.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
def audit_text(text, url):
|
6 |
+
data = {
|
7 |
+
"text": text,
|
8 |
+
"source": "ALIYUN",
|
9 |
+
"model": "ALIYUN_TEXT_AUDIT"
|
10 |
+
}
|
11 |
+
res = requests.post(url, json=data)
|
12 |
+
data = res.json()
|
13 |
+
|
14 |
+
is_pass = data.get('data', {}).get('pass')
|
15 |
+
if is_pass:
|
16 |
+
return data, f'合规'
|
17 |
+
return data, '不合规'
|
18 |
+
|
19 |
+
|
20 |
+
def audit_tab():
|
21 |
+
with gr.Tab("关键词审核"):
|
22 |
+
with gr.Row():
|
23 |
+
url_input = gr.Textbox(label='接口链接')
|
24 |
+
text_input = gr.Textbox(label="待审核的关键词", lines=10)
|
25 |
+
gr.Examples(['你是傻逼吧', '你是天才'], inputs=text_input)
|
26 |
+
with gr.Row():
|
27 |
+
text_output = gr.Json(label="结果", lines=10)
|
28 |
+
label_output = gr.Label()
|
29 |
+
text_button = gr.Button('检测')
|
30 |
+
|
31 |
+
text_button.click(audit_text, inputs=[text_input, url_input], outputs=[text_output, label_output])
|
tabs/chatbot.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
|
6 |
+
def chatbot_tab():
|
7 |
+
with gr.Tab("chat bot"):
|
8 |
+
chatbot = gr.Chatbot(label='对话框', elem_id='ddd1233')
|
9 |
+
msg = gr.Textbox()
|
10 |
+
clear = gr.Button("Clear")
|
11 |
+
|
12 |
+
def user(user_message, history):
|
13 |
+
return "", history + [[user_message, None]]
|
14 |
+
|
15 |
+
def bot(history):
|
16 |
+
history[-1][1] = f"你说: {history[-1][0]}"
|
17 |
+
time.sleep(1)
|
18 |
+
return history
|
19 |
+
|
20 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
21 |
+
bot, chatbot, chatbot
|
22 |
+
)
|
23 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
tabs/helloworld.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
|
4 |
+
def hello_world(name, request: gr.Request):
|
5 |
+
return f'Hello {name}!'
|
6 |
+
|
7 |
+
|
8 |
+
def hello_world_tab():
|
9 |
+
with gr.Tab("hello world!"):
|
10 |
+
with gr.Row():
|
11 |
+
text_input = gr.Textbox(label='姓名', placeholder='请输入你的姓名')
|
12 |
+
text_output = gr.Textbox(label="结果", lines=10)
|
13 |
+
text_button = gr.Button('hello world')
|
14 |
+
|
15 |
+
text_button.click(hello_world, inputs=text_input, outputs=text_output)
|
tabs/ocr.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
request_method_map = {
|
6 |
+
'同步': 'sync',
|
7 |
+
'异步': 'async',
|
8 |
+
}
|
9 |
+
|
10 |
+
tasks_map = {
|
11 |
+
'关键词提取': 'keywords',
|
12 |
+
'ocr': 'ocr',
|
13 |
+
'表格': 'ocr_table',
|
14 |
+
}
|
15 |
+
|
16 |
+
subtask_map = {
|
17 |
+
'身份证': 'id_card',
|
18 |
+
'信息表': 'inform_table',
|
19 |
+
'营业执照': 'bus_lic',
|
20 |
+
'银行卡信息': 'bank_info',
|
21 |
+
'用户信息采集表': 'user_info_collect',
|
22 |
+
}
|
23 |
+
|
24 |
+
|
25 |
+
def ocr_task(request_method, tasks, subtask, file, url):
|
26 |
+
"""
|
27 |
+
|
28 |
+
:param request_method:
|
29 |
+
:param tasks:
|
30 |
+
:param subtask:
|
31 |
+
:param file:
|
32 |
+
:param url:
|
33 |
+
:return:
|
34 |
+
"""
|
35 |
+
res = requests.post(url, data={
|
36 |
+
'file': file,
|
37 |
+
'request_method': request_method_map.get(request_method),
|
38 |
+
'tasks': tasks_map.get(tasks),
|
39 |
+
'subtask': subtask_map.get(subtask),
|
40 |
+
'angle': 0,
|
41 |
+
})
|
42 |
+
return str(res.json())
|
43 |
+
|
44 |
+
|
45 |
+
def ocr_tab():
|
46 |
+
with gr.Tab("ocr 识别"):
|
47 |
+
gr.Markdown('ocr 工具')
|
48 |
+
with gr.Accordion("说明", open=False):
|
49 |
+
gr.Markdown("""
|
50 |
+
# Hello World!
|
51 |
+
嘤嘤嘤
|
52 |
+
嘻嘻嘻
|
53 |
+
""")
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
url_input = gr.Textbox(label='接口链接')
|
57 |
+
request_method = gr.Dropdown(choices=list(request_method_map.keys()), label='运行方式')
|
58 |
+
tasks = gr.Dropdown(choices=list(tasks_map.keys()), label='模式')
|
59 |
+
subtask = gr.Dropdown(choices=list(subtask_map.keys()), label='文件类型')
|
60 |
+
file = gr.File(type='file')
|
61 |
+
text_output = gr.Textbox(label="结果", lines=10)
|
62 |
+
text_button = gr.Button('识别')
|
63 |
+
|
64 |
+
text_button.click(ocr_task, inputs=[request_method, tasks, subtask, file, url_input], outputs=text_output)
|
tabs/openai.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
|
4 |
+
|
5 |
+
def generate_image(prompt, api_key):
|
6 |
+
# api_key = os.environ.get('OPENAI_API_KEY')
|
7 |
+
if not api_key:
|
8 |
+
raise gr.Error("缺少OPENAI_API_KEY")
|
9 |
+
|
10 |
+
response = openai.Image.create(
|
11 |
+
prompt=prompt,
|
12 |
+
n=1,
|
13 |
+
size="1024x1024",
|
14 |
+
api_key=api_key
|
15 |
+
)
|
16 |
+
image_url = response['data'][0]['url']
|
17 |
+
return image_url
|
18 |
+
|
19 |
+
|
20 |
+
def audio(file, api_key):
|
21 |
+
if not api_key:
|
22 |
+
raise gr.Error("缺少OPENAI_API_KEY")
|
23 |
+
|
24 |
+
with open(file, 'rb') as f:
|
25 |
+
transcript = openai.Audio.transcribe(model="whisper-1", file=f,
|
26 |
+
api_key=api_key)
|
27 |
+
return transcript['text']
|
28 |
+
|
29 |
+
|
30 |
+
def openai_tab():
|
31 |
+
with gr.Tab("openai"):
|
32 |
+
gr.Markdown('### 文字生成图片')
|
33 |
+
api_key_input = gr.Textbox(label='OPENAI_API_KEY', placeholder='请输入你的OPENAI_API_KEY', type='password')
|
34 |
+
|
35 |
+
with gr.Row():
|
36 |
+
text_input = gr.Textbox(label='请输入你的创意', placeholder='请输入你的创意')
|
37 |
+
text_output = gr.Image(label="图片")
|
38 |
+
text_button = gr.Button('生成创意')
|
39 |
+
text_button.click(generate_image, inputs=[text_input, api_key_input], outputs=text_output)
|
40 |
+
|
41 |
+
gr.Markdown('### 音频识别')
|
42 |
+
with gr.Row():
|
43 |
+
file_input = gr.Audio(label='音频文件', type='filepath')
|
44 |
+
text_output = gr.Text()
|
45 |
+
text_button = gr.Button('音频识别')
|
46 |
+
text_button.click(audio, inputs=[file_input, api_key_input], outputs=text_output)
|
47 |
+
|
48 |
+
|
49 |
+
|