|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import os |
|
import pandas as pd |
|
from typing import List, Dict, Tuple |
|
import json |
|
import io |
|
import traceback |
|
import csv |
|
|
|
|
|
hf_client = InferenceClient( |
|
"CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN") |
|
) |
|
|
|
def load_code(filename: str) -> str: |
|
try: |
|
with open(filename, 'r', encoding='utf-8') as file: |
|
return file.read() |
|
except FileNotFoundError: |
|
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค." |
|
except Exception as e: |
|
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
|
|
def load_parquet(filename: str) -> str: |
|
try: |
|
df = pd.read_parquet(filename, engine='pyarrow') |
|
return df.head(10).to_markdown(index=False) |
|
except FileNotFoundError: |
|
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค." |
|
except Exception as e: |
|
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
|
|
def respond( |
|
message: str, |
|
history: List[Dict[str, str]], |
|
system_message: str = "", |
|
max_tokens: int = 4000, |
|
temperature: float = 0.5, |
|
top_p: float = 0.9, |
|
parquet_data: str = None |
|
) -> str: |
|
|
|
if parquet_data: |
|
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์
๋ก๋๋ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ง๋ฌธ์ ๋ต๋ณํ๋ ์ญํ ์ ํ๋ค. ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ์ฌ ์ฌ์ฉ์์๊ฒ ๋์์ด ๋๋ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ผ. ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ์ฌ ์์ธํ๊ณ ์ ํํ ๋ต๋ณ์ ์ ๊ณตํ๋, ๋ฏผ๊ฐํ ์ ๋ณด๋ ๊ฐ์ธ ์ ๋ณด๋ฅผ ๋
ธ์ถํ์ง ๋ง๋ผ.""" |
|
try: |
|
df = pd.read_json(io.StringIO(parquet_data)) |
|
|
|
data_summary = df.describe(include='all').to_string() |
|
system_prefix += f"\n\n์
๋ก๋๋ ๋ฐ์ดํฐ์ ์์ฝ ์ ๋ณด:\n{data_summary}" |
|
except Exception as e: |
|
print(f"๋ฐ์ดํฐ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}\n{traceback.format_exc()}") |
|
system_prefix += "\n\n๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค." |
|
else: |
|
system_prefix = system_message or "๋๋ AI ์กฐ์ธ์ ์ญํ ์ด๋ค." |
|
|
|
|
|
prompt = system_prefix + "\n\n" |
|
for chat in history: |
|
if chat['role'] == 'user': |
|
prompt += f"์ฌ์ฉ์: {chat['content']}\n" |
|
else: |
|
prompt += f"AI: {chat['content']}\n" |
|
prompt += f"์ฌ์ฉ์: {message}\nAI:" |
|
|
|
try: |
|
|
|
response = "" |
|
stream = hf_client.text_generation( |
|
prompt=prompt, |
|
max_new_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
) |
|
for msg in stream: |
|
if msg: |
|
response += msg |
|
yield response |
|
except Exception as e: |
|
error_message = f"์ถ๋ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n{traceback.format_exc()}" |
|
print(error_message) |
|
yield error_message |
|
|
|
def upload_csv(file_path: str) -> Tuple[str, str]: |
|
try: |
|
|
|
df = pd.read_csv(file_path, sep=',') |
|
|
|
required_columns = {'id', 'text', 'label', 'metadata'} |
|
available_columns = set(df.columns) |
|
missing_columns = required_columns - available_columns |
|
if missing_columns: |
|
return f"CSV ํ์ผ์ ๋ค์ ํ์ ์ปฌ๋ผ์ด ๋๋ฝ๋์์ต๋๋ค: {', '.join(missing_columns)}", "" |
|
|
|
df.drop_duplicates(inplace=True) |
|
df.fillna('', inplace=True) |
|
|
|
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'category', 'metadata': 'string'}) |
|
|
|
parquet_filename = os.path.splitext(os.path.basename(file_path))[0] + '.parquet' |
|
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy') |
|
return f"{parquet_filename} ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์
๋ก๋๋๊ณ ๋ณํ๋์์ต๋๋ค.", parquet_filename |
|
except Exception as e: |
|
return f"CSV ํ์ผ ์
๋ก๋ ๋ฐ ๋ณํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "" |
|
|
|
def upload_parquet(file_path: str) -> Tuple[str, str, str]: |
|
try: |
|
|
|
df = pd.read_parquet(file_path, engine='pyarrow') |
|
|
|
parquet_content = df.head(10).to_markdown(index=False) |
|
|
|
parquet_json = df.to_json(orient='records', force_ascii=False) |
|
return "Parquet ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์
๋ก๋๋์์ต๋๋ค.", parquet_content, parquet_json |
|
except Exception as e: |
|
return f"Parquet ํ์ผ ์
๋ก๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", "" |
|
|
|
def text_to_parquet(text: str) -> Tuple[str, str, str]: |
|
try: |
|
from io import StringIO |
|
|
|
csv_data = StringIO(text) |
|
df = pd.read_csv( |
|
csv_data, |
|
sep=',', |
|
dtype=str, |
|
quoting=csv.QUOTE_ALL, |
|
escapechar='\\', |
|
engine='python', |
|
header=None, |
|
names=['id', 'text', 'label', 'metadata'] |
|
) |
|
|
|
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'string', 'metadata': 'string'}) |
|
|
|
parquet_filename = 'text_to_parquet.parquet' |
|
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy') |
|
|
|
parquet_content = load_parquet(parquet_filename) |
|
return f"{parquet_filename} ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ๋ณํ๋์์ต๋๋ค.", parquet_content, parquet_filename |
|
except Exception as e: |
|
error_message = f"ํ
์คํธ ๋ณํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n{traceback.format_exc()}" |
|
print(error_message) |
|
return error_message, "", "" |
|
|
|
def preprocess_text_with_llm(input_text: str) -> str: |
|
if not input_text.strip(): |
|
return "์
๋ ฅ ํ
์คํธ๊ฐ ๋น์ด์์ต๋๋ค." |
|
|
|
system_prompt = """๋น์ ์ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ์ ๋ฌธ๊ฐ์
๋๋ค. ์
๋ ฅ๋ ํ
์คํธ๋ฅผ CSV ๋ฐ์ดํฐ์
ํ์์ผ๋ก ๋ณํํ์ธ์. |
|
|
|
๊ท์น: |
|
1. ์ถ๋ ฅ ํ์: id,text,label,metadata |
|
2. id: 1๋ถํฐ ์์ํ๋ ์์ฐจ์ ๋ฒํธ |
|
3. text: ์๋ฏธ ์๋ ๋จ์๋ก ๋ถ๋ฆฌ๋ ํ
์คํธ |
|
4. label: ํ
์คํธ์ ์ฃผ์ ๋ ์นดํ
๊ณ ๋ฆฌ |
|
5. metadata: ์ถ๊ฐ ์ ๋ณด(๋ ์ง, ์ถ์ฒ ๋ฑ) |
|
|
|
์ฃผ์์ฌํญ: |
|
- ํ
์คํธ์ ์ผํ๊ฐ ์์ผ๋ฉด ํฐ๋ฐ์ดํ๋ก ๊ฐ์ธ๊ธฐ |
|
- ํฐ๋ฐ์ดํ๋ ๋ฐฑ์ฌ๋์๋ก ์ด์ค์ผ์ดํ ์ฒ๋ฆฌ |
|
- ๊ฐ ํ์ ์๋ก์ด ์ค๋ก ๊ตฌ๋ถ |
|
- ๋ชจ๋ ํ๋๋ ์ผํ๋ก ๊ตฌ๋ถ |
|
|
|
์
๋ ฅ ํ
์คํธ: |
|
""" |
|
|
|
full_prompt = f"{system_prompt}\n\n{input_text}\n\n์ถ๋ ฅ:" |
|
|
|
try: |
|
response = "" |
|
stream = hf_client.text_generation( |
|
prompt=full_prompt, |
|
max_new_tokens=4000, |
|
temperature=0.3, |
|
top_p=0.9, |
|
stream=True, |
|
) |
|
|
|
for msg in stream: |
|
if msg: |
|
response += msg |
|
|
|
|
|
processed_text = response.strip() |
|
|
|
|
|
try: |
|
|
|
from io import StringIO |
|
import csv |
|
csv.reader(StringIO(processed_text)) |
|
return processed_text |
|
except csv.Error: |
|
return "LLM์ด ์ฌ๋ฐ๋ฅธ CSV ํ์์ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์." |
|
|
|
except Exception as e: |
|
error_message = f"์ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}\n{traceback.format_exc()}" |
|
print(error_message) |
|
return error_message |
|
|
|
|
|
css = """ |
|
footer { |
|
visibility: hidden; |
|
} |
|
#chatbot-container, #chatbot-data-upload { |
|
height: 700px; |
|
overflow-y: scroll; |
|
} |
|
#chatbot-container .message, #chatbot-data-upload .message { |
|
font-size: 14px; |
|
} |
|
/* ์
๋ ฅ์ฐฝ ๋ฐฐ๊ฒฝ์ ๋ฐ ๊ธ์์ ๋ณ๊ฒฝ */ |
|
textarea, input[type="text"] { |
|
background-color: #ffffff; /* ํฐ์ ๋ฐฐ๊ฒฝ */ |
|
color: #000000; /* ๊ฒ์ ์ ๊ธ์ */ |
|
} |
|
/* ํ์ผ ์
๋ก๋ ์์ญ ๋์ด ์กฐ์ */ |
|
#parquet-upload-area { |
|
max-height: 150px; |
|
overflow-y: auto; |
|
} |
|
/* ์ด๊ธฐ ์ค๋ช
๊ธ์จ ํฌ๊ธฐ ์กฐ์ */ |
|
#initial-description { |
|
font-size: 14px; |
|
} |
|
""" |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("# My RAG: LLM์ด ๋๋ง์ ๋ฐ์ดํฐ๋ก ํ์ตํ ์ฝํ
์ธ ์์ฑ/๋ต๋ณ", elem_id="initial-description") |
|
gr.Markdown( |
|
"### 1) ๋๋ง์ ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅ ๋๋ CSV ์
๋ก๋๋ก Parquet ๋ฐ์ดํฐ์
์๋ ๋ณํ 2) Parquet ๋ฐ์ดํฐ์
์ ์
๋ก๋ํ๋ฉด, LLM์ด ๋ง์ถค ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ ์๋ต\n" |
|
"### Tip) '์์ '๋ฅผ ํตํด ๋ค์ํ ํ์ฉ ๋ฐฉ๋ฒ์ ์ฒดํํ๊ณ ์์ฉํด ๋ณด์ธ์, ๋ฐ์ดํฐ์
์
๋ก๋์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ 10๊ฑด๋ง ์ถ๋ ฅ", |
|
elem_id="initial-description" |
|
) |
|
|
|
|
|
|
|
|
|
with gr.Tab("My ๋ฐ์ดํฐ์
+LLM"): |
|
gr.Markdown("### LLM๊ณผ ๋ํํ๊ธฐ") |
|
chatbot_data_upload = gr.Chatbot(label="์ฑ๋ด", type="messages", elem_id="chatbot-data-upload") |
|
msg_data_upload = gr.Textbox(label="๋ฉ์์ง ์
๋ ฅ", placeholder="์ฌ๊ธฐ์ ๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...") |
|
send_data_upload = gr.Button("์ ์ก") |
|
|
|
with gr.Accordion("์์คํ
ํ๋กฌํํธ ๋ฐ ์ต์
์ค์ ", open=False): |
|
system_message = gr.Textbox(label="System Message", value="๋๋ AI ์กฐ์ธ์ ์ญํ ์ด๋ค.") |
|
max_tokens = gr.Slider(minimum=1, maximum=8000, value=1000, label="Max Tokens") |
|
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature") |
|
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P") |
|
|
|
parquet_data_state = gr.State() |
|
|
|
def handle_message_data_upload( |
|
message: str, |
|
history: List[Dict[str, str]], |
|
system_message: str, |
|
max_tokens: int, |
|
temperature: float, |
|
top_p: float, |
|
parquet_data: str |
|
): |
|
history = history or [] |
|
try: |
|
|
|
history.append({"role": "user", "content": message}) |
|
|
|
response_gen = respond( |
|
message, history, system_message, max_tokens, temperature, top_p, parquet_data |
|
) |
|
partial_response = "" |
|
for partial in response_gen: |
|
partial_response = partial |
|
|
|
display_history = history + [ |
|
{"role": "assistant", "content": partial_response} |
|
] |
|
yield display_history, "" |
|
|
|
history.append({"role": "assistant", "content": partial_response}) |
|
except Exception as e: |
|
response = f"์ถ๋ก ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
history.append({"role": "assistant", "content": response}) |
|
yield history, "" |
|
|
|
send_data_upload.click( |
|
handle_message_data_upload, |
|
inputs=[ |
|
msg_data_upload, |
|
chatbot_data_upload, |
|
system_message, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
parquet_data_state, |
|
], |
|
outputs=[chatbot_data_upload, msg_data_upload], |
|
queue=True |
|
) |
|
|
|
|
|
with gr.Accordion("์์ ", open=False): |
|
gr.Examples( |
|
examples=[ |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
์ ๋ํด ์์ฝ ์ค๋ช
ํ๋ผ."], |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ๋ณธ ์๋น์ค๋ฅผ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ(๊ฐ์, ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ, ๊ธฐ์กด ์ ์ฌ ์ ํ/์๋น์ค์ ๋น๊ตํ์ฌ ํน์ฅ์ , ํ์ฉ์ฒ, ๊ฐ์น, ๊ธฐ๋ํจ๊ณผ, ๊ฒฐ๋ก ์ ํฌํจ)๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"], |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ์ฐจ๋ณ์ , ํน์ง, ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"], |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ์ ํ ์์ธ ํ์ด์ง ํ์์ ๋ด์ฉ์ 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
ํ๋ผ"], |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, FAQ 20๊ฑด์ ์์ธํ๊ฒ ์์ฑํ๋ผ. 4000ํ ํฐ ์ด์ ์ฌ์ฉํ๋ผ."], |
|
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ํนํ ์ถ์์ ํ์ฉํ ๊ธฐ์ ๋ฐ ๋น์ฆ๋์ค ๋ชจ๋ธ ์ธก๋ฉด์ ํฌํจํ์ฌ ํนํ ์ถ์์ ๊ตฌ์ฑ์ ๋ง๊ฒ ํ์ ์ ์ธ ์ฐฝ์ ๋ฐ๋ช
๋ด์ฉ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ."], |
|
], |
|
inputs=msg_data_upload, |
|
label="์์ ์ ํ", |
|
) |
|
|
|
|
|
gr.Markdown("### Parquet ํ์ผ ์
๋ก๋") |
|
with gr.Row(): |
|
with gr.Column(): |
|
parquet_upload = gr.File( |
|
label="Parquet ํ์ผ ์
๋ก๋", type="filepath", elem_id="parquet-upload-area" |
|
) |
|
parquet_upload_button = gr.Button("์
๋ก๋") |
|
parquet_upload_status = gr.Textbox(label="์
๋ก๋ ์ํ", interactive=False) |
|
parquet_preview_chat = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ") |
|
|
|
def handle_parquet_upload(file_path: str): |
|
message, parquet_content, parquet_json = upload_parquet(file_path) |
|
if parquet_json: |
|
return message, parquet_content, parquet_json |
|
else: |
|
return message, "", "" |
|
|
|
parquet_upload_button.click( |
|
handle_parquet_upload, |
|
inputs=parquet_upload, |
|
outputs=[parquet_upload_status, parquet_preview_chat, parquet_data_state] |
|
) |
|
|
|
|
|
with gr.Tab("CSV to My ๋ฐ์ดํฐ์
"): |
|
gr.Markdown("### CSV ํ์ผ ์
๋ก๋ ๋ฐ Parquet ๋ณํ") |
|
with gr.Row(): |
|
with gr.Column(): |
|
csv_file = gr.File(label="CSV ํ์ผ ์
๋ก๋", type="filepath") |
|
upload_button = gr.Button("์
๋ก๋ ๋ฐ ๋ณํ") |
|
upload_status = gr.Textbox(label="์
๋ก๋ ์ํ", interactive=False) |
|
parquet_preview = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ") |
|
download_button = gr.File(label="Parquet ํ์ผ ๋ค์ด๋ก๋", interactive=False) |
|
|
|
def handle_csv_upload(file_path: str): |
|
message, parquet_filename = upload_csv(file_path) |
|
if parquet_filename: |
|
parquet_content = load_parquet(parquet_filename) |
|
return message, parquet_content, parquet_filename |
|
else: |
|
return message, "", None |
|
|
|
upload_button.click( |
|
handle_csv_upload, |
|
inputs=csv_file, |
|
outputs=[upload_status, parquet_preview, download_button] |
|
) |
|
|
|
|
|
with gr.Tab("Text to My ๋ฐ์ดํฐ์
"): |
|
gr.Markdown("### ํ
์คํธ๋ฅผ ์
๋ ฅํ๋ฉด CSV๋ก ๋ณํ ํ Parquet์ผ๋ก ์๋ ์ ํ๋ฉ๋๋ค.") |
|
with gr.Row(): |
|
with gr.Column(): |
|
text_input = gr.Textbox( |
|
label="ํ
์คํธ ์
๋ ฅ (๊ฐ ํ์ `id,text,label,metadata` ํ์์ผ๋ก ์
๋ ฅ)", |
|
lines=10, |
|
placeholder='์: 1,"์ด์์ ","์ฅ๊ตฐ","๊ฑฐ๋ถ์ "\n2,"์๊ท ","์ฅ๊ตฐ","๋ชจํจ"\n3,"์ ์กฐ","์","์๊ธฐ"\n4,"๋์ํ ๋ฏธ ํ๋ฐ์์","์","์นจ๋ต"' |
|
) |
|
convert_button = gr.Button("๋ณํ ๋ฐ ๋ค์ด๋ก๋") |
|
convert_status = gr.Textbox(label="๋ณํ ์ํ", interactive=False) |
|
parquet_preview_convert = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ") |
|
download_parquet_convert = gr.File(label="Parquet ํ์ผ ๋ค์ด๋ก๋", interactive=False) |
|
|
|
def handle_text_to_parquet(text: str): |
|
message, parquet_content, parquet_filename = text_to_parquet(text) |
|
if parquet_filename: |
|
return message, parquet_content, parquet_filename |
|
else: |
|
return message, "", None |
|
|
|
convert_button.click( |
|
handle_text_to_parquet, |
|
inputs=text_input, |
|
outputs=[convert_status, parquet_preview_convert, download_parquet_convert] |
|
) |
|
|
|
|
|
with gr.Tab("Text Preprocessing with LLM"): |
|
gr.Markdown("### ํ
์คํธ๋ฅผ ์
๋ ฅํ๋ฉด LLM์ด ๋ฐ์ดํฐ์
ํ์์ ๋ง๊ฒ ์ ์ฒ๋ฆฌํ์ฌ ์ถ๋ ฅํฉ๋๋ค.") |
|
with gr.Row(): |
|
with gr.Column(): |
|
raw_text_input = gr.Textbox( |
|
label="ํ
์คํธ ์
๋ ฅ", |
|
lines=15, |
|
placeholder="์ฌ๊ธฐ์ ์ ์ฒ๋ฆฌํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์..." |
|
) |
|
|
|
with gr.Row(): |
|
preprocess_button = gr.Button("์ ์ฒ๋ฆฌ ์คํ", variant="primary") |
|
clear_button = gr.Button("์ด๊ธฐํ") |
|
|
|
preprocess_status = gr.Textbox( |
|
label="์ ์ฒ๋ฆฌ ์ํ", |
|
interactive=False, |
|
value="๋๊ธฐ ์ค..." |
|
) |
|
|
|
processed_text_output = gr.Textbox( |
|
label="์ ์ฒ๋ฆฌ๋ ๋ฐ์ดํฐ์
์ถ๋ ฅ", |
|
lines=15, |
|
interactive=False |
|
) |
|
|
|
|
|
with gr.Row(): |
|
convert_to_parquet_button = gr.Button("Parquet์ผ๋ก ๋ณํ", visible=True) |
|
download_parquet = gr.File( |
|
label="๋ณํ๋ Parquet ํ์ผ ๋ค์ด๋ก๋", |
|
visible=False |
|
) |
|
|
|
def handle_text_preprocessing(input_text: str): |
|
if not input_text.strip(): |
|
return "์
๋ ฅ ํ
์คํธ๊ฐ ์์ต๋๋ค.", "" |
|
|
|
try: |
|
preprocess_status_msg = "์ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค..." |
|
yield preprocess_status_msg, "" |
|
|
|
processed_text = preprocess_text_with_llm(input_text) |
|
|
|
if processed_text: |
|
preprocess_status_msg = "์ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์์ต๋๋ค." |
|
yield preprocess_status_msg, processed_text |
|
else: |
|
preprocess_status_msg = "์ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค." |
|
yield preprocess_status_msg, "" |
|
|
|
except Exception as e: |
|
error_msg = f"์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
yield error_msg, "" |
|
|
|
def clear_inputs(): |
|
return "", "๋๊ธฐ ์ค...", "" |
|
|
|
def convert_to_parquet_file(processed_text: str): |
|
if not processed_text.strip(): |
|
return "๋ณํํ ํ
์คํธ๊ฐ ์์ต๋๋ค.", None |
|
|
|
try: |
|
message, parquet_content, parquet_filename = text_to_parquet(processed_text) |
|
if parquet_filename: |
|
return message, parquet_filename |
|
return message, None |
|
except Exception as e: |
|
return f"Parquet ๋ณํ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}", None |
|
|
|
|
|
preprocess_button.click( |
|
handle_text_preprocessing, |
|
inputs=[raw_text_input], |
|
outputs=[preprocess_status, processed_text_output], |
|
queue=True |
|
) |
|
|
|
clear_button.click( |
|
clear_inputs, |
|
outputs=[raw_text_input, preprocess_status, processed_text_output] |
|
) |
|
|
|
convert_to_parquet_button.click( |
|
convert_to_parquet_file, |
|
inputs=[processed_text_output], |
|
outputs=[preprocess_status, download_parquet] |
|
) |
|
|
|
|
|
with gr.Accordion("์์ ํ
์คํธ", open=False): |
|
gr.Examples( |
|
examples=[ |
|
["์ด์์ ์ ์กฐ์ ์ค๊ธฐ์ ๋ฌด์ ์ด๋ค. ๊ทธ๋ ์์ง์๋ ๋น์ ํด๊ตฐ์ ์ด๋์๋ค. ๊ฑฐ๋ถ์ ์ ๋ง๋ค์ด ์๊ตฐ๊ณผ ์ธ์ ๋ค."], |
|
["์ธ๊ณต์ง๋ฅ์ ์ปดํจํฐ ๊ณผํ์ ํ ๋ถ์ผ์ด๋ค. ๊ธฐ๊ณํ์ต์ ์ธ๊ณต์ง๋ฅ์ ํ์ ๋ถ์ผ์ด๋ค. ๋ฅ๋ฌ๋์ ๊ธฐ๊ณํ์ต์ ํ ๋ฐฉ๋ฒ์ด๋ค."] |
|
], |
|
inputs=raw_text_input, |
|
label="์์ ์ ํ" |
|
) |
|
|
|
gr.Markdown("### [email protected]", elem_id="initial-description") |
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |