Update app.py
Browse fileschange api key input
app.py
CHANGED
@@ -7,20 +7,9 @@ import tempfile
|
|
7 |
import os
|
8 |
import io
|
9 |
|
10 |
-
# API ํค ๋ถ๋ฌ์ค๊ธฐ
|
11 |
-
UPSTAGE_API_KEY = os.getenv("UPSTAGE_API_KEY")
|
12 |
|
13 |
-
# Upstage API ํด๋ผ์ด์ธํธ ์ค์ (์คํค๋ง ์์ฑ์ฉ)
|
14 |
-
schema_client = OpenAI(
|
15 |
-
api_key=UPSTAGE_API_KEY,
|
16 |
-
base_url="https://api.upstage.ai/v1/information-extraction/schema-generation"
|
17 |
-
)
|
18 |
|
19 |
-
|
20 |
-
extraction_client = OpenAI(
|
21 |
-
api_key=UPSTAGE_API_KEY,
|
22 |
-
base_url="https://api.upstage.ai/v1/information-extraction"
|
23 |
-
)
|
24 |
|
25 |
# ์ด๋ฏธ์ง ํ์ผ์ base64 ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๋ ํจ์
|
26 |
def encode_img_to_base64(img_path):
|
@@ -46,7 +35,21 @@ def flatten_to_rows(data):
|
|
46 |
return flat
|
47 |
|
48 |
# ๋ช
ํจ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ ์ ๋ณด ์ถ์ถ ๋ฐ CSV ์ ์ฅ์ ์ฒ๋ฆฌํ๋ ํต์ฌ ํจ์
|
49 |
-
def extract_info(image_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
base64_data = encode_img_to_base64(image_path)
|
51 |
|
52 |
# ์คํค๋ง ์์ฑ
|
@@ -93,10 +96,14 @@ def extract_info(image_path):
|
|
93 |
return json_result, df_result
|
94 |
|
95 |
|
96 |
-
|
|
|
97 |
with gr.Blocks() as demo:
|
98 |
gr.Markdown("## ๐ ๋ช
ํจ ์ ๋ณด ์๋ ์ถ์ถ๊ธฐ")
|
99 |
-
gr.Markdown("๋ช
ํจ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ์ ๋ณด๋ฅผ
|
|
|
|
|
|
|
100 |
|
101 |
with gr.Row():
|
102 |
with gr.Column():
|
@@ -104,16 +111,22 @@ with gr.Blocks() as demo:
|
|
104 |
extract_btn = gr.Button("๐ค ์ ๋ณด ์ถ์ถํ๊ธฐ")
|
105 |
|
106 |
with gr.Column():
|
107 |
-
json_output = gr.Textbox(label="๐ ์ถ์ถ๋ JSON", lines=
|
108 |
table_output = gr.Dataframe(label="๐ ์ถ์ถ๋ ์ ๋ณด (ํ ํํ)", interactive=False)
|
|
|
|
|
109 |
|
110 |
-
# ๋ฒํผ ํด๋ฆญ ์ ์ ๋ณด ์ถ์ถ ํจ์ ์คํ
|
111 |
extract_btn.click(
|
112 |
-
fn=extract_info,
|
113 |
-
inputs=image_input,
|
114 |
-
outputs=[json_output, table_output]
|
115 |
)
|
116 |
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
|
119 |
# ์ฑ ์คํ
|
|
|
7 |
import os
|
8 |
import io
|
9 |
|
|
|
|
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
UPSTAGE_API_KEY = ""
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# ์ด๋ฏธ์ง ํ์ผ์ base64 ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๋ ํจ์
|
15 |
def encode_img_to_base64(img_path):
|
|
|
35 |
return flat
|
36 |
|
37 |
# ๋ช
ํจ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ ์ ๋ณด ์ถ์ถ ๋ฐ CSV ์ ์ฅ์ ์ฒ๋ฆฌํ๋ ํต์ฌ ํจ์
|
38 |
+
def extract_info(image_path, UPSTAGE_API_KEY):
|
39 |
+
|
40 |
+
|
41 |
+
# Upstage API ํด๋ผ์ด์ธํธ ์ค์ (์คํค๋ง ์์ฑ์ฉ)
|
42 |
+
schema_client = OpenAI(
|
43 |
+
api_key=UPSTAGE_API_KEY,
|
44 |
+
base_url="https://api.upstage.ai/v1/information-extraction/schema-generation"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Upstage API ํด๋ผ์ด์ธํธ ์ค์ (์ ๋ณด ์ถ์ถ์ฉ)
|
48 |
+
extraction_client = OpenAI(
|
49 |
+
api_key=UPSTAGE_API_KEY,
|
50 |
+
base_url="https://api.upstage.ai/v1/information-extraction"
|
51 |
+
)
|
52 |
+
|
53 |
base64_data = encode_img_to_base64(image_path)
|
54 |
|
55 |
# ์คํค๋ง ์์ฑ
|
|
|
96 |
return json_result, df_result
|
97 |
|
98 |
|
99 |
+
|
100 |
+
# Gradio UI
|
101 |
with gr.Blocks() as demo:
|
102 |
gr.Markdown("## ๐ ๋ช
ํจ ์ ๋ณด ์๋ ์ถ์ถ๊ธฐ")
|
103 |
+
gr.Markdown("๋ช
ํจ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด ์ ๋ณด๋ฅผ ์ถ์ถํ๊ณ , ๋ค์ด๋ก๋ ๋ฒํผ์ ๋๋ฌ CSV๋ก ์ ์ฅํ ์ ์์ต๋๋ค.")
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
api_key_input = gr.Textbox(label="๐ Upstage API Key", type="password", placeholder="Paste your API key here")
|
107 |
|
108 |
with gr.Row():
|
109 |
with gr.Column():
|
|
|
111 |
extract_btn = gr.Button("๐ค ์ ๋ณด ์ถ์ถํ๊ธฐ")
|
112 |
|
113 |
with gr.Column():
|
114 |
+
json_output = gr.Textbox(label="๐ ์ถ์ถ๋ JSON", lines=20)
|
115 |
table_output = gr.Dataframe(label="๐ ์ถ์ถ๋ ์ ๋ณด (ํ ํํ)", interactive=False)
|
116 |
+
download_btn = gr.Button("๐ฅ CSV ๋ค์ด๋ก๋ ๋ฐ๊ธฐ")
|
117 |
+
file_output = gr.File(label="", visible=False)
|
118 |
|
|
|
119 |
extract_btn.click(
|
120 |
+
fn=extract_info,
|
121 |
+
inputs=[image_input, api_key_input],
|
122 |
+
outputs=[json_output, table_output, file_output]
|
123 |
)
|
124 |
|
125 |
+
download_btn.click(
|
126 |
+
fn=lambda x: x,
|
127 |
+
inputs=file_output,
|
128 |
+
outputs=file_output
|
129 |
+
)
|
130 |
|
131 |
|
132 |
# ์ฑ ์คํ
|