Spaces:
Runtime error
Runtime error
import gradio as gr | |
from openai import OpenAI | |
import os | |
from PIL import Image | |
import base64 | |
import io | |
# OpenAI ํด๋ผ์ด์ธํธ๋ฅผ API ํค๋ก ์ด๊ธฐํ( ๋ ๊ฑฐ์๊ฐ ์ค๋ฅ๋์ ์๋ก ์์ ํจ) | |
api_key = os.getenv("OPENAI_API_KEY") | |
if api_key is None: | |
raise ValueError("OPENAI_API_KEY ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") | |
client = OpenAI(api_key=api_key) | |
def image_to_base64(image): | |
buffered = io.BytesIO() | |
image.save(buffered, format="JPEG") | |
img_str = base64.b64encode(buffered.getvalue()).decode() | |
return img_str | |
def extract_and_summarize(image): | |
# Convert image to base64 | |
image_base64 = image_to_base64(image) | |
# Prepare the prompt for GPT-4 | |
#์ฌ๊ธฐ ์ด๋ฏธ์ง๋ก ๋ ๋ฌธ์๊ฐ ์์ด. ๋ฌธ์๋ฅผ ๋ณด๊ณ ๋ด์ฉ์ 3์ค๋ก ์์ฝํด. ์ ์ถํด์ผํ๋ ๋ฌธ์์ ๊ฒฝ์ฐ ์ ์ถ ๊ธฐ๊ฐ๊ณผ ๋ฐฉ๋ฒ์ ๋ฐ๋์ ํฌํจํด | |
# Summarize the image document in 3 lines. If submission is needed, include the deadline, method, and link. Output in Korean | |
# Summarize the image document in 3 lines. If submission details are included, add the deadline, method, and link. If there is no link, omit the link information. Output in Korean. | |
prompt = [ | |
{ | |
"role": "system", | |
"content": "You are a helpful assistant. Summarize the text content of the document image provided." | |
}, | |
{ | |
"role": "user", | |
"content": [ | |
{"type": "text", "text": "Summarize the image document in 3 lines. If submission details are included, add the deadline, method, and link. If there is no link, omit the link information. Output in Korean."}, | |
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}} | |
] | |
} | |
] | |
# Call GPT-4 API for summarization | |
response = client.chat.completions.create( | |
model="gpt-4o-mini", | |
messages=prompt, | |
temperature=0.0, | |
max_tokens=300, | |
) | |
# Extract summary from GPT-4 response | |
summary = response.choices[0].message.content | |
return summary | |
# ์ํ ๋ฌธ์ ์ด๋ฏธ์ง ์ถ๊ฐ | |
examples = ["./sample1.png", "./sample2.png", "./sample3.png"] | |
# Define Gradio interface | |
iface = gr.Interface( | |
fn=extract_and_summarize, | |
inputs=gr.Image(type="pil", label="Upload Document Image"), | |
outputs=gr.Textbox(label="Summarized Text"), | |
title="๊ณต๋ฌธ์ ์์ฝ ์์ฑ๊ธฐ", | |
description="๋ฌธ์์ ํ๋ฉด์ ์บก์ณํ์ฌ ์ ๋ก๋ํ๋ฉด ์์ฝํด์ค๋๋ค.", | |
examples=examples | |
) | |
# Launch the interface | |
iface.launch() | |