Update app.py
Browse files
app.py
CHANGED
@@ -20,20 +20,30 @@ LLM_MODELS = {
|
|
20 |
class ChatHistory:
|
21 |
def __init__(self):
|
22 |
self.history = []
|
23 |
-
self.history_file = "/tmp/chat_history.json"
|
24 |
self.load_history()
|
25 |
|
26 |
-
def
|
27 |
-
|
28 |
-
"
|
29 |
-
"
|
30 |
-
|
|
|
|
|
31 |
}
|
32 |
-
self.history.append(
|
33 |
self.save_history()
|
34 |
|
35 |
-
def
|
36 |
-
return self.history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def clear_history(self):
|
39 |
self.history = []
|
@@ -135,15 +145,11 @@ def read_uploaded_file(file):
|
|
135 |
except Exception as e:
|
136 |
return f"β νμΌ μ½κΈ° μ€λ₯: {str(e)}", "error"
|
137 |
|
138 |
-
def format_history(history):
|
139 |
-
formatted_history = []
|
140 |
-
for user_msg, assistant_msg in history:
|
141 |
-
formatted_history.append({"role": "user", "content": user_msg})
|
142 |
-
if assistant_msg:
|
143 |
-
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
144 |
-
return formatted_history
|
145 |
|
146 |
def chat(message, history, uploaded_file, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9):
|
|
|
|
|
|
|
147 |
system_prefix = """μ λ μ¬λ¬λΆμ μΉκ·Όνκ³ μ§μ μΈ AI μ΄μμ€ν΄νΈ 'GiniGEN'μ
λλ€.. λ€μκ³Ό κ°μ μμΉμΌλ‘ μν΅νκ² μ΅λλ€:
|
148 |
|
149 |
1. π€ μΉκ·Όνκ³ κ³΅κ°μ μΈ νλλ‘ λν
|
@@ -155,26 +161,23 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
155 |
νμ μμ λ°λ₯΄κ³ μΉμ νκ² μλ΅νλ©°, νμν κ²½μ° κ΅¬μ²΄μ μΈ μμλ μ€λͺ
μ μΆκ°νμ¬
|
156 |
μ΄ν΄λ₯Ό λκ² μ΅λλ€."""
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
{"role": "assistant", "content": error_message}]
|
168 |
-
|
169 |
-
file_summary = analyze_file_content(content, file_type)
|
170 |
-
|
171 |
-
if file_type in ['parquet', 'csv']:
|
172 |
-
system_message += f"\n\nνμΌ λ΄μ©:\n```markdown\n{content}\n```"
|
173 |
-
else:
|
174 |
-
system_message += f"\n\nνμΌ λ΄μ©:\n```\n{content}\n```"
|
175 |
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
λ€μ κ΄μ μμ λμμ λλ¦¬κ² μ΅λλ€:
|
180 |
1. π μ λ°μ μΈ λ΄μ© νμ
|
@@ -183,24 +186,22 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
183 |
4. β¨ κ°μ μ μ
|
184 |
5. π¬ μΆκ° μ§λ¬Έμ΄λ νμν μ€λͺ
"""
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
messages.append({"role": "user", "content": message})
|
198 |
|
199 |
-
try:
|
200 |
client = get_client()
|
201 |
partial_message = ""
|
202 |
-
current_history = []
|
203 |
|
|
|
204 |
for msg in client.chat_completion(
|
205 |
messages,
|
206 |
max_tokens=max_tokens,
|
@@ -211,29 +212,26 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
211 |
token = msg.choices[0].delta.get('content', None)
|
212 |
if token:
|
213 |
partial_message += token
|
214 |
-
current_history = [
|
215 |
-
{"role": "user", "content": message},
|
216 |
-
{"role": "assistant", "content": partial_message}
|
217 |
-
]
|
218 |
yield "", current_history
|
|
|
|
|
|
|
219 |
|
220 |
-
# μμ±λ μλ΅ μ μ₯
|
221 |
-
chat_history.add_message("assistant", partial_message)
|
222 |
-
|
223 |
except Exception as e:
|
224 |
error_msg = f"β μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
225 |
-
chat_history.
|
226 |
-
|
227 |
-
{"role": "user", "content": message},
|
228 |
-
{"role": "assistant", "content": error_msg}
|
229 |
-
]
|
230 |
-
yield "", error_history
|
231 |
|
232 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo:
|
233 |
|
|
|
|
|
|
|
|
|
234 |
with gr.Row():
|
235 |
with gr.Column(scale=2):
|
236 |
chatbot = gr.Chatbot(
|
|
|
237 |
height=600,
|
238 |
label="λνμ°½ π¬",
|
239 |
show_label=True,
|
@@ -267,7 +265,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
267 |
gr.Examples(
|
268 |
examples=[
|
269 |
["μλ
νμΈμ! μ΄λ€ λμμ΄ νμνμ κ°μ? π€"],
|
270 |
-
["μ΄ λ΄μ©μ λν΄ μ’ λ μμΈν μ€λͺ
ν΄ μ£Όμ€ μ μλμ? π‘"],
|
271 |
["μ κ° μ΄ν΄νκΈ° μ½κ² μ€λͺ
ν΄ μ£Όμκ² μ΄μ? π"],
|
272 |
["μ΄ λ΄μ©μ μ€μ λ‘ μ΄λ»κ² νμ©ν μ μμκΉμ? π―"],
|
273 |
["μΆκ°λ‘ μ‘°μΈν΄ μ£Όμ€ λ΄μ©μ΄ μμΌμ κ°μ? β¨"],
|
@@ -310,4 +307,4 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
310 |
)
|
311 |
|
312 |
if __name__ == "__main__":
|
313 |
-
demo.launch()
|
|
|
20 |
class ChatHistory:
|
21 |
def __init__(self):
|
22 |
self.history = []
|
23 |
+
self.history_file = "/tmp/chat_history.json"
|
24 |
self.load_history()
|
25 |
|
26 |
+
def add_conversation(self, user_msg: str, assistant_msg: str):
|
27 |
+
conversation = {
|
28 |
+
"timestamp": datetime.now().isoformat(),
|
29 |
+
"conversation": [
|
30 |
+
{"role": "user", "content": user_msg},
|
31 |
+
{"role": "assistant", "content": assistant_msg}
|
32 |
+
]
|
33 |
}
|
34 |
+
self.history.append(conversation)
|
35 |
self.save_history()
|
36 |
|
37 |
+
def get_recent_conversations(self, limit=10):
|
38 |
+
return self.history[-limit:] if self.history else []
|
39 |
+
|
40 |
+
def format_for_display(self):
|
41 |
+
formatted = []
|
42 |
+
for conv in self.history:
|
43 |
+
formatted.extend([
|
44 |
+
[conv["conversation"][0]["content"], conv["conversation"][1]["content"]]
|
45 |
+
])
|
46 |
+
return formatted
|
47 |
|
48 |
def clear_history(self):
|
49 |
self.history = []
|
|
|
145 |
except Exception as e:
|
146 |
return f"β νμΌ μ½κΈ° μ€λ₯: {str(e)}", "error"
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
def chat(message, history, uploaded_file, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9):
|
150 |
+
if not message:
|
151 |
+
return "", history
|
152 |
+
|
153 |
system_prefix = """μ λ μ¬λ¬λΆμ μΉκ·Όνκ³ μ§μ μΈ AI μ΄μμ€ν΄νΈ 'GiniGEN'μ
λλ€.. λ€μκ³Ό κ°μ μμΉμΌλ‘ μν΅νκ² μ΅λλ€:
|
154 |
|
155 |
1. π€ μΉκ·Όνκ³ κ³΅κ°μ μΈ νλλ‘ λν
|
|
|
161 |
νμ μμ λ°λ₯΄κ³ μΉμ νκ² μλ΅νλ©°, νμν κ²½μ° κ΅¬μ²΄μ μΈ μμλ μ€λͺ
μ μΆκ°νμ¬
|
162 |
μ΄ν΄λ₯Ό λκ² μ΅λλ€."""
|
163 |
|
164 |
+
try:
|
165 |
+
if uploaded_file:
|
166 |
+
content, file_type = read_uploaded_file(uploaded_file)
|
167 |
+
if file_type == "error":
|
168 |
+
error_message = content
|
169 |
+
chat_history.add_conversation(message, error_message)
|
170 |
+
return "", history + [[message, error_message]]
|
171 |
+
|
172 |
+
file_summary = analyze_file_content(content, file_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
+
if file_type in ['parquet', 'csv']:
|
175 |
+
system_message += f"\n\nνμΌ λ΄μ©:\n```markdown\n{content}\n```"
|
176 |
+
else:
|
177 |
+
system_message += f"\n\nνμΌ λ΄μ©:\n```\n{content}\n```"
|
178 |
+
|
179 |
+
if message == "νμΌ λΆμμ μμν©λλ€...":
|
180 |
+
message = f"""[νμΌ κ΅¬μ‘° λΆμ] {file_summary}
|
181 |
|
182 |
λ€μ κ΄μ μμ λμμ λλ¦¬κ² μ΅λλ€:
|
183 |
1. π μ λ°μ μΈ λ΄μ© νμ
|
|
|
186 |
4. β¨ κ°μ μ μ
|
187 |
5. π¬ μΆκ° μ§λ¬Έμ΄λ νμν μ€λͺ
"""
|
188 |
|
189 |
+
# μμ€ν
λ©μμ§ λ° νμ€ν 리 μ€μ
|
190 |
+
messages = [{"role": "system", "content": system_prefix + system_message}]
|
191 |
+
|
192 |
+
# μ΄μ λν νμ€ν 리 μΆκ°
|
193 |
+
if history:
|
194 |
+
for h in history:
|
195 |
+
messages.append({"role": "user", "content": h[0]})
|
196 |
+
if h[1]:
|
197 |
+
messages.append({"role": "assistant", "content": h[1]})
|
198 |
+
|
199 |
+
messages.append({"role": "user", "content": message})
|
|
|
200 |
|
|
|
201 |
client = get_client()
|
202 |
partial_message = ""
|
|
|
203 |
|
204 |
+
# μ€νΈλ¦¬λ° μλ΅ μ²λ¦¬
|
205 |
for msg in client.chat_completion(
|
206 |
messages,
|
207 |
max_tokens=max_tokens,
|
|
|
212 |
token = msg.choices[0].delta.get('content', None)
|
213 |
if token:
|
214 |
partial_message += token
|
215 |
+
current_history = history + [[message, partial_message]]
|
|
|
|
|
|
|
216 |
yield "", current_history
|
217 |
+
|
218 |
+
# μμ±λ λν μ μ₯
|
219 |
+
chat_history.add_conversation(message, partial_message)
|
220 |
|
|
|
|
|
|
|
221 |
except Exception as e:
|
222 |
error_msg = f"β μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}"
|
223 |
+
chat_history.add_conversation(message, error_msg)
|
224 |
+
yield "", history + [[message, error_msg]]
|
|
|
|
|
|
|
|
|
225 |
|
|
|
226 |
|
227 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo:
|
228 |
+
# κΈ°μ‘΄ νμ€ν 리 λ‘λ
|
229 |
+
initial_history = chat_history.format_for_display()
|
230 |
+
|
231 |
with gr.Row():
|
232 |
with gr.Column(scale=2):
|
233 |
chatbot = gr.Chatbot(
|
234 |
+
value=initial_history, # μ΄κΈ° νμ€ν 리 μ€μ
|
235 |
height=600,
|
236 |
label="λνμ°½ π¬",
|
237 |
show_label=True,
|
|
|
265 |
gr.Examples(
|
266 |
examples=[
|
267 |
["μλ
νμΈμ! μ΄λ€ λμμ΄ νμνμ κ°μ? π€"],
|
|
|
268 |
["μ κ° μ΄ν΄νκΈ° μ½κ² μ€λͺ
ν΄ μ£Όμκ² μ΄μ? π"],
|
269 |
["μ΄ λ΄μ©μ μ€μ λ‘ μ΄λ»κ² νμ©ν μ μμκΉμ? π―"],
|
270 |
["μΆκ°λ‘ μ‘°μΈν΄ μ£Όμ€ λ΄μ©μ΄ μμΌμ κ°μ? β¨"],
|
|
|
307 |
)
|
308 |
|
309 |
if __name__ == "__main__":
|
310 |
+
demo.launch()
|