Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,10 @@
|
|
| 1 |
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
|
| 2 |
import gradio as gr # Gradio: ์น ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
| 3 |
import requests # API ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
| 4 |
-
import os # ํ๊ฒฝ ๋ณ์์์ API ํค๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํด ์ฌ์ฉ
|
| 5 |
from openai import OpenAI # Upstage Solar LLM ์ฌ์ฉ์ ์ํ OpenAI ํธํ ํด๋ผ์ด์ธํธ
|
| 6 |
from io import BytesIO # ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ ์์์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋๊ตฌ
|
| 7 |
|
| 8 |
-
|
| 9 |
-
UPSTAGE_API_KEY = os.getenv("UPSTAGE_API_KEY")
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def extract_text_from_image(image):
|
| 13 |
"""
|
| 14 |
์ด๋ฏธ์ง์์ ํ
์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์ (Upstage Document OCR API ์ฌ์ฉ)
|
| 15 |
"""
|
|
@@ -17,7 +12,7 @@ def extract_text_from_image(image):
|
|
| 17 |
url = "https://api.upstage.ai/v1/document-digitization"
|
| 18 |
|
| 19 |
# API Key ์ธ์ฆ์ ์ํ ํค๋ ์ค์
|
| 20 |
-
headers = {'Authorization': f'Bearer {
|
| 21 |
|
| 22 |
# ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ ๋ฒํผ์ ์ ์ฅ (JPEG ํ์)
|
| 23 |
buffer = BytesIO()
|
|
@@ -41,13 +36,13 @@ def extract_text_from_image(image):
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
-
def translate_text_with_solar(english_text):
|
| 45 |
"""
|
| 46 |
์์ด ํ
์คํธ๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๋ ํจ์ (Upstage Solar Pro API ์ฌ์ฉ)
|
| 47 |
"""
|
| 48 |
# Solar LLM ํธ์ถ์ ์ํ OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 49 |
client = OpenAI(
|
| 50 |
-
api_key=
|
| 51 |
base_url="https://api.upstage.ai/v1"
|
| 52 |
)
|
| 53 |
|
|
@@ -82,6 +77,9 @@ with gr.Blocks() as demo:
|
|
| 82 |
gr.Markdown("ํธ์ง ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด Upstage Docuemnt OCR์ด ์์ด ํ
์คํธ๋ฅผ ์ถ์ถํ๊ณ ,\n๐ ๋ฒ์ญํ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด Solar LLM์ ํธ์ถํ์ฌ ํ๊ตญ์ด๋ก ๋ฒ์ญํฉ๋๋ค!")
|
| 83 |
gr.Markdown("์์ ์ด๋ฏธ์ง๋ GenAI๋ฅผ ํตํด ์์ฑ๋ ์ด๋ฏธ์ง์ด๋ฉฐ, Files ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ์ธ ๋ฐ ๋ค์ด๋ก๋ ๊ฐ๋ฅํฉ๋๋ค.")
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
# ๋ ์ด์์: ์ข์ฐ 2๋จ ๊ตฌ์ฑ
|
| 86 |
with gr.Row():
|
| 87 |
# ์ผ์ชฝ ์ด: ์ด๋ฏธ์ง ์
๋ก๋
|
|
@@ -95,10 +93,10 @@ with gr.Blocks() as demo:
|
|
| 95 |
korean_box = gr.Textbox(label="๐ฐ๐ท ๋ฒ์ญ๋ ํ๊ตญ์ด ํ
์คํธ", lines=10)
|
| 96 |
|
| 97 |
# Step 1: ์ด๋ฏธ์ง ์
๋ก๋ ์ OCR ํจ์ ์คํ โ ์ถ์ถ๋ ํ
์คํธ๋ฅผ ์์ด ํ
์คํธ ๋ฐ์ค์ ํ์
|
| 98 |
-
image_input.change(fn=extract_text_from_image, inputs=image_input, outputs=english_box)
|
| 99 |
|
| 100 |
# Step 2: ๋ฒํผ ํด๋ฆญ ์ ๋ฒ์ญ ํจ์ ์คํ โ ๋ฒ์ญ๋ ๊ฒฐ๊ณผ๋ฅผ ํ๊ตญ์ด ํ
์คํธ ๋ฐ์ค์ ํ์
|
| 101 |
-
translate_button.click(fn=translate_text_with_solar, inputs=english_box, outputs=korean_box)
|
| 102 |
|
| 103 |
# ์ฑ ์คํ
|
| 104 |
if __name__ == "__main__":
|
|
|
|
| 1 |
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
|
| 2 |
import gradio as gr # Gradio: ์น ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
| 3 |
import requests # API ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
|
|
|
|
| 4 |
from openai import OpenAI # Upstage Solar LLM ์ฌ์ฉ์ ์ํ OpenAI ํธํ ํด๋ผ์ด์ธํธ
|
| 5 |
from io import BytesIO # ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ ์์์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋๊ตฌ
|
| 6 |
|
| 7 |
+
def extract_text_from_image(image, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
์ด๋ฏธ์ง์์ ํ
์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์ (Upstage Document OCR API ์ฌ์ฉ)
|
| 10 |
"""
|
|
|
|
| 12 |
url = "https://api.upstage.ai/v1/document-digitization"
|
| 13 |
|
| 14 |
# API Key ์ธ์ฆ์ ์ํ ํค๋ ์ค์
|
| 15 |
+
headers = {'Authorization': f'Bearer {api_key}'}
|
| 16 |
|
| 17 |
# ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ ๋ฒํผ์ ์ ์ฅ (JPEG ํ์)
|
| 18 |
buffer = BytesIO()
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
+
def translate_text_with_solar(english_text, api_key):
|
| 40 |
"""
|
| 41 |
์์ด ํ
์คํธ๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๋ ํจ์ (Upstage Solar Pro API ์ฌ์ฉ)
|
| 42 |
"""
|
| 43 |
# Solar LLM ํธ์ถ์ ์ํ OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
| 44 |
client = OpenAI(
|
| 45 |
+
api_key=api_key,
|
| 46 |
base_url="https://api.upstage.ai/v1"
|
| 47 |
)
|
| 48 |
|
|
|
|
| 77 |
gr.Markdown("ํธ์ง ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด Upstage Docuemnt OCR์ด ์์ด ํ
์คํธ๋ฅผ ์ถ์ถํ๊ณ ,\n๐ ๋ฒ์ญํ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด Solar LLM์ ํธ์ถํ์ฌ ํ๊ตญ์ด๋ก ๋ฒ์ญํฉ๋๋ค!")
|
| 78 |
gr.Markdown("์์ ์ด๋ฏธ์ง๋ GenAI๋ฅผ ํตํด ์์ฑ๋ ์ด๋ฏธ์ง์ด๋ฉฐ, Files ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ์ธ ๋ฐ ๋ค์ด๋ก๋ ๊ฐ๋ฅํฉ๋๋ค.")
|
| 79 |
|
| 80 |
+
# โ
API Key ์
๋ ฅ์ฐฝ ์ถ๊ฐ
|
| 81 |
+
api_key_input = gr.Textbox(label="๐ Upstage API Key", type="password", placeholder="Paste your API key here")
|
| 82 |
+
|
| 83 |
# ๋ ์ด์์: ์ข์ฐ 2๋จ ๊ตฌ์ฑ
|
| 84 |
with gr.Row():
|
| 85 |
# ์ผ์ชฝ ์ด: ์ด๋ฏธ์ง ์
๋ก๋
|
|
|
|
| 93 |
korean_box = gr.Textbox(label="๐ฐ๐ท ๋ฒ์ญ๋ ํ๊ตญ์ด ํ
์คํธ", lines=10)
|
| 94 |
|
| 95 |
# Step 1: ์ด๋ฏธ์ง ์
๋ก๋ ์ OCR ํจ์ ์คํ โ ์ถ์ถ๋ ํ
์คํธ๋ฅผ ์์ด ํ
์คํธ ๋ฐ์ค์ ํ์
|
| 96 |
+
image_input.change(fn=extract_text_from_image, inputs=[image_input, api_key_input], outputs=english_box)
|
| 97 |
|
| 98 |
# Step 2: ๋ฒํผ ํด๋ฆญ ์ ๋ฒ์ญ ํจ์ ์คํ โ ๋ฒ์ญ๋ ๊ฒฐ๊ณผ๋ฅผ ํ๊ตญ์ด ํ
์คํธ ๋ฐ์ค์ ํ์
|
| 99 |
+
translate_button.click(fn=translate_text_with_solar, inputs=[english_box, api_key_input], outputs=korean_box)
|
| 100 |
|
| 101 |
# ์ฑ ์คํ
|
| 102 |
if __name__ == "__main__":
|