Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import os | |
from openai import OpenAI | |
UPSTAGE_API_KEY = os.getenv("UPSTAGE_API_KEY") | |
def extract_text_from_image(filename): | |
# Define the API endpoint | |
url = "https://api.upstage.ai/v1/document-digitization" | |
# Set the authorization header with your API key | |
headers = {'Authorization': f'Bearer {UPSTAGE_API_KEY}'} | |
# Open the PDF file in binary mode and attach it to the request | |
filename = 'letter.jpeg' | |
files = {"document": open(filename, "rb")} | |
data = {"model": "ocr"} | |
response = requests.post(url, headers=headers, files=files, data=data) | |
if response.status_code == 200: | |
text = response.json().get("text", "") | |
return text.strip() | |
else: | |
return f"OCR ์คํจ: {response.status_code} - {response.text}" | |
def translate_text_with_solar(english_text): | |
# Initialize the OpenAI client for Solar LLM | |
client = OpenAI( | |
api_key=UPSTAGE_API_KEY, | |
base_url="https://api.upstage.ai/v1" | |
) | |
prompt = "์๋๋ ์์ด ํธ์ง๋ฅผ OCR์ ํตํด ์ถ์ถํ ๋ด์ฉ์ ๋๋ค. ์์ด๋ฅผ ํ๊ตญ์ด๋ก ๋ฒ์ญํ๊ณ , ํ๊ตญ์ด ์ด์, ์ด๋ฒ์ ๋ง๋๋ก ์น๊ทผํ ์ดํฌ๋ก ์ค๋ฌธํ ์ต์ข ๊ฒฐ๊ณผ๋ง ํ๊ธ๋ก ์์ฑํด์ค. " | |
response = client.chat.completions.create( | |
model="solar-pro", | |
messages=[{"role": "user", "content": prompt}], | |
temperature=0.5, | |
max_tokens=1024 | |
) | |
return response.choices[0].message.content | |
with gr.Blocks(title="๐ ์๊ธ์จ ํธ์ง ๋ฒ์ญ๊ธฐ") as demo: | |
gr.Markdown("##๐ ์๊ธ์จ ํธ์ง ๋ฒ์ญ๊ธฐ") | |
gr.Markdown("๐ท ํธ์ง ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ๋ฉด Upstage Docuemnt OCR์ด ์์ด ํ ์คํธ๋ฅผ ์ถ์ถํ๊ณ ,\n๐ ๋ฒ์ญํ๊ธฐ ๋ฒํผ์ ๋๋ฅด๋ฉด Solar LLM์ ํธ์ถํ์ฌ ํ๊ตญ์ด๋ก ๋ฒ์ญํฉ๋๋ค!") | |
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) | |
# with gr.Row(): | |
# image_input = gr.Image(type="pil", label="1๏ธโฃ ์์ด ํธ์ง ์ด๋ฏธ์ง ์ ๋ก๋") | |
# english_box = gr.Textbox(label="๐ ์ถ์ถ๋ ์์ด ํ ์คํธ", lines=10) | |
# with gr.Row(): | |
# 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, outputs=english_box) | |
# Step 2: ๋ฒํผ ๋๋ฅด๋ฉด ๋ฒ์ญ | |
translate_button.click(fn=translate_text_with_solar, inputs=english_box, outputs=korean_box) | |
if __name__ == "__main__": | |
demo.launch() | |