Kims12 commited on
Commit
f75b944
Β·
verified Β·
1 Parent(s): 4784f5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -1 +1,34 @@
1
- ㅁㄴ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # μ˜μ–΄ β†’ ν•œκ΅­μ–΄ λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ λ‘œλ“œ
6
+ translator_en_ko = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ko")
7
+ # ν•œκ΅­μ–΄ β†’ μ˜μ–΄ λ²ˆμ—­ νŒŒμ΄ν”„λΌμΈ λ‘œλ“œ
8
+ translator_ko_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
9
+
10
+ def translate_text(text: str) -> str:
11
+ """
12
+ μž…λ ₯ ν…μŠ€νŠΈμ— ν•œκΈ€μ΄ ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ νŒλ³„ν•˜μ—¬
13
+ μ˜μ–΄μ™€ ν•œκ΅­μ–΄ κ°„μ˜ λ²ˆμ—­μ„ μˆ˜ν–‰ν•˜λŠ” ν•¨μˆ˜.
14
+ """
15
+ if re.search(r'[κ°€-힣]', text):
16
+ # ν…μŠ€νŠΈμ— ν•œκΈ€μ΄ μ‘΄μž¬ν•˜λ©΄ ν•œκ΅­μ–΄ -> μ˜μ–΄ λ²ˆμ—­
17
+ result = translator_ko_en(text)
18
+ return result[0]['translation_text']
19
+ else:
20
+ # ν…μŠ€νŠΈμ— ν•œκΈ€μ΄ μ—†μœΌλ©΄ μ˜μ–΄ -> ν•œκ΅­μ–΄ λ²ˆμ—­
21
+ result = translator_en_ko(text)
22
+ return result[0]['translation_text']
23
+
24
+ # κ·ΈλΌλ””μ˜€ μΈν„°νŽ˜μ΄μŠ€ ꡬ성
25
+ iface = gr.Interface(
26
+ fn=translate_text,
27
+ inputs=gr.inputs.Textbox(lines=5, label="μž…λ ₯ ν…μŠ€νŠΈ"),
28
+ outputs=gr.outputs.Textbox(label="λ²ˆμ—­ κ²°κ³Ό"),
29
+ title="μ–‘λ°©ν–₯ μ˜μ–΄-ν•œκ΅­μ–΄ λ²ˆμ—­κΈ°",
30
+ description="μž…λ ₯된 ν…μŠ€νŠΈμ˜ μ–Έμ–΄λ₯Ό κ°μ§€ν•˜μ—¬ μ˜μ–΄μ™€ ν•œκ΅­μ–΄ κ°„ λ²ˆμ—­μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€."
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ iface.launch()