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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
import pandas as pd
|
5 |
+
from typing import List, Dict, Tuple
|
6 |
+
import json
|
7 |
+
import io
|
8 |
+
import traceback
|
9 |
+
import csv
|
10 |
+
# HuggingFace ํด๋ผ์ด์ธํธ ๋์ OpenAI ํด๋ผ์ด์ธํธ ์ฌ์ฉ
|
11 |
+
from openai import OpenAI
|
12 |
+
import os
|
13 |
+
|
14 |
+
# ์ถ๋ก API ํด๋ผ์ด์ธํธ ์ค์
|
15 |
+
hf_client = InferenceClient(
|
16 |
+
"CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN")
|
17 |
+
)
|
18 |
+
|
19 |
+
def load_code(filename: str) -> str:
|
20 |
+
try:
|
21 |
+
with open(filename, 'r', encoding='utf-8') as file:
|
22 |
+
return file.read()
|
23 |
+
except FileNotFoundError:
|
24 |
+
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
25 |
+
except Exception as e:
|
26 |
+
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
27 |
+
|
28 |
+
def load_parquet(filename: str) -> str:
|
29 |
+
try:
|
30 |
+
df = pd.read_parquet(filename, engine='pyarrow')
|
31 |
+
return df.head(10).to_markdown(index=False)
|
32 |
+
except FileNotFoundError:
|
33 |
+
return f"{filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
34 |
+
except Exception as e:
|
35 |
+
return f"ํ์ผ์ ์ฝ๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
36 |
+
|
37 |
+
|
38 |
+
# OpenAI ํด๋ผ์ด์ธํธ ์ค์
|
39 |
+
client = OpenAI(api_key=os.getenv("OPEN_AI"))
|
40 |
+
|
41 |
+
# respond ํจ์ ์์
|
42 |
+
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:
|
43 |
+
# ์์คํ
ํ๋กฌํํธ ์ค์
|
44 |
+
system_prefix = """๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ. ๋๋ ์
๋ก๋๋ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ง๋ฌธ์ ๋ต๋ณํ๋ ์ญํ ์ ํ๋ค.
|
45 |
+
|
46 |
+
์ฃผ์ ์ง์นจ:
|
47 |
+
1. ์ง๋ฌธ๊ณผ ์ง์ ๊ด๋ จ๋ ๋ด์ฉ๋ง ๊ฐ๋จ๋ช
๋ฃํ๊ฒ ๋ต๋ณํ ๊ฒ
|
48 |
+
2. ์ด์ ๋ต๋ณ๊ณผ ์ค๋ณต๋๋ ๋ด์ฉ์ ์ ์ธํ ๊ฒ
|
49 |
+
3. ๋ถํ์ํ ์์๋ ๋ถ์ฐ ์ค๋ช
์ ํ์ง ๋ง ๊ฒ
|
50 |
+
4. ๋์ผํ ๋ด์ฉ์ ๋ค๋ฅธ ํํ์ผ๋ก ๋ฐ๋ณตํ์ง ๋ง ๊ฒ
|
51 |
+
5. ํต์ฌ ์ ๋ณด๋ง ์ ๋ฌํ ๊ฒ
|
52 |
+
"""
|
53 |
+
|
54 |
+
if parquet_data:
|
55 |
+
try:
|
56 |
+
df = pd.read_json(io.StringIO(parquet_data))
|
57 |
+
data_summary = df.describe(include='all').to_string()
|
58 |
+
system_prefix += f"\n\n๋ฐ์ดํฐ ์์ฝ:\n{data_summary}"
|
59 |
+
except Exception as e:
|
60 |
+
print(f"๋ฐ์ดํฐ ๋ก๋ ์ค๋ฅ: {str(e)}")
|
61 |
+
|
62 |
+
# ๋ํ ํ์คํ ๋ฆฌ ๊ตฌ์ฑ
|
63 |
+
messages = [{"role": "system", "content": system_prefix}]
|
64 |
+
|
65 |
+
# ์ต๊ทผ ๋ํ ์ปจํ
์คํธ๋ง ์ ์ง
|
66 |
+
recent_history = history[-3:] if history else []
|
67 |
+
for chat in recent_history:
|
68 |
+
messages.append({"role": chat["role"], "content": chat["content"]})
|
69 |
+
|
70 |
+
messages.append({"role": "user", "content": message})
|
71 |
+
|
72 |
+
try:
|
73 |
+
# OpenAI API ํธ์ถ
|
74 |
+
response = client.chat.completions.create(
|
75 |
+
model="gpt-4o-mini", # GPT-4-mini ๋ชจ๋ธ ์ฌ์ฉ
|
76 |
+
messages=messages,
|
77 |
+
max_tokens=max_tokens,
|
78 |
+
temperature=temperature,
|
79 |
+
top_p=top_p,
|
80 |
+
stream=True
|
81 |
+
)
|
82 |
+
|
83 |
+
full_response = ""
|
84 |
+
for chunk in response:
|
85 |
+
if chunk.choices[0].delta.content:
|
86 |
+
full_response += chunk.choices[0].delta.content
|
87 |
+
# ์๋ต ์ ์
|
88 |
+
cleaned_response = clean_response(full_response)
|
89 |
+
yield cleaned_response
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
error_message = f"์ถ๋ก ์ค๋ฅ: {str(e)}"
|
93 |
+
print(error_message)
|
94 |
+
yield error_message
|
95 |
+
|
96 |
+
def clean_response(text: str) -> str:
|
97 |
+
"""์๋ต ํ
์คํธ ์ ์ ํจ์"""
|
98 |
+
# ๋ฌธ์ฅ ๋จ์๋ก ๋ถ๋ฆฌ
|
99 |
+
sentences = [s.strip() for s in text.split('.') if s.strip()]
|
100 |
+
|
101 |
+
# ์ค๋ณต ์ ๊ฑฐ
|
102 |
+
unique_sentences = []
|
103 |
+
seen = set()
|
104 |
+
|
105 |
+
for sentence in sentences:
|
106 |
+
# ๋ฌธ์ฅ ์ ๊ทํ (๊ณต๋ฐฑ ์ ๊ฑฐ, ์๋ฌธ์ ๋ณํ)
|
107 |
+
normalized = ' '.join(sentence.lower().split())
|
108 |
+
if normalized not in seen:
|
109 |
+
seen.add(normalized)
|
110 |
+
unique_sentences.append(sentence)
|
111 |
+
|
112 |
+
# ์ ์ ๋ ๋ฌธ์ฅ ๊ฒฐํฉ
|
113 |
+
cleaned_text = '. '.join(unique_sentences)
|
114 |
+
if cleaned_text and not cleaned_text.endswith('.'):
|
115 |
+
cleaned_text += '.'
|
116 |
+
|
117 |
+
return cleaned_text
|
118 |
+
|
119 |
+
def remove_duplicates(text: str) -> str:
|
120 |
+
"""์ค๋ณต ๋ฌธ์ฅ ์ ๊ฑฐ ํจ์"""
|
121 |
+
sentences = text.split('.')
|
122 |
+
unique_sentences = []
|
123 |
+
seen = set()
|
124 |
+
|
125 |
+
for sentence in sentences:
|
126 |
+
sentence = sentence.strip()
|
127 |
+
if sentence and sentence not in seen:
|
128 |
+
seen.add(sentence)
|
129 |
+
unique_sentences.append(sentence)
|
130 |
+
|
131 |
+
return '. '.join(unique_sentences)
|
132 |
+
|
133 |
+
def upload_csv(file_path: str) -> Tuple[str, str]:
|
134 |
+
try:
|
135 |
+
# CSV ํ์ผ ์ฝ๊ธฐ
|
136 |
+
df = pd.read_csv(file_path, sep=',')
|
137 |
+
# ํ์ ์ปฌ๋ผ ํ์ธ
|
138 |
+
required_columns = {'id', 'text', 'label', 'metadata'}
|
139 |
+
available_columns = set(df.columns)
|
140 |
+
missing_columns = required_columns - available_columns
|
141 |
+
if missing_columns:
|
142 |
+
return f"CSV ํ์ผ์ ๋ค์ ํ์ ์ปฌ๋ผ์ด ๋๋ฝ๋์์ต๋๋ค: {', '.join(missing_columns)}", ""
|
143 |
+
# ๋ฐ์ดํฐ ํด๋ ์ง
|
144 |
+
df.drop_duplicates(inplace=True)
|
145 |
+
df.fillna('', inplace=True)
|
146 |
+
# ๋ฐ์ดํฐ ์ ํ ์ต์ ํ
|
147 |
+
df = df.astype({'id': 'int32', 'text': 'string', 'label': 'category', 'metadata': 'string'})
|
148 |
+
# Parquet ํ์ผ๋ก ๋ณํ
|
149 |
+
parquet_filename = os.path.splitext(os.path.basename(file_path))[0] + '.parquet'
|
150 |
+
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy')
|
151 |
+
return f"{parquet_filename} ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์
๋ก๋๋๊ณ ๋ณํ๋์์ต๋๋ค.", parquet_filename
|
152 |
+
except Exception as e:
|
153 |
+
return f"CSV ํ์ผ ์
๋ก๋ ๋ฐ ๋ณํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", ""
|
154 |
+
|
155 |
+
def upload_parquet(file_path: str) -> Tuple[str, str, str]:
|
156 |
+
try:
|
157 |
+
# Parquet ํ์ผ ์ฝ๊ธฐ
|
158 |
+
df = pd.read_parquet(file_path, engine='pyarrow')
|
159 |
+
|
160 |
+
# ๋ฐ์ดํฐ ๊ธฐ๋ณธ ์ ๋ณด ์์ง
|
161 |
+
data_info = {
|
162 |
+
"์ด ๋ ์ฝ๋ ์": len(df),
|
163 |
+
"์ปฌ๋ผ ๋ชฉ๋ก": list(df.columns),
|
164 |
+
"๋ฐ์ดํฐ ํ์
": df.dtypes.to_dict(),
|
165 |
+
"๊ฒฐ์ธก์น ์ ๋ณด": df.isnull().sum().to_dict()
|
166 |
+
}
|
167 |
+
|
168 |
+
# ๋ฐ์ดํฐ ์์ฝ ์ ๋ณด ์์ฑ
|
169 |
+
summary = []
|
170 |
+
summary.append(f"### ๋ฐ์ดํฐ์
๊ธฐ๋ณธ ์ ๋ณด:")
|
171 |
+
summary.append(f"- ์ด ๋ ์ฝ๋ ์: {data_info['์ด ๋ ์ฝ๋ ์']}")
|
172 |
+
summary.append(f"- ์ปฌ๋ผ ๋ชฉ๋ก: {', '.join(data_info['์ปฌ๋ผ ๋ชฉ๋ก'])}")
|
173 |
+
|
174 |
+
# ๊ฐ ์ปฌ๋ผ๋ณ ํต๊ณ ์ ๋ณด ์์ฑ
|
175 |
+
summary.append("\n### ์ปฌ๋ผ๋ณ ์ ๋ณด:")
|
176 |
+
for col in df.columns:
|
177 |
+
if df[col].dtype in ['int64', 'float64']:
|
178 |
+
# ์์นํ ๋ฐ์ดํฐ
|
179 |
+
stats = df[col].describe()
|
180 |
+
summary.append(f"\n{col} (์์นํ):")
|
181 |
+
summary.append(f"- ํ๊ท : {stats['mean']:.2f}")
|
182 |
+
summary.append(f"- ์ต์: {stats['min']}")
|
183 |
+
summary.append(f"- ์ต๋: {stats['max']}")
|
184 |
+
elif df[col].dtype == 'object' or df[col].dtype == 'string':
|
185 |
+
# ๋ฌธ์์ด ๋ฐ์ดํฐ
|
186 |
+
unique_count = df[col].nunique()
|
187 |
+
summary.append(f"\n{col} (ํ
์คํธ):")
|
188 |
+
summary.append(f"- ๊ณ ์ ๊ฐ ์: {unique_count}")
|
189 |
+
if unique_count < 10: # ๊ณ ์ ๊ฐ์ด ์ ์ ๊ฒฝ์ฐ๋ง ํ์
|
190 |
+
value_counts = df[col].value_counts().head(5)
|
191 |
+
summary.append("- ์์ 5๊ฐ ๊ฐ:")
|
192 |
+
for val, count in value_counts.items():
|
193 |
+
summary.append(f" โข {val}: {count}๊ฐ")
|
194 |
+
|
195 |
+
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์์ฑ
|
196 |
+
preview = df.head(10).to_markdown(index=False)
|
197 |
+
summary.append("\n### ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:")
|
198 |
+
summary.append(preview)
|
199 |
+
|
200 |
+
parquet_content = "\n".join(summary)
|
201 |
+
|
202 |
+
# DataFrame์ JSON ๋ฌธ์์ด๋ก ๋ณํ (Q&A์์ ์ฌ์ฉ)
|
203 |
+
parquet_json = df.to_json(orient='records', force_ascii=False)
|
204 |
+
|
205 |
+
return "Parquet ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ์
๋ก๋๋์์ต๋๋ค.", parquet_content, parquet_json
|
206 |
+
except Exception as e:
|
207 |
+
return f"Parquet ํ์ผ ์
๋ก๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}", "", ""
|
208 |
+
|
209 |
+
|
210 |
+
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:
|
211 |
+
try:
|
212 |
+
if parquet_data:
|
213 |
+
# JSON ๋ฌธ์์ด์ DataFrame์ผ๋ก ๋ณํ
|
214 |
+
df = pd.read_json(io.StringIO(parquet_data))
|
215 |
+
|
216 |
+
# ๋ฐ์ดํฐ์
์ปจํ
์คํธ ์์ฑ
|
217 |
+
columns_info = []
|
218 |
+
for col in df.columns:
|
219 |
+
if df[col].dtype in ['int64', 'float64']:
|
220 |
+
col_type = "์์นํ"
|
221 |
+
stats = df[col].describe()
|
222 |
+
col_info = f"- {col} ({col_type}): ํ๊ท ={stats['mean']:.2f}, ์ต์={stats['min']}, ์ต๋={stats['max']}"
|
223 |
+
else:
|
224 |
+
col_type = "ํ
์คํธ"
|
225 |
+
unique_count = df[col].nunique()
|
226 |
+
col_info = f"- {col} ({col_type}): ๊ณ ์ ๊ฐ {unique_count}๊ฐ"
|
227 |
+
columns_info.append(col_info)
|
228 |
+
|
229 |
+
data_context = f"""
|
230 |
+
ํ์ฌ ์
๋ก๋๋ ๋ฐ์ดํฐ์
์ ๋ณด:
|
231 |
+
- ์ด {len(df)} ๊ฐ์ ๋ ์ฝ๋
|
232 |
+
- ์ปฌ๋ผ ์ ๋ณด:
|
233 |
+
{chr(10).join(columns_info)}
|
234 |
+
|
235 |
+
์ํ ๋ฐ์ดํฐ:
|
236 |
+
{df.head(20).to_string()}
|
237 |
+
"""
|
238 |
+
system_prompt = f"""๋น์ ์ ์
๋ก๋๋ ๋ฐ์ดํฐ์
์ ๋ถ์ํ๊ณ ์ง๋ฌธ์ ๋ต๋ณํ๋ AI ์ด์์คํดํธ์
๋๋ค.
|
239 |
+
|
240 |
+
์ฃผ์ ์ง์นจ:
|
241 |
+
1. ๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ
|
242 |
+
2. ๋ฐ์ดํฐ์
์ ์ค์ ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ ํํ๊ฒ ๋ต๋ณํ ๊ฒ
|
243 |
+
3. ๋ฐ์ดํฐ์ ์๋ ๋ด์ฉ์ ์ถ์ธกํ์ง ๋ง ๊ฒ
|
244 |
+
4. ๋ต๋ณ์ ๊ฐ๋จ๋ช
๋ฃํ๊ฒ ํ ๊ฒ
|
245 |
+
5. ๋ฐ์ดํฐ ํ๋ผ์ด๋ฒ์๋ฅผ ๊ณ ๋ คํ์ฌ ๋ต๋ณํ ๊ฒ
|
246 |
+
|
247 |
+
๋ฐ์ดํฐ์
๊ตฌ์กฐ ์ค๋ช
:
|
248 |
+
{chr(10).join(columns_info)}
|
249 |
+
|
250 |
+
์ฐธ๊ณ ํ ๋ฐ์ดํฐ ์ํ:
|
251 |
+
{data_context}
|
252 |
+
"""
|
253 |
+
else:
|
254 |
+
system_prompt = system_message or "๋๋ AI ์กฐ์ธ์ ์ญํ ์ด๋ค."
|
255 |
+
|
256 |
+
# OpenAI API ํธ์ถ
|
257 |
+
messages = [{"role": "system", "content": system_prompt}]
|
258 |
+
|
259 |
+
# ์ต๊ทผ ๋ํ ๊ธฐ๋ก ์ถ๊ฐ
|
260 |
+
recent_history = history[-3:] if history else []
|
261 |
+
for chat in recent_history:
|
262 |
+
messages.append({"role": chat["role"], "content": chat["content"]})
|
263 |
+
|
264 |
+
messages.append({"role": "user", "content": message})
|
265 |
+
|
266 |
+
response = client.chat.completions.create(
|
267 |
+
model="gpt-4-0125-preview",
|
268 |
+
messages=messages,
|
269 |
+
max_tokens=max_tokens,
|
270 |
+
temperature=temperature,
|
271 |
+
top_p=top_p,
|
272 |
+
stream=True
|
273 |
+
)
|
274 |
+
|
275 |
+
full_response = ""
|
276 |
+
for chunk in response:
|
277 |
+
if chunk.choices[0].delta.content:
|
278 |
+
full_response += chunk.choices[0].delta.content
|
279 |
+
yield clean_response(full_response)
|
280 |
+
|
281 |
+
except Exception as e:
|
282 |
+
error_message = f"์๋ต ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
283 |
+
print(f"{error_message}\n{traceback.format_exc()}")
|
284 |
+
yield error_message
|
285 |
+
|
286 |
+
def text_to_parquet(text: str) -> Tuple[str, str, str]:
|
287 |
+
try:
|
288 |
+
# ์
๋ ฅ ํ
์คํธ๋ฅผ ์ค ๋จ์๋ก ๋ถ๋ฆฌ
|
289 |
+
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
290 |
+
|
291 |
+
# ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๋ฆฌ์คํธ
|
292 |
+
data = []
|
293 |
+
|
294 |
+
for line in lines:
|
295 |
+
try:
|
296 |
+
# ์ ๊ท์์ ์ฌ์ฉํ์ฌ CSV ํ์ ํ์ฑ
|
297 |
+
import re
|
298 |
+
pattern = r'(\d+),([^,]+),([^,]+),(.+)'
|
299 |
+
match = re.match(pattern, line)
|
300 |
+
|
301 |
+
if match:
|
302 |
+
id_val, text_val, label_val, metadata_val = match.groups()
|
303 |
+
|
304 |
+
# ์๋ฐ์ดํ ์ ๊ฑฐ ๋ฐ ์ ์
|
305 |
+
text_val = text_val.strip().strip('"')
|
306 |
+
label_val = label_val.strip().strip('"')
|
307 |
+
metadata_val = metadata_val.strip().strip('"')
|
308 |
+
|
309 |
+
data.append({
|
310 |
+
'id': int(id_val),
|
311 |
+
'text': text_val,
|
312 |
+
'label': label_val,
|
313 |
+
'metadata': metadata_val
|
314 |
+
})
|
315 |
+
except Exception as e:
|
316 |
+
print(f"๋ผ์ธ ํ์ฑ ์ค๋ฅ: {line}\n{str(e)}")
|
317 |
+
continue
|
318 |
+
|
319 |
+
if not data:
|
320 |
+
return "๋ณํํ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.", "", ""
|
321 |
+
|
322 |
+
# DataFrame ์์ฑ
|
323 |
+
df = pd.DataFrame(data)
|
324 |
+
|
325 |
+
# ๋ฐ์ดํฐ ํ์
์ค์
|
326 |
+
df = df.astype({
|
327 |
+
'id': 'int32',
|
328 |
+
'text': 'string',
|
329 |
+
'label': 'string',
|
330 |
+
'metadata': 'string'
|
331 |
+
})
|
332 |
+
|
333 |
+
# Parquet ํ์ผ๋ก ๋ณํ
|
334 |
+
parquet_filename = 'text_to_parquet.parquet'
|
335 |
+
df.to_parquet(parquet_filename, engine='pyarrow', compression='snappy')
|
336 |
+
|
337 |
+
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์์ฑ
|
338 |
+
preview = df.to_markdown(index=False)
|
339 |
+
|
340 |
+
return (
|
341 |
+
f"{parquet_filename} ํ์ผ์ด ์ฑ๊ณต์ ์ผ๋ก ๋ณํ๋์์ต๋๋ค. ์ด {len(df)}๊ฐ์ ๋ ์ฝ๋๊ฐ ์ฒ๋ฆฌ๋์์ต๋๋ค.",
|
342 |
+
preview,
|
343 |
+
parquet_filename
|
344 |
+
)
|
345 |
+
|
346 |
+
except Exception as e:
|
347 |
+
error_message = f"ํ
์คํธ ๋ณํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
348 |
+
print(f"{error_message}\n{traceback.format_exc()}")
|
349 |
+
return error_message, "", ""
|
350 |
+
|
351 |
+
# preprocess_text_with_llm ํจ์๋ ์์
|
352 |
+
def preprocess_text_with_llm(input_text: str) -> str:
|
353 |
+
if not input_text.strip():
|
354 |
+
return "์
๋ ฅ ํ
์คํธ๊ฐ ๋น์ด์์ต๋๋ค."
|
355 |
+
|
356 |
+
system_prompt = """๋ฐ๋์ ํ๊ธ(ํ๊ตญ์ด)๋ก ๋ต๋ณํ์์ค. ๋น์ ์ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ์ ๋ฌธ๊ฐ์
๋๋ค. ์
๋ ฅ๋ ํ
์คํธ๋ฅผ CSV ๋ฐ์ดํฐ์
ํ์์ผ๋ก ๋ณํํ์ธ์.
|
357 |
+
|
358 |
+
๊ท์น:
|
359 |
+
1. ์ถ๋ ฅ ํ์: id,text,label,metadata
|
360 |
+
2. id: 1๋ถํฐ ์์ํ๋ ์์ฐจ์ ๋ฒํธ
|
361 |
+
3. text: ์๋ฏธ ์๋ ๋จ์๋ก ๋ถ๋ฆฌ๋ ํ
์คํธ
|
362 |
+
4. label: ํ
์คํธ์ ์ฃผ์ ๋ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ์๋ ๊ธฐ์ค์ผ๋ก ์ ํํ๊ฒ ํ ๊ฐ๋ง ์ ํ
|
363 |
+
- Historical_Figure (์ญ์ฌ์ ์ธ๋ฌผ)
|
364 |
+
- Military_History (๊ตฐ์ฌ ์ญ์ฌ)
|
365 |
+
- Technology (๊ธฐ์ )
|
366 |
+
- Politics (์ ์น)
|
367 |
+
- Culture (๋ฌธํ)
|
368 |
+
5. metadata: ๋ ์ง, ์ถ์ฒ ๋ฑ ์ถ๊ฐ ์ ๋ณด"""
|
369 |
+
|
370 |
+
try:
|
371 |
+
response = client.chat.completions.create(
|
372 |
+
model="gpt-4-0125-preview",
|
373 |
+
messages=[
|
374 |
+
{"role": "system", "content": system_prompt},
|
375 |
+
{"role": "user", "content": input_text}
|
376 |
+
],
|
377 |
+
max_tokens=4000,
|
378 |
+
temperature=0.1,
|
379 |
+
stream=True
|
380 |
+
)
|
381 |
+
|
382 |
+
full_response = ""
|
383 |
+
for chunk in response:
|
384 |
+
if chunk.choices[0].delta.content:
|
385 |
+
full_response += chunk.choices[0].delta.content
|
386 |
+
|
387 |
+
# ์๋ต ์ ์
|
388 |
+
processed_text = clean_response(full_response)
|
389 |
+
|
390 |
+
# CSV ํ์ ๊ฒ์ฆ
|
391 |
+
try:
|
392 |
+
from io import StringIO
|
393 |
+
import csv
|
394 |
+
csv.reader(StringIO(processed_text))
|
395 |
+
return processed_text
|
396 |
+
except csv.Error:
|
397 |
+
return "LLM์ด ์ฌ๋ฐ๋ฅธ CSV ํ์์ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์."
|
398 |
+
|
399 |
+
except Exception as e:
|
400 |
+
error_message = f"์ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
401 |
+
print(error_message)
|
402 |
+
return error_message# preprocess_text_with_llm ํจ์๋ ์์
|
403 |
+
def preprocess_text_with_llm(input_text: str) -> str:
|
404 |
+
if not input_text.strip():
|
405 |
+
return "์
๋ ฅ ํ
์คํธ๊ฐ ๋น์ด์์ต๋๋ค."
|
406 |
+
|
407 |
+
system_prompt = """๋ฐ๋์ ํ๊ธ(ํ๊ตญ์ด)๋ก ๋ต๋ณํ์์ค. ๋น์ ์ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ์ ๋ฌธ๊ฐ์
๋๋ค. ์
๋ ฅ๋ ํ
์คํธ๋ฅผ CSV ๋ฐ์ดํฐ์
ํ์์ผ๋ก ๋ณํํ์ธ์.
|
408 |
+
|
409 |
+
๊ท์น:
|
410 |
+
1. ์ถ๋ ฅ ํ์: id,text,label,metadata
|
411 |
+
2. id: 1๋ถํฐ ์์ํ๋ ์์ฐจ์ ๋ฒํธ
|
412 |
+
3. text: ์๋ฏธ ์๋ ๋จ์๋ก ๋ถ๋ฆฌ๋ ํ
์คํธ
|
413 |
+
4. label: ํ
์คํธ์ ์ฃผ์ ๋ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ์๋ ๊ธฐ์ค์ผ๋ก ์ ํํ๊ฒ ํ ๊ฐ๋ง ์ ํ
|
414 |
+
- Historical_Figure (์ญ์ฌ์ ์ธ๋ฌผ)
|
415 |
+
- Military_History (๊ตฐ์ฌ ์ญ์ฌ)
|
416 |
+
- Technology (๊ธฐ์ )
|
417 |
+
- Politics (์ ์น)
|
418 |
+
- Culture (๋ฌธํ)
|
419 |
+
5. metadata: ๋ ์ง, ์ถ์ฒ ๋ฑ ์ถ๊ฐ ์ ๋ณด"""
|
420 |
+
|
421 |
+
try:
|
422 |
+
response = client.chat.completions.create(
|
423 |
+
model="gpt-4o-mini",
|
424 |
+
messages=[
|
425 |
+
{"role": "system", "content": system_prompt},
|
426 |
+
{"role": "user", "content": input_text}
|
427 |
+
],
|
428 |
+
max_tokens=4000,
|
429 |
+
temperature=0.1,
|
430 |
+
stream=True
|
431 |
+
)
|
432 |
+
|
433 |
+
full_response = ""
|
434 |
+
for chunk in response:
|
435 |
+
if chunk.choices[0].delta.content:
|
436 |
+
full_response += chunk.choices[0].delta.content
|
437 |
+
|
438 |
+
# ์๋ต ์ ์
|
439 |
+
processed_text = clean_response(full_response)
|
440 |
+
|
441 |
+
# CSV ํ์ ๊ฒ์ฆ
|
442 |
+
try:
|
443 |
+
from io import StringIO
|
444 |
+
import csv
|
445 |
+
csv.reader(StringIO(processed_text))
|
446 |
+
return processed_text
|
447 |
+
except csv.Error:
|
448 |
+
return "LLM์ด ์ฌ๋ฐ๋ฅธ CSV ํ์์ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ๋ค์ ์๋ํด์ฃผ์ธ์."
|
449 |
+
|
450 |
+
except Exception as e:
|
451 |
+
error_message = f"์ ์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
452 |
+
print(error_message)
|
453 |
+
return error_message
|
454 |
+
|
455 |
+
# CSS ์ค์
|
456 |
+
css = """
|
457 |
+
footer {
|
458 |
+
visibility: hidden;
|
459 |
+
}
|
460 |
+
#chatbot-container, #chatbot-data-upload {
|
461 |
+
height: 700px;
|
462 |
+
overflow-y: scroll;
|
463 |
+
}
|
464 |
+
#chatbot-container .message, #chatbot-data-upload .message {
|
465 |
+
font-size: 14px;
|
466 |
+
}
|
467 |
+
/* ์
๋ ฅ์ฐฝ ๋ฐฐ๊ฒฝ์ ๋ฐ ๊ธ์์ ๋ณ๊ฒฝ */
|
468 |
+
textarea, input[type="text"] {
|
469 |
+
background-color: #ffffff; /* ํฐ์ ๋ฐฐ๊ฒฝ */
|
470 |
+
color: #000000; /* ๊ฒ์ ์ ๊ธ์ */
|
471 |
+
}
|
472 |
+
/* ํ์ผ ์
๋ก๋ ์์ญ ๋์ด ์กฐ์ */
|
473 |
+
#parquet-upload-area {
|
474 |
+
max-height: 150px;
|
475 |
+
overflow-y: auto;
|
476 |
+
}
|
477 |
+
/* ์ด๊ธฐ ์ค๋ช
๊ธ์จ ํฌ๊ธฐ ์กฐ์ */
|
478 |
+
#initial-description {
|
479 |
+
font-size: 14px;
|
480 |
+
}
|
481 |
+
"""
|
482 |
+
|
483 |
+
# Gradio Blocks ์ธํฐํ์ด์ค ์ค์
|
484 |
+
with gr.Blocks(css=css) as demo:
|
485 |
+
gr.Markdown("# MyEzRAG: LLM์ด ๋๋ง์ ๋ฐ์ดํฐ๋ก ํ์ตํ ์ฝํ
์ธ ์์ฑ/๋ต๋ณ", elem_id="initial-description")
|
486 |
+
gr.Markdown(
|
487 |
+
"### '์ฌ์ฉ ๋ฐฉ๋ฒ' ํญ์ ํตํด ์์ธํ ์ด์ฉ ๋ฐฉ๋ฒ์ ์ฐธ๊ณ ํ์ธ์.\n"
|
488 |
+
"### Tip) '์์ '๋ฅผ ํตํด ๋ค์ํ ํ์ฉ ๋ฐฉ๋ฒ์ ์ฒดํํ๊ณ ์์ฉํด ๋ณด์ธ์, ๋ฐ์ดํฐ์
์
๋ก๋์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ 10๊ฑด๋ง ์ถ๋ ฅ",
|
489 |
+
elem_id="initial-description"
|
490 |
+
)
|
491 |
+
|
492 |
+
|
493 |
+
|
494 |
+
# ์ฒซ ๋ฒ์งธ ํญ: ์ฑ๋ด ๋ฐ์ดํฐ ์
๋ก๋ (ํญ ์ด๋ฆ ๋ณ๊ฒฝ: "My ๋ฐ์ดํฐ์
+LLM")
|
495 |
+
with gr.Tab("My ๋ฐ์ดํฐ์
+LLM"):
|
496 |
+
gr.Markdown("### LLM๊ณผ ๋ํํ๊ธฐ")
|
497 |
+
chatbot_data_upload = gr.Chatbot(label="์ฑ๋ด", type="messages", elem_id="chatbot-data-upload")
|
498 |
+
msg_data_upload = gr.Textbox(label="๋ฉ์์ง ์
๋ ฅ", placeholder="์ฌ๊ธฐ์ ๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์...")
|
499 |
+
send_data_upload = gr.Button("์ ์ก")
|
500 |
+
|
501 |
+
with gr.Accordion("์์คํ
ํ๋กฌํํธ ๋ฐ ์ต์
์ค์ ", open=False):
|
502 |
+
system_message = gr.Textbox(label="System Message", value="๋๋ AI ์กฐ์ธ์ ์ญํ ์ด๋ค.")
|
503 |
+
max_tokens = gr.Slider(minimum=1, maximum=8000, value=1000, label="Max Tokens")
|
504 |
+
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
|
505 |
+
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P")
|
506 |
+
|
507 |
+
parquet_data_state = gr.State()
|
508 |
+
|
509 |
+
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):
|
510 |
+
history = history or []
|
511 |
+
|
512 |
+
# ์ค๋ณต ์ง๋ฌธ ๊ฒ์ฌ
|
513 |
+
recent_questions = [chat['content'].strip().lower() for chat in history[-3:] if chat['role'] == 'user']
|
514 |
+
if message.strip().lower() in recent_questions:
|
515 |
+
yield history + [{"role": "assistant", "content": "๋์ผํ ์ง๋ฌธ์ด ์ต๊ทผ์ ์์์ต๋๋ค. ๋ค๋ฅธ ์ง๋ฌธ์ ํด์ฃผ์ธ์."}], ""
|
516 |
+
return
|
517 |
+
|
518 |
+
try:
|
519 |
+
history.append({"role": "user", "content": message})
|
520 |
+
response_gen = respond(
|
521 |
+
message,
|
522 |
+
history,
|
523 |
+
system_message,
|
524 |
+
max_tokens,
|
525 |
+
temperature=0.3, # ๋ฎ์ temperature ์ฌ์ฉ
|
526 |
+
top_p=top_p,
|
527 |
+
parquet_data=parquet_data
|
528 |
+
)
|
529 |
+
|
530 |
+
partial_response = ""
|
531 |
+
for partial in response_gen:
|
532 |
+
partial_response = partial
|
533 |
+
display_history = history + [{"role": "assistant", "content": partial_response}]
|
534 |
+
yield display_history, ""
|
535 |
+
|
536 |
+
history.append({"role": "assistant", "content": partial_response})
|
537 |
+
except Exception as e:
|
538 |
+
response = f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
539 |
+
history.append({"role": "assistant", "content": response})
|
540 |
+
yield history, ""
|
541 |
+
|
542 |
+
|
543 |
+
|
544 |
+
|
545 |
+
send_data_upload.click(
|
546 |
+
handle_message_data_upload,
|
547 |
+
inputs=[
|
548 |
+
msg_data_upload,
|
549 |
+
chatbot_data_upload,
|
550 |
+
system_message,
|
551 |
+
max_tokens,
|
552 |
+
temperature,
|
553 |
+
top_p,
|
554 |
+
parquet_data_state, # parquet_data_state๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ก๋๋ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ
|
555 |
+
],
|
556 |
+
outputs=[chatbot_data_upload, msg_data_upload],
|
557 |
+
queue=True
|
558 |
+
)
|
559 |
+
|
560 |
+
# ์์ ์ถ๊ฐ
|
561 |
+
with gr.Accordion("์์ ", open=False):
|
562 |
+
gr.Examples(
|
563 |
+
examples=[
|
564 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
์ ๋ํด ์์ฝ ์ค๋ช
ํ๋ผ."],
|
565 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ๋ณธ ์๋น์ค๋ฅผ SEO ์ต์ ํํ์ฌ ๋ธ๋ก๊ทธ ํฌ์คํธ(๊ฐ์, ๋ฐฐ๊ฒฝ ๋ฐ ํ์์ฑ, ๊ธฐ์กด ์ ์ฌ ์ ํ/์๋น์ค์ ๋น๊ตํ์ฌ ํน์ฅ์ , ํ์ฉ์ฒ, ๊ฐ์น, ๊ธฐ๋ํจ๊ณผ, ๊ฒฐ๋ก ์ ํฌํจ)๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ"],
|
566 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ์ฌ์ฉ ๋ฐฉ๋ฒ๊ณผ ์ฐจ๋ณ์ , ํน์ง, ๊ฐ์ ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์ ํ๋ธ ์์ ์คํฌ๋ฆฝํธ ํํ๋ก ์์ฑํ๋ผ"],
|
567 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ์ ํ ์์ธ ํ์ด์ง ํ์์ ๋ด์ฉ์ 4000 ํ ํฐ ์ด์ ์์ธํ ์ค๋ช
ํ๋ผ"],
|
568 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, FAQ 20๊ฑด์ ์์ธํ๊ฒ ์์ฑํ๋ผ. 4000ํ ํฐ ์ด์ ์ฌ์ฉํ๋ผ."],
|
569 |
+
["์
๋ก๋๋ ๋ฐ์ดํฐ์
ํ์ผ์ ํ์ต ๋ฐ์ดํฐ๋ก ํ์ฉํ์ฌ, ํนํ ์ถ์์ ํ์ฉํ ๊ธฐ์ ๋ฐ ๋น์ฆ๋์ค ๋ชจ๋ธ ์ธก๋ฉด์ ํฌํจํ์ฌ ํนํ ์ถ์์ ๊ตฌ์ฑ์ ๋ง๊ฒ ํ์ ์ ์ธ ์ฐฝ์ ๋ฐ๋ช
๋ด์ฉ์ ์ค์ฌ์ผ๋ก 4000 ํ ํฐ ์ด์ ์์ฑํ๋ผ."],
|
570 |
+
],
|
571 |
+
inputs=msg_data_upload,
|
572 |
+
label="์์ ์ ํ",
|
573 |
+
)
|
574 |
+
|
575 |
+
# Parquet ํ์ผ ์
๋ก๋๋ฅผ ํ๋ฉด ํ๋จ์ผ๋ก ์ด๋
|
576 |
+
gr.Markdown("### Parquet ํ์ผ ์
๋ก๋")
|
577 |
+
with gr.Row():
|
578 |
+
with gr.Column():
|
579 |
+
parquet_upload = gr.File(
|
580 |
+
label="Parquet ํ์ผ ์
๋ก๋", type="filepath", elem_id="parquet-upload-area"
|
581 |
+
)
|
582 |
+
parquet_upload_button = gr.Button("์
๋ก๋")
|
583 |
+
parquet_upload_status = gr.Textbox(label="์
๋ก๋ ์ํ", interactive=False)
|
584 |
+
parquet_preview_chat = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
585 |
+
|
586 |
+
def handle_parquet_upload(file_path: str):
|
587 |
+
message, parquet_content, parquet_json = upload_parquet(file_path)
|
588 |
+
if parquet_json:
|
589 |
+
return message, parquet_content, parquet_json
|
590 |
+
else:
|
591 |
+
return message, "", ""
|
592 |
+
|
593 |
+
parquet_upload_button.click(
|
594 |
+
handle_parquet_upload,
|
595 |
+
inputs=parquet_upload,
|
596 |
+
outputs=[parquet_upload_status, parquet_preview_chat, parquet_data_state]
|
597 |
+
)
|
598 |
+
|
599 |
+
# ๋ ๋ฒ์งธ ํญ: ๋ฐ์ดํฐ ๋ณํ (ํญ ์ด๋ฆ ๋ณ๊ฒฝ: "CSV to My ๋ฐ์ดํฐ์
")
|
600 |
+
with gr.Tab("CSV to My ๋ฐ์ดํฐ์
"):
|
601 |
+
gr.Markdown("### CSV ํ์ผ ์
๋ก๋ ๋ฐ Parquet ๋ณํ")
|
602 |
+
with gr.Row():
|
603 |
+
with gr.Column():
|
604 |
+
csv_file = gr.File(label="CSV ํ์ผ ์
๋ก๋", type="filepath")
|
605 |
+
upload_button = gr.Button("์
๋ก๋ ๋ฐ ๋ณํ")
|
606 |
+
upload_status = gr.Textbox(label="์
๋ก๋ ์ํ", interactive=False)
|
607 |
+
parquet_preview = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
608 |
+
download_button = gr.File(label="Parquet ํ์ผ ๋ค์ด๋ก๋", interactive=False)
|
609 |
+
|
610 |
+
def handle_csv_upload(file_path: str):
|
611 |
+
message, parquet_filename = upload_csv(file_path)
|
612 |
+
if parquet_filename:
|
613 |
+
parquet_content = load_parquet(parquet_filename)
|
614 |
+
return message, parquet_content, parquet_filename
|
615 |
+
else:
|
616 |
+
return message, "", None
|
617 |
+
|
618 |
+
upload_button.click(
|
619 |
+
handle_csv_upload,
|
620 |
+
inputs=csv_file,
|
621 |
+
outputs=[upload_status, parquet_preview, download_button]
|
622 |
+
)
|
623 |
+
|
624 |
+
# ์ธ ๋ฒ์งธ ํญ: ํ
์คํธ to csv to parquet ๋ณํ (ํญ ์ด๋ฆ ๋ณ๊ฒฝ: "Text to My ๋ฐ์ดํฐ์
")
|
625 |
+
with gr.Tab("Text to My ๋ฐ์ดํฐ์
"):
|
626 |
+
gr.Markdown("### ํ
์คํธ๋ฅผ ์
๋ ฅํ๋ฉด CSV๋ก ๋ณํ ํ Parquet์ผ๋ก ์๋ ์ ํ๋ฉ๋๋ค.")
|
627 |
+
with gr.Row():
|
628 |
+
with gr.Column():
|
629 |
+
text_input = gr.Textbox(
|
630 |
+
label="ํ
์คํธ ์
๋ ฅ (๊ฐ ํ์ `id,text,label,metadata` ํ์์ผ๋ก ์
๋ ฅ)",
|
631 |
+
lines=10,
|
632 |
+
placeholder='์: 1,"์ด์์ ","์ฅ๊ตฐ","๊ฑฐ๋ถ์ "\n2,"์๊ท ","์ฅ๊ตฐ","๋ชจํจ"\n3,"์ ์กฐ","์","์๊ธฐ"\n4,"๋์ํ ๋ฏธ ํ๋ฐ์์","์","์นจ๋ต"'
|
633 |
+
)
|
634 |
+
convert_button = gr.Button("๋ณํ ๋ฐ ๋ค์ด๋ก๋")
|
635 |
+
convert_status = gr.Textbox(label="๋ณํ ์ํ", interactive=False)
|
636 |
+
parquet_preview_convert = gr.Markdown(label="Parquet ํ์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
637 |
+
download_parquet_convert = gr.File(label="Parquet ํ์ผ ๋ค์ด๋ก๋", interactive=False)
|
638 |
+
|
639 |
+
def handle_text_to_parquet(text: str):
|
640 |
+
message, parquet_content, parquet_filename = text_to_parquet(text)
|
641 |
+
if parquet_filename:
|
642 |
+
return message, parquet_content, parquet_filename
|
643 |
+
else:
|
644 |
+
return message, "", None
|
645 |
+
|
646 |
+
convert_button.click(
|
647 |
+
handle_text_to_parquet,
|
648 |
+
inputs=text_input,
|
649 |
+
outputs=[convert_status, parquet_preview_convert, download_parquet_convert]
|
650 |
+
)
|
651 |
+
|
652 |
+
# ๋ค๋ฒ์งธ ํญ์ UI ๋ถ๋ถ ์์
|
653 |
+
with gr.Tab("Text Preprocessing with LLM"):
|
654 |
+
gr.Markdown("### ํ
์คํธ๋ฅผ ์
๋ ฅํ๋ฉด LLM์ด ๋ฐ์ดํฐ์
ํ์์ ๋ง๊ฒ ์ ์ฒ๋ฆฌํ์ฌ ์ถ๋ ฅํฉ๋๋ค.")
|
655 |
+
with gr.Row():
|
656 |
+
with gr.Column():
|
657 |
+
raw_text_input = gr.Textbox(
|
658 |
+
label="ํ
์คํธ ์
๋ ฅ",
|
659 |
+
lines=15,
|
660 |
+
placeholder="์ฌ๊ธฐ์ ์ ์ฒ๋ฆฌํ ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์..."
|
661 |
+
)
|
662 |
+
|
663 |
+
with gr.Row():
|
664 |
+
preprocess_button = gr.Button("์ ์ฒ๋ฆฌ ์คํ", variant="primary")
|
665 |
+
clear_button = gr.Button("์ด๊ธฐํ")
|
666 |
+
|
667 |
+
preprocess_status = gr.Textbox(
|
668 |
+
label="์ ์ฒ๋ฆฌ ์ํ",
|
669 |
+
interactive=False,
|
670 |
+
value="๋๊ธฐ ์ค..."
|
671 |
+
)
|
672 |
+
|
673 |
+
processed_text_output = gr.Textbox(
|
674 |
+
label="์ ์ฒ๋ฆฌ๋ ๋ฐ์ดํฐ์
์ถ๋ ฅ",
|
675 |
+
lines=15,
|
676 |
+
interactive=False
|
677 |
+
)
|
678 |
+
|
679 |
+
# Parquet ๋ณํ ๋ฐ ๋ค์ด๋ก๋ ์น์
|
680 |
+
convert_to_parquet_button = gr.Button("Parquet์ผ๋ก ๋ณํ")
|
681 |
+
download_parquet = gr.File(label="๋ณํ๋ Parquet ํ์ผ ๋ค์ด๋ก๋")
|
682 |
+
|
683 |
+
|
684 |
+
|
685 |
+
|
686 |
+
def handle_text_preprocessing(input_text: str):
|
687 |
+
if not input_text.strip():
|
688 |
+
return "์
๋ ฅ ํ
์คํธ๊ฐ ์์ต๋๋ค.", ""
|
689 |
+
|
690 |
+
try:
|
691 |
+
preprocess_status_msg = "์ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค..."
|
692 |
+
yield preprocess_status_msg, ""
|
693 |
+
|
694 |
+
processed_text = preprocess_text_with_llm(input_text)
|
695 |
+
|
696 |
+
if processed_text:
|
697 |
+
preprocess_status_msg = "์ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋์์ต๋๋ค."
|
698 |
+
yield preprocess_status_msg, processed_text
|
699 |
+
else:
|
700 |
+
preprocess_status_msg = "์ ์ฒ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
701 |
+
yield preprocess_status_msg, ""
|
702 |
+
|
703 |
+
except Exception as e:
|
704 |
+
error_msg = f"์ฒ๋ฆฌ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
705 |
+
yield error_msg, ""
|
706 |
+
|
707 |
+
def clear_inputs():
|
708 |
+
return "", "๋๊ธฐ ์ค...", ""
|
709 |
+
|
710 |
+
def convert_to_parquet_file(processed_text: str):
|
711 |
+
if not processed_text.strip():
|
712 |
+
return "๋ณํํ ํ
์คํธ๊ฐ ์์ต๋๋ค.", None
|
713 |
+
|
714 |
+
try:
|
715 |
+
message, parquet_content, parquet_filename = text_to_parquet(processed_text)
|
716 |
+
if parquet_filename:
|
717 |
+
return message, parquet_filename
|
718 |
+
return message, None
|
719 |
+
except Exception as e:
|
720 |
+
return f"Parquet ๋ณํ ์ค ์ค๋ฅ ๋ฐ๏ฟฝ๏ฟฝ๏ฟฝ: {str(e)}", None
|
721 |
+
|
722 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
|
723 |
+
preprocess_button.click(
|
724 |
+
handle_text_preprocessing,
|
725 |
+
inputs=[raw_text_input],
|
726 |
+
outputs=[preprocess_status, processed_text_output],
|
727 |
+
queue=True
|
728 |
+
)
|
729 |
+
|
730 |
+
clear_button.click(
|
731 |
+
clear_inputs,
|
732 |
+
outputs=[raw_text_input, preprocess_status, processed_text_output]
|
733 |
+
)
|
734 |
+
|
735 |
+
convert_to_parquet_button.click(
|
736 |
+
convert_to_parquet_file,
|
737 |
+
inputs=[processed_text_output],
|
738 |
+
outputs=[preprocess_status, download_parquet]
|
739 |
+
)
|
740 |
+
|
741 |
+
# ์์ ํ
์คํธ ์ถ๊ฐ
|
742 |
+
with gr.Accordion("์์ ํ
์คํธ", open=False):
|
743 |
+
gr.Examples(
|
744 |
+
examples=[
|
745 |
+
["์ด์์ ์ ์กฐ์ ์ค๊ธฐ์ ๋ฌด์ ์ด๋ค. ๊ทธ๋ ์์ง์๋ ๋น์ ํด๊ตฐ์ ์ด๋์๋ค. ๊ฑฐ๋ถ์ ์ ๋ง๋ค์ด ์๊ตฐ๊ณผ ์ธ์ ๋ค."],
|
746 |
+
["์ธ๊ณต์ง๋ฅ์ ์ปดํจํฐ ๊ณผํ์ ํ ๋ถ์ผ์ด๋ค. ๊ธฐ๊ณํ์ต์ ์ธ๊ณต์ง๋ฅ์ ํ์ ๋ถ์ผ์ด๋ค. ๋ฅ๋ฌ๋์ ๊ธฐ๊ณํ์ต์ ํ ๋ฐฉ๋ฒ์ด๋ค."]
|
747 |
+
],
|
748 |
+
inputs=raw_text_input,
|
749 |
+
label="์์ ์ ํ"
|
750 |
+
)
|
751 |
+
|
752 |
+
with gr.Tab("๐ ์ฌ์ฉ ๋ฐฉ๋ฒ"):
|
753 |
+
gr.Markdown("""
|
754 |
+
# MyEzRAG ์ฌ์ฉ ๊ฐ์ด๋
|
755 |
+
|
756 |
+
## 1๏ธโฃ My ๋ฐ์ดํฐ์
+LLM ํญ
|
757 |
+
![Tab1](https://your-image-url.com/tab1.png)
|
758 |
+
### ๊ธฐ๋ฅ
|
759 |
+
- ์
๋ก๋๋ Parquet ๋ฐ์ดํฐ์
์ ๊ธฐ๋ฐ์ผ๋ก LLM๊ณผ ๋ํ
|
760 |
+
- ๋ฐ์ดํฐ์
์ ๋ด์ฉ์ ํ์ฉํ ์ฝํ
์ธ ์์ฑ
|
761 |
+
|
762 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ
|
763 |
+
1. Parquet ํ์ผ ์
๋ก๋ ์น์
์์ ๋ฐ์ดํฐ์
ํ์ผ์ ์
๋ก๋
|
764 |
+
2. ์ฑํ
์ฐฝ์ ์ํ๋ ์ง๋ฌธ์ด๋ ์์ฒญ์ฌํญ ์
๋ ฅ
|
765 |
+
3. ์์ ๋ฒํผ์ ํ์ฉํ์ฌ ๋ค์ํ ํ์ฉ ์ฌ๋ก ์ฒดํ
|
766 |
+
|
767 |
+
### ํ
|
768 |
+
- ์์คํ
ํ๋กฌํํธ ์ค์ ์ผ๋ก ์๋ต ์คํ์ผ ์กฐ์ ๊ฐ๋ฅ
|
769 |
+
- ์์ธํ ์ง๋ฌธ์ผ์๋ก ๋ ์ ํํ ๋ต๋ณ ์ ๊ณต
|
770 |
+
|
771 |
+
---
|
772 |
+
|
773 |
+
## 2๏ธโฃ CSV to My ๋ฐ์ดํฐ์
ํญ
|
774 |
+
![Tab2](https://your-image-url.com/tab2.png)
|
775 |
+
### ๊ธฐ๋ฅ
|
776 |
+
- CSV ํ์ผ์ Parquet ํ์์ผ๋ก ๋ณํ
|
777 |
+
- ๋ฐ์ดํฐ ์ต์ ํ ๋ฐ ์ ์
|
778 |
+
|
779 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ
|
780 |
+
1. CSV ํ์ผ ์ค๋น (ํ์ ์ปฌ๋ผ: id, text, label, metadata)
|
781 |
+
2. ํ์ผ ์
๋ก๋ ํ '์
๋ก๋ ๋ฐ ๋ณํ' ๋ฒํผ ํด๋ฆญ
|
782 |
+
3. ๋ณํ๋ Parquet ํ์ผ ๋ค์ด๋ก๋
|
783 |
+
|
784 |
+
### ์ฃผ์์ฌํญ
|
785 |
+
- CSV ํ์ผ์ ๋ฐ๋์ ํ์ ์ปฌ๋ผ์ ํฌํจํด์ผ ํจ
|
786 |
+
- ์ธ์ฝ๋ฉ์ UTF-8 ๊ถ์ฅ
|
787 |
+
|
788 |
+
---
|
789 |
+
|
790 |
+
## 3๏ธโฃ Text to My ๋ฐ์ดํฐ์
ํญ
|
791 |
+
![Tab3](https://your-image-url.com/tab3.png)
|
792 |
+
### ๊ธฐ๋ฅ
|
793 |
+
- ํ
์คํธ ํ์์ ๋ฐ์ดํฐ๋ฅผ Parquet์ผ๋ก ๋ณํ
|
794 |
+
- ์๋ ๋ฐ์ดํฐ ์
๋ ฅ ์ง์
|
795 |
+
|
796 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ
|
797 |
+
1. ์ง์ ๋ ํ์์ผ๋ก ํ
์คํธ ์
๋ ฅ
|
798 |
+
```
|
799 |
+
1,"์ด์์ ","์ฅ๊ตฐ","๊ฑฐ๋ถ์ "
|
800 |
+
2,"์๊ท ","์ฅ๊ตฐ","๋ชจํจ"
|
801 |
+
```
|
802 |
+
2. '๋ณํ ๋ฐ ๋ค์ด๋ก๋' ๋ฒํผ ํด๋ฆญ
|
803 |
+
3. ๋ณํ๋ ํ์ผ ํ์ธ ๋ฐ ๋ค์ด๋ก๋
|
804 |
+
|
805 |
+
### ์
๋ ฅ ํ์
|
806 |
+
- id: ์์ฐจ์ ๋ฒํธ
|
807 |
+
- text: ์ค์ ํ
์คํธ ๋ด์ฉ
|
808 |
+
- label: ๋ถ๋ฅ ๋ผ๋ฒจ
|
809 |
+
- metadata: ๋ถ๊ฐ ์ ๋ณด
|
810 |
+
|
811 |
+
---
|
812 |
+
|
813 |
+
## 4๏ธโฃ Text Preprocessing with LLM ํญ
|
814 |
+
![Tab4](https://your-image-url.com/tab4.png)
|
815 |
+
### ๊ธฐ๋ฅ
|
816 |
+
- LLM์ ํ์ฉํ ์๋ ํ
์คํธ ์ ์ฒ๋ฆฌ
|
817 |
+
- ๊ตฌ์กฐํ๋ ๋ฐ์ดํฐ์
์์ฑ
|
818 |
+
|
819 |
+
### ์ฌ์ฉ ๋ฐฉ๋ฒ
|
820 |
+
1. ์๋ฌธ ํ
์คํธ ์
๋ ฅ
|
821 |
+
2. '์ ์ฒ๋ฆฌ ์คํ' ๋ฒํผ ํด๋ฆญ
|
822 |
+
3. ๊ฒฐ๊ณผ ํ์ธ ํ ํ์์ Parquet ๋ณํ
|
823 |
+
|
824 |
+
### ํน์ง
|
825 |
+
- ์๋ ๋ ์ด๋ธ๋ง
|
826 |
+
- ๋ฌธ์ฅ ๋จ์ ๋ถ๋ฆฌ
|
827 |
+
- ์ค๋ณต ์ ๊ฑฐ
|
828 |
+
- ๋ฐ์ดํฐ ์ ๊ทํ
|
829 |
+
|
830 |
+
## ๐ก ์ผ๋ฐ์ ์ธ ํ
|
831 |
+
- ๊ฐ ํญ์ ์์ ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ฌ์ฉ๋ฒ ์ตํ๊ธฐ
|
832 |
+
- ๋ฐ์ดํฐ ํ์ง์ด ์ข์์๋ก ๋ ๋์ ๊ฒฐ๊ณผ ์ ๊ณต
|
833 |
+
- ์ค๋ฅ ๋ฐ์ ์ ์
๋ ฅ ๋ฐ์ดํฐ ํ์ ํ์ธ
|
834 |
+
- ๋์ฉ๋ ์ฒ๋ฆฌ ์ ์ ์ ํ ์ฒญํฌ ํฌ๊ธฐ๋ก ๋ถํ ์ฒ๋ฆฌ
|
835 |
+
|
836 |
+
## โ ๏ธ ์ฃผ์์ฌํญ
|
837 |
+
- ๋ฏผ๊ฐํ ๊ฐ์ธ์ ๋ณด ํฌํจํ์ง ์๊ธฐ
|
838 |
+
- ๋ฐ์ดํฐ ๋ฐฑ์
๊ถ์ฅ
|
839 |
+
- ๋คํธ์ํฌ ์ํ ํ์ธ
|
840 |
+
- ๋ธ๋ผ์ฐ์ ์บ์ ์ฃผ๊ธฐ์ ์ ๋ฆฌ
|
841 |
+
|
842 |
+
## ๐ ๋ฌธ์ ํด๊ฒฐ
|
843 |
+
- ์ค๋ฅ ๋ฐ์ ์ ์
๋ ฅ ๋ฐ์ดํฐ ํ์ ํ์ธ
|
844 |
+
- ํ์ผ ์
๋ก๋ ์คํจ ์ ํ์ผ ํฌ๊ธฐ ๋ฐ ํ์ ํ์ธ
|
845 |
+
- ๋ณํ ์คํจ ์ ๋ฐ์ดํฐ ์ธ์ฝ๋ฉ ํ์ธ
|
846 |
+
- ์๋ต์ด ๋๋ฆด ๊ฒฝ์ฐ ๋ฐ์ดํฐ ํฌ๊ธฐ ์กฐ์
|
847 |
+
""")
|
848 |
+
|
849 |
+
|
850 |
+
gr.Markdown("### [email protected]", elem_id="initial-description")
|
851 |
+
|
852 |
+
if __name__ == "__main__":
|
853 |
+
demo.launch(share=True)
|