Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""DaRi โ ENโKO Technical Translation API | ๋ค๋ฆฌ(bridge) between languages"""
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
API_URL = "http://168.107.54.158:8000"
|
| 6 |
+
API_KEY = "kt_b22b7beb939da8b0e963679413b5b663"
|
| 7 |
+
HEADERS = {"X-API-Key": API_KEY, "Content-Type": "application/json"}
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def translate(text, direction):
|
| 11 |
+
src = "en" if direction == "EN โ KO" else "ko"
|
| 12 |
+
tgt = "ko" if direction == "EN โ KO" else "en"
|
| 13 |
+
try:
|
| 14 |
+
r = requests.post(f"{API_URL}/v1/translate", json={
|
| 15 |
+
"text": text, "source_lang": src, "target_lang": tgt, "use_tm": True
|
| 16 |
+
}, headers=HEADERS, timeout=15)
|
| 17 |
+
data = r.json()
|
| 18 |
+
translation = data.get("translation") or ""
|
| 19 |
+
suggestions = data.get("tm_suggestions", [])
|
| 20 |
+
tm_text = ""
|
| 21 |
+
for s in suggestions[:3]:
|
| 22 |
+
sim = s.get("similarity", 0)
|
| 23 |
+
tm_text += f"**[{sim:.0%}]** {s['source']}\nโ {s['target']}\n\n"
|
| 24 |
+
if not translation and tm_text:
|
| 25 |
+
translation = suggestions[0]["target"] if suggestions else "๋งค์นญ ์์"
|
| 26 |
+
return translation or "๋งค์นญ ์์ (MT ๋ชจ๋ธ ์ค๋น ์ค)", tm_text or "๋งค์นญ ์์"
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"์ค๋ฅ: {e}", ""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def glossary(term):
|
| 32 |
+
try:
|
| 33 |
+
r = requests.get(f"{API_URL}/v1/glossary", params={"term": term}, headers=HEADERS, timeout=10)
|
| 34 |
+
results = r.json().get("results", [])
|
| 35 |
+
if not results:
|
| 36 |
+
return "๊ฒฐ๊ณผ ์์"
|
| 37 |
+
return "\n".join(f"**{g['source']}** โ {g['target']} `{g.get('domain','')}`" for g in results[:10])
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"์ค๋ฅ: {e}"
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def quality(source, translation):
|
| 43 |
+
try:
|
| 44 |
+
r = requests.post(f"{API_URL}/v1/quality", json={
|
| 45 |
+
"source": source, "translation": translation
|
| 46 |
+
}, headers=HEADERS, timeout=5)
|
| 47 |
+
m = r.json().get("metrics", {})
|
| 48 |
+
score = m.get("quality_score", "N/A")
|
| 49 |
+
emoji = "๐ข" if score == 1 else "๐ก" if score >= 0.7 else "๐ด"
|
| 50 |
+
return f"""{emoji} **ํ์ง ์ ์**: {score}
|
| 51 |
+
๐ **๊ธธ์ด ๋น์จ**: {m.get('length_ratio', 'N/A')}
|
| 52 |
+
๐ **๋น ๋ฒ์ญ**: {'์ โ ๏ธ' if m.get('empty') else '์๋์ค โ
'}
|
| 53 |
+
๐ **์์ค ๋ณต์ฌ**: {'๊ฐ์ง โ ๏ธ' if m.get('copy_detected') else '์ ์ โ
'}"""
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"์ค๋ฅ: {e}"
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def get_api_key(email, name):
|
| 59 |
+
try:
|
| 60 |
+
r = requests.post(f"{API_URL}/v1/auth/token", json={"email": email, "name": name}, timeout=5)
|
| 61 |
+
data = r.json()
|
| 62 |
+
key = data.get("api_key", "")
|
| 63 |
+
return f"""โ
**API Key**: `{key}`
|
| 64 |
+
|
| 65 |
+
**์ฌ์ฉ๋ฒ**:
|
| 66 |
+
```bash
|
| 67 |
+
curl -X POST {API_URL}/v1/translate \\
|
| 68 |
+
-H "X-API-Key: {key}" \\
|
| 69 |
+
-H "Content-Type: application/json" \\
|
| 70 |
+
-d '{{"text":"patent application","source_lang":"en","target_lang":"ko"}}'
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
๋ฌด๋ฃ ํ๋: ์ 50,000์ | [API ๋ฌธ์]({API_URL}/docs)"""
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"์ค๋ฅ: {e}"
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
CSS = """
|
| 79 |
+
.gradio-container {max-width: 960px !important}
|
| 80 |
+
h1 {background: linear-gradient(90deg, #00d2ff, #3a7bd5); -webkit-background-clip: text; -webkit-text-fill-color: transparent}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(title="DaRi โ ENโKO Translation API", theme=gr.themes.Soft(), css=CSS) as demo:
|
| 84 |
+
gr.Markdown("""
|
| 85 |
+
# DaRi โ ๋ค๋ฆฌ
|
| 86 |
+
### ENโKO Technical Translation API
|
| 87 |
+
|
| 88 |
+
**62.9M** parallel pairs ยท **844K** glossary terms ยท Patent ยท Medical ยท IT ยท Legal ยท ESG
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
+
with gr.Tab("๐ค ๋ฒ์ญ"):
|
| 92 |
+
direction = gr.Radio(["EN โ KO", "KO โ EN"], value="EN โ KO", label="๋ฐฉํฅ")
|
| 93 |
+
input_text = gr.Textbox(label="์
๋ ฅ", lines=3, placeholder="The device shall comply with FDA regulations.")
|
| 94 |
+
translate_btn = gr.Button("๋ฒ์ญ", variant="primary")
|
| 95 |
+
output_text = gr.Textbox(label="๊ฒฐ๊ณผ", lines=3)
|
| 96 |
+
tm_output = gr.Markdown(label="Translation Memory ๋งค์นญ")
|
| 97 |
+
translate_btn.click(translate, [input_text, direction], [output_text, tm_output])
|
| 98 |
+
|
| 99 |
+
with gr.Tab("๐ ์ฉ์ด์ง"):
|
| 100 |
+
term_input = gr.Textbox(label="๊ฒ์์ด", placeholder="patent, surgery, device...")
|
| 101 |
+
term_btn = gr.Button("๊ฒ์", variant="primary")
|
| 102 |
+
term_output = gr.Markdown()
|
| 103 |
+
term_btn.click(glossary, term_input, term_output)
|
| 104 |
+
|
| 105 |
+
with gr.Tab("โ
ํ์ง ํ๊ฐ"):
|
| 106 |
+
with gr.Row():
|
| 107 |
+
qa_source = gr.Textbox(label="์๋ฌธ", lines=2, placeholder="medical device")
|
| 108 |
+
qa_trans = gr.Textbox(label="๋ฒ์ญ", lines=2, placeholder="์๋ฃ๊ธฐ๊ธฐ")
|
| 109 |
+
qa_btn = gr.Button("ํ๊ฐ", variant="primary")
|
| 110 |
+
qa_output = gr.Markdown()
|
| 111 |
+
qa_btn.click(quality, [qa_source, qa_trans], qa_output)
|
| 112 |
+
|
| 113 |
+
with gr.Tab("๐ API Key"):
|
| 114 |
+
gr.Markdown("๋ฌด๋ฃ API ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ผ์ธ์. ์ 50,000์๊น์ง ๋ฌด๋ฃ์
๋๋ค.")
|
| 115 |
+
with gr.Row():
|
| 116 |
+
key_email = gr.Textbox(label="์ด๋ฉ์ผ")
|
| 117 |
+
key_name = gr.Textbox(label="์ด๋ฆ")
|
| 118 |
+
key_btn = gr.Button("๋ฐ๊ธ", variant="primary")
|
| 119 |
+
key_output = gr.Markdown()
|
| 120 |
+
key_btn.click(get_api_key, [key_email, key_name], key_output)
|
| 121 |
+
|
| 122 |
+
gr.Markdown(f"""
|
| 123 |
+
---
|
| 124 |
+
**DaRi** ยฉ 2026 | [API Docs]({API_URL}/docs) | [Contact](mailto:dogdoh1338@icloud.com)
|
| 125 |
+
|
| 126 |
+
*DaRi(๋ค๋ฆฌ) = Bridge between ENโKO. Powered by 63M parallel corpus.*
|
| 127 |
+
""")
|
| 128 |
+
|
| 129 |
+
demo.launch()
|