openfree commited on
Commit
3fb4d5f
ยท
verified ยท
1 Parent(s): aa9237f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -24
app.py CHANGED
@@ -4,18 +4,16 @@ import requests
4
  import json
5
 
6
  def create_deepseek_interface():
7
- # ์ฑ„ํŒ… ๊ธฐ๋ก ์ดˆ๊ธฐํ™” (๋ฉ”์‹œ์ง€ ํ˜•์‹)
8
- chat_history = []
9
-
10
- # DeepSeek API ํ˜ธ์ถœ ํ•จ์ˆ˜
11
  def query_deepseek(message, history, api_key):
12
  if not api_key:
13
  return history, "Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
14
 
15
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋Œ€ํ™” ๊ธฐ๋ก ์ค€๋น„
16
  messages = []
17
- for h in history:
18
- messages.append(h)
 
19
 
20
  # ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
21
  messages.append({"role": "user", "content": message})
@@ -47,14 +45,12 @@ def create_deepseek_interface():
47
  result = response.json()
48
  assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
49
 
50
- # ๋Œ€ํ™” ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ (๋ฉ”์‹œ์ง€ ํ˜•์‹์œผ๋กœ)
51
- new_history = history.copy()
52
- new_history.append({"role": "user", "content": message})
53
- new_history.append({"role": "assistant", "content": assistant_response})
54
- return new_history, ""
55
  except requests.exceptions.RequestException as e:
56
  error_msg = f"API ์˜ค๋ฅ˜: {str(e)}"
57
- if hasattr(response, 'status_code') and response.status_code == 401:
58
  error_msg = "์ธ์ฆ ์‹คํŒจ. API ํ‚ค๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
59
  return history, error_msg
60
 
@@ -68,9 +64,6 @@ def create_deepseek_interface():
68
  """
69
  )
70
 
71
- # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ƒํƒœ
72
- error_state = gr.State("")
73
-
74
  # ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ (๋‘ ๊ฐœ์˜ ์—ด)
75
  with gr.Row():
76
  # ์‚ฌ์ด๋“œ๋ฐ” - ๋ชจ๋ธ ์ •๋ณด ๋ฐ API ํ‚ค
@@ -115,8 +108,7 @@ def create_deepseek_interface():
115
  chatbot = gr.Chatbot(
116
  height=500,
117
  show_label=False,
118
- container=True,
119
- type="messages"
120
  )
121
 
122
  # ์ž…๋ ฅ ์˜์—ญ
@@ -146,22 +138,20 @@ def create_deepseek_interface():
146
  # ํผ ์ œ์ถœ ์ฒ˜๋ฆฌ
147
  def process_query(message, history, api_key):
148
  if not message.strip():
149
- return history
150
 
151
  updated_history, error = query_deepseek(message, history, api_key)
152
 
153
  if error:
154
- error_box.value = f"**์˜ค๋ฅ˜:** {error}"
155
- return history
156
  else:
157
- error_box.value = ""
158
- return updated_history
159
 
160
  # ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
161
  submit.click(
162
  process_query,
163
  inputs=[msg, chatbot, api_key],
164
- outputs=[chatbot]
165
  ).then(
166
  lambda: "",
167
  None,
@@ -172,7 +162,7 @@ def create_deepseek_interface():
172
  msg.submit(
173
  process_query,
174
  inputs=[msg, chatbot, api_key],
175
- outputs=[chatbot]
176
  ).then(
177
  lambda: "",
178
  None,
 
4
  import json
5
 
6
  def create_deepseek_interface():
7
+ # ์ฑ„ํŒ… ๊ธฐ๋ก ์ƒํƒœ๋ฅผ ์ €์žฅํ•  State ๊ฐ์ฒด
 
 
 
8
  def query_deepseek(message, history, api_key):
9
  if not api_key:
10
  return history, "Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
11
 
12
  # API ์š”์ฒญ์„ ์œ„ํ•œ ๋Œ€ํ™” ๊ธฐ๋ก ์ค€๋น„
13
  messages = []
14
+ for user, assistant in history:
15
+ messages.append({"role": "user", "content": user})
16
+ messages.append({"role": "assistant", "content": assistant})
17
 
18
  # ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
19
  messages.append({"role": "user", "content": message})
 
45
  result = response.json()
46
  assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
47
 
48
+ # ๋Œ€ํ™” ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ (Gradio chatbot ํ˜•์‹์œผ๋กœ)
49
+ history.append((message, assistant_response))
50
+ return history, ""
 
 
51
  except requests.exceptions.RequestException as e:
52
  error_msg = f"API ์˜ค๋ฅ˜: {str(e)}"
53
+ if hasattr(e, 'response') and e.response and e.response.status_code == 401:
54
  error_msg = "์ธ์ฆ ์‹คํŒจ. API ํ‚ค๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
55
  return history, error_msg
56
 
 
64
  """
65
  )
66
 
 
 
 
67
  # ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ (๋‘ ๊ฐœ์˜ ์—ด)
68
  with gr.Row():
69
  # ์‚ฌ์ด๋“œ๋ฐ” - ๋ชจ๋ธ ์ •๋ณด ๋ฐ API ํ‚ค
 
108
  chatbot = gr.Chatbot(
109
  height=500,
110
  show_label=False,
111
+ container=True
 
112
  )
113
 
114
  # ์ž…๋ ฅ ์˜์—ญ
 
138
  # ํผ ์ œ์ถœ ์ฒ˜๋ฆฌ
139
  def process_query(message, history, api_key):
140
  if not message.strip():
141
+ return history, ""
142
 
143
  updated_history, error = query_deepseek(message, history, api_key)
144
 
145
  if error:
146
+ return history, f"**์˜ค๋ฅ˜:** {error}"
 
147
  else:
148
+ return updated_history, ""
 
149
 
150
  # ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋Šฅ ์—ฐ๊ฒฐ
151
  submit.click(
152
  process_query,
153
  inputs=[msg, chatbot, api_key],
154
+ outputs=[chatbot, error_box]
155
  ).then(
156
  lambda: "",
157
  None,
 
162
  msg.submit(
163
  process_query,
164
  inputs=[msg, chatbot, api_key],
165
+ outputs=[chatbot, error_box]
166
  ).then(
167
  lambda: "",
168
  None,