File size: 5,228 Bytes
27a35ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""DaRi โ€” ENโ†”KO Technical Translation API | ๋‹ค๋ฆฌ(bridge) between languages"""
import gradio as gr
import requests

API_URL = "http://168.107.54.158:8000"
API_KEY = "kt_b22b7beb939da8b0e963679413b5b663"
HEADERS = {"X-API-Key": API_KEY, "Content-Type": "application/json"}


def translate(text, direction):
    src = "en" if direction == "EN โ†’ KO" else "ko"
    tgt = "ko" if direction == "EN โ†’ KO" else "en"
    try:
        r = requests.post(f"{API_URL}/v1/translate", json={
            "text": text, "source_lang": src, "target_lang": tgt, "use_tm": True
        }, headers=HEADERS, timeout=15)
        data = r.json()
        translation = data.get("translation") or ""
        suggestions = data.get("tm_suggestions", [])
        tm_text = ""
        for s in suggestions[:3]:
            sim = s.get("similarity", 0)
            tm_text += f"**[{sim:.0%}]** {s['source']}\nโ†’ {s['target']}\n\n"
        if not translation and tm_text:
            translation = suggestions[0]["target"] if suggestions else "๋งค์นญ ์—†์Œ"
        return translation or "๋งค์นญ ์—†์Œ (MT ๋ชจ๋ธ ์ค€๋น„ ์ค‘)", tm_text or "๋งค์นญ ์—†์Œ"
    except Exception as e:
        return f"์˜ค๋ฅ˜: {e}", ""


def glossary(term):
    try:
        r = requests.get(f"{API_URL}/v1/glossary", params={"term": term}, headers=HEADERS, timeout=10)
        results = r.json().get("results", [])
        if not results:
            return "๊ฒฐ๊ณผ ์—†์Œ"
        return "\n".join(f"**{g['source']}** โ†’ {g['target']} `{g.get('domain','')}`" for g in results[:10])
    except Exception as e:
        return f"์˜ค๋ฅ˜: {e}"


def quality(source, translation):
    try:
        r = requests.post(f"{API_URL}/v1/quality", json={
            "source": source, "translation": translation
        }, headers=HEADERS, timeout=5)
        m = r.json().get("metrics", {})
        score = m.get("quality_score", "N/A")
        emoji = "๐ŸŸข" if score == 1 else "๐ŸŸก" if score >= 0.7 else "๐Ÿ”ด"
        return f"""{emoji} **ํ’ˆ์งˆ ์ ์ˆ˜**: {score}
๐Ÿ“ **๊ธธ์ด ๋น„์œจ**: {m.get('length_ratio', 'N/A')}
๐Ÿ” **๋นˆ ๋ฒˆ์—ญ**: {'์˜ˆ โš ๏ธ' if m.get('empty') else '์•„๋‹ˆ์˜ค โœ…'}
๐Ÿ“‹ **์†Œ์Šค ๋ณต์‚ฌ**: {'๊ฐ์ง€ โš ๏ธ' if m.get('copy_detected') else '์ •์ƒ โœ…'}"""
    except Exception as e:
        return f"์˜ค๋ฅ˜: {e}"


def get_api_key(email, name):
    try:
        r = requests.post(f"{API_URL}/v1/auth/token", json={"email": email, "name": name}, timeout=5)
        data = r.json()
        key = data.get("api_key", "")
        return f"""โœ… **API Key**: `{key}`

**์‚ฌ์šฉ๋ฒ•**:
```bash
curl -X POST {API_URL}/v1/translate \\
  -H "X-API-Key: {key}" \\
  -H "Content-Type: application/json" \\
  -d '{{"text":"patent application","source_lang":"en","target_lang":"ko"}}'
```

๋ฌด๋ฃŒ ํ”Œ๋žœ: ์›” 50,000์ž | [API ๋ฌธ์„œ]({API_URL}/docs)"""
    except Exception as e:
        return f"์˜ค๋ฅ˜: {e}"


CSS = """
.gradio-container {max-width: 960px !important}
h1 {background: linear-gradient(90deg, #00d2ff, #3a7bd5); -webkit-background-clip: text; -webkit-text-fill-color: transparent}
"""

with gr.Blocks(title="DaRi โ€” ENโ†”KO Translation API", theme=gr.themes.Soft(), css=CSS) as demo:
    gr.Markdown("""
    # DaRi โ€” ๋‹ค๋ฆฌ
    ### ENโ†”KO Technical Translation API
    
    **62.9M** parallel pairs ยท **844K** glossary terms ยท Patent ยท Medical ยท IT ยท Legal ยท ESG
    """)
    
    with gr.Tab("๐Ÿ”ค ๋ฒˆ์—ญ"):
        direction = gr.Radio(["EN โ†’ KO", "KO โ†’ EN"], value="EN โ†’ KO", label="๋ฐฉํ–ฅ")
        input_text = gr.Textbox(label="์ž…๋ ฅ", lines=3, placeholder="The device shall comply with FDA regulations.")
        translate_btn = gr.Button("๋ฒˆ์—ญ", variant="primary")
        output_text = gr.Textbox(label="๊ฒฐ๊ณผ", lines=3)
        tm_output = gr.Markdown(label="Translation Memory ๋งค์นญ")
        translate_btn.click(translate, [input_text, direction], [output_text, tm_output])
    
    with gr.Tab("๐Ÿ“– ์šฉ์–ด์ง‘"):
        term_input = gr.Textbox(label="๊ฒ€์ƒ‰์–ด", placeholder="patent, surgery, device...")
        term_btn = gr.Button("๊ฒ€์ƒ‰", variant="primary")
        term_output = gr.Markdown()
        term_btn.click(glossary, term_input, term_output)
    
    with gr.Tab("โœ… ํ’ˆ์งˆ ํ‰๊ฐ€"):
        with gr.Row():
            qa_source = gr.Textbox(label="์›๋ฌธ", lines=2, placeholder="medical device")
            qa_trans = gr.Textbox(label="๋ฒˆ์—ญ", lines=2, placeholder="์˜๋ฃŒ๊ธฐ๊ธฐ")
        qa_btn = gr.Button("ํ‰๊ฐ€", variant="primary")
        qa_output = gr.Markdown()
        qa_btn.click(quality, [qa_source, qa_trans], qa_output)
    
    with gr.Tab("๐Ÿ”‘ API Key"):
        gr.Markdown("๋ฌด๋ฃŒ API ํ‚ค๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์œผ์„ธ์š”. ์›” 50,000์ž๊นŒ์ง€ ๋ฌด๋ฃŒ์ž…๋‹ˆ๋‹ค.")
        with gr.Row():
            key_email = gr.Textbox(label="์ด๋ฉ”์ผ")
            key_name = gr.Textbox(label="์ด๋ฆ„")
        key_btn = gr.Button("๋ฐœ๊ธ‰", variant="primary")
        key_output = gr.Markdown()
        key_btn.click(get_api_key, [key_email, key_name], key_output)
    
    gr.Markdown(f"""
    ---
    **DaRi** ยฉ 2026 | [API Docs]({API_URL}/docs) | [Contact](mailto:dogdoh1338@icloud.com)
    
    *DaRi(๋‹ค๋ฆฌ) = Bridge between ENโ†”KO. Powered by 63M parallel corpus.*
    """)

demo.launch()