ginipick commited on
Commit
9f4a039
β€’
1 Parent(s): 071b7e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -19
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: Dict = None
40
  ) -> str:
41
  # μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ μ„€μ •
42
  if parquet_data:
43
  system_prefix = """λ°˜λ“œμ‹œ ν•œκΈ€λ‘œ λ‹΅λ³€ν•  것. λ„ˆλŠ” μ—…λ‘œλ“œλœ 데이터λ₯Ό 기반으둜 μ§ˆλ¬Έμ— λ‹΅λ³€ν•˜λŠ” 역할을 ν•œλ‹€. 데이터λ₯Ό λΆ„μ„ν•˜μ—¬ μ‚¬μš©μžμ—κ²Œ 도움이 λ˜λŠ” 정보λ₯Ό μ œκ³΅ν•˜λΌ. 데이터λ₯Ό ν™œμš©ν•˜μ—¬ μƒμ„Έν•˜κ³  μ •ν™•ν•œ 닡변을 μ œκ³΅ν•˜λ˜, λ―Όκ°ν•œ μ •λ³΄λ‚˜ 개인 정보λ₯Ό λ…ΈμΆœν•˜μ§€ 마라."""
44
-
45
- # μˆ˜μ •λœ λΆ€λΆ„: StringIO둜 κ°μ‹Έμ„œ λ°μ΄ν„°ν”„λ ˆμž„ 생성
46
- df = pd.read_json(io.StringIO(parquet_data))
47
-
48
- # λ°μ΄ν„°μ˜ μš”μ•½ 정보 생성
49
- data_summary = df.describe(include='all').to_string()
50
- system_prefix += f"\n\nμ—…λ‘œλ“œλœ λ°μ΄ν„°μ˜ μš”μ•½ 정보:\n{data_summary}"
 
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
- messages.append({"role": chat['role'], "content": chat['content']})
 
 
 
 
60
 
61
  try:
62
  # λͺ¨λΈμ— λ©”μ‹œμ§€ 전솑 및 응닡 λ°›κΈ°
63
  response = ""
64
- for msg in hf_client.chat_completion(
65
- messages,
66
- max_tokens=max_tokens,
67
  stream=True,
68
  temperature=temperature,
69
  top_p=top_p,
70
- ):
71
- token = msg.get('delta', {}).get('content', '')
 
72
  if token:
73
  response += token
74
  yield response
75
  except Exception as e:
76
- yield f"μΆ”λ‘  쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
 
 
 
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: