Spaces:
Running
Running
File size: 4,841 Bytes
3790afa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
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()
|