File size: 6,715 Bytes
80ad90c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import argparse
import socket
import json
import requests
import gradio as gr

DEFAULT_LANGUAGES = [
    "English",
    "Chinese",
    "Japanese",
    "Korean",
    "French",
    "German",
    "Spanish",
    "Italian",
    "Portuguese",
    "Russian",
    "Arabic",
    "Hindi",
    "Bengali",
    "Thai",
    "Vietnamese",
    "Indonesian",
    "Turkish",
    "Polish",
    "Dutch",
    "Swedish",
    "Danish",
    "Norwegian",
    "Finnish",
    "Greek",
    "Czech",
    "Hungarian",
    "Romanian",
    "Ukrainian",
    "Malay",
    "Filipino",
    "Urdu",
    "Hebrew",
    "Persian",
]


def _get_ipv4_address() -> str:
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except Exception:
        return "127.0.0.1"


def build_prompt(source_text: str, target_language: str, use_zh_template: bool) -> str:
    if use_zh_template:
        return (
            f"将以下文本翻译为{target_language},注意只需要输出翻译后的结果,不要额外解释:\n"
            f"{source_text}"
        )
    return (
        f"Translate the following segment into {target_language}, without additional explanation.\n"
        f"{source_text}"
    )


def create_demo(api_base: str, model_name: str):
    def translate_stream(
        text,
        target_language,
        use_zh_template,
        temperature,
        top_p,
        top_k,
        repetition_penalty,
        max_new_tokens,
    ):
        if not text or not text.strip():
            yield ""
            return

        payload = {
            "model": model_name,
            "messages": [{"role": "user", "content": text.strip()}],
            "stream": True,
            "temperature": temperature,
            "top_p": top_p,
            "top_k": int(top_k),
            "repetition_penalty": repetition_penalty,
            "max_tokens": int(max_new_tokens),
            "target_language": target_language,
            "use_zh_template": bool(use_zh_template),
        }

        url = f"{api_base}/v1/chat/completions"
        with requests.post(url, json=payload, stream=True, timeout=300) as resp:
            resp.raise_for_status()
            resp.encoding = "utf-8"
            buffer = ""
            for raw_line in resp.iter_lines(decode_unicode=False):
                if not raw_line:
                    continue
                try:
                    line = raw_line.decode("utf-8")
                except Exception:
                    line = raw_line.decode("utf-8", errors="replace")

                if line.startswith("data: "):
                    data = line[len("data: "):].strip()
                else:
                    data = line.strip()
                if data == "[DONE]":
                    break
                if data:
                    try:
                        obj = json.loads(data)
                        delta = obj.get("choices", [{}])[0].get("delta", {})
                        content = delta.get("content", "")
                        if content:
                            buffer += content
                            yield buffer.strip()
                    except Exception:
                        continue

    with gr.Blocks(title="HY-MT1.5-1.8B_GPTQ_INT4 Multilingual Translation (C++ Backend)") as demo:
        gr.Markdown("## HY-MT1.5-1.8B_GPTQ_INT4 Multilingual Translation (C++ Backend)")

        with gr.Group():
            input_text = gr.Textbox(
                label="Input Text",
                placeholder="Please enter the text you want to translate...",
                lines=6,
            )

        with gr.Group():
            with gr.Row(equal_height=True):
                target_language = gr.Dropdown(
                    choices=DEFAULT_LANGUAGES,
                    value="English",
                    label="Target Language",
                )
                use_zh_template = gr.Checkbox(
                    label="Use Chinese Prompt Template",
                    value=False,
                )
        with gr.Group():
            with gr.Row(equal_height=True):
                temperature = gr.Slider(
                    minimum=0.1,
                    maximum=1.5,
                    value=0.7,
                    step=0.05,
                    label="Temperature",
                )
                top_p = gr.Slider(
                    minimum=0.1,
                    maximum=1.0,
                    value=0.6,
                    step=0.05,
                    label="Top-p",
                )
                top_k = gr.Slider(
                    minimum=1,
                    maximum=100,
                    value=20,
                    step=1,
                    label="Top-k",
                )

        with gr.Group():
            with gr.Row(equal_height=True):
                repetition_penalty = gr.Slider(
                    minimum=1.0,
                    maximum=1.5,
                    value=1.05,
                    step=0.01,
                    label="Repetition Penalty",
                )
                max_new_tokens = gr.Slider(
                    minimum=1,
                    maximum=1024,
                    value=512,
                    step=1,
                    label="Max New Tokens",
                )

        translate_btn = gr.Button("Translate", variant="primary")
        output_text = gr.Textbox(
            label="Translation Result",
            lines=6,
            interactive=False,
        )

        translate_btn.click(
            translate_stream,
            inputs=[
                input_text,
                target_language,
                use_zh_template,
                temperature,
                top_p,
                top_k,
                repetition_penalty,
                max_new_tokens,
            ],
            outputs=output_text,
        )

    return demo


def parse_args():
    parser = argparse.ArgumentParser(description="HY-MT1.5-1.8B_GPTQ_INT4 Gradio Demo (C++ Backend)")
    parser.add_argument("--api_base", type=str, default="http://127.0.0.1:8000")
    parser.add_argument("--model", type=str, default="AXERA-TECH/HY-MT1.5-1.8B_GPTQ_INT4-AX620E")
    parser.add_argument("--server_name", type=str, default="0.0.0.0")
    parser.add_argument("--server_port", type=int, default=7860)
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()
    app = create_demo(args.api_base, args.model)
    ipv4 = _get_ipv4_address()
    print(f"* Running on local URL:  http://{ipv4}:{args.server_port}")
    app.launch(server_name=args.server_name, server_port=args.server_port)