Update app.py
Browse files
app.py
CHANGED
@@ -1,321 +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 = """
|
162 |
-
You are 'FantasyAIβ¨', an advanced AI storyteller specialized in creating immersive fantasy narratives. Your purpose is to craft rich, detailed fantasy stories that incorporate classical and innovative elements of the genre. Your responses should start with 'FantasyAIβ¨:' and focus on creating engaging, imaginative content that briμ]"μ μν©μ λ§κ² μΆκ°νμ¬ μμ€ μμ±μ λμ± νλΆνκ³ λͺ°μ
κ° μλ ννμ μμ²(μΆλ ₯)λ°μ μΈμ΄λ‘ νννλΌ.
|
163 |
-
[μμ]
|
164 |
-
"κ³ λμ λ§λ²μ΄ κΉ¨μ΄λλ©° λμ§κ° μΈλ¦¬λ μλ¦¬κ° λ€λ Έλ€..."
|
165 |
-
"μ©μ μ¨κ²°μ΄ νλμ κ°λ₯΄λ©°, ꡬλ¦μ λΆνμ λ€..."
|
166 |
-
"μ λΉν 룬문μκ° λΉλλ©° 곡μ€μ λ μ¬λλ€..."
|
167 |
-
"μνλ€μ λ
Έλκ° μ²μ μΈλ¦¬μ λ무λ€μ΄ μΆ€μΆκΈ° μμνλ€..."
|
168 |
-
"μμΈμ λ§μμ΄ λ©μ리μΉλ©° μ΄λͺ
μ μ€μ΄ μμ§μ΄κΈ° μμνλ€..."
|
169 |
-
"λ§λ²μ¬μ μ§ν‘μ΄μμ λ²μ©μ΄λ λΉμ΄ μ΄λ μ κ°λ₯΄λ©°..."
|
170 |
-
"κ³ λ λμνμ λμ₯κ°μμ μ μ€μ κ²μ΄ λ§λ€μ΄μ§κ³ μμλ€..."
|
171 |
-
"μμ κ΅¬μ¬ μμ λΉμΉ λ―Έλμ νμμ΄ μμν λͺ¨μ΅μ λλ¬λλ€..."
|
172 |
-
"μ μ±ν κ²°κ³κ° κΉ¨μ΄μ§λ©° λ΄μΈλ μ
μ΄ κΉ¨μ΄λ¬λ€..."
|
173 |
-
"μμ
μ λ°κ±Έμμ΄ μ΄λͺ
μ κΈΈμ λ°λΌ μΈλ € νΌμ‘λ€..."
|
174 |
-
|
175 |
-
"""
|
176 |
-
|
177 |
-
try:
|
178 |
-
# νμΌ μ
λ‘λ μ²λ¦¬
|
179 |
-
if uploaded_file:
|
180 |
-
content, file_type = read_uploaded_file(uploaded_file)
|
181 |
-
if file_type == "error":
|
182 |
-
error_message = content
|
183 |
-
chat_history.add_conversation(message, error_message)
|
184 |
-
return "", history + [[message, error_message]]
|
185 |
-
|
186 |
-
file_summary = analyze_file_content(content, file_type)
|
187 |
-
|
188 |
-
if file_type in ['parquet', 'csv']:
|
189 |
-
system_message += f"\n\nνμΌ λ΄μ©:\n```markdown\n{content}\n```"
|
190 |
-
else:
|
191 |
-
system_message += f"\n\nνμΌ λ΄μ©:\n```\n{content}\n```"
|
192 |
-
|
193 |
-
if message == "νμΌ λΆμμ μμν©λλ€...":
|
194 |
-
message = f"""[νμΌ κ΅¬μ‘° λΆμ] {file_summary}
|
195 |
-
λ€μ κ΄μ μμ λμμ λλ¦¬κ² μ΅λλ€:
|
196 |
-
1. π μ λ°μ μΈ λ΄μ© νμ
|
197 |
-
2. π‘ μ£Όμ νΉμ§ μ€λͺ
|
198 |
-
3. π― μ€μ©μ μΈ νμ© λ°©μ
|
199 |
-
4. β¨ κ°μ μ μ
|
200 |
-
5. π¬ μΆκ° μ§λ¬Έμ΄λ νμν μ€λͺ
"""
|
201 |
-
|
202 |
-
# λ©μμ§ μ²λ¦¬
|
203 |
-
messages = [{"role": "system", "content": system_prefix + system_message}]
|
204 |
-
|
205 |
-
# μ΄μ λν νμ€ν 리 μΆκ°
|
206 |
-
if history:
|
207 |
-
for user_msg, assistant_msg in history:
|
208 |
-
messages.append({"role": "user", "content": user_msg})
|
209 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
210 |
-
|
211 |
-
messages.append({"role": "user", "content": message})
|
212 |
-
|
213 |
-
# API νΈμΆ λ° μλ΅ μ²λ¦¬
|
214 |
-
client = get_client()
|
215 |
-
partial_message = ""
|
216 |
-
|
217 |
-
for msg in client.chat_completion(
|
218 |
-
messages,
|
219 |
-
max_tokens=max_tokens,
|
220 |
-
stream=True,
|
221 |
-
temperature=temperature,
|
222 |
-
top_p=top_p,
|
223 |
-
):
|
224 |
-
token = msg.choices[0].delta.get('content', None)
|
225 |
-
if token:
|
226 |
-
partial_message += token
|
227 |
-
current_history = history + [[message, partial_message]]
|
228 |
-
yield "", current_history
|
229 |
-
|
230 |
-
# μμ±λ λν μ μ₯
|
231 |
-
chat_history.add_conversation(message, partial_message)
|
232 |
-
|
233 |
-
except Exception as e:
|
234 |
-
error_msg = f"β μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
235 |
-
chat_history.add_conversation(message, error_msg)
|
236 |
-
yield "", history + [[message, error_msg]]
|
237 |
-
|
238 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo:
|
239 |
-
# κΈ°μ‘΄ νμ€ν 리 λ‘λ
|
240 |
-
initial_history = chat_history.format_for_display()
|
241 |
-
with gr.Row():
|
242 |
-
with gr.Column(scale=2):
|
243 |
-
chatbot = gr.Chatbot(
|
244 |
-
value=initial_history, # μ μ₯λ νμ€ν λ¦¬λ‘ μ΄κΈ°ν
|
245 |
-
height=600,
|
246 |
-
label="λνμ°½ π¬",
|
247 |
-
show_label=True
|
248 |
-
)
|
249 |
-
|
250 |
-
|
251 |
-
msg = gr.Textbox(
|
252 |
-
label="λ©μμ§ μ
λ ₯",
|
253 |
-
show_label=False,
|
254 |
-
placeholder="무μμ΄λ λ¬Όμ΄λ³΄μΈμ... π",
|
255 |
-
container=False
|
256 |
-
)
|
257 |
-
with gr.Row():
|
258 |
-
clear = gr.ClearButton([msg, chatbot], value="λνλ΄μ© μ§μ°κΈ°")
|
259 |
-
send = gr.Button("보λ΄κΈ° π€")
|
260 |
-
|
261 |
-
with gr.Column(scale=1):
|
262 |
-
gr.Markdown("### GiniGEN π€ [νμΌ μ
λ‘λ] π\nμ§μ νμ: ν
μ€νΈ, μ½λ, CSV, Parquet νμΌ")
|
263 |
-
file_upload = gr.File(
|
264 |
-
label="νμΌ μ ν",
|
265 |
-
file_types=["text", ".csv", ".parquet"],
|
266 |
-
type="filepath"
|
267 |
-
)
|
268 |
-
|
269 |
-
with gr.Accordion("κ³ κΈ μ€μ βοΈ", open=False):
|
270 |
-
system_message = gr.Textbox(label="μμ€ν
λ©μμ§ π", value="")
|
271 |
-
max_tokens = gr.Slider(minimum=1, maximum=8000, value=4000, label="μ΅λ ν ν° μ π")
|
272 |
-
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="μ°½μμ± μμ€ π‘οΈ")
|
273 |
-
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="μλ΅ λ€μμ± π")
|
274 |
-
|
275 |
-
# μμ μ§λ¬Έ
|
276 |
-
gr.Examples(
|
277 |
-
examples=[
|
278 |
-
["ν₯λ―Έλ‘μ΄ μμ¬ 10κ°μ§λ₯Ό μ μν΄μ€μ π€"],
|
279 |
-
["λμ± νμμ μ΄κ³ μμΈν λ¬μ¬λ₯Ό μμΈνν΄μ€μ π"],
|
280 |
-
["μ΄μΈκ²(λ€λ₯Έ μ°¨μμ μΈμ) λ°°κ²½μΌλ‘ ν΄μ€μ π―"],
|
281 |
-
["μμ§μλ μλλ‘ νμμν μμ¬λ‘ μμ±μ± β¨"],
|
282 |
-
["κ³μ μ΄μ΄μ μμ±ν΄μ€ π€"],
|
283 |
-
],
|
284 |
-
inputs=msg,
|
285 |
-
)
|
286 |
-
|
287 |
-
# λνλ΄μ© μ§μ°κΈ° λ²νΌμ νμ€ν 리 μ΄κΈ°ν κΈ°λ₯ μΆκ°
|
288 |
-
def clear_chat():
|
289 |
-
chat_history.clear_history()
|
290 |
-
return None, None
|
291 |
-
|
292 |
-
# μ΄λ²€νΈ λ°μΈλ©
|
293 |
-
msg.submit(
|
294 |
-
chat,
|
295 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
296 |
-
outputs=[msg, chatbot]
|
297 |
-
)
|
298 |
-
|
299 |
-
send.click(
|
300 |
-
chat,
|
301 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
302 |
-
outputs=[msg, chatbot]
|
303 |
-
)
|
304 |
-
|
305 |
-
clear.click(
|
306 |
-
clear_chat,
|
307 |
-
outputs=[msg, chatbot]
|
308 |
-
)
|
309 |
-
|
310 |
-
# νμΌ μ
λ‘λμ μλ λΆμ
|
311 |
-
file_upload.change(
|
312 |
-
lambda: "νμΌ λΆμμ μμν©λλ€...",
|
313 |
-
outputs=msg
|
314 |
-
).then(
|
315 |
-
chat,
|
316 |
-
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
317 |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