Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,147 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from openai import OpenAI
|
5 |
-
|
|
|
|
|
|
|
6 |
def parse_document(file, api_key):
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
data = {
|
11 |
-
"base64_encoding": "['table']",
|
12 |
-
"model": "document-parse"
|
13 |
}
|
14 |
|
15 |
-
response = requests.post(url, headers=headers, files=files, data=data)
|
16 |
-
result = response.json()
|
17 |
-
html_text = result.get("content", {}).get("html", "")
|
18 |
return html_text
|
19 |
|
20 |
|
|
|
|
|
|
|
21 |
def chat_with_document(history, html_text, user_question, api_key):
|
|
|
|
|
|
|
22 |
if not html_text.strip():
|
23 |
-
return history, history, "โ ๏ธ ๋จผ์ ๋ฌธ์๋ฅผ ๋ณํํด์ฃผ์ธ์."
|
24 |
|
|
|
25 |
client = OpenAI(
|
26 |
api_key=api_key,
|
27 |
base_url="https://api.upstage.ai/v1"
|
28 |
)
|
29 |
|
|
|
30 |
history = history or []
|
31 |
-
system_prompt = f"""The following is a financial statement document extracted in HTML format.
|
32 |
-
Please answer user questions accurately and concisely in Korean, based on the text within HTML tags.
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
|
|
38 |
messages = [{"role": "system", "content": system_prompt}]
|
39 |
for user, bot in history:
|
40 |
messages.append({"role": "user", "content": user})
|
41 |
messages.append({"role": "assistant", "content": bot})
|
42 |
messages.append({"role": "user", "content": user_question})
|
43 |
|
|
|
44 |
try:
|
45 |
response = client.chat.completions.create(
|
46 |
-
model="solar-pro",
|
47 |
-
messages=messages,
|
48 |
-
temperature=0,
|
49 |
-
max_tokens=1024
|
50 |
)
|
51 |
-
bot_reply = response.choices[0].message.content
|
52 |
except Exception as e:
|
53 |
-
bot_reply = f"โ ๏ธ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
54 |
|
|
|
55 |
history.append((user_question, bot_reply))
|
56 |
return history, history, ""
|
57 |
|
58 |
|
|
|
|
|
|
|
59 |
def toggle_html_view(current_html, is_visible):
|
|
|
|
|
|
|
60 |
return (
|
61 |
-
gr.update(value=current_html, visible=not is_visible),
|
62 |
-
gr.update(value=current_html, visible=is_visible),
|
63 |
-
not is_visible
|
64 |
)
|
65 |
|
|
|
|
|
|
|
|
|
66 |
with gr.Blocks() as demo:
|
|
|
67 |
gr.Markdown("# ๐ ์ฌ๋ฌด์ ํ ๋ถ์ ์ฑ๋ด")
|
68 |
gr.Markdown("1. Document Parse API๋ก PDF ๋ฌธ์๋ฅผ HTML๋ก ๋ณํํฉ๋๋ค.\n"
|
69 |
"2. Solar LLM์ ํตํด ๋ฌธ์ ๊ธฐ๋ฐ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค.")
|
70 |
|
71 |
-
# ๐ API Key ์
๋ ฅ์ฐฝ
|
72 |
api_key_input = gr.Textbox(label="๐ Upstage API Key", type="password", placeholder="Paste your API key here")
|
73 |
|
74 |
-
# ํ์ผ ์
๋ก๋
|
75 |
with gr.Row():
|
76 |
file_input = gr.File(label="๐ ์ฌ๋ฌด์ ํ ์
๋ก๋")
|
77 |
parse_btn = gr.Button("๋ฌธ์ HTML ๋ณํ")
|
78 |
|
|
|
79 |
html_output = gr.Textbox(label="๐ ๋ฌธ์ ๋ด์ฉ", lines=10, visible=True, elem_id="scrollable-html")
|
80 |
html_display = gr.HTML(visible=False, elem_id="scrollable-html-display")
|
81 |
toggle_html_btn = gr.Button("๐ HTML ๋ณด๊ธฐ ์ ํ")
|
82 |
-
html_visible_state = gr.State(False)
|
83 |
|
84 |
-
# ๋ฌธ์ ๋ณํ ์
|
85 |
parse_btn.click(
|
86 |
fn=parse_document,
|
87 |
inputs=[file_input, api_key_input],
|
88 |
outputs=html_output
|
89 |
)
|
90 |
|
|
|
91 |
toggle_html_btn.click(
|
92 |
fn=toggle_html_view,
|
93 |
inputs=[html_output, html_visible_state],
|
94 |
outputs=[html_output, html_display, html_visible_state]
|
95 |
)
|
96 |
|
|
|
97 |
chatbot = gr.Chatbot(label="๐ฌ ๋ฌธ์ ๊ธฐ๋ฐ Q&A", height=400)
|
98 |
user_question = gr.Textbox(label="โ ์ง๋ฌธ์ ์
๋ ฅํ์ธ์", lines=2)
|
99 |
answer_btn = gr.Button("๋ต๋ณ ์์ฑ")
|
100 |
-
chat_state = gr.State([])
|
101 |
|
|
|
102 |
with gr.Row():
|
103 |
gr.Markdown("๐ก ์์ ์ง๋ฌธ:")
|
104 |
ex1 = gr.Button("์ด๋ค ๊ธฐ์
์ ์ฌ๋ฌด์ ํ์ธ๊ฐ์?")
|
105 |
ex2 = gr.Button("3๋ถ๊ธฐ ์ด ์๋งค์ถ์ ์ผ๋ง์ธ๊ฐ์?")
|
106 |
|
|
|
107 |
for btn, question in [(ex1, "์ด๋ค ๊ธฐ์
์ ์ฌ๋ฌด์ ํ์ธ๊ฐ์?"), (ex2, "1๋ถ๊ธฐ ์ด ์๋งค์ถ์ ์ผ๋ง์ธ๊ฐ์?")]:
|
108 |
btn.click(
|
109 |
-
fn=lambda q=question: q,
|
110 |
inputs=[],
|
111 |
outputs=user_question
|
112 |
).then(
|
@@ -116,21 +151,7 @@ with gr.Blocks() as demo:
|
|
116 |
show_progress=True
|
117 |
)
|
118 |
|
|
|
119 |
answer_btn.click(
|
120 |
fn=chat_with_document,
|
121 |
-
inputs=[
|
122 |
-
outputs=[chatbot, chat_state, user_question],
|
123 |
-
show_progress=True
|
124 |
-
)
|
125 |
-
|
126 |
-
demo.css = """
|
127 |
-
#scrollable-html, #scrollable-html-display {
|
128 |
-
max-height: 400px;
|
129 |
-
overflow: auto;
|
130 |
-
border: 1px solid #ccc;
|
131 |
-
padding: 10px;
|
132 |
-
}
|
133 |
-
"""
|
134 |
-
|
135 |
-
if __name__ == "__main__":
|
136 |
-
demo.launch()
|
|
|
1 |
+
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
|
2 |
+
import gradio as gr # Gradio: ์น ์ธํฐํ์ด์ค ๊ตฌ์ฑ์ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
3 |
+
import requests # requests: HTTP ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
4 |
+
from openai import OpenAI # OpenAI: Upstage Solar API์ ํธํ๋๋ ํด๋ผ์ด์ธํธ
|
5 |
+
|
6 |
+
# ------------------------------
|
7 |
+
# ๐ ๋ฌธ์ ํ์ฑ ํจ์ ์ ์
|
8 |
+
# ------------------------------
|
9 |
def parse_document(file, api_key):
|
10 |
+
"""
|
11 |
+
์
๋ก๋๋ PDF ๋ฌธ์๋ฅผ HTML๋ก ๋ณํํ๋ ํจ์ (Upstage Document Parse API ์ฌ์ฉ)
|
12 |
+
"""
|
13 |
+
url = "https://api.upstage.ai/v1/document-ai/document-parse" # API ์์ฒญ URL
|
14 |
+
headers = {'Authorization': f'Bearer {api_key}'} # ์ธ์ฆ ํค๋ ์ค์
|
15 |
+
files = {"document": open(file.name, "rb")} # ํ์ผ ์ฝ๊ธฐ
|
16 |
data = {
|
17 |
+
"base64_encoding": "['table']", # ํ
์ด๋ธ ๋ฐ์ดํฐ๋ base64๋ก ์ธ์ฝ๋ฉ
|
18 |
+
"model": "document-parse" # ์ฌ์ฉ ๋ชจ๋ธ ๋ช
์
|
19 |
}
|
20 |
|
21 |
+
response = requests.post(url, headers=headers, files=files, data=data) # POST ์์ฒญ
|
22 |
+
result = response.json() # ์๋ต ๊ฒฐ๊ณผ ํ์ฑ
|
23 |
+
html_text = result.get("content", {}).get("html", "") # HTML ์ถ์ถ
|
24 |
return html_text
|
25 |
|
26 |
|
27 |
+
# ------------------------------
|
28 |
+
# ๐ฌ ๋ฌธ์ ๊ธฐ๋ฐ Q&A ํจ์ ์ ์
|
29 |
+
# ------------------------------
|
30 |
def chat_with_document(history, html_text, user_question, api_key):
|
31 |
+
"""
|
32 |
+
๋ฌธ์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ฌ์ฉ์ ์ง๋ฌธ์ ๋ต๋ณํ๋ Solar LLM ํจ์
|
33 |
+
"""
|
34 |
if not html_text.strip():
|
35 |
+
return history, history, "โ ๏ธ ๋จผ์ ๋ฌธ์๋ฅผ ๋ณํํด์ฃผ์ธ์." # ๋ฌธ์๊ฐ ์๋ ๊ฒฝ์ฐ ์๋ด
|
36 |
|
37 |
+
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (Upstage Solar LLM)
|
38 |
client = OpenAI(
|
39 |
api_key=api_key,
|
40 |
base_url="https://api.upstage.ai/v1"
|
41 |
)
|
42 |
|
43 |
+
# ์ด์ ๋ํ ๊ธฐ๋ก ์ด๊ธฐํ
|
44 |
history = history or []
|
|
|
|
|
45 |
|
46 |
+
# ์์คํ
ํ๋กฌํํธ: HTML ๋ฌธ์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ๋ต๋ณ ์์ฒญ
|
47 |
+
system_prompt = f"""The following is a financial statement document extracted in HTML format.
|
48 |
+
Please answer user questions accurately and concisely in Korean, based on the text within HTML tags.
|
49 |
+
|
50 |
+
Document:
|
51 |
+
{html_text}
|
52 |
+
"""
|
53 |
|
54 |
+
# ๋ฉ์์ง ๊ตฌ์ฑ (์์คํ
โ ์ฌ์ฉ์/๋ด ๋ํ โ ํ์ฌ ์ง๋ฌธ)
|
55 |
messages = [{"role": "system", "content": system_prompt}]
|
56 |
for user, bot in history:
|
57 |
messages.append({"role": "user", "content": user})
|
58 |
messages.append({"role": "assistant", "content": bot})
|
59 |
messages.append({"role": "user", "content": user_question})
|
60 |
|
61 |
+
# Solar LLM ํธ์ถ
|
62 |
try:
|
63 |
response = client.chat.completions.create(
|
64 |
+
model="solar-pro", # ์ฌ์ฉํ ๋ชจ๋ธ ์ด๋ฆ
|
65 |
+
messages=messages, # ์ ์ฒด ๋ฉ์์ง ์ ๋ฌ
|
66 |
+
temperature=0, # ์ฐฝ์์ฑ ์ต์ํ
|
67 |
+
max_tokens=1024 # ์ต๋ ์๋ต ๊ธธ์ด
|
68 |
)
|
69 |
+
bot_reply = response.choices[0].message.content # ์๋ต ๋ฉ์์ง ์ถ์ถ
|
70 |
except Exception as e:
|
71 |
+
bot_reply = f"โ ๏ธ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" # ์๋ฌ ์ฒ๋ฆฌ
|
72 |
|
73 |
+
# ๋ํ ์ด๋ ฅ ์
๋ฐ์ดํธ ํ ๋ฐํ
|
74 |
history.append((user_question, bot_reply))
|
75 |
return history, history, ""
|
76 |
|
77 |
|
78 |
+
# ------------------------------
|
79 |
+
# ๐ HTML ๋ณด๊ธฐ ํ ๊ธ ํจ์
|
80 |
+
# ------------------------------
|
81 |
def toggle_html_view(current_html, is_visible):
|
82 |
+
"""
|
83 |
+
HTML ๋ณด๊ธฐ/์จ๊ธฐ๊ธฐ ์ํ๋ฅผ ํ ๊ธํ๋ ํจ์
|
84 |
+
"""
|
85 |
return (
|
86 |
+
gr.update(value=current_html, visible=not is_visible), # ํ
์คํธ๋ฐ์ค ์จ๊ธฐ๊ธฐ/๋ณด์ด๊ธฐ
|
87 |
+
gr.update(value=current_html, visible=is_visible), # HTML ๋ ๋๋ง ๋ฐ๋ ๋์
|
88 |
+
not is_visible # ์ํ ๋ฐ์
|
89 |
)
|
90 |
|
91 |
+
|
92 |
+
# ------------------------------
|
93 |
+
# ๐ฆ Gradio UI ๊ตฌ์ฑ
|
94 |
+
# ------------------------------
|
95 |
with gr.Blocks() as demo:
|
96 |
+
# ์ ๋ชฉ ๋ฐ ์ค๋ช
ํ์
|
97 |
gr.Markdown("# ๐ ์ฌ๋ฌด์ ํ ๋ถ์ ์ฑ๋ด")
|
98 |
gr.Markdown("1. Document Parse API๋ก PDF ๋ฌธ์๋ฅผ HTML๋ก ๋ณํํฉ๋๋ค.\n"
|
99 |
"2. Solar LLM์ ํตํด ๋ฌธ์ ๊ธฐ๋ฐ ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค.")
|
100 |
|
101 |
+
# ๐ API Key ์
๋ ฅ์ฐฝ (์ฌ์ฉ์๊ฐ ์ง์ ์
๋ ฅ)
|
102 |
api_key_input = gr.Textbox(label="๐ Upstage API Key", type="password", placeholder="Paste your API key here")
|
103 |
|
104 |
+
# ๐ ํ์ผ ์
๋ก๋ + ๋ฌธ์ ๋ณํ ๋ฒํผ
|
105 |
with gr.Row():
|
106 |
file_input = gr.File(label="๐ ์ฌ๋ฌด์ ํ ์
๋ก๋")
|
107 |
parse_btn = gr.Button("๋ฌธ์ HTML ๋ณํ")
|
108 |
|
109 |
+
# ๐ HTML ์ถ๋ ฅ ์์ญ (ํ
์คํธ + HTML ํ ๊ธ ๋ทฐ)
|
110 |
html_output = gr.Textbox(label="๐ ๋ฌธ์ ๋ด์ฉ", lines=10, visible=True, elem_id="scrollable-html")
|
111 |
html_display = gr.HTML(visible=False, elem_id="scrollable-html-display")
|
112 |
toggle_html_btn = gr.Button("๐ HTML ๋ณด๊ธฐ ์ ํ")
|
113 |
+
html_visible_state = gr.State(False) # ๋ณด๊ธฐ ์ํ ์ ์ฅ
|
114 |
|
115 |
+
# ๋ฌธ์ ๋ณํ ๋ฒํผ ํด๋ฆญ ์ โ HTML ์์ฑ
|
116 |
parse_btn.click(
|
117 |
fn=parse_document,
|
118 |
inputs=[file_input, api_key_input],
|
119 |
outputs=html_output
|
120 |
)
|
121 |
|
122 |
+
# HTML ๋ณด๊ธฐ ์ ํ ๋ฒํผ ํด๋ฆญ ์ โ ํ ๊ธ ๋์ ์คํ
|
123 |
toggle_html_btn.click(
|
124 |
fn=toggle_html_view,
|
125 |
inputs=[html_output, html_visible_state],
|
126 |
outputs=[html_output, html_display, html_visible_state]
|
127 |
)
|
128 |
|
129 |
+
# ๐ฌ ์ฑ๋ด ์ธํฐํ์ด์ค
|
130 |
chatbot = gr.Chatbot(label="๐ฌ ๋ฌธ์ ๊ธฐ๋ฐ Q&A", height=400)
|
131 |
user_question = gr.Textbox(label="โ ์ง๋ฌธ์ ์
๋ ฅํ์ธ์", lines=2)
|
132 |
answer_btn = gr.Button("๋ต๋ณ ์์ฑ")
|
133 |
+
chat_state = gr.State([]) # ๋ํ ์ํ ์ ์ฅ
|
134 |
|
135 |
+
# ๐ก ์์ ์ง๋ฌธ ๋ฒํผ ๊ตฌ์ฑ
|
136 |
with gr.Row():
|
137 |
gr.Markdown("๐ก ์์ ์ง๋ฌธ:")
|
138 |
ex1 = gr.Button("์ด๋ค ๊ธฐ์
์ ์ฌ๋ฌด์ ํ์ธ๊ฐ์?")
|
139 |
ex2 = gr.Button("3๋ถ๊ธฐ ์ด ์๋งค์ถ์ ์ผ๋ง์ธ๊ฐ์?")
|
140 |
|
141 |
+
# ์์ ์ง๋ฌธ ๋ฒํผ ํด๋ฆญ ์ โ ์ง๋ฌธ + ์๋ต ์คํ
|
142 |
for btn, question in [(ex1, "์ด๋ค ๊ธฐ์
์ ์ฌ๋ฌด์ ํ์ธ๊ฐ์?"), (ex2, "1๋ถ๊ธฐ ์ด ์๋งค์ถ์ ์ผ๋ง์ธ๊ฐ์?")]:
|
143 |
btn.click(
|
144 |
+
fn=lambda q=question: q, # ์ง๋ฌธ ํ
์คํธ ์ ๋ฌ
|
145 |
inputs=[],
|
146 |
outputs=user_question
|
147 |
).then(
|
|
|
151 |
show_progress=True
|
152 |
)
|
153 |
|
154 |
+
# ์ฌ์ฉ์ ์ง๋ฌธ ์ ์ถ โ Solar LLM ๋ต๋ณ
|
155 |
answer_btn.click(
|
156 |
fn=chat_with_document,
|
157 |
+
inputs=[chat_state_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|