Spaces:
Sleeping
Sleeping
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ | |
import gradio as gr # Gradio: ์น ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ | |
import requests # API ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ | |
from openai import OpenAI # Upstage Solar LLM ์ฌ์ฉ์ ์ํ OpenAI ํธํ ํด๋ผ์ด์ธํธ | |
from io import BytesIO # ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ ์์์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋๊ตฌ | |
def extract_text_from_image(image, api_key): | |
""" | |
์ด๋ฏธ์ง์์ ํ ์คํธ๋ฅผ ์ถ์ถํ๋ ํจ์ (Upstage Document OCR API ์ฌ์ฉ) | |
""" | |
# Upstage API Endpoint ์ฃผ์ | |
url = "https://api.upstage.ai/v1/document-digitization" | |
# API Key ์ธ์ฆ์ ์ํ ํค๋ ์ค์ | |
headers = {'Authorization': f'Bearer {api_key}'} | |
# ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ ๋ฒํผ์ ์ ์ฅ (JPEG ํ์) | |
buffer = BytesIO() | |
image.save(buffer, format="JPEG") | |
buffer.seek(0) | |
# ํ์ผ๊ณผ ์ถ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์์ฒญ ํ์์ ๋ง๊ฒ ๊ตฌ์ฑ | |
files = {"document": ("image.jpg", buffer, "image/jpeg")} | |
data = {"model": "ocr"} # ์ฌ์ฉํ ๋ชจ๋ธ: OCR | |
# POST ์์ฒญ ๋ณด๋ด๊ธฐ | |
response = requests.post(url, headers=headers, files=files, data=data) | |
# ์์ฒญ ์ฑ๊ณต ์ ํ ์คํธ ์ถ์ถ | |
if response.status_code == 200: | |
text = response.json().get("text", "") # JSON ์๋ต์์ ํ ์คํธ ์ถ์ถ | |
return text.strip() # ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ ํ ๋ฐํ | |
else: | |
# ์คํจ ์ ์๋ฌ ๋ฉ์์ง ๋ฐํ | |
return f"OCR ์คํจ: {response.status_code} - {response.text}" | |
def translate_text_with_solar(english_text, api_key): | |
""" | |
์์ด ํ ์คํธ๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๋ ํจ์ (Upstage Solar Pro API ์ฌ์ฉ) | |
""" | |
# Solar LLM ํธ์ถ์ ์ํ OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ | |
client = OpenAI( | |
api_key=api_key, | |
base_url="https://api.upstage.ai/v1" | |
) | |
# print("== ์ฑํ ํจ์ ํธ์ถ๋จ ==") # ๋ก๊ทธ์ฉ ์ถ๋ ฅ | |
# ์ฌ์ฉ์์๊ฒ ์ ๋ฌํ ํ๋กฌํํธ ๊ตฌ์ฑ | |
prompt = f""" | |
๋ค์์ ์์ด ์๊ธ์จ ํธ์ง ๋ด์ฉ์ ๋๋ค.\n | |
{english_text} \n | |
์์ด๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํด์ฃผ์ธ์.\n\n | |
ํ๊ตญ์ด๋ก ๋ณ์ญ๋ ํธ์ง ๋ด์ฉ: " | |
""" | |
# Solar LLM ํธ์ถํ์ฌ ๋ฒ์ญ ์ํ | |
response = client.chat.completions.create( | |
model="solar-pro", # ์ฌ์ฉํ ๋ชจ๋ธ ์ด๋ฆ | |
messages=[{"role": "user", "content": prompt}], # ์ฌ์ฉ์ ๋ฉ์์ง ์ค์ | |
temperature=0.5, # ์ฐฝ์์ฑ ์ ๋ (0.0~1.0) | |
max_tokens=1024 # ์ต๋ ์๋ต ๊ธธ์ด ์ค์ | |
) | |
# print(response) # ์ ์ฒด ์๋ต ๋ก๊ทธ๋ก ์ถ๋ ฅ | |
# ๋ฒ์ญ๋ ๊ฒฐ๊ณผ ํ ์คํธ ๋ฐํ | |
return response.choices[0].message.content | |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ | |
with gr.Blocks() as demo: | |
# ์๋จ ์ค๋ช ๋ถ๋ถ | |
gr.Markdown("# ๐ ์๊ธ์จ ํธ์ง ๋ฒ์ญ๊ธฐ") | |
gr.Markdown("ํธ์ง ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ๋ฉด Upstage Docuemnt OCR์ด ์์ด ํ ์คํธ๋ฅผ ์ถ์ถํ๊ณ ,\n๐ ๋ฒ์ญํ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด Solar LLM์ ํธ์ถํ์ฌ ํ๊ตญ์ด๋ก ๋ฒ์ญํฉ๋๋ค!") | |
gr.Markdown("์์ ์ด๋ฏธ์ง๋ GenAI๋ฅผ ํตํด ์์ฑ๋ ์ด๋ฏธ์ง์ด๋ฉฐ, Files ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ์ธ ๋ฐ ๋ค์ด๋ก๋ ๊ฐ๋ฅํฉ๋๋ค.") | |
# โ API Key ์ ๋ ฅ์ฐฝ ์ถ๊ฐ | |
api_key_input = gr.Textbox(label="๐ Upstage API Key", type="password", placeholder="Paste your API key here") | |
# ๋ ์ด์์: ์ข์ฐ 2๋จ ๊ตฌ์ฑ | |
with gr.Row(): | |
# ์ผ์ชฝ ์ด: ์ด๋ฏธ์ง ์ ๋ก๋ | |
with gr.Column(scale=1): | |
image_input = gr.Image(type="pil", label=" ๐ ํธ์ง ์ด๋ฏธ์ง ์ ๋ก๋") | |
# ์ค๋ฅธ์ชฝ ์ด: ์ถ์ถ๋ ํ ์คํธ ๋ฐ ๋ฒ์ญ ๊ฒฐ๊ณผ | |
with gr.Column(scale=2): | |
english_box = gr.Textbox(label="๐ ์ถ์ถ๋ ์์ด ํ ์คํธ", lines=10) | |
translate_button = gr.Button("๐ ๋ฒ์ญํ๊ธฐ") | |
korean_box = gr.Textbox(label="๐ฐ๐ท ๋ฒ์ญ๋ ํ๊ตญ์ด ํ ์คํธ", lines=10) | |
# Step 1: ์ด๋ฏธ์ง ์ ๋ก๋ ์ OCR ํจ์ ์คํ โ ์ถ์ถ๋ ํ ์คํธ๋ฅผ ์์ด ํ ์คํธ ๋ฐ์ค์ ํ์ | |
image_input.change(fn=extract_text_from_image, inputs=[image_input, api_key_input], outputs=english_box) | |
# Step 2: ๋ฒํผ ํด๋ฆญ ์ ๋ฒ์ญ ํจ์ ์คํ โ ๋ฒ์ญ๋ ๊ฒฐ๊ณผ๋ฅผ ํ๊ตญ์ด ํ ์คํธ ๋ฐ์ค์ ํ์ | |
translate_button.click(fn=translate_text_with_solar, inputs=[english_box, api_key_input], outputs=korean_box) | |
# ์ฑ ์คํ | |
if __name__ == "__main__": | |
demo.launch() | |