Gradio

command

install

1
pip install gradio

Example

1st example

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Gradio 1st example
# [Hugging Face](https://huggingface.co/)
# pip install gradio
# http://127.0.0.1:7860/
# https://609f0dd05ce375b555.gradio.live/
import gradio as gr

def rStr(text):
return text.replace("morning", "night")

# 建立 gradio
# rStr : 處理函數
# inputs : 輸入欄位
# outputs : 輸出欄位
grobj = gr.Interface(fn = rStr,
inputs = gr.Textbox(),
outputs = gr.Textbox())
# 啟動 gradio
# share=True : public 執行
# grobj.launch(share=True)
grobj.launch()
public run
local run
QR code Brave
QR code Chrome

萌典 Web App

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 萌典 Web App
# [Hugging Face](https://huggingface.co/)
# pip install gradio
# http://127.0.0.1:7860/
# https://609f0dd05ce375b555.gradio.live/
import gradio as gr
import requests
import json

def rStr(word):
reStr = ""
url = f"https://www.moedict.tw/uni/{word}"
resp = requests.get(url)
if resp and resp.status_code == 200:
info = json.loads(resp.text)
print(f"查詢字詞:{info['title']}\n")
reStr += f"查詢字詞:{info['title']}\n"
reStr += f"部首:{info['radical']}\n"
reStr += f"筆畫:{info['stroke_count']}\n"
for index, data in enumerate(info['heteronyms']):
reStr += f"[{index+1}]"
reStr += f" 注音:{data['bopomofo']}\n"
reStr += f" 羅馬拼音:{data['bopomofo2']}\n"
reStr += f" 漢語拼音:{data['pinyin']}\n"
for i, item in enumerate(data['definitions']):
reStr += f" ({i+1}):{item['def']}\n"
if "type" in item:
reStr += f" 詞性:{item['type']}\n"
if "example" in item:
reStr += f" 解釋:{item['example']}\n"
if "quote" in item:
reStr += f" 引用:{item['quote']}\n"
if "link" in item:
reStr += f" 參考\n"
for ref in item['link']:
reStr += f" {ref}\n"

return reStr

# 建立 gradio
grobj = gr.Interface(fn = rStr,
inputs = gr.Textbox(),
outputs = gr.Textbox())
# 啟動 gradio
# share=True : public 執行
# grobj.launch(share=True)
grobj.launch()

Ref