Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,155 +1,257 @@
|
|
1 |
"""
|
2 |
-
SMARTok ๋ฐ๋ชจ โ
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"""
|
7 |
|
8 |
import gradio as gr
|
9 |
import openai, os, io, tempfile, mimetypes
|
10 |
from dotenv import load_dotenv
|
11 |
from PIL import Image
|
12 |
-
import pdfplumber,
|
13 |
|
14 |
-
#
|
15 |
load_dotenv()
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
LC = {"Korean":"ko","English":"en","Japanese":"ja","Chinese":"zh",
|
21 |
-
"Thai":"th","Russian":"ru","Vietnamese":"vi","Spanish":"es","French":"fr"}
|
22 |
-
VOICE= {l:("nova" if l in ["Korean","Japanese","Chinese"] else "alloy") for l in LANG}
|
23 |
-
FOUR = ["English","Chinese","Thai","Russian"]
|
24 |
-
CHUNK = 4 # sec
|
25 |
|
26 |
-
#
|
27 |
-
def _safe(
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
def
|
30 |
-
|
|
|
31 |
model="gpt-3.5-turbo",
|
32 |
-
messages=[
|
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 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
70 |
try:
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
74 |
with pdfplumber.open(ocr_pdf) as pdf:
|
75 |
-
|
76 |
-
except Exception:
|
77 |
-
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
-
return f"โ ์ถ์ถ ์ค๋ฅ: {e}",""
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
if new:
|
93 |
-
state["
|
94 |
-
state["
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
if new:
|
107 |
-
state["
|
108 |
-
for
|
109 |
-
state[
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
115 |
with gr.Tabs():
|
116 |
-
# ํญ1
|
117 |
with gr.TabItem("๐๏ธ ์ค๋์ค ๋ฒ์ญ"):
|
118 |
-
s1=gr.Dropdown(
|
119 |
-
t1=gr.Dropdown(
|
120 |
-
|
121 |
-
btn1=gr.Button("๋ฒ์ญ")
|
122 |
-
o1=gr.Textbox(label="์๋ฌธ",
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
127 |
with gr.TabItem("๐ ๋ฌธ์ยท์ด๋ฏธ์ง ๋ฒ์ญ"):
|
128 |
-
s2=gr.Dropdown(
|
129 |
-
t2=gr.Dropdown(
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
btn2.
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
136 |
with gr.TabItem("โฑ๏ธ ์ค์๊ฐ 1์ธ์ด"):
|
137 |
-
s3=gr.Dropdown(
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
142 |
|
143 |
-
# ํญ4
|
144 |
with gr.TabItem("๐ ์ค์๊ฐ 4์ธ์ด"):
|
145 |
-
s4=gr.Dropdown(
|
146 |
-
mic4=gr.Audio(sources=["microphone"],streaming=True)
|
147 |
-
o4=gr.Textbox(label="์๋ฌธ",
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
+
SMARTok ๋ฐ๋ชจ โ ์ต์ข
์์ ํ (2025-06-09)
|
3 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
4 |
+
โ ํญ1 ๐๏ธ ์ค๋์ค ๋ฒ์ญ : ๋ง์ดํฌ/ํ์ผ โ ๋ฒ์ญ + TTS
|
5 |
+
โ ํญ2 ๐ ๋ฌธ์โง์ด๋ฏธ์ง ๋ฒ์ญ : PDF / ์ด๋ฏธ์ง(OCR) โ ๋ฒ์ญ
|
6 |
+
โ ํญ3 โฑ๏ธ ์ค์๊ฐ 1์ธ์ด ๋ฒ์ญ : ๋ง์ดํฌ โ ์ค์๊ฐ ์๋ง(1๊ฐ ์ธ์ด)
|
7 |
+
โ ํญ4 ๐ ์ค์๊ฐ 4์ธ์ด ๋ฒ์ญ : ๋ง์ดํฌ โ ์ยท์คยทํยท๋ฌ ๋์ ์๋ง
|
8 |
+
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
9 |
+
ํ์ APT ํจํค์ง (packages.txt)
|
10 |
+
tesseract-ocr
|
11 |
+
libtesseract-dev
|
12 |
+
ghostscript
|
13 |
+
tesseract-ocr-kor tesseract-ocr-eng
|
14 |
+
tesseract-ocr-rus tesseract-ocr-tha
|
15 |
+
tesseract-ocr-chi-sim
|
16 |
+
ffmpeg
|
17 |
+
|
18 |
+
ํ์ PIP ํจํค์ง (requirements.txt)
|
19 |
+
gradio>=5.33
|
20 |
+
openai
|
21 |
+
python-dotenv
|
22 |
+
pdfplumber
|
23 |
+
ocrmypdf
|
24 |
+
pytesseract
|
25 |
+
pillow
|
26 |
"""
|
27 |
|
28 |
import gradio as gr
|
29 |
import openai, os, io, tempfile, mimetypes
|
30 |
from dotenv import load_dotenv
|
31 |
from PIL import Image
|
32 |
+
import pdfplumber, ocrmypdf, pytesseract
|
33 |
|
34 |
+
# โโโโโโโโโโโโโโโโโโโ 0. ์ด๊ธฐํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
35 |
load_dotenv()
|
36 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
37 |
+
if not OPENAI_API_KEY:
|
38 |
+
raise RuntimeError("OPENAI_API_KEY ๊ฐ .env ์ ์์ต๋๋ค!")
|
39 |
+
client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
40 |
+
|
41 |
+
LANGUAGES = [
|
42 |
+
"Korean", "English", "Japanese", "Chinese",
|
43 |
+
"Thai", "Russian", "Vietnamese",
|
44 |
+
"Spanish", "French"
|
45 |
+
]
|
46 |
+
LANG_CODE = {
|
47 |
+
"Korean": "kor", "English": "eng", "Japanese": "jpn", "Chinese": "chi_sim",
|
48 |
+
"Thai": "tha", "Russian": "rus", "Vietnamese": "vie",
|
49 |
+
"Spanish": "spa", "French": "fra"
|
50 |
+
}
|
51 |
+
VOICE = {l: ("nova" if l in ["Korean", "Japanese", "Chinese"] else "alloy")
|
52 |
+
for l in LANGUAGES}
|
53 |
|
54 |
+
FOUR = ["English", "Chinese", "Thai", "Russian"] # ๋์ ๋ฒ์ญ ๋์
|
55 |
+
STREAM_SEC = 4 # ์ค์๊ฐ ์ฒญํฌ ๊ธธ์ด
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
# โโโโโโโโโโโโโโโโโโโ 1. ๊ณตํต ํจ์ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
58 |
+
def _safe(val):
|
59 |
+
"""Gradio File/Audio โ ๊ฒฝ๋ก"""
|
60 |
+
if val is None:
|
61 |
+
return None
|
62 |
+
return val["name"] if isinstance(val, dict) else val
|
63 |
|
64 |
+
def _gpt_translate(text: str, src: str, tgt: str) -> str:
|
65 |
+
"""GPT-3.5 ๋ฒ์ญ"""
|
66 |
+
resp = client.chat.completions.create(
|
67 |
model="gpt-3.5-turbo",
|
68 |
+
messages=[
|
69 |
+
{"role": "system",
|
70 |
+
"content": f"Translate the following {src} text to {tgt}. "
|
71 |
+
"Return only the translated text."},
|
72 |
+
{"role": "user", "content": text}
|
73 |
+
],
|
74 |
+
temperature=0.3,
|
75 |
+
max_tokens=4096
|
76 |
+
)
|
77 |
+
return resp.choices[0].message.content.strip()
|
78 |
+
|
79 |
+
def _tts(text: str, lang: str) -> str:
|
80 |
+
"""ํ
์คํธ๋ฅผ mp3(TTS-1)๋ก ๋ณํ ํ ๊ฒฝ๋ก ๋ฐํ"""
|
81 |
+
out = client.audio.speech.create(
|
82 |
+
model="tts-1",
|
83 |
+
voice=VOICE.get(lang, "alloy"),
|
84 |
+
input=text[:4096]
|
85 |
+
)
|
86 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
87 |
+
tmp.write(out.content)
|
88 |
+
tmp.close()
|
89 |
+
return tmp.name
|
90 |
+
|
91 |
+
# โโโโโโโโโโโโโโโโโโโ 2. ์ค๋์ค(๋จ๊ฑด) ๋ฒ์ญ โโโโโโโโโโโโโโโโโโโโโโโโโ
|
92 |
+
def translate_audio(audio_in, src, tgt):
|
93 |
+
path = _safe(audio_in)
|
94 |
+
if not path or not os.path.exists(path):
|
95 |
+
return "โ ๏ธ ์์ฑ ํ์ผ์ ๋
น์ํ๊ฑฐ๋ ์
๋ก๋ํ์ธ์.", "", None
|
96 |
+
|
97 |
+
with open(path, "rb") as f:
|
98 |
+
stt = client.audio.transcriptions.create(
|
99 |
+
model="whisper-1", file=f, language=LANG_CODE.get(src, "eng")
|
100 |
+
)
|
101 |
+
|
102 |
+
origin = stt.text.strip()
|
103 |
+
if not origin:
|
104 |
+
return "โ ๏ธ ์์ฑ ์ธ์ ์คํจ", "", None
|
105 |
+
|
106 |
+
translated = _gpt_translate(origin, src, tgt)
|
107 |
+
tts_path = _tts(translated, tgt)
|
108 |
+
return origin, translated, tts_path
|
109 |
+
|
110 |
+
# โโโโโโโโโโโโโโโโโโโ 3. ๋ฌธ์/์ด๋ฏธ์ง ๋ฒ์ญ โโโโโโโโโโโโโโโโโโโโโโโโโ
|
111 |
+
def translate_doc(file_in, src, tgt):
|
112 |
+
path = _safe(file_in)
|
113 |
+
if not path or not os.path.exists(path):
|
114 |
+
return "โ ๏ธ PDF ๋๋ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์.", ""
|
115 |
+
|
116 |
+
ext = os.path.splitext(path)[1].lower()
|
117 |
+
mime = mimetypes.guess_type(path)[0] or ""
|
118 |
+
text = ""
|
119 |
+
|
120 |
try:
|
121 |
+
# (A) PDF ์ง์ ํ
์คํธ ์ถ์ถ
|
122 |
+
if ext == ".pdf" or "pdf" in mime:
|
123 |
+
with pdfplumber.open(path) as pdf:
|
124 |
+
text = "\n".join(page.extract_text() or "" for page in pdf.pages[:5])
|
125 |
+
|
126 |
+
# (B) ์ด๋ฏธ์ง โ OCR PDF โ ํ
์คํธ
|
127 |
+
else:
|
128 |
+
tmp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
129 |
+
Image.open(path).save(tmp_pdf, "PDF")
|
130 |
+
|
131 |
+
ocr_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
132 |
try:
|
133 |
+
# OCR ๋ ์ด์ด ์ฝ์
(์ธ์ด ๋ฐ์ดํฐ ์๋ ๊ฒฝ์ฐ ์คํจํ ์ ์์)
|
134 |
+
ocrmypdf.ocr(
|
135 |
+
tmp_pdf, ocr_pdf,
|
136 |
+
lang=f"{LANG_CODE.get(src,'eng')}+eng",
|
137 |
+
deskew=True, optimize=0, progress_bar=False
|
138 |
+
)
|
139 |
with pdfplumber.open(ocr_pdf) as pdf:
|
140 |
+
text = "\n".join(p.extract_text() or "" for p in pdf.pages)
|
141 |
+
except Exception:
|
142 |
+
# ocrmypdf ์คํจ โ pytesseract ์ง์
|
143 |
+
text = pytesseract.image_to_string(
|
144 |
+
Image.open(path), lang=LANG_CODE.get(src, "eng")
|
145 |
+
)
|
146 |
except Exception as e:
|
147 |
+
return f"โ ํ
์คํธ ์ถ์ถ ์ค๋ฅ: {e}", ""
|
148 |
+
|
149 |
+
text = text.strip()
|
150 |
+
if not text:
|
151 |
+
return "โ ๏ธ ํ
์คํธ๋ฅผ ์ถ์ถํ์ง ๋ชปํ์ต๋๋ค.", ""
|
152 |
+
|
153 |
+
return text, _gpt_translate(text, src, tgt)
|
154 |
+
|
155 |
+
# โโโโโโโโโโโโโโโโโโโ 4. ์ค์๊ฐ 1์ธ์ด ์คํธ๋ฆผ โโโโโโโโโโโโโโโโโโโโโโ
|
156 |
+
def stream_one(audio_path, src, tgt, state):
|
157 |
+
"""state = {'orig': str, 'trans': str}"""
|
158 |
+
state = state or {"orig": "", "trans": ""}
|
159 |
+
|
160 |
+
if not audio_path or not os.path.exists(audio_path):
|
161 |
+
return state["orig"], state["trans"], state
|
162 |
+
|
163 |
+
with open(audio_path, "rb") as f:
|
164 |
+
stt = client.audio.transcriptions.create(
|
165 |
+
model="whisper-1", file=f, language=LANG_CODE.get(src, "eng")
|
166 |
+
)
|
167 |
+
|
168 |
+
full = stt.text.strip()
|
169 |
+
new = full[len(state["orig"]):]
|
170 |
if new:
|
171 |
+
state["orig"] = full
|
172 |
+
state["trans"] += " " + _gpt_translate(new, src, tgt)
|
173 |
+
|
174 |
+
return state["orig"], state["trans"].strip(), state
|
175 |
+
|
176 |
+
# โโโโโโโโโโโโโโโโโโโ 5. ์ค์๊ฐ 4์ธ์ด ์คํธ๋ฆผ โโโโโโโโโโโโโโโโโโโโโโ
|
177 |
+
def stream_four(audio_path, src, state):
|
178 |
+
"""
|
179 |
+
state keys: orig / English / Chinese / Thai / Russian
|
180 |
+
"""
|
181 |
+
state = state or {k: "" for k in ["orig"] + FOUR}
|
182 |
+
|
183 |
+
if not audio_path or not os.path.exists(audio_path):
|
184 |
+
return (state["orig"], state["English"], state["Chinese"],
|
185 |
+
state["Thai"], state["Russian"], state)
|
186 |
+
|
187 |
+
with open(audio_path, "rb") as f:
|
188 |
+
stt = client.audio.transcriptions.create(
|
189 |
+
model="whisper-1", file=f, language=LANG_CODE.get(src, "eng")
|
190 |
+
)
|
191 |
+
|
192 |
+
full = stt.text.strip()
|
193 |
+
new = full[len(state["orig"]):]
|
194 |
if new:
|
195 |
+
state["orig"] = full
|
196 |
+
for tgt in FOUR:
|
197 |
+
state[tgt] += " " + _gpt_translate(new, src, tgt)
|
198 |
+
|
199 |
+
return (state["orig"].strip(), state["English"].strip(), state["Chinese"].strip(),
|
200 |
+
state["Thai"].strip(), state["Russian"].strip(), state)
|
201 |
+
|
202 |
+
# โโโโโโโโโโโโโโโโโโโ 6. Gradio UI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
203 |
+
with gr.Blocks(title="SMARTok Demo", theme=gr.themes.Soft()) as app:
|
204 |
with gr.Tabs():
|
205 |
+
# ํญ 1 โ ์ค๋์ค ๋ฒ์ญ
|
206 |
with gr.TabItem("๐๏ธ ์ค๋์ค ๋ฒ์ญ"):
|
207 |
+
s1 = gr.Dropdown(LANGUAGES, value="Korean", label="์
๋ ฅ ์ธ์ด")
|
208 |
+
t1 = gr.Dropdown(LANGUAGES, value="English", label="์ถ๋ ฅ ์ธ์ด")
|
209 |
+
aud1 = gr.Audio(sources=["microphone", "upload"], type="filepath")
|
210 |
+
btn1 = gr.Button("๋ฒ์ญ")
|
211 |
+
o1 = gr.Textbox(label="์๋ฌธ", lines=5)
|
212 |
+
tr1 = gr.Textbox(label="๋ฒ์ญ", lines=5)
|
213 |
+
a1 = gr.Audio(label="TTS", type="filepath", autoplay=True)
|
214 |
+
btn1.click(translate_audio, [aud1, s1, t1], [o1, tr1, a1])
|
215 |
+
|
216 |
+
# ํญ 2 โ ๋ฌธ์ยท์ด๋ฏธ์ง ๋ฒ์ญ
|
217 |
with gr.TabItem("๐ ๋ฌธ์ยท์ด๋ฏธ์ง ๋ฒ์ญ"):
|
218 |
+
s2 = gr.Dropdown(LANGUAGES, value="Korean", label="์
๋ ฅ ์ธ์ด")
|
219 |
+
t2 = gr.Dropdown(LANGUAGES, value="English", label="์ถ๋ ฅ ์ธ์ด")
|
220 |
+
file2 = gr.File(label="PDF / ์ด๋ฏธ์ง ์
๋ก๋",
|
221 |
+
file_types=[".pdf", ".png", ".jpg", ".jpeg",
|
222 |
+
".bmp", ".tiff", ".gif"])
|
223 |
+
btn2 = gr.Button("๋ฒ์ญ")
|
224 |
+
o2 = gr.Textbox(label="์ถ์ถ ์๋ฌธ", lines=15)
|
225 |
+
tr2 = gr.Textbox(label="๋ฒ์ญ ๊ฒฐ๊ณผ", lines=15)
|
226 |
+
btn2.click(translate_doc, [file2, s2, t2], [o2, tr2])
|
227 |
+
|
228 |
+
# ํญ 3 โ ์ค์๊ฐ 1์ธ์ด ๋ฒ์ญ
|
229 |
with gr.TabItem("โฑ๏ธ ์ค์๊ฐ 1์ธ์ด"):
|
230 |
+
s3 = gr.Dropdown(LANGUAGES, value="Korean", label="์
๋ ฅ ์ธ์ด")
|
231 |
+
t3 = gr.Dropdown(LANGUAGES, value="English", label="์ถ๋ ฅ ์ธ์ด")
|
232 |
+
mic3 = gr.Audio(sources=["microphone"], streaming=True)
|
233 |
+
o3 = gr.Textbox(label="์๋ฌธ(์ค์๊ฐ)", lines=8)
|
234 |
+
tr3 = gr.Textbox(label="๋ฒ์ญ(์ค์๊ฐ)", lines=8)
|
235 |
+
st3 = gr.State()
|
236 |
+
mic3.stream(stream_one, inputs=[s3, t3, st3],
|
237 |
+
outputs=[o3, tr3, st3])
|
238 |
|
239 |
+
# ํญ 4 โ ์ค์๊ฐ 4์ธ์ด ๋ฒ์ญ
|
240 |
with gr.TabItem("๐ ์ค์๊ฐ 4์ธ์ด"):
|
241 |
+
s4 = gr.Dropdown(LANGUAGES, value="Korean", label="์
๋ ฅ ์ธ์ด")
|
242 |
+
mic4 = gr.Audio(sources=["microphone"], streaming=True)
|
243 |
+
o4 = gr.Textbox(label="์๋ฌธ", lines=8)
|
244 |
+
e4 = gr.Textbox(label="English", lines=8)
|
245 |
+
c4 = gr.Textbox(label="Chinese(็ฎไฝ)", lines=8)
|
246 |
+
th4 = gr.Textbox(label="Thai", lines=8)
|
247 |
+
r4 = gr.Textbox(label="Russian", lines=8)
|
248 |
+
st4 = gr.State()
|
249 |
+
mic4.stream(stream_four, inputs=[s4, st4],
|
250 |
+
outputs=[o4, e4, c4, th4, r4, st4])
|
251 |
+
|
252 |
+
# โโโโโโโโโโโโโโโโโโโ 7. ์คํ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
253 |
+
if __name__ == "__main__":
|
254 |
+
app.launch(server_name="0.0.0.0",
|
255 |
+
server_port=7860,
|
256 |
+
share=False,
|
257 |
+
debug=True)
|