wanda222's picture
Create app.py
3790afa verified
raw
history blame
4.84 kB
import gradio as gr
import requests
import os
from openai import OpenAI
UPSTAGE_API_KEY = os.getenv("UPSTAGE_API_KEY")
def parse_document(filename):
"""Parses a PDF document using the Upstage Document Parse API and returns the extracted HTML."""
# Define the API endpoint
url = "https://api.upstage.ai/v1/document-ai/document-parse"
# 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
files = {"document": open(filename, "rb")}
# Define additional request parameters
data = {
"base64_encoding": "['table']", # Request base64 encoding of table elements
"model": "document-parse" # Specify the model to use
}
# Send the POST request to the API
response = requests.post(url, headers=headers, files=files, data=data)
# Parse the JSON response
result = response.json()
# For debugging: print the entire API response
# print(response.json())
# Extract the HTML content from the response
html_text = result.get("content", {}).get("html", "")
return html_text
def chat_with_document(history, html_text, user_question):
"""Handles multi-turn Q&A based on the parsed HTML document using Upstage Solar Pro LLM."""
# Initialize the OpenAI client for Solar LLM
client = OpenAI(
api_key=UPSTAGE_API_KEY,
base_url="https://api.upstage.ai/v1"
)
# If this is the first turn, initialize an empty history
history = history or []
# Construct a system prompt with instructions and the HTML content
system_prompt = f"""The following is a financial statement document extracted in HTML format.
Please answer user questions accurately and concisely in Korean, based on the text within HTML tags.
Document:
{html_text}
"""
# Build the conversation history for the chat model
messages = [{"role": "system", "content": system_prompt}]
for user, bot in history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": bot})
# Add the current user question
messages.append({"role": "user", "content": user_question})
# Call the Solar LLM to generate a response
response = client.chat.completions.create(
model="solar-pro",
messages=messages,
temperature=0,
max_tokens=1024
)
# Extract the assistant's reply
bot_reply = response.choices[0].message.content
# Update the chat history
history.append((user_question, bot_reply))
# Return updated chatbot display, state, and clear the input
return history, history, ""
def set_example_question(example_text):
return example_text
def toggle_html_view(current_html, is_visible):
return (
gr.update(value=current_html, visible=not is_visible), # html_output toggle
gr.update(value=current_html, visible=is_visible), # html_display ๋ฐ˜๋Œ€๋กœ toggle
not is_visible
)
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# ๐Ÿ“„ ์žฌ๋ฌด์ œํ‘œ ๋ถ„์„ ์ฑ—๋ด‡")
gr.Markdown("1. Document Parse API๋กœ PDF ๋ฌธ์„œ๋ฅผ HTML๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.\n"
"2. Solar LLM์„ ํ†ตํ•ด ๋ฌธ์„œ ๊ธฐ๋ฐ˜ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•ฉ๋‹ˆ๋‹ค.")
with gr.Row():
file_input = gr.File(label="๐Ÿ“Ž ์žฌ๋ฌด์ œํ‘œ ์—…๋กœ๋“œ")
parse_btn = gr.Button("๋ฌธ์„œ HTML ๋ณ€ํ™˜")
html_output = gr.Textbox(label="๐Ÿ“˜ ๋ฌธ์„œ ๋‚ด์šฉ", lines=10, visible=True)
html_display = gr.HTML(visible=False)
toggle_html_btn = gr.Button("๐Ÿ” HTML ๋ณด๊ธฐ ์ „ํ™˜")
html_visible_state = gr.State(False)
parse_btn.click(fn=parse_document, inputs=file_input, outputs=html_output)
toggle_html_btn.click(
fn=toggle_html_view,
inputs=[html_output, html_visible_state],
outputs=[html_output, html_display, html_visible_state]
)
chatbot = gr.Chatbot(label="๐Ÿ’ฌ ๋ฌธ์„œ ๊ธฐ๋ฐ˜ Q&A", height=400)
user_question = gr.Textbox(label="โ“ ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”", lines=2)
answer_btn = gr.Button("๋‹ต๋ณ€ ์ƒ์„ฑ")
chat_state = gr.State([])
with gr.Row():
gr.Markdown("๐Ÿ’ก ์˜ˆ์ œ ์งˆ๋ฌธ:")
ex1 = gr.Button("์–ด๋–ค ๊ธฐ์—…์˜ ์žฌ๋ฌด์ œํ‘œ์ธ๊ฐ€์š”?")
ex2 = gr.Button("Q3 ๋ถ„๊ธฐ์˜ ์ด ๋งค์ถœ์•ก์€ ์–ผ๋งˆ์ธ๊ฐ€์š”?")
ex1.click(set_example_question, inputs=[], outputs=user_question)
ex2.click(set_example_question, inputs=[], outputs=user_question)
answer_btn.click(
fn=chat_with_document,
inputs=[chat_state, html_output, user_question],
outputs=[chatbot, chat_state, user_question],
show_progress=True
)
demo.launch()