Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,315 +1,2 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
import gradio as gr
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
-
import pandas as pd
|
6 |
-
from typing import List, Tuple
|
7 |
-
import json
|
8 |
-
from datetime import datetime
|
9 |
-
|
10 |
-
# ํ๊ฒฝ ๋ณ์ ์ค์
|
11 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
12 |
-
|
13 |
-
# LLM Models Definition
|
14 |
-
LLM_MODELS = {
|
15 |
-
"Cohere c4ai-crp-08-2024": "CohereForAI/c4ai-command-r-plus-08-2024", # Default
|
16 |
-
"Meta Llama3.3-70B": "meta-llama/Llama-3.3-70B-Instruct" # Backup model
|
17 |
-
}
|
18 |
-
|
19 |
-
class ChatHistory:
|
20 |
-
def __init__(self):
|
21 |
-
self.history = []
|
22 |
-
self.history_file = "/tmp/chat_history.json"
|
23 |
-
self.load_history()
|
24 |
-
|
25 |
-
def add_conversation(self, user_msg: str, assistant_msg: str):
|
26 |
-
conversation = {
|
27 |
-
"timestamp": datetime.now().isoformat(),
|
28 |
-
"messages": [
|
29 |
-
{"role": "user", "content": user_msg},
|
30 |
-
{"role": "assistant", "content": assistant_msg}
|
31 |
-
]
|
32 |
-
}
|
33 |
-
self.history.append(conversation)
|
34 |
-
self.save_history()
|
35 |
-
|
36 |
-
def format_for_display(self):
|
37 |
-
# Gradio Chatbot ์ปดํฌ๋ํธ์ ๋ง๋ ํ์์ผ๋ก ๋ณํ
|
38 |
-
formatted = []
|
39 |
-
for conv in self.history:
|
40 |
-
formatted.append([
|
41 |
-
conv["messages"][0]["content"], # user message
|
42 |
-
conv["messages"][1]["content"] # assistant message
|
43 |
-
])
|
44 |
-
return formatted
|
45 |
-
|
46 |
-
def get_messages_for_api(self):
|
47 |
-
# API ํธ์ถ์ ์ํ ๋ฉ์์ง ํ์
|
48 |
-
messages = []
|
49 |
-
for conv in self.history:
|
50 |
-
messages.extend([
|
51 |
-
{"role": "user", "content": conv["messages"][0]["content"]},
|
52 |
-
{"role": "assistant", "content": conv["messages"][1]["content"]}
|
53 |
-
])
|
54 |
-
return messages
|
55 |
-
|
56 |
-
def clear_history(self):
|
57 |
-
self.history = []
|
58 |
-
self.save_history()
|
59 |
-
|
60 |
-
def save_history(self):
|
61 |
-
try:
|
62 |
-
with open(self.history_file, 'w', encoding='utf-8') as f:
|
63 |
-
json.dump(self.history, f, ensure_ascii=False, indent=2)
|
64 |
-
except Exception as e:
|
65 |
-
print(f"ํ์คํ ๋ฆฌ ์ ์ฅ ์คํจ: {e}")
|
66 |
-
|
67 |
-
def load_history(self):
|
68 |
-
try:
|
69 |
-
if os.path.exists(self.history_file):
|
70 |
-
with open(self.history_file, 'r', encoding='utf-8') as f:
|
71 |
-
self.history = json.load(f)
|
72 |
-
except Exception as e:
|
73 |
-
print(f"ํ์คํ ๋ฆฌ ๋ก๋ ์คํจ: {e}")
|
74 |
-
self.history = []
|
75 |
-
|
76 |
-
|
77 |
-
# ์ ์ญ ChatHistory ์ธ์คํด์ค ์์ฑ
|
78 |
-
chat_history = ChatHistory()
|
79 |
-
|
80 |
-
def get_client(model_name="Cohere c4ai-crp-08-2024"):
|
81 |
-
try:
|
82 |
-
return InferenceClient(LLM_MODELS[model_name], token=HF_TOKEN)
|
83 |
-
except Exception:
|
84 |
-
return InferenceClient(LLM_MODELS["Meta Llama3.3-70B"], token=HF_TOKEN)
|
85 |
-
|
86 |
-
def analyze_file_content(content, file_type):
|
87 |
-
"""Analyze file content and return structural summary"""
|
88 |
-
if file_type in ['parquet', 'csv']:
|
89 |
-
try:
|
90 |
-
lines = content.split('\n')
|
91 |
-
header = lines[0]
|
92 |
-
columns = header.count('|') - 1
|
93 |
-
rows = len(lines) - 3
|
94 |
-
return f"๐ ๋ฐ์ดํฐ์
๊ตฌ์กฐ: {columns}๊ฐ ์ปฌ๋ผ, {rows}๊ฐ ๋ฐ์ดํฐ"
|
95 |
-
except:
|
96 |
-
return "โ ๋ฐ์ดํฐ์
๊ตฌ์กฐ ๋ถ์ ์คํจ"
|
97 |
-
|
98 |
-
lines = content.split('\n')
|
99 |
-
total_lines = len(lines)
|
100 |
-
non_empty_lines = len([line for line in lines if line.strip()])
|
101 |
-
|
102 |
-
if any(keyword in content.lower() for keyword in ['def ', 'class ', 'import ', 'function']):
|
103 |
-
functions = len([line for line in lines if 'def ' in line])
|
104 |
-
classes = len([line for line in lines if 'class ' in line])
|
105 |
-
imports = len([line for line in lines if 'import ' in line or 'from ' in line])
|
106 |
-
return f"๐ป ์ฝ๋ ๊ตฌ์กฐ: {total_lines}์ค (ํจ์: {functions}, ํด๋์ค: {classes}, ์ํฌํธ: {imports})"
|
107 |
-
|
108 |
-
paragraphs = content.count('\n\n') + 1
|
109 |
-
words = len(content.split())
|
110 |
-
return f"๐ ๋ฌธ์ ๊ตฌ์กฐ: {total_lines}์ค, {paragraphs}๋จ๋ฝ, ์ฝ {words}๋จ์ด"
|
111 |
-
|
112 |
-
def read_uploaded_file(file):
|
113 |
-
if file is None:
|
114 |
-
return "", ""
|
115 |
-
try:
|
116 |
-
file_ext = os.path.splitext(file.name)[1].lower()
|
117 |
-
|
118 |
-
if file_ext == '.parquet':
|
119 |
-
df = pd.read_parquet(file.name, engine='pyarrow')
|
120 |
-
content = df.head(10).to_markdown(index=False)
|
121 |
-
return content, "parquet"
|
122 |
-
elif file_ext == '.csv':
|
123 |
-
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
124 |
-
for encoding in encodings:
|
125 |
-
try:
|
126 |
-
df = pd.read_csv(file.name, encoding=encoding)
|
127 |
-
content = f"๐ ๋ฐ์ดํฐ ๋ฏธ๋ฆฌ๋ณด๊ธฐ:\n{df.head(10).to_markdown(index=False)}\n\n"
|
128 |
-
content += f"\n๐ ๋ฐ์ดํฐ ์ ๋ณด:\n"
|
129 |
-
content += f"- ์ ์ฒด ํ ์: {len(df)}\n"
|
130 |
-
content += f"- ์ ์ฒด ์ด ์: {len(df.columns)}\n"
|
131 |
-
content += f"- ์ปฌ๋ผ ๋ชฉ๋ก: {', '.join(df.columns)}\n"
|
132 |
-
content += f"\n๐ ์ปฌ๋ผ ๋ฐ์ดํฐ ํ์
:\n"
|
133 |
-
for col, dtype in df.dtypes.items():
|
134 |
-
content += f"- {col}: {dtype}\n"
|
135 |
-
null_counts = df.isnull().sum()
|
136 |
-
if null_counts.any():
|
137 |
-
content += f"\nโ ๏ธ ๊ฒฐ์ธก์น:\n"
|
138 |
-
for col, null_count in null_counts[null_counts > 0].items():
|
139 |
-
content += f"- {col}: {null_count}๊ฐ ๋๋ฝ\n"
|
140 |
-
return content, "csv"
|
141 |
-
except UnicodeDecodeError:
|
142 |
-
continue
|
143 |
-
raise UnicodeDecodeError(f"โ ์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
144 |
-
else:
|
145 |
-
encodings = ['utf-8', 'cp949', 'euc-kr', 'latin1']
|
146 |
-
for encoding in encodings:
|
147 |
-
try:
|
148 |
-
with open(file.name, 'r', encoding=encoding) as f:
|
149 |
-
content = f.read()
|
150 |
-
return content, "text"
|
151 |
-
except UnicodeDecodeError:
|
152 |
-
continue
|
153 |
-
raise UnicodeDecodeError(f"โ ์ง์๋๋ ์ธ์ฝ๋ฉ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค ({', '.join(encodings)})")
|
154 |
-
except Exception as e:
|
155 |
-
return f"โ ํ์ผ ์ฝ๊ธฐ ์ค๋ฅ: {str(e)}", "error"
|
156 |
-
|
157 |
-
def chat(message, history, uploaded_file, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9):
|
158 |
-
if not message:
|
159 |
-
return "", history
|
160 |
-
|
161 |
-
system_prefix = """์ ๋ ์ฌ๋ฌ๋ถ์ ์น๊ทผํ๊ณ ์ง์ ์ธ AI ์ด์์คํดํธ 'GiniGEN'์
๋๋ค.. ๋ค์๊ณผ ๊ฐ์ ์์น์ผ๋ก ์ํตํ๊ฒ ์ต๋๋ค:
|
162 |
-
1. ๐ค ์น๊ทผํ๊ณ ๊ณต๊ฐ์ ์ธ ํ๋๋ก ๋ํ
|
163 |
-
2. ๐ก ๋ช
ํํ๊ณ ์ดํดํ๊ธฐ ์ฌ์ด ์ค๋ช
์ ๊ณต
|
164 |
-
3. ๐ฏ ์ง๋ฌธ์ ์๋๋ฅผ ์ ํํ ํ์
ํ์ฌ ๋ง์ถคํ ๋ต๋ณ
|
165 |
-
4. ๐ ํ์ํ ๊ฒฝ์ฐ ์
๋ก๋๋ ํ์ผ ๋ด์ฉ์ ์ฐธ๊ณ ํ์ฌ ๊ตฌ์ฒด์ ์ธ ๋์ ์ ๊ณต
|
166 |
-
5. โจ ์ถ๊ฐ์ ์ธ ํต์ฐฐ๊ณผ ์ ์์ ํตํ ๊ฐ์น ์๋ ๋ํ
|
167 |
-
|
168 |
-
ํญ์ ์์ ๋ฐ๋ฅด๊ณ ์น์ ํ๊ฒ ์๋ตํ๋ฉฐ, ํ์ํ ๊ฒฝ์ฐ ๊ตฌ์ฒด์ ์ธ ์์๋ ์ค๋ช
์ ์ถ๊ฐํ์ฌ
|
169 |
-
์ดํด๋ฅผ ๋๊ฒ ์ต๋๋ค."""
|
170 |
-
|
171 |
-
try:
|
172 |
-
# ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ
|
173 |
-
if uploaded_file:
|
174 |
-
content, file_type = read_uploaded_file(uploaded_file)
|
175 |
-
if file_type == "error":
|
176 |
-
error_message = content
|
177 |
-
chat_history.add_conversation(message, error_message)
|
178 |
-
return "", history + [[message, error_message]]
|
179 |
-
|
180 |
-
file_summary = analyze_file_content(content, file_type)
|
181 |
-
|
182 |
-
if file_type in ['parquet', 'csv']:
|
183 |
-
system_message += f"\n\nํ์ผ ๋ด์ฉ:\n```markdown\n{content}\n```"
|
184 |
-
else:
|
185 |
-
system_message += f"\n\nํ์ผ ๋ด์ฉ:\n```\n{content}\n```"
|
186 |
-
|
187 |
-
if message == "ํ์ผ ๋ถ์์ ์์ํฉ๋๋ค...":
|
188 |
-
message = f"""[ํ์ผ ๊ตฌ์กฐ ๋ถ์] {file_summary}
|
189 |
-
๋ค์ ๊ด์ ์์ ๋์์ ๋๋ฆฌ๊ฒ ์ต๋๋ค:
|
190 |
-
1. ๐ ์ ๋ฐ์ ์ธ ๋ด์ฉ ํ์
|
191 |
-
2. ๐ก ์ฃผ์ ํน์ง ์ค๋ช
|
192 |
-
3. ๐ฏ ์ค์ฉ์ ์ธ ํ์ฉ ๋ฐฉ์
|
193 |
-
4. โจ ๊ฐ์ ์ ์
|
194 |
-
5. ๐ฌ ์ถ๊ฐ ์ง๋ฌธ์ด๋ ํ์ํ ์ค๋ช
"""
|
195 |
-
|
196 |
-
# ๋ฉ์์ง ์ฒ๋ฆฌ
|
197 |
-
messages = [{"role": "system", "content": system_prefix + system_message}]
|
198 |
-
|
199 |
-
# ์ด์ ๋ํ ํ์คํ ๋ฆฌ ์ถ๊ฐ
|
200 |
-
if history:
|
201 |
-
for user_msg, assistant_msg in history:
|
202 |
-
messages.append({"role": "user", "content": user_msg})
|
203 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
204 |
-
|
205 |
-
messages.append({"role": "user", "content": message})
|
206 |
-
|
207 |
-
# API ํธ์ถ ๋ฐ ์๋ต ์ฒ๋ฆฌ
|
208 |
-
client = get_client()
|
209 |
-
partial_message = ""
|
210 |
-
|
211 |
-
for msg in client.chat_completion(
|
212 |
-
messages,
|
213 |
-
max_tokens=max_tokens,
|
214 |
-
stream=True,
|
215 |
-
temperature=temperature,
|
216 |
-
top_p=top_p,
|
217 |
-
):
|
218 |
-
token = msg.choices[0].delta.get('content', None)
|
219 |
-
if token:
|
220 |
-
partial_message += token
|
221 |
-
current_history = history + [[message, partial_message]]
|
222 |
-
yield "", current_history
|
223 |
-
|
224 |
-
# ์์ฑ๋ ๋ํ ์ ์ฅ
|
225 |
-
chat_history.add_conversation(message, partial_message)
|
226 |
-
|
227 |
-
except Exception as e:
|
228 |
-
error_msg = f"โ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
229 |
-
chat_history.add_conversation(message, error_msg)
|
230 |
-
yield "", history + [[message, error_msg]]
|
231 |
-
|
232 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN ๐ค") as demo:
|
233 |
-
# ๊ธฐ์กด ํ์คํ ๋ฆฌ ๋ก๋
|
234 |
-
initial_history = chat_history.format_for_display()
|
235 |
-
with gr.Row():
|
236 |
-
with gr.Column(scale=2):
|
237 |
-
chatbot = gr.Chatbot(
|
238 |
-
value=initial_history, # ์ ์ฅ๋ ํ์คํ ๋ฆฌ๋ก ์ด๊ธฐํ
|
239 |
-
height=600,
|
240 |
-
label="๋ํ์ฐฝ ๐ฌ",
|
241 |
-
show_label=True
|
242 |
-
)
|
243 |
-
|
244 |
-
|
245 |
-
msg = gr.Textbox(
|
246 |
-
label="๋ฉ์์ง ์
๋ ฅ",
|
247 |
-
show_label=False,
|
248 |
-
placeholder="๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์... ๐ญ",
|
249 |
-
container=False
|
250 |
-
)
|
251 |
-
with gr.Row():
|
252 |
-
clear = gr.ClearButton([msg, chatbot], value="๋ํ๋ด์ฉ ์ง์ฐ๊ธฐ")
|
253 |
-
send = gr.Button("๋ณด๋ด๊ธฐ ๐ค")
|
254 |
-
|
255 |
-
with gr.Column(scale=1):
|
256 |
-
gr.Markdown("### GiniGEN ๐ค [ํ์ผ ์
๋ก๋] ๐\n์ง์ ํ์: ํ
์คํธ, ์ฝ๋, CSV, Parquet ํ์ผ")
|
257 |
-
file_upload = gr.File(
|
258 |
-
label="ํ์ผ ์ ํ",
|
259 |
-
file_types=["text", ".csv", ".parquet"],
|
260 |
-
type="filepath"
|
261 |
-
)
|
262 |
-
|
263 |
-
with gr.Accordion("๊ณ ๊ธ ์ค์ โ๏ธ", open=False):
|
264 |
-
system_message = gr.Textbox(label="์์คํ
๋ฉ์์ง ๐", value="")
|
265 |
-
max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="์ต๋ ํ ํฐ ์ ๐")
|
266 |
-
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="์ฐฝ์์ฑ ์์ค ๐ก๏ธ")
|
267 |
-
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="์๋ต ๋ค์์ฑ ๐")
|
268 |
-
|
269 |
-
# ์์ ์ง๋ฌธ
|
270 |
-
gr.Examples(
|
271 |
-
examples=[
|
272 |
-
["์๋
ํ์ธ์! ์ด๋ค ๋์์ด ํ์ํ์ ๊ฐ์? ๐ค"],
|
273 |
-
["์ ๊ฐ ์ดํดํ๊ธฐ ์ฝ๊ฒ ์ค๋ช
ํด ์ฃผ์๊ฒ ์ด์? ๐"],
|
274 |
-
["์ด ๋ด์ฉ์ ์ค์ ๋ก ์ด๋ป๊ฒ ํ์ฉํ ์ ์์๊น์? ๐ฏ"],
|
275 |
-
["์ถ๊ฐ๋ก ์กฐ์ธํด ์ฃผ์ค ๋ด์ฉ์ด ์์ผ์ ๊ฐ์? โจ"],
|
276 |
-
["๊ถ๊ธํ ์ ์ด ๋ ์๋๋ฐ ์ฌ์ญค๋ด๋ ๋ ๊น์? ๐ค"],
|
277 |
-
],
|
278 |
-
inputs=msg,
|
279 |
-
)
|
280 |
-
|
281 |
-
# ๋ํ๋ด์ฉ ์ง์ฐ๊ธฐ ๋ฒํผ์ ํ์คํ ๋ฆฌ ์ด๊ธฐํ ๊ธฐ๋ฅ ์ถ๊ฐ
|
282 |
-
def clear_chat():
|
283 |
-
chat_history.clear_history()
|
284 |
-
return None, None
|
285 |
-
|
286 |
-
# ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ
|
287 |
-
msg.submit(
|
288 |
-
chat,
|
289 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
290 |
-
outputs=[msg, chatbot]
|
291 |
-
)
|
292 |
-
|
293 |
-
send.click(
|
294 |
-
chat,
|
295 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
296 |
-
outputs=[msg, chatbot]
|
297 |
-
)
|
298 |
-
|
299 |
-
clear.click(
|
300 |
-
clear_chat,
|
301 |
-
outputs=[msg, chatbot]
|
302 |
-
)
|
303 |
-
|
304 |
-
# ํ์ผ ์
๋ก๋์ ์๋ ๋ถ์
|
305 |
-
file_upload.change(
|
306 |
-
lambda: "ํ์ผ ๋ถ์์ ์์ํฉ๋๋ค...",
|
307 |
-
outputs=msg
|
308 |
-
).then(
|
309 |
-
chat,
|
310 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
311 |
-
outputs=[msg, chatbot]
|
312 |
-
)
|
313 |
-
|
314 |
-
if __name__ == "__main__":
|
315 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|