Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai, os, io, tempfile, mimetypes
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
|
| 5 |
-
#
|
| 6 |
load_dotenv()
|
| 7 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
if not api_key:
|
|
@@ -19,16 +30,21 @@ LANG_CODE = {
|
|
| 19 |
"Thai":"th","Russian":"ru","Vietnamese":"vi",
|
| 20 |
"Spanish":"es","French":"fr"
|
| 21 |
}
|
| 22 |
-
|
| 23 |
-
VOICE = {l:("nova" if l in ["Korean","Japanese","Chinese"] else "alloy")
|
| 24 |
for l in LANGUAGES}
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
| 27 |
def _safe_path(v):
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
return v.get("name") if isinstance(v, dict) else v
|
| 30 |
|
| 31 |
-
def _gpt_translate(text, src, tgt):
|
|
|
|
| 32 |
rsp = client.chat.completions.create(
|
| 33 |
model="gpt-3.5-turbo",
|
| 34 |
messages=[
|
|
@@ -37,25 +53,27 @@ def _gpt_translate(text, src, tgt):
|
|
| 37 |
f"Only provide the translated text."},
|
| 38 |
{"role":"user","content":text}
|
| 39 |
],
|
| 40 |
-
temperature=0.3,
|
| 41 |
)
|
| 42 |
return rsp.choices[0].message.content.strip()
|
| 43 |
|
| 44 |
-
def _tts(text, lang):
|
|
|
|
| 45 |
out = client.audio.speech.create(
|
| 46 |
model="tts-1",
|
| 47 |
voice=VOICE.get(lang,"alloy"),
|
| 48 |
input=text[:4096]
|
| 49 |
)
|
| 50 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 51 |
-
tmp.write(out.content)
|
|
|
|
| 52 |
return tmp.name
|
| 53 |
|
| 54 |
-
#
|
| 55 |
def translate_audio(audio_in, src, tgt):
|
| 56 |
path = _safe_path(audio_in)
|
| 57 |
if not path or not os.path.exists(path):
|
| 58 |
-
return "โ ๏ธ ์์ฑ ํ์ผ์
|
| 59 |
|
| 60 |
with open(path,"rb") as f:
|
| 61 |
stt = client.audio.transcriptions.create(
|
|
@@ -71,13 +89,13 @@ def translate_audio(audio_in, src, tgt):
|
|
| 71 |
tts_path = _tts(translated, tgt)
|
| 72 |
return original, translated, tts_path
|
| 73 |
|
| 74 |
-
#
|
| 75 |
def translate_document(file_in, src, tgt):
|
| 76 |
path = _safe_path(file_in)
|
| 77 |
if not path or not os.path.exists(path):
|
| 78 |
-
return "โ ๏ธ PDF
|
| 79 |
|
| 80 |
-
ext
|
| 81 |
mime = mimetypes.guess_type(path)[0] or ""
|
| 82 |
text = ""
|
| 83 |
|
|
@@ -85,14 +103,16 @@ def translate_document(file_in, src, tgt):
|
|
| 85 |
if ext == ".pdf" or "pdf" in mime:
|
| 86 |
import pdfplumber
|
| 87 |
with pdfplumber.open(path) as pdf:
|
| 88 |
-
pages = pdf.pages[:5] # ๋ฐ๋ชจ:
|
| 89 |
-
text
|
| 90 |
-
elif ext in [".png",".jpg",".jpeg",".bmp",".tiff",".gif"] or "image" in mime:
|
| 91 |
-
from PIL import Image
|
| 92 |
-
import pytesseract
|
| 93 |
-
text = pytesseract.image_to_string(Image.open(path))
|
| 94 |
else:
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"โ ํ
์คํธ ์ถ์ถ ์คํจ: {e}", ""
|
| 98 |
|
|
@@ -103,25 +123,27 @@ def translate_document(file_in, src, tgt):
|
|
| 103 |
translated = _gpt_translate(text, src, tgt)
|
| 104 |
return text, translated
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
STREAM_SEC = 4 # Whisper ํธ์ถ ์ฃผ๊ธฐ
|
| 108 |
-
|
| 109 |
def stream_single(mic_stream, src, tgt):
|
| 110 |
buf, header = io.BytesIO(), None
|
| 111 |
o_acc, t_acc = "", ""
|
| 112 |
while True:
|
| 113 |
chunk = mic_stream.recv()
|
| 114 |
-
if chunk is None:
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
buf.write(chunk)
|
| 117 |
if buf.getbuffer().nbytes > 16000*2*STREAM_SEC:
|
| 118 |
wav = header + buf.getvalue()
|
| 119 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
| 120 |
tmp.write(wav); tmp.close()
|
| 121 |
o, t, _ = translate_audio(tmp.name, src, tgt)
|
| 122 |
-
o_acc += " " + o
|
|
|
|
| 123 |
yield o_acc.strip(), t_acc.strip()
|
| 124 |
buf = io.BytesIO()
|
|
|
|
| 125 |
if buf.getbuffer().nbytes:
|
| 126 |
wav = header + buf.getvalue()
|
| 127 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
@@ -129,17 +151,18 @@ def stream_single(mic_stream, src, tgt):
|
|
| 129 |
o, t, _ = translate_audio(tmp.name, src, tgt)
|
| 130 |
yield (o_acc+" "+o).strip(), (t_acc+" "+t).strip()
|
| 131 |
|
| 132 |
-
#
|
| 133 |
def stream_multi(mic_stream, src):
|
| 134 |
buf, header = io.BytesIO(), None
|
| 135 |
-
acc = {lang:
|
| 136 |
|
| 137 |
while True:
|
| 138 |
chunk = mic_stream.recv()
|
| 139 |
-
if chunk is None:
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
buf.write(chunk)
|
| 142 |
-
|
| 143 |
if buf.getbuffer().nbytes > 16000*2*STREAM_SEC:
|
| 144 |
wav = header + buf.getvalue()
|
| 145 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
@@ -151,17 +174,16 @@ def stream_multi(mic_stream, src):
|
|
| 151 |
)
|
| 152 |
orig = stt.text.strip()
|
| 153 |
if orig:
|
| 154 |
-
acc["
|
| 155 |
for lang in FOUR_LANGS:
|
| 156 |
acc[lang] += " " + _gpt_translate(orig, src, lang)
|
| 157 |
-
yield (acc["
|
| 158 |
acc["English"].strip(),
|
| 159 |
acc["Chinese"].strip(),
|
| 160 |
acc["Thai"].strip(),
|
| 161 |
acc["Russian"].strip())
|
| 162 |
buf = io.BytesIO()
|
| 163 |
|
| 164 |
-
# ๋จ์ ๋ฒํผ
|
| 165 |
if buf.getbuffer().nbytes:
|
| 166 |
wav = header + buf.getvalue()
|
| 167 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
@@ -173,19 +195,19 @@ def stream_multi(mic_stream, src):
|
|
| 173 |
)
|
| 174 |
orig = stt.text.strip()
|
| 175 |
if orig:
|
| 176 |
-
acc["
|
| 177 |
for lang in FOUR_LANGS:
|
| 178 |
acc[lang] += " " + _gpt_translate(orig, src, lang)
|
| 179 |
-
yield (acc["
|
| 180 |
acc["English"].strip(),
|
| 181 |
acc["Chinese"].strip(),
|
| 182 |
acc["Thai"].strip(),
|
| 183 |
acc["Russian"].strip())
|
| 184 |
|
| 185 |
-
#
|
| 186 |
with gr.Blocks(title="SMARTok Demo", theme=gr.themes.Soft()) as app:
|
| 187 |
with gr.Tabs():
|
| 188 |
-
# 1
|
| 189 |
with gr.TabItem("๐๏ธ ์ค๋์ค ๋ฒ์ญ"):
|
| 190 |
src1 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 191 |
tgt1 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
|
@@ -196,51 +218,46 @@ with gr.Blocks(title="SMARTok Demo", theme=gr.themes.Soft()) as app:
|
|
| 196 |
stt1 = gr.Textbox(label="์๋ฌธ", lines=5)
|
| 197 |
tlt1 = gr.Textbox(label="๋ฒ์ญ", lines=5)
|
| 198 |
out1 = gr.Audio(label="TTS",type="filepath",autoplay=True)
|
| 199 |
-
btn1.click(translate_audio,
|
| 200 |
|
| 201 |
-
# 2
|
| 202 |
with gr.TabItem("๐ ๋ฌธ์/์ด๋ฏธ์ง ๋ฒ์ญ"):
|
| 203 |
src2 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 204 |
tgt2 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
| 205 |
file2= gr.File(label="PDF ๋๋ ์ด๋ฏธ์ง ์
๋ก๋",
|
| 206 |
-
file_types=[".pdf",".png",".jpg",".jpeg",
|
| 207 |
-
".bmp",".tiff",".gif"])
|
| 208 |
btn2 = gr.Button("๋ฒ์ญ")
|
| 209 |
org2 = gr.Textbox(label="์ถ์ถ ์๋ฌธ",lines=15)
|
| 210 |
trs2 = gr.Textbox(label="๋ฒ์ญ ๊ฒฐ๊ณผ",lines=15)
|
| 211 |
btn2.click(translate_document,[file2,src2,tgt2],[org2,trs2])
|
| 212 |
|
| 213 |
-
# 3
|
| 214 |
with gr.TabItem("โฑ๏ธ ์ค์๊ฐ 1์ธ์ด"):
|
| 215 |
src3 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 216 |
tgt3 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
| 217 |
mic3 = gr.Audio(sources=["microphone"],
|
| 218 |
-
streaming=True,
|
|
|
|
| 219 |
stt3 = gr.Textbox(label="์๋ฌธ(์ค์๊ฐ)",lines=8)
|
| 220 |
tlt3 = gr.Textbox(label="๋ฒ์ญ(์ค์๊ฐ)",lines=8)
|
| 221 |
-
mic3.stream(
|
| 222 |
-
inputs=[src3,tgt3],
|
| 223 |
-
outputs=[stt3,tlt3])
|
| 224 |
|
| 225 |
-
# 4
|
| 226 |
with gr.TabItem("๐ ์ค์๊ฐ 4๊ฐ ์ธ์ด"):
|
| 227 |
-
gr.Markdown("๋ง์ดํฌ ์
๋ ฅ์ **English / Chinese(็ฎไฝ) / Thai / Russian** "
|
| 228 |
-
"4๊ฐ ์ธ์ด๋ก ์ค์๊ฐ(3-4 ์ด ์ง์ฐ) ๋์ ๋ฒ์ญํฉ๋๋ค.")
|
| 229 |
src4 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ ์ธ์ด")
|
| 230 |
mic4 = gr.Audio(sources=["microphone"],
|
| 231 |
-
streaming=True,
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
mic4.stream(lambda a,s: stream_multi(a,s),
|
| 240 |
-
inputs=[src4],
|
| 241 |
outputs=[o4,e4,z4,t4,r4])
|
| 242 |
|
| 243 |
-
#
|
| 244 |
if __name__ == "__main__":
|
| 245 |
app.launch(server_name="0.0.0.0",
|
| 246 |
server_port=7860,
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SMARTok ํต์ฌ ๋ฐ๋ชจ
|
| 3 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 4 |
+
โ ํญ 1 : ์ค๋์ค(๋
น์ยทํ์ผ) ๋จ๊ฑด ๋ฒ์ญ + TTS ์ฌ์
|
| 5 |
+
โ ํญ 2 : PDF / ์ด๋ฏธ์ง ๋ฒ์ญ (Tesseract ์์ผ๋ฉด PDF๋ง ์๋ด)
|
| 6 |
+
โ ํญ 3 : ์ค์๊ฐ 1๊ฐ ์ธ์ด(์ ํํ) ๋ฒ์ญ
|
| 7 |
+
โ ํญ 4 : ์ค์๊ฐ 4๊ฐ ์ธ์ด(์ยท์ค(๊ฐ)ยทํยท๋ฌ) ๋์ ๋ฒ์ญ
|
| 8 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 9 |
+
Python โฅ3.10, Gradio 4.x, OpenAI Python SDK ํ์
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
import gradio as gr
|
| 13 |
import openai, os, io, tempfile, mimetypes
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
| 16 |
+
# โโโโโโโโโโโโโโโโโโโ 0. ๊ณตํต ์ด๊ธฐํ โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 17 |
load_dotenv()
|
| 18 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
if not api_key:
|
|
|
|
| 30 |
"Thai":"th","Russian":"ru","Vietnamese":"vi",
|
| 31 |
"Spanish":"es","French":"fr"
|
| 32 |
}
|
| 33 |
+
VOICE = {l: ("nova" if l in ["Korean","Japanese","Chinese"] else "alloy")
|
|
|
|
| 34 |
for l in LANGUAGES}
|
| 35 |
|
| 36 |
+
FOUR_LANGS = ["English", "Chinese", "Thai", "Russian"] # ์ค์๊ฐ ๋์ ๋ฒ์ญ์ฉ
|
| 37 |
+
STREAM_SEC = 4 # Whisper ํธ์ถ ๊ฐ๊ฒฉ(์ด) โ 3~4 ์ด ์ ๋ ์ง์ฐ
|
| 38 |
+
|
| 39 |
+
# โโโโโโโโโโโโโโโโโโโ 1. ์ ํธ ํจ์ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 40 |
def _safe_path(v):
|
| 41 |
+
"""Gradio File/Audio ์
๋ ฅ โ ์ค์ ๊ฒฝ๋ก ์ถ์ถ"""
|
| 42 |
+
if v is None:
|
| 43 |
+
return None
|
| 44 |
return v.get("name") if isinstance(v, dict) else v
|
| 45 |
|
| 46 |
+
def _gpt_translate(text: str, src: str, tgt: str) -> str:
|
| 47 |
+
"""GPT-3.5-turbo ๋ฒ์ญ (์ค๋ช
์์ด ๊ฒฐ๊ณผ๋ง)"""
|
| 48 |
rsp = client.chat.completions.create(
|
| 49 |
model="gpt-3.5-turbo",
|
| 50 |
messages=[
|
|
|
|
| 53 |
f"Only provide the translated text."},
|
| 54 |
{"role":"user","content":text}
|
| 55 |
],
|
| 56 |
+
temperature=0.3,max_tokens=4096
|
| 57 |
)
|
| 58 |
return rsp.choices[0].message.content.strip()
|
| 59 |
|
| 60 |
+
def _tts(text: str, lang: str) -> str:
|
| 61 |
+
"""OpenAI TTS-1 โ MP3 ํ์ผ ๊ฒฝ๋ก ๋ฐํ"""
|
| 62 |
out = client.audio.speech.create(
|
| 63 |
model="tts-1",
|
| 64 |
voice=VOICE.get(lang,"alloy"),
|
| 65 |
input=text[:4096]
|
| 66 |
)
|
| 67 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 68 |
+
tmp.write(out.content)
|
| 69 |
+
tmp.close()
|
| 70 |
return tmp.name
|
| 71 |
|
| 72 |
+
# โโโโโโโโโโโโโโโโโโโ 2. ๋จ๊ฑด ์ค๋์ค ๋ฒ์ญ โโโโโโโโโโโโโโโโโโโโโโโ
|
| 73 |
def translate_audio(audio_in, src, tgt):
|
| 74 |
path = _safe_path(audio_in)
|
| 75 |
if not path or not os.path.exists(path):
|
| 76 |
+
return "โ ๏ธ ์์ฑ ํ์ผ์ ๋
น์-์
๋ก๋ํ์ธ์.", "", None
|
| 77 |
|
| 78 |
with open(path,"rb") as f:
|
| 79 |
stt = client.audio.transcriptions.create(
|
|
|
|
| 89 |
tts_path = _tts(translated, tgt)
|
| 90 |
return original, translated, tts_path
|
| 91 |
|
| 92 |
+
# โโโโโโโโโโโโโโโโโโโ 3. PDF / ์ด๋ฏธ์ง ๋ฒ์ญ โโโโโโโโโโโโโโโโโโโโโ
|
| 93 |
def translate_document(file_in, src, tgt):
|
| 94 |
path = _safe_path(file_in)
|
| 95 |
if not path or not os.path.exists(path):
|
| 96 |
+
return "โ ๏ธ PDF(๋๋ ์ด๋ฏธ์ง) ํ์ผ์ ์
๋ก๋ํ์ธ์.", ""
|
| 97 |
|
| 98 |
+
ext = os.path.splitext(path)[1].lower()
|
| 99 |
mime = mimetypes.guess_type(path)[0] or ""
|
| 100 |
text = ""
|
| 101 |
|
|
|
|
| 103 |
if ext == ".pdf" or "pdf" in mime:
|
| 104 |
import pdfplumber
|
| 105 |
with pdfplumber.open(path) as pdf:
|
| 106 |
+
pages = pdf.pages[:5] # ๋ฐ๋ชจ: 5์ชฝ ์ ํ
|
| 107 |
+
text = "\n".join(p.extract_text() or "" for p in pages)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
+
# ์ด๋ฏธ์ง์ ๊ฒฝ์ฐ Tesseract ํ์
|
| 110 |
+
try:
|
| 111 |
+
from PIL import Image
|
| 112 |
+
import pytesseract
|
| 113 |
+
text = pytesseract.image_to_string(Image.open(path))
|
| 114 |
+
except Exception:
|
| 115 |
+
return "โ ๏ธ ์๋ฒ์ Tesseract OCR๊ฐ ์์ด์ ์ด๋ฏธ์ง OCR์ ์ง์๋์ง ์์ต๋๋ค. PDF๋ง ์ฌ์ฉํ์ธ์.", ""
|
| 116 |
except Exception as e:
|
| 117 |
return f"โ ํ
์คํธ ์ถ์ถ ์คํจ: {e}", ""
|
| 118 |
|
|
|
|
| 123 |
translated = _gpt_translate(text, src, tgt)
|
| 124 |
return text, translated
|
| 125 |
|
| 126 |
+
# โโโโโโโโโโโโโโโโโโโ 4. ์ค์๊ฐ 1๊ฐ ์ธ์ด ๋ฒ์ญ โโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
| 127 |
def stream_single(mic_stream, src, tgt):
|
| 128 |
buf, header = io.BytesIO(), None
|
| 129 |
o_acc, t_acc = "", ""
|
| 130 |
while True:
|
| 131 |
chunk = mic_stream.recv()
|
| 132 |
+
if chunk is None:
|
| 133 |
+
break
|
| 134 |
+
if header is None:
|
| 135 |
+
header = chunk[:44]
|
| 136 |
buf.write(chunk)
|
| 137 |
if buf.getbuffer().nbytes > 16000*2*STREAM_SEC:
|
| 138 |
wav = header + buf.getvalue()
|
| 139 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
| 140 |
tmp.write(wav); tmp.close()
|
| 141 |
o, t, _ = translate_audio(tmp.name, src, tgt)
|
| 142 |
+
o_acc += " " + o
|
| 143 |
+
t_acc += " " + t
|
| 144 |
yield o_acc.strip(), t_acc.strip()
|
| 145 |
buf = io.BytesIO()
|
| 146 |
+
|
| 147 |
if buf.getbuffer().nbytes:
|
| 148 |
wav = header + buf.getvalue()
|
| 149 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
|
|
| 151 |
o, t, _ = translate_audio(tmp.name, src, tgt)
|
| 152 |
yield (o_acc+" "+o).strip(), (t_acc+" "+t).strip()
|
| 153 |
|
| 154 |
+
# โโโโโโโโโโโโโโโโโโโ 5. ์ค์๊ฐ 4๊ฐ ์ธ์ด ๋ฒ์ญ โโโโโโโโโโโโโโโโโโ
|
| 155 |
def stream_multi(mic_stream, src):
|
| 156 |
buf, header = io.BytesIO(), None
|
| 157 |
+
acc = {lang:"" for lang in ["orig"]+FOUR_LANGS}
|
| 158 |
|
| 159 |
while True:
|
| 160 |
chunk = mic_stream.recv()
|
| 161 |
+
if chunk is None:
|
| 162 |
+
break
|
| 163 |
+
if header is None:
|
| 164 |
+
header = chunk[:44]
|
| 165 |
buf.write(chunk)
|
|
|
|
| 166 |
if buf.getbuffer().nbytes > 16000*2*STREAM_SEC:
|
| 167 |
wav = header + buf.getvalue()
|
| 168 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
|
|
| 174 |
)
|
| 175 |
orig = stt.text.strip()
|
| 176 |
if orig:
|
| 177 |
+
acc["orig"] += " " + orig
|
| 178 |
for lang in FOUR_LANGS:
|
| 179 |
acc[lang] += " " + _gpt_translate(orig, src, lang)
|
| 180 |
+
yield (acc["orig"].strip(),
|
| 181 |
acc["English"].strip(),
|
| 182 |
acc["Chinese"].strip(),
|
| 183 |
acc["Thai"].strip(),
|
| 184 |
acc["Russian"].strip())
|
| 185 |
buf = io.BytesIO()
|
| 186 |
|
|
|
|
| 187 |
if buf.getbuffer().nbytes:
|
| 188 |
wav = header + buf.getvalue()
|
| 189 |
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as tmp:
|
|
|
|
| 195 |
)
|
| 196 |
orig = stt.text.strip()
|
| 197 |
if orig:
|
| 198 |
+
acc["orig"] += " " + orig
|
| 199 |
for lang in FOUR_LANGS:
|
| 200 |
acc[lang] += " " + _gpt_translate(orig, src, lang)
|
| 201 |
+
yield (acc["orig"].strip(),
|
| 202 |
acc["English"].strip(),
|
| 203 |
acc["Chinese"].strip(),
|
| 204 |
acc["Thai"].strip(),
|
| 205 |
acc["Russian"].strip())
|
| 206 |
|
| 207 |
+
# โโโโโโโโโโโโโโโโโ๏ฟฝ๏ฟฝ๏ฟฝโ 6. Gradio UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 208 |
with gr.Blocks(title="SMARTok Demo", theme=gr.themes.Soft()) as app:
|
| 209 |
with gr.Tabs():
|
| 210 |
+
# ํญ 1 โ ์ค๋์ค ๋ฒ์ญ
|
| 211 |
with gr.TabItem("๐๏ธ ์ค๋์ค ๋ฒ์ญ"):
|
| 212 |
src1 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 213 |
tgt1 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
|
|
|
| 218 |
stt1 = gr.Textbox(label="์๋ฌธ", lines=5)
|
| 219 |
tlt1 = gr.Textbox(label="๋ฒ์ญ", lines=5)
|
| 220 |
out1 = gr.Audio(label="TTS",type="filepath",autoplay=True)
|
| 221 |
+
btn1.click(translate_audio,[aud1,src1,tgt1],[stt1,tlt1,out1])
|
| 222 |
|
| 223 |
+
# ํญ 2 โ ๋ฌธ์/์ด๋ฏธ์ง ๋ฒ์ญ
|
| 224 |
with gr.TabItem("๐ ๋ฌธ์/์ด๋ฏธ์ง ๋ฒ์ญ"):
|
| 225 |
src2 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 226 |
tgt2 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
| 227 |
file2= gr.File(label="PDF ๋๋ ์ด๋ฏธ์ง ์
๋ก๋",
|
| 228 |
+
file_types=[".pdf",".png",".jpg",".jpeg",".bmp",".tiff",".gif"])
|
|
|
|
| 229 |
btn2 = gr.Button("๋ฒ์ญ")
|
| 230 |
org2 = gr.Textbox(label="์ถ์ถ ์๋ฌธ",lines=15)
|
| 231 |
trs2 = gr.Textbox(label="๋ฒ์ญ ๊ฒฐ๊ณผ",lines=15)
|
| 232 |
btn2.click(translate_document,[file2,src2,tgt2],[org2,trs2])
|
| 233 |
|
| 234 |
+
# ํญ 3 โ ์ค์๊ฐ 1์ธ์ด ๋ฒ์ญ
|
| 235 |
with gr.TabItem("โฑ๏ธ ์ค์๊ฐ 1์ธ์ด"):
|
| 236 |
src3 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ")
|
| 237 |
tgt3 = gr.Dropdown(LANGUAGES,value="English",label="์ถ๋ ฅ")
|
| 238 |
mic3 = gr.Audio(sources=["microphone"],
|
| 239 |
+
streaming=True,
|
| 240 |
+
label="์ค์๊ฐ ๋ง์ดํฌ")
|
| 241 |
stt3 = gr.Textbox(label="์๋ฌธ(์ค์๊ฐ)",lines=8)
|
| 242 |
tlt3 = gr.Textbox(label="๋ฒ์ญ(์ค์๊ฐ)",lines=8)
|
| 243 |
+
mic3.stream(stream_single,inputs=[src3,tgt3],outputs=[stt3,tlt3])
|
|
|
|
|
|
|
| 244 |
|
| 245 |
+
# ํญ 4 โ ์ค์๊ฐ 4๊ฐ ์ธ์ด ๋ฒ์ญ
|
| 246 |
with gr.TabItem("๐ ์ค์๊ฐ 4๊ฐ ์ธ์ด"):
|
| 247 |
+
gr.Markdown("๋ง์ดํฌ ์
๋ ฅ์ 3-4 ์ด ๊ฐ๊ฒฉ์ผ๋ก **English / Chinese(็ฎไฝ) / Thai / Russian** 4๊ฐ ์ธ์ด๋ก ๋์ ๋ฒ์ญํฉ๋๋ค.")
|
|
|
|
| 248 |
src4 = gr.Dropdown(LANGUAGES,value="Korean",label="์
๋ ฅ ์ธ์ด")
|
| 249 |
mic4 = gr.Audio(sources=["microphone"],
|
| 250 |
+
streaming=True,
|
| 251 |
+
label="์ค์๊ฐ ๋ง์ดํฌ")
|
| 252 |
+
o4 = gr.Textbox(label="์๋ฌธ",lines=8)
|
| 253 |
+
e4 = gr.Textbox(label="English",lines=8)
|
| 254 |
+
z4 = gr.Textbox(label="Chinese(็ฎไฝ)",lines=8)
|
| 255 |
+
t4 = gr.Textbox(label="Thai",lines=8)
|
| 256 |
+
r4 = gr.Textbox(label="Russian",lines=8)
|
| 257 |
+
mic4.stream(stream_multi,inputs=[src4],
|
|
|
|
|
|
|
| 258 |
outputs=[o4,e4,z4,t4,r4])
|
| 259 |
|
| 260 |
+
# โโโโโโโโโโโโโโโโโโโ 7. ์คํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 261 |
if __name__ == "__main__":
|
| 262 |
app.launch(server_name="0.0.0.0",
|
| 263 |
server_port=7860,
|