Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
from transformers import (
|
| 6 |
AutoTokenizer,
|
|
@@ -34,53 +33,65 @@ viru_model = AutoModelForSeq2SeqLM.from_pretrained(
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# =========================
|
| 37 |
-
#
|
| 38 |
# =========================
|
| 39 |
|
| 40 |
-
def
|
| 41 |
|
| 42 |
try:
|
| 43 |
|
| 44 |
-
url =
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
| 49 |
|
| 50 |
response = requests.get(
|
| 51 |
url,
|
| 52 |
-
|
| 53 |
timeout=10
|
| 54 |
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
response.text,
|
| 58 |
-
"html.parser"
|
| 59 |
-
)
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
text
|
| 69 |
-
and text.lower() != word.lower()
|
| 70 |
-
and len(text) > 1
|
| 71 |
-
):
|
| 72 |
-
syns.append(text)
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
return
|
| 80 |
|
| 81 |
-
except
|
| 82 |
-
return "Lỗi tìm synonym"
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# =========================
|
| 86 |
# TRANSLATION FUNCTIONS
|
|
@@ -100,10 +111,15 @@ def ru_to_vi(word):
|
|
| 100 |
skip_special_tokens=True
|
| 101 |
)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
def vi_to_ru(word):
|
| 109 |
|
|
@@ -121,7 +137,6 @@ def vi_to_ru(word):
|
|
| 121 |
|
| 122 |
return translated
|
| 123 |
|
| 124 |
-
|
| 125 |
# =========================
|
| 126 |
# UI
|
| 127 |
# =========================
|
|
@@ -146,8 +161,12 @@ with gr.Blocks() as demo:
|
|
| 146 |
label="🇻🇳 Nghĩa tiếng Việt"
|
| 147 |
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
ru_btn = gr.Button("Tra cứu")
|
|
@@ -157,7 +176,8 @@ with gr.Blocks() as demo:
|
|
| 157 |
inputs=ru_input,
|
| 158 |
outputs=[
|
| 159 |
vi_output,
|
| 160 |
-
|
|
|
|
| 161 |
]
|
| 162 |
)
|
| 163 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
from transformers import (
|
| 5 |
AutoTokenizer,
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# =========================
|
| 36 |
+
# WIKTIONARY LOOKUP
|
| 37 |
# =========================
|
| 38 |
|
| 39 |
+
def get_russian_info(word):
|
| 40 |
|
| 41 |
try:
|
| 42 |
|
| 43 |
+
url = (
|
| 44 |
+
"https://ru.wiktionary.org/w/api.php"
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
params = {
|
| 48 |
+
"action": "query",
|
| 49 |
+
"prop": "extracts",
|
| 50 |
+
"exintro": True,
|
| 51 |
+
"explaintext": True,
|
| 52 |
+
"titles": word,
|
| 53 |
+
"format": "json"
|
| 54 |
}
|
| 55 |
|
| 56 |
response = requests.get(
|
| 57 |
url,
|
| 58 |
+
params=params,
|
| 59 |
timeout=10
|
| 60 |
)
|
| 61 |
|
| 62 |
+
data = response.json()
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
pages = data["query"]["pages"]
|
| 65 |
|
| 66 |
+
page = next(iter(pages.values()))
|
| 67 |
|
| 68 |
+
extract = page.get(
|
| 69 |
+
"extract",
|
| 70 |
+
"Không tìm thấy định nghĩa"
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
lines = extract.split("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
definition = (
|
| 76 |
+
lines[0]
|
| 77 |
+
if len(lines) > 0
|
| 78 |
+
else "Không tìm thấy"
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
example = (
|
| 82 |
+
lines[1]
|
| 83 |
+
if len(lines) > 1
|
| 84 |
+
else "Không có ví dụ"
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
return definition, example
|
| 88 |
|
| 89 |
+
except:
|
|
|
|
| 90 |
|
| 91 |
+
return (
|
| 92 |
+
"Không tìm thấy định nghĩa",
|
| 93 |
+
"Không có ví dụ"
|
| 94 |
+
)
|
| 95 |
|
| 96 |
# =========================
|
| 97 |
# TRANSLATION FUNCTIONS
|
|
|
|
| 111 |
skip_special_tokens=True
|
| 112 |
)
|
| 113 |
|
| 114 |
+
definition, example = (
|
| 115 |
+
get_russian_info(word)
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
return (
|
| 119 |
+
translated,
|
| 120 |
+
definition,
|
| 121 |
+
example
|
| 122 |
+
)
|
| 123 |
|
| 124 |
def vi_to_ru(word):
|
| 125 |
|
|
|
|
| 137 |
|
| 138 |
return translated
|
| 139 |
|
|
|
|
| 140 |
# =========================
|
| 141 |
# UI
|
| 142 |
# =========================
|
|
|
|
| 161 |
label="🇻🇳 Nghĩa tiếng Việt"
|
| 162 |
)
|
| 163 |
|
| 164 |
+
definition_output = gr.Textbox(
|
| 165 |
+
label="📖 Định nghĩa"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
example_output = gr.Textbox(
|
| 169 |
+
label="💬 Ví dụ"
|
| 170 |
)
|
| 171 |
|
| 172 |
ru_btn = gr.Button("Tra cứu")
|
|
|
|
| 176 |
inputs=ru_input,
|
| 177 |
outputs=[
|
| 178 |
vi_output,
|
| 179 |
+
definition_output,
|
| 180 |
+
example_output
|
| 181 |
]
|
| 182 |
)
|
| 183 |
|