Spaces:
Running
on
Zero
Running
on
Zero
cutechicken
commited on
Commit
β’
1cfe513
1
Parent(s):
229bfa6
Update app.py
Browse files
app.py
CHANGED
@@ -6,15 +6,36 @@ 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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
class ChatHistory:
|
20 |
def __init__(self):
|
@@ -34,17 +55,15 @@ class ChatHistory:
|
|
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"],
|
42 |
-
conv["messages"][1]["content"]
|
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([
|
@@ -73,15 +92,12 @@ class ChatHistory:
|
|
73 |
print(f"νμ€ν 리 λ‘λ μ€ν¨: {e}")
|
74 |
self.history = []
|
75 |
|
76 |
-
|
77 |
-
# μ μ ChatHistory μΈμ€ν΄μ€ μμ±
|
78 |
chat_history = ChatHistory()
|
|
|
79 |
|
80 |
-
def get_client(
|
81 |
-
|
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"""
|
@@ -164,12 +180,10 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
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":
|
@@ -193,10 +207,8 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
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})
|
@@ -204,7 +216,6 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
204 |
|
205 |
messages.append({"role": "user", "content": message})
|
206 |
|
207 |
-
# API νΈμΆ λ° μλ΅ μ²λ¦¬
|
208 |
client = get_client()
|
209 |
partial_message = ""
|
210 |
|
@@ -221,7 +232,6 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
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:
|
@@ -230,18 +240,16 @@ def chat(message, history, uploaded_file, system_message="", max_tokens=4000, te
|
|
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,
|
@@ -266,7 +274,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
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 |
["μλ
νμΈμ! μ΄λ€ λμμ΄ νμνμ κ°μ? π€"],
|
@@ -278,12 +285,10 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
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],
|
@@ -301,7 +306,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
301 |
outputs=[msg, chatbot]
|
302 |
)
|
303 |
|
304 |
-
# νμΌ μ
λ‘λμ μλ λΆμ
|
305 |
file_upload.change(
|
306 |
lambda: "νμΌ λΆμμ μμν©λλ€...",
|
307 |
outputs=msg
|
@@ -312,4 +316,4 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo
|
|
312 |
)
|
313 |
|
314 |
if __name__ == "__main__":
|
315 |
-
demo.launch()
|
|
|
6 |
from typing import List, Tuple
|
7 |
import json
|
8 |
from datetime import datetime
|
9 |
+
import torch
|
10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
11 |
+
import spaces
|
12 |
|
13 |
# νκ²½ λ³μ μ€μ
|
14 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
15 |
+
MODEL_ID = "CohereForAI/c4ai-command-r-plus-08-2024"
|
16 |
|
17 |
+
class ModelManager:
|
18 |
+
def __init__(self):
|
19 |
+
self.model = None
|
20 |
+
self.tokenizer = None
|
21 |
+
self.setup_model()
|
22 |
+
|
23 |
+
def setup_model(self):
|
24 |
+
try:
|
25 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
26 |
+
MODEL_ID,
|
27 |
+
token=HF_TOKEN,
|
28 |
+
trust_remote_code=True
|
29 |
+
)
|
30 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
31 |
+
MODEL_ID,
|
32 |
+
token=HF_TOKEN,
|
33 |
+
torch_dtype=torch.float16,
|
34 |
+
device_map="auto",
|
35 |
+
trust_remote_code=True
|
36 |
+
)
|
37 |
+
except Exception as e:
|
38 |
+
raise Exception(f"Model loading failed: {e}")
|
39 |
|
40 |
class ChatHistory:
|
41 |
def __init__(self):
|
|
|
55 |
self.save_history()
|
56 |
|
57 |
def format_for_display(self):
|
|
|
58 |
formatted = []
|
59 |
for conv in self.history:
|
60 |
formatted.append([
|
61 |
+
conv["messages"][0]["content"],
|
62 |
+
conv["messages"][1]["content"]
|
63 |
])
|
64 |
return formatted
|
65 |
|
66 |
def get_messages_for_api(self):
|
|
|
67 |
messages = []
|
68 |
for conv in self.history:
|
69 |
messages.extend([
|
|
|
92 |
print(f"νμ€ν 리 λ‘λ μ€ν¨: {e}")
|
93 |
self.history = []
|
94 |
|
95 |
+
# μ μ μΈμ€ν΄μ€ μμ±
|
|
|
96 |
chat_history = ChatHistory()
|
97 |
+
model_manager = ModelManager()
|
98 |
|
99 |
+
def get_client():
|
100 |
+
return InferenceClient(MODEL_ID, token=HF_TOKEN)
|
|
|
|
|
|
|
101 |
|
102 |
def analyze_file_content(content, file_type):
|
103 |
"""Analyze file content and return structural summary"""
|
|
|
180 |
3. π― μ§λ¬Έμ μλλ₯Ό μ νν νμ
νμ¬ λ§μΆ€ν λ΅λ³
|
181 |
4. π νμν κ²½μ° μ
λ‘λλ νμΌ λ΄μ©μ μ°Έκ³ νμ¬ κ΅¬μ²΄μ μΈ λμ μ 곡
|
182 |
5. β¨ μΆκ°μ μΈ ν΅μ°°κ³Ό μ μμ ν΅ν κ°μΉ μλ λν
|
|
|
183 |
νμ μμ λ°λ₯΄κ³ μΉμ νκ² μλ΅νλ©°, νμν κ²½μ° κ΅¬μ²΄μ μΈ μμλ μ€λͺ
μ μΆκ°νμ¬
|
184 |
μ΄ν΄λ₯Ό λκ² μ΅λλ€."""
|
185 |
|
186 |
try:
|
|
|
187 |
if uploaded_file:
|
188 |
content, file_type = read_uploaded_file(uploaded_file)
|
189 |
if file_type == "error":
|
|
|
207 |
4. β¨ κ°μ μ μ
|
208 |
5. π¬ μΆκ° μ§λ¬Έμ΄λ νμν μ€λͺ
"""
|
209 |
|
|
|
210 |
messages = [{"role": "system", "content": system_prefix + system_message}]
|
211 |
|
|
|
212 |
if history:
|
213 |
for user_msg, assistant_msg in history:
|
214 |
messages.append({"role": "user", "content": user_msg})
|
|
|
216 |
|
217 |
messages.append({"role": "user", "content": message})
|
218 |
|
|
|
219 |
client = get_client()
|
220 |
partial_message = ""
|
221 |
|
|
|
232 |
current_history = history + [[message, partial_message]]
|
233 |
yield "", current_history
|
234 |
|
|
|
235 |
chat_history.add_conversation(message, partial_message)
|
236 |
|
237 |
except Exception as e:
|
|
|
240 |
yield "", history + [[message, error_msg]]
|
241 |
|
242 |
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", title="GiniGEN π€") as demo:
|
|
|
243 |
initial_history = chat_history.format_for_display()
|
244 |
with gr.Row():
|
245 |
with gr.Column(scale=2):
|
246 |
chatbot = gr.Chatbot(
|
247 |
+
value=initial_history,
|
248 |
height=600,
|
249 |
label="λνμ°½ π¬",
|
250 |
show_label=True
|
251 |
)
|
252 |
|
|
|
253 |
msg = gr.Textbox(
|
254 |
label="λ©μμ§ μ
λ ₯",
|
255 |
show_label=False,
|
|
|
274 |
temperature = gr.Slider(minimum=0, maximum=1, value=0.7, label="μ°½μμ± μμ€ π‘οΈ")
|
275 |
top_p = gr.Slider(minimum=0, maximum=1, value=0.9, label="μλ΅ λ€μμ± π")
|
276 |
|
|
|
277 |
gr.Examples(
|
278 |
examples=[
|
279 |
["μλ
νμΈμ! μ΄λ€ λμμ΄ νμνμ κ°μ? π€"],
|
|
|
285 |
inputs=msg,
|
286 |
)
|
287 |
|
|
|
288 |
def clear_chat():
|
289 |
chat_history.clear_history()
|
290 |
return None, None
|
291 |
|
|
|
292 |
msg.submit(
|
293 |
chat,
|
294 |
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
|
|
306 |
outputs=[msg, chatbot]
|
307 |
)
|
308 |
|
|
|
309 |
file_upload.change(
|
310 |
lambda: "νμΌ λΆμμ μμν©λλ€...",
|
311 |
outputs=msg
|
|
|
316 |
)
|
317 |
|
318 |
if __name__ == "__main__":
|
319 |
+
demo.launch()
|