dogdoh commited on
Commit
27a35ea
ยท
verified ยท
1 Parent(s): afd00ed

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +129 -0
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()