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