一、基于文心一言的意查词应用
可以通过描述的意思来查找名词,包括汉语同意、近义词,汉语转英语的同意、近义词,英语同义、近义词,英语转汉语同意、近义词语等。基于最先进的文心一言4.0大模型实现。
1.创意思路
根据词的描述,找到对应的词子。
2.例子
例如输入: 坚贞不渝
输出如下:
The Chinese phrase “坚贞不渝” (jiān zhēn bù yú) means “steadfast and unwavering.” Here are 20 synonyms in English:
-
- Loyal
-
- Faithful
-
- Constant
-
- Steadfast
-
- Unwavering
-
- Resolute
-
- Firm
-
- Committed
-
- Dedicated
-
- Tenacious
-
- Determined
-
- Strong-willed
-
- Resilient
-
- Persevering
-
- Gritty
-
- Dogged
-
- Indomitable
-
- Unyielding
-
- Inflexible
-
- Obstinate
Note: While these words share similar meanings, their connotations and nuances may differ slightly depending on the context in which they are used.
3.应用界面
二、gradio部署代码
1.安装erniebot包
import os
os.system("pip install -U erniebot -i https://mirrors.aliyun.com/pypi/simple/")
2.导入erniebot、gradio
当然也可以用 qianfan 包了。
import gradio as gr
import erniebot
3.预测
def predict(content, state):
erniebot.ak = "XXXXXXXXXXXXXXXXXXXXXXx"
erniebot.sk = "XXXXXXXXXXXXXXXXXXXXXXx"
print(state)
if state == "汉":
message = f"你是一名非常著名的语文老师,请给出【{content}】的同义词、近义词 ,不少于20条"
elif state == "汉英":
message = f"You are a great chinese-english translator, please translate the following words【{content}】, and give his synonyms, synonyms, no less than 20"
elif state == "English":
message = f"You are a very good English teacher, please give [{content}] synonyms, synonyms, no less than 20"
elif state == "English-Chinese":
message = f"你是一名非常著名的英语-汉语翻译家,请给出【{content}】的中文同义词、近义词,不少于20条"
response = erniebot.ChatCompletion.create(model="ernie-bot-4", messages=[{"role": "user",
"content": message}])
# print(response.result)
return response.result
4.撰写UI
with gr.Blocks(theme=gr.themes.Glass()) as demo:
5.应用介绍
with gr.Row():
gr.HTML(
"""<h1 align="center">词意查词</h1>""")
with gr.Row():
gr.HTML(
"""<img align="center" src='https://ai-studio-static-online.cdn.bcebos.com/9f4c4e3836954417b874c22acd8e8139c916709fb2e5414e9d6d96204500c035' width='100%'> <br>""")
with gr.Row():
gr.HTML(
"""<h3 align="center">可以通过描述的意思来查找词语。基于最先进的文心一言大模型实现。</h3>""")
6.各功能区、以及按钮事件
with gr.Row():
with gr.Tab("汉语反向词典") as chinese_tab:
state = gr.Textbox(label="描述", interactive=True,
value="汉", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn1 = gr.Button("🚀一键生成🚀")
btn2 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn1.click(fn=predict, inputs=[input_text,state], outputs=output)
btn2.click(lambda _: (None), inputs=btn2, outputs=[input_text])
with gr.Tab("汉英反向词典") as english_tab:
state = gr.Textbox(label="描述", interactive=True,
value="汉英", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn3 = gr.Button("🚀一键生成🚀")
btn4 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn3.click(fn=predict, inputs=[input_text, state], outputs=output)
btn4.click(lambda _: (None), inputs=btn4, outputs=[input_text])
with gr.Tab("English Reverse Dictionary") as chinese_english_tab:
state = gr.Textbox(label="描述", interactive=True,
value="English", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn5 = gr.Button("🚀一键生成🚀")
btn6 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn5.click(fn=predict, inputs=[input_text, state], outputs=output)
btn6.click(lambda _: (None), inputs=btn6, outputs=[input_text])
with gr.Tab("English-Chinese Reverse Dictionary") as english_chinese_tab:
state = gr.Textbox(label="描述", interactive=True,
value="English-Chinese", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn7 = gr.Button("🚀一键生成🚀")
btn8 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn7.click(fn=predict, inputs=[input_text, state], outputs=output)
btn8.click(lambda _: (None), inputs=btn8, outputs=[input_text])
demo.launch()
7.启动应用
demo.launch()
三、完整代码
import os
os.system("pip install -U erniebot -i https://mirrors.aliyun.com/pypi/simple/")
import gradio as gr
import erniebot
def predict(content, state):
erniebot.ak = "XXXXXXXXXXXXXXXXXXXXXXXX"
erniebot.sk = "XXXXXXXXXXXXXXXXXXXXXXXX"
print(state)
if state == "汉":
message = f"你是一名非常著名的语文老师,请给出【{content}】的同义词、近义词 ,不少于20条"
elif state == "汉英":
message = f"You are a great chinese-english translator, please translate the following words【{content}】, and give his synonyms, synonyms, no less than 20"
elif state == "English":
message = f"You are a very good English teacher, please give [{content}] synonyms, synonyms, no less than 20"
elif state == "English-Chinese":
message = f"你是一名非常著名的英语-汉语翻译家,请给出【{content}】的中文同义词、近义词,不少于20条"
response = erniebot.ChatCompletion.create(model="ernie-bot-4", messages=[{"role": "user",
"content": message}])
# print(response.result)
return response.result
with gr.Blocks(theme=gr.themes.Glass()) as demo:
with gr.Row():
gr.HTML(
"""<h1 align="center">词意查词</h1>""")
with gr.Row():
gr.HTML(
"""<img align="center" src='https://ai-studio-static-online.cdn.bcebos.com/9f4c4e3836954417b874c22acd8e8139c916709fb2e5414e9d6d96204500c035' width='100%'> <br>""")
with gr.Row():
gr.HTML(
"""<h3 align="center">可以通过描述的意思来查找词语。基于最先进的文心一言大模型实现。</h3>""")
with gr.Row():
with gr.Tab("汉语反向词典") as chinese_tab:
state = gr.Textbox(label="描述", interactive=True,
value="汉", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn1 = gr.Button("🚀一键生成🚀")
btn2 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn1.click(fn=predict, inputs=[input_text,state], outputs=output)
btn2.click(lambda _: (None), inputs=btn2, outputs=[input_text])
with gr.Tab("汉英反向词典") as english_tab:
state = gr.Textbox(label="描述", interactive=True,
value="汉英", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn3 = gr.Button("🚀一键生成🚀")
btn4 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn3.click(fn=predict, inputs=[input_text, state], outputs=output)
btn4.click(lambda _: (None), inputs=btn4, outputs=[input_text])
with gr.Tab("English Reverse Dictionary") as chinese_english_tab:
state = gr.Textbox(label="描述", interactive=True,
value="English", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn5 = gr.Button("🚀一键生成🚀")
btn6 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn5.click(fn=predict, inputs=[input_text, state], outputs=output)
btn6.click(lambda _: (None), inputs=btn6, outputs=[input_text])
with gr.Tab("English-Chinese Reverse Dictionary") as english_chinese_tab:
state = gr.Textbox(label="描述", interactive=True,
value="English-Chinese", visible=False)
with gr.Row():
input_text = gr.Textbox(label="描述", interactive=True,
value="请输入描述,或者直接搜索,或者使用输入例子")
with gr.Row():
btn7 = gr.Button("🚀一键生成🚀")
btn8 = gr.Button("✨一键清理✨")
with gr.Row():
output = gr.Textbox(lines=15, label='输出')
btn7.click(fn=predict, inputs=[input_text, state], outputs=output)
btn8.click(lambda _: (None), inputs=btn8, outputs=[input_text])
demo.launch()