Update app.py
Browse files
app.py
CHANGED
|
@@ -785,12 +785,13 @@ def process_file(file_path: str) -> tuple:
|
|
| 785 |
|
| 786 |
def chat_response(
|
| 787 |
message: str,
|
| 788 |
-
history: List[
|
| 789 |
file: Optional[str],
|
| 790 |
session_id: str,
|
| 791 |
groq_api_key: str,
|
| 792 |
fireworks_api_key: str
|
| 793 |
) -> Generator[tuple, None, None]:
|
|
|
|
| 794 |
|
| 795 |
if not message.strip() and file is None:
|
| 796 |
yield history, session_id
|
|
@@ -812,24 +813,37 @@ def chat_response(
|
|
| 812 |
})
|
| 813 |
|
| 814 |
if file_type == "error":
|
| 815 |
-
history = history + [
|
|
|
|
|
|
|
|
|
|
| 816 |
yield history, session_id
|
| 817 |
return
|
| 818 |
elif file_type == "unsupported":
|
| 819 |
-
history = history + [
|
|
|
|
|
|
|
|
|
|
| 820 |
yield history, session_id
|
| 821 |
return
|
| 822 |
|
|
|
|
| 823 |
user_display_message = message
|
| 824 |
if file is not None:
|
| 825 |
filename = os.path.basename(file)
|
| 826 |
user_display_message = f"๐ {filename}\n\n{message}" if message else f"๐ {filename}"
|
| 827 |
|
| 828 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
yield history, session_id
|
| 830 |
|
|
|
|
| 831 |
db_messages = get_session_messages(session_id, limit=10)
|
| 832 |
|
|
|
|
| 833 |
api_messages = []
|
| 834 |
|
| 835 |
api_messages.append({
|
|
@@ -857,17 +871,20 @@ def chat_response(
|
|
| 857 |
if file_type == "image":
|
| 858 |
for chunk in call_fireworks_api_stream(api_messages, file_content, file_mime, fireworks_api_key):
|
| 859 |
full_response += chunk
|
| 860 |
-
|
|
|
|
| 861 |
yield history, session_id
|
| 862 |
else:
|
| 863 |
for chunk in call_groq_api_stream(api_messages, groq_api_key):
|
| 864 |
full_response += chunk
|
| 865 |
-
history[-1]
|
| 866 |
yield history, session_id
|
| 867 |
|
|
|
|
| 868 |
save_message(session_id, "user", current_content, file_info)
|
| 869 |
save_message(session_id, "assistant", full_response)
|
| 870 |
|
|
|
|
| 871 |
if len(db_messages) == 0 and message:
|
| 872 |
title = message[:50] + "..." if len(message) > 50 else message
|
| 873 |
update_session_title(session_id, title)
|
|
@@ -881,18 +898,10 @@ def load_session(session_id: str) -> tuple:
|
|
| 881 |
return [], ""
|
| 882 |
|
| 883 |
messages = get_session_messages(session_id, limit=50)
|
|
|
|
| 884 |
history = []
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
while i < len(messages):
|
| 888 |
-
if messages[i]["role"] == "user":
|
| 889 |
-
user_msg = messages[i]["content"]
|
| 890 |
-
assistant_msg = ""
|
| 891 |
-
if i + 1 < len(messages) and messages[i + 1]["role"] == "assistant":
|
| 892 |
-
assistant_msg = messages[i + 1]["content"]
|
| 893 |
-
i += 1
|
| 894 |
-
history.append([user_msg, assistant_msg])
|
| 895 |
-
i += 1
|
| 896 |
|
| 897 |
return history, session_id
|
| 898 |
|
|
@@ -1141,10 +1150,6 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 1141 |
|
| 1142 |
# ============== CSS ์คํ์ผ ==============
|
| 1143 |
css = """
|
| 1144 |
-
#col-container {
|
| 1145 |
-
margin: 0 auto;
|
| 1146 |
-
max-width: 1200px;
|
| 1147 |
-
}
|
| 1148 |
.upload-box {
|
| 1149 |
border: 2px dashed #6366f1 !important;
|
| 1150 |
border-radius: 12px !important;
|
|
@@ -1152,7 +1157,6 @@ css = """
|
|
| 1152 |
.download-box {
|
| 1153 |
border: 2px solid #22c55e !important;
|
| 1154 |
border-radius: 12px !important;
|
| 1155 |
-
background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%) !important;
|
| 1156 |
}
|
| 1157 |
.preview-box {
|
| 1158 |
max-height: 400px;
|
|
@@ -1217,6 +1221,7 @@ with gr.Blocks(title="AI ๋ฌธ์ ์ด์์คํดํธ") as demo:
|
|
| 1217 |
refresh_sessions_btn = gr.Button("๐ ์๋ก๊ณ ์นจ", size="sm")
|
| 1218 |
|
| 1219 |
with gr.Column(scale=3):
|
|
|
|
| 1220 |
chatbot = gr.Chatbot(
|
| 1221 |
label="๋ํ",
|
| 1222 |
height=500
|
|
@@ -1245,71 +1250,73 @@ with gr.Blocks(title="AI ๋ฌธ์ ์ด์์คํดํธ") as demo:
|
|
| 1245 |
|
| 1246 |
# Tab 2: HWP ๋ณํ๊ธฐ
|
| 1247 |
with gr.Tab("๐ HWP ๋ณํ๊ธฐ", id="converter"):
|
| 1248 |
-
|
| 1249 |
-
|
| 1250 |
-
|
| 1251 |
-
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
with gr.
|
| 1255 |
-
|
| 1256 |
-
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
)
|
| 1263 |
-
|
| 1264 |
-
format_select = gr.Radio(
|
| 1265 |
-
label="๋ณํ ํ์",
|
| 1266 |
-
choices=["HTML", "ODT (OpenDocument)", "TXT (ํ
์คํธ)", "Markdown", "XML"],
|
| 1267 |
-
value="TXT (ํ
์คํธ)",
|
| 1268 |
-
info="์ํ๋ ์ถ๋ ฅ ํ์์ ์ ํํ์ธ์"
|
| 1269 |
-
)
|
| 1270 |
-
|
| 1271 |
-
convert_btn = gr.Button("๐ ๋ณํํ๊ธฐ", variant="primary", size="lg")
|
| 1272 |
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
|
| 1277 |
-
|
| 1278 |
-
|
| 1279 |
-
|
| 1280 |
-
|
| 1281 |
-
file_output = gr.File(
|
| 1282 |
-
label="๋ค์ด๋ก๋",
|
| 1283 |
-
elem_classes=["download-box"]
|
| 1284 |
-
)
|
| 1285 |
|
| 1286 |
-
with gr.
|
| 1287 |
-
|
| 1288 |
-
|
| 1289 |
-
|
| 1290 |
-
max_lines=25,
|
| 1291 |
interactive=False,
|
| 1292 |
-
|
| 1293 |
)
|
| 1294 |
-
|
| 1295 |
-
|
| 1296 |
-
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
|
| 1300 |
-
|
| 1301 |
-
|
| 1302 |
-
|
| 1303 |
-
|
| 1304 |
-
|
| 1305 |
-
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1309 |
|
| 1310 |
# ============== ์ด๋ฒคํธ ํธ๋ค๋ฌ ==============
|
| 1311 |
|
| 1312 |
def on_submit(message, history, file, session_id, groq_key, fireworks_key):
|
|
|
|
|
|
|
|
|
|
| 1313 |
for result in chat_response(message, history, file, session_id, groq_key, fireworks_key):
|
| 1314 |
yield result[0], result[1], "", None
|
| 1315 |
|
|
|
|
| 785 |
|
| 786 |
def chat_response(
|
| 787 |
message: str,
|
| 788 |
+
history: List[Dict],
|
| 789 |
file: Optional[str],
|
| 790 |
session_id: str,
|
| 791 |
groq_api_key: str,
|
| 792 |
fireworks_api_key: str
|
| 793 |
) -> Generator[tuple, None, None]:
|
| 794 |
+
"""์ฑํ
์๋ต ์์ฑ (Gradio 6.0 messages ํ์)"""
|
| 795 |
|
| 796 |
if not message.strip() and file is None:
|
| 797 |
yield history, session_id
|
|
|
|
| 813 |
})
|
| 814 |
|
| 815 |
if file_type == "error":
|
| 816 |
+
history = history + [
|
| 817 |
+
{"role": "user", "content": message or "ํ์ผ ์
๋ก๋"},
|
| 818 |
+
{"role": "assistant", "content": f"โ {file_content}"}
|
| 819 |
+
]
|
| 820 |
yield history, session_id
|
| 821 |
return
|
| 822 |
elif file_type == "unsupported":
|
| 823 |
+
history = history + [
|
| 824 |
+
{"role": "user", "content": message or "ํ์ผ ์
๋ก๋"},
|
| 825 |
+
{"role": "assistant", "content": f"โ ๏ธ {file_content}"}
|
| 826 |
+
]
|
| 827 |
yield history, session_id
|
| 828 |
return
|
| 829 |
|
| 830 |
+
# ์ฌ์ฉ์ ๋ฉ์์ง ๊ตฌ์ฑ
|
| 831 |
user_display_message = message
|
| 832 |
if file is not None:
|
| 833 |
filename = os.path.basename(file)
|
| 834 |
user_display_message = f"๐ {filename}\n\n{message}" if message else f"๐ {filename}"
|
| 835 |
|
| 836 |
+
# ํ์คํ ๋ฆฌ์ ์ฌ์ฉ์ ๋ฉ์์ง์ ๋น ์ด์์คํดํธ ๋ฉ์์ง ์ถ๊ฐ
|
| 837 |
+
history = history + [
|
| 838 |
+
{"role": "user", "content": user_display_message},
|
| 839 |
+
{"role": "assistant", "content": ""}
|
| 840 |
+
]
|
| 841 |
yield history, session_id
|
| 842 |
|
| 843 |
+
# DB์์ ์ด์ ๋ํ ๊ฐ์ ธ์ค๊ธฐ
|
| 844 |
db_messages = get_session_messages(session_id, limit=10)
|
| 845 |
|
| 846 |
+
# API ๋ฉ์์ง ๊ตฌ์ฑ
|
| 847 |
api_messages = []
|
| 848 |
|
| 849 |
api_messages.append({
|
|
|
|
| 871 |
if file_type == "image":
|
| 872 |
for chunk in call_fireworks_api_stream(api_messages, file_content, file_mime, fireworks_api_key):
|
| 873 |
full_response += chunk
|
| 874 |
+
# ๋ง์ง๋ง ๋ฉ์์ง(assistant) ์
๋ฐ์ดํธ
|
| 875 |
+
history[-1] = {"role": "assistant", "content": full_response}
|
| 876 |
yield history, session_id
|
| 877 |
else:
|
| 878 |
for chunk in call_groq_api_stream(api_messages, groq_api_key):
|
| 879 |
full_response += chunk
|
| 880 |
+
history[-1] = {"role": "assistant", "content": full_response}
|
| 881 |
yield history, session_id
|
| 882 |
|
| 883 |
+
# DB์ ์ ์ฅ
|
| 884 |
save_message(session_id, "user", current_content, file_info)
|
| 885 |
save_message(session_id, "assistant", full_response)
|
| 886 |
|
| 887 |
+
# ์ฒซ ๋ฉ์์ง๋ฉด ์ธ์
์ ๋ชฉ ์
๋ฐ์ดํธ
|
| 888 |
if len(db_messages) == 0 and message:
|
| 889 |
title = message[:50] + "..." if len(message) > 50 else message
|
| 890 |
update_session_title(session_id, title)
|
|
|
|
| 898 |
return [], ""
|
| 899 |
|
| 900 |
messages = get_session_messages(session_id, limit=50)
|
| 901 |
+
# Gradio 6.0 messages ํ์์ผ๋ก ๋ณํ
|
| 902 |
history = []
|
| 903 |
+
for msg in messages:
|
| 904 |
+
history.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
|
| 906 |
return history, session_id
|
| 907 |
|
|
|
|
| 1150 |
|
| 1151 |
# ============== CSS ์คํ์ผ ==============
|
| 1152 |
css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1153 |
.upload-box {
|
| 1154 |
border: 2px dashed #6366f1 !important;
|
| 1155 |
border-radius: 12px !important;
|
|
|
|
| 1157 |
.download-box {
|
| 1158 |
border: 2px solid #22c55e !important;
|
| 1159 |
border-radius: 12px !important;
|
|
|
|
| 1160 |
}
|
| 1161 |
.preview-box {
|
| 1162 |
max-height: 400px;
|
|
|
|
| 1221 |
refresh_sessions_btn = gr.Button("๐ ์๋ก๊ณ ์นจ", size="sm")
|
| 1222 |
|
| 1223 |
with gr.Column(scale=3):
|
| 1224 |
+
# Gradio 6.0: Chatbot์ ๊ธฐ๋ณธ์ ์ผ๋ก messages ํ์ ์ฌ์ฉ
|
| 1225 |
chatbot = gr.Chatbot(
|
| 1226 |
label="๋ํ",
|
| 1227 |
height=500
|
|
|
|
| 1250 |
|
| 1251 |
# Tab 2: HWP ๋ณํ๊ธฐ
|
| 1252 |
with gr.Tab("๐ HWP ๋ณํ๊ธฐ", id="converter"):
|
| 1253 |
+
gr.Markdown("""
|
| 1254 |
+
### HWP ํ์ผ ๋ณํ๊ธฐ
|
| 1255 |
+
ํ๊ธ(HWP) ๋ฌธ์๋ฅผ ๋ค์ํ ํ์์ผ๋ก ๋ณํํฉ๋๋ค.
|
| 1256 |
+
""")
|
| 1257 |
+
|
| 1258 |
+
with gr.Row():
|
| 1259 |
+
with gr.Column(scale=1):
|
| 1260 |
+
gr.Markdown("#### ๐ค ํ์ผ ์
๋ก๋")
|
| 1261 |
+
hwp_file_input = gr.File(
|
| 1262 |
+
label="HWP ํ์ผ ์ ํ",
|
| 1263 |
+
file_types=[".hwp"],
|
| 1264 |
+
type="filepath",
|
| 1265 |
+
elem_classes=["upload-box"]
|
| 1266 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1267 |
|
| 1268 |
+
format_select = gr.Radio(
|
| 1269 |
+
label="๋ณํ ํ์",
|
| 1270 |
+
choices=["HTML", "ODT (OpenDocument)", "TXT (ํ
์คํธ)", "Markdown", "XML"],
|
| 1271 |
+
value="TXT (ํ
์คํธ)",
|
| 1272 |
+
info="์ํ๋ ์ถ๋ ฅ ํ์์ ์ ํํ์ธ์"
|
| 1273 |
+
)
|
| 1274 |
+
|
| 1275 |
+
convert_btn = gr.Button("๐ ๋ณํํ๊ธฐ", variant="primary", size="lg")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1276 |
|
| 1277 |
+
with gr.Column(scale=1):
|
| 1278 |
+
gr.Markdown("#### ๐ฅ ๋ณํ ๊ฒฐ๊ณผ")
|
| 1279 |
+
status_output = gr.Textbox(
|
| 1280 |
+
label="์ํ",
|
|
|
|
| 1281 |
interactive=False,
|
| 1282 |
+
lines=2
|
| 1283 |
)
|
| 1284 |
+
|
| 1285 |
+
file_output = gr.File(
|
| 1286 |
+
label="๋ค์ด๋ก๋",
|
| 1287 |
+
elem_classes=["download-box"]
|
| 1288 |
+
)
|
| 1289 |
+
|
| 1290 |
+
with gr.Accordion("๐ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ", open=False):
|
| 1291 |
+
preview_output = gr.Textbox(
|
| 1292 |
+
label="",
|
| 1293 |
+
lines=15,
|
| 1294 |
+
max_lines=25,
|
| 1295 |
+
interactive=False,
|
| 1296 |
+
elem_classes=["preview-box"]
|
| 1297 |
+
)
|
| 1298 |
+
|
| 1299 |
+
gr.Markdown("""
|
| 1300 |
+
---
|
| 1301 |
+
#### โน๏ธ ์๋ด์ฌํญ
|
| 1302 |
+
|
| 1303 |
+
| ํ์ | ์ค๋ช
| ์ฉ๋ |
|
| 1304 |
+
|------|------|------|
|
| 1305 |
+
| **HTML** | ์น ํ์ด์ง ํ์ | ๋ธ๋ผ์ฐ์ ์์ ๋ณด๊ธฐ |
|
| 1306 |
+
| **ODT** | OpenDocument | LibreOffice, Google Docs |
|
| 1307 |
+
| **TXT** | ์์ ํ
์คํธ | ํ
์คํธ ์ถ์ถ |
|
| 1308 |
+
| **Markdown** | ๋งํฌ๋ค์ด ํ์ | GitHub, ๋
ธ์
|
|
| 1309 |
+
| **XML** | ๊ตฌ์กฐํ ๋ฐ์ดํฐ | ๋ฐ์ดํฐ ์ฒ๋ฆฌ |
|
| 1310 |
+
|
| 1311 |
+
> โ ๏ธ HWP v5 ํ์(ํ๊ธ 2007+)๋ง ์ง์ | ์ํธํ ํ์ผ ๋ถ๊ฐ
|
| 1312 |
+
""")
|
| 1313 |
|
| 1314 |
# ============== ์ด๋ฒคํธ ํธ๋ค๋ฌ ==============
|
| 1315 |
|
| 1316 |
def on_submit(message, history, file, session_id, groq_key, fireworks_key):
|
| 1317 |
+
# history๊ฐ None์ด๋ฉด ๋น ๋ฆฌ์คํธ๋ก ์ด๊ธฐํ
|
| 1318 |
+
if history is None:
|
| 1319 |
+
history = []
|
| 1320 |
for result in chat_response(message, history, file, session_id, groq_key, fireworks_key):
|
| 1321 |
yield result[0], result[1], "", None
|
| 1322 |
|