Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from typing import List, Dict, Tuple
|
|
6 |
import json
|
7 |
import io
|
8 |
|
|
|
9 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
10 |
hf_client = InferenceClient(
|
11 |
"CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN")
|
@@ -36,44 +37,51 @@ def respond(
|
|
36 |
max_tokens: int = 1000,
|
37 |
temperature: float = 0.7,
|
38 |
top_p: float = 0.9,
|
39 |
-
parquet_data:
|
40 |
) -> str:
|
41 |
# μμ€ν
ν둬ννΈ μ€μ
|
42 |
if parquet_data:
|
43 |
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ
λ‘λλ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ§λ¬Έμ λ΅λ³νλ μν μ νλ€. λ°μ΄ν°λ₯Ό λΆμνμ¬ μ¬μ©μμκ² λμμ΄ λλ μ 보λ₯Ό μ 곡νλΌ. λ°μ΄ν°λ₯Ό νμ©νμ¬ μμΈνκ³ μ νν λ΅λ³μ μ 곡νλ, λ―Όκ°ν μ 보λ κ°μΈ μ 보λ₯Ό λ
ΈμΆνμ§ λ§λΌ."""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
else:
|
52 |
system_prefix = system_message or "λλ AI μ‘°μΈμ μν μ΄λ€."
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
messages = [{"role": "system", "content": system_prefix}]
|
58 |
for chat in history:
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
try:
|
62 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
63 |
response = ""
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
stream=True,
|
68 |
temperature=temperature,
|
69 |
top_p=top_p,
|
70 |
-
)
|
71 |
-
|
|
|
72 |
if token:
|
73 |
response += token
|
74 |
yield response
|
75 |
except Exception as e:
|
76 |
-
|
|
|
|
|
|
|
77 |
|
78 |
def upload_csv(file_path: str) -> Tuple[str, str]:
|
79 |
try:
|
|
|
6 |
import json
|
7 |
import io
|
8 |
|
9 |
+
import traceback
|
10 |
# μΆλ‘ API ν΄λΌμ΄μΈνΈ μ€μ
|
11 |
hf_client = InferenceClient(
|
12 |
"CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN")
|
|
|
37 |
max_tokens: int = 1000,
|
38 |
temperature: float = 0.7,
|
39 |
top_p: float = 0.9,
|
40 |
+
parquet_data: str = None
|
41 |
) -> str:
|
42 |
# μμ€ν
ν둬ννΈ μ€μ
|
43 |
if parquet_data:
|
44 |
system_prefix = """λ°λμ νκΈλ‘ λ΅λ³ν κ². λλ μ
λ‘λλ λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ μ§λ¬Έμ λ΅λ³νλ μν μ νλ€. λ°μ΄ν°λ₯Ό λΆμνμ¬ μ¬μ©μμκ² λμμ΄ λλ μ 보λ₯Ό μ 곡νλΌ. λ°μ΄ν°λ₯Ό νμ©νμ¬ μμΈνκ³ μ νν λ΅λ³μ μ 곡νλ, λ―Όκ°ν μ 보λ κ°μΈ μ 보λ₯Ό λ
ΈμΆνμ§ λ§λΌ."""
|
45 |
+
try:
|
46 |
+
df = pd.read_json(io.StringIO(parquet_data))
|
47 |
+
# λ°μ΄ν°μ μμ½ μ 보 μμ±
|
48 |
+
data_summary = df.describe(include='all').to_string()
|
49 |
+
system_prefix += f"\n\nμ
λ‘λλ λ°μ΄ν°μ μμ½ μ 보:\n{data_summary}"
|
50 |
+
except Exception as e:
|
51 |
+
print(f"λ°μ΄ν° λ‘λ μ€ μ€λ₯ λ°μ: {str(e)}\n{traceback.format_exc()}")
|
52 |
+
system_prefix += "\n\nλ°μ΄ν°λ₯Ό λ‘λνλ μ€ μ€λ₯κ° λ°μνμ΅λλ€."
|
53 |
else:
|
54 |
system_prefix = system_message or "λλ AI μ‘°μΈμ μν μ΄λ€."
|
|
|
55 |
|
56 |
+
# λ©μμ§ μμ± (API μꡬ μ¬νμ λ§κ² μμ )
|
57 |
+
prompt = system_prefix + "\n\n"
|
|
|
58 |
for chat in history:
|
59 |
+
if chat['role'] == 'user':
|
60 |
+
prompt += f"μ¬μ©μ: {chat['content']}\n"
|
61 |
+
else:
|
62 |
+
prompt += f"AI: {chat['content']}\n"
|
63 |
+
prompt += f"μ¬μ©μ: {message}\nAI:"
|
64 |
|
65 |
try:
|
66 |
# λͺ¨λΈμ λ©μμ§ μ μ‘ λ° μλ΅ λ°κΈ°
|
67 |
response = ""
|
68 |
+
stream = hf_client.text_generation(
|
69 |
+
prompt=prompt,
|
70 |
+
max_new_tokens=max_tokens,
|
71 |
stream=True,
|
72 |
temperature=temperature,
|
73 |
top_p=top_p,
|
74 |
+
)
|
75 |
+
for msg in stream:
|
76 |
+
token = msg.get('generated_text', '')
|
77 |
if token:
|
78 |
response += token
|
79 |
yield response
|
80 |
except Exception as e:
|
81 |
+
error_message = f"μΆλ‘ μ€ μ€λ₯κ° λ°μνμ΅λλ€: {str(e)}\n{traceback.format_exc()}"
|
82 |
+
print(error_message)
|
83 |
+
yield error_message
|
84 |
+
|
85 |
|
86 |
def upload_csv(file_path: str) -> Tuple[str, str]:
|
87 |
try:
|