openfree commited on
Commit
3db5c7b
ยท
verified ยท
1 Parent(s): 8d26a35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -69
app.py CHANGED
@@ -4,24 +4,24 @@ import requests
4
  import json
5
 
6
  def create_deepseek_interface():
7
- # Initialize chat history
8
  chat_history = []
9
 
10
- # Function to call the DeepSeek API
11
  def query_deepseek(message, history, api_key):
12
  if not api_key:
13
- return history, "Please provide your Fireworks AI API key"
14
 
15
- # Prepare the conversation history for the API
16
  messages = []
17
  for h in history:
18
  messages.append({"role": "user", "content": h[0]})
19
  messages.append({"role": "assistant", "content": h[1]})
20
 
21
- # Add the new user message
22
  messages.append({"role": "user", "content": message})
23
 
24
- # Prepare the API request
25
  url = "https://api.fireworks.ai/inference/v1/chat/completions"
26
  payload = {
27
  "model": "accounts/fireworks/models/deepseek-v3-0324",
@@ -40,74 +40,78 @@ def create_deepseek_interface():
40
  }
41
 
42
  try:
43
- # Make the API request
44
  response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
45
- response.raise_for_status() # Raise exception for HTTP errors
46
 
47
- # Extract the response
48
  result = response.json()
49
  assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
50
 
51
- # Update history with the new exchange
52
- history.append((message, assistant_response))
53
- return history, ""
 
54
  except requests.exceptions.RequestException as e:
55
- error_msg = f"API Error: {str(e)}"
56
- if response.status_code == 401:
57
- error_msg = "Authentication failed. Please check your API key."
58
  return history, error_msg
59
 
60
- # Create Gradio interface
61
  with gr.Blocks(theme="soft", fill_height=True) as demo:
62
- # Header Section
63
  gr.Markdown(
64
  """
65
- # ๐Ÿค– DeepSeek V3 Inference Interface
66
- ### Advanced AI Model Powered by Fireworks AI
67
  """
68
  )
69
 
70
- # State for error messages
71
- error_msg = gr.State("")
72
 
73
- # Main layout with two columns
74
  with gr.Row():
75
- # Sidebar with Model Information and API Key
76
  with gr.Column(scale=1):
77
  gr.Markdown(
78
  """
79
- ## ๐Ÿ”‘ Access Control
80
- ### Inference Provider
81
- This interface connects to the DeepSeek-V3 model, served by the Fireworks AI API.
82
 
83
- #### Authentication
84
- - Enter your Fireworks AI API key below
85
- - Secure API access with end-to-end encryption
86
  """
87
  )
88
 
89
- # API Key input
90
  api_key = gr.Textbox(
91
- label="Fireworks AI API Key",
92
- placeholder="Enter your API key...",
93
  type="password"
94
  )
95
 
96
- # Model Details Section
97
  gr.Markdown(
98
  """
99
- ### ๐Ÿ“Š Model Details
100
- - **Model**: DeepSeek-V3-0324
101
- - **Provider**: Fireworks AI
102
- - **Max Tokens**: 20,480
103
- - **Temperature**: 0.6
104
- - **Capabilities**: Advanced Language Understanding
105
  """
106
  )
 
 
 
107
 
108
- # Main Content Area
109
  with gr.Column(scale=2):
110
- # Chat interface
111
  chatbot = gr.Chatbot(
112
  height=500,
113
  show_label=False,
@@ -115,64 +119,69 @@ def create_deepseek_interface():
115
  type="messages"
116
  )
117
 
118
- # Error message display
119
- error_display = gr.Markdown("")
120
-
121
- # Input area
122
  with gr.Row():
123
  msg = gr.Textbox(
124
- label="Your Message",
125
- placeholder="Type your prompt here...",
126
  show_label=False,
127
  scale=9
128
  )
129
- submit = gr.Button("Send", variant="primary", scale=1)
130
 
131
- # Buttons for clearing and examples
132
  with gr.Row():
133
- clear = gr.ClearButton([msg, chatbot], value="๐Ÿงน Clear Conversation")
134
 
135
- # Example queries
136
  gr.Examples(
137
  examples=[
138
- "Explain the differences between transformers and RNNs in deep learning.",
139
- "Write a Python function to find prime numbers in a range.",
140
- "Summarize the key concepts of reinforcement learning."
141
  ],
142
  inputs=msg
143
  )
144
 
145
- # Handle form submission
146
- def process_submission(message, history, api_key):
147
  if not message.strip():
148
- return history, error_display.update(visible=False)
149
 
150
- updated_history, error = query_deepseek(message, history.copy(), api_key)
151
 
152
  if error:
153
- return history, error_display.update(value=f"**Error:** {error}", visible=True)
 
154
  else:
155
- return updated_history, error_display.update(visible=False)
 
156
 
157
- # Connect the button to the function
158
  submit.click(
159
- process_submission,
160
  inputs=[msg, chatbot, api_key],
161
- outputs=[chatbot],
162
- postprocess=lambda _: "" # Clear input box after sending
 
 
 
163
  )
164
 
165
- # Also allow pressing Enter to submit
166
  msg.submit(
167
- process_submission,
168
  inputs=[msg, chatbot, api_key],
169
- outputs=[chatbot],
170
- postprocess=lambda _: "" # Clear input box after sending
 
 
 
171
  )
172
 
173
  return demo
174
 
175
- # Launch the interface
176
  if __name__ == "__main__":
177
  demo = create_deepseek_interface()
178
  demo.launch(debug=True)
 
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({"role": "user", "content": h[0]})
19
  messages.append({"role": "assistant", "content": h[1]})
20
 
21
+ # ์ƒˆ ์‚ฌ์šฉ์ž ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
22
  messages.append({"role": "user", "content": message})
23
 
24
+ # API ์š”์ฒญ ์ค€๋น„
25
  url = "https://api.fireworks.ai/inference/v1/chat/completions"
26
  payload = {
27
  "model": "accounts/fireworks/models/deepseek-v3-0324",
 
40
  }
41
 
42
  try:
43
+ # API ์š”์ฒญ ์ „์†ก
44
  response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
45
+ response.raise_for_status() # HTTP ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
46
 
47
+ # ์‘๋‹ต ์ถ”์ถœ
48
  result = response.json()
49
  assistant_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
50
 
51
+ # ๋Œ€ํ™” ๊ธฐ๋ก ์—…๋ฐ์ดํŠธ
52
+ new_history = history.copy()
53
+ new_history.append((message, 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
 
61
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
62
  with gr.Blocks(theme="soft", fill_height=True) as demo:
63
+ # ํ—ค๋” ์„น์…˜
64
  gr.Markdown(
65
  """
66
+ # ๐Ÿค– DeepSeek V3 ์ถ”๋ก  ์ธํ„ฐํŽ˜์ด์Šค
67
+ ### Fireworks AI๊ฐ€ ์ œ๊ณตํ•˜๋Š” ๊ณ ๊ธ‰ AI ๋ชจ๋ธ
68
  """
69
  )
70
 
71
+ # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ์ƒํƒœ
72
+ error_state = gr.State("")
73
 
74
+ # ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ (๋‘ ๊ฐœ์˜ ์—ด)
75
  with gr.Row():
76
+ # ์‚ฌ์ด๋“œ๋ฐ” - ๋ชจ๋ธ ์ •๋ณด ๋ฐ API ํ‚ค
77
  with gr.Column(scale=1):
78
  gr.Markdown(
79
  """
80
+ ## ๐Ÿ”‘ ์ ‘๊ทผ ์ œ์–ด
81
+ ### ์ถ”๋ก  ์ œ๊ณต์ž
82
+ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋Š” Fireworks AI API๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋˜๋Š” DeepSeek-V3 ๋ชจ๋ธ์— ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค.
83
 
84
+ #### ์ธ์ฆ
85
+ - ์•„๋ž˜์— Fireworks AI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
86
+ - ์—”๋“œ-ํˆฌ-์—”๋“œ ์•”ํ˜ธํ™”๋กœ ์•ˆ์ „ํ•œ API ์ ‘๊ทผ
87
  """
88
  )
89
 
90
+ # API ํ‚ค ์ž…๋ ฅ
91
  api_key = gr.Textbox(
92
+ label="Fireworks AI API ํ‚ค",
93
+ placeholder="API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
94
  type="password"
95
  )
96
 
97
+ # ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด ์„น์…˜
98
  gr.Markdown(
99
  """
100
+ ### ๐Ÿ“Š ๋ชจ๋ธ ์„ธ๋ถ€ ์ •๋ณด
101
+ - **๋ชจ๋ธ**: DeepSeek-V3-0324
102
+ - **์ œ๊ณต์ž**: Fireworks AI
103
+ - **์ตœ๋Œ€ ํ† ํฐ**: 20,480
104
+ - **์˜จ๋„**: 0.6
105
+ - **๊ธฐ๋Šฅ**: ๊ณ ๊ธ‰ ์–ธ์–ด ์ดํ•ด
106
  """
107
  )
108
+
109
+ # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ํ‘œ์‹œ
110
+ error_box = gr.Markdown("")
111
 
112
+ # ๋ฉ”์ธ ์ฝ˜ํ…์ธ  ์˜์—ญ
113
  with gr.Column(scale=2):
114
+ # ์ฑ„ํŒ… ์ธํ„ฐํŽ˜์ด์Šค
115
  chatbot = gr.Chatbot(
116
  height=500,
117
  show_label=False,
 
119
  type="messages"
120
  )
121
 
122
+ # ์ž…๋ ฅ ์˜์—ญ
 
 
 
123
  with gr.Row():
124
  msg = gr.Textbox(
125
+ label="๋ฉ”์‹œ์ง€",
126
+ placeholder="์—ฌ๊ธฐ์— ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...",
127
  show_label=False,
128
  scale=9
129
  )
130
+ submit = gr.Button("์ „์†ก", variant="primary", scale=1)
131
 
132
+ # ๋Œ€ํ™” ์ดˆ๊ธฐํ™” ๋ฒ„ํŠผ
133
  with gr.Row():
134
+ clear = gr.ClearButton([msg, chatbot], value="๐Ÿงน ๋Œ€ํ™” ์ดˆ๊ธฐํ™”")
135
 
136
+ # ์˜ˆ์ œ ์ฟผ๋ฆฌ
137
  gr.Examples(
138
  examples=[
139
+ "๋”ฅ๋Ÿฌ๋‹์—์„œ ํŠธ๋žœ์Šคํฌ๋จธ์™€ RNN์˜ ์ฐจ์ด์ ์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.",
140
+ "ํŠน์ • ๋ฒ”์œ„ ๋‚ด์˜ ์†Œ์ˆ˜๋ฅผ ์ฐพ๋Š” ํŒŒ์ด์ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.",
141
+ "๊ฐ•ํ™”ํ•™์Šต์˜ ์ฃผ์š” ๊ฐœ๋…์„ ์š”์•ฝํ•ด์ฃผ์„ธ์š”."
142
  ],
143
  inputs=msg
144
  )
145
 
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,
168
+ [msg]
169
  )
170
 
171
+ # Enter ํ‚ค ์ œ์ถœ ํ—ˆ์šฉ
172
  msg.submit(
173
+ process_query,
174
  inputs=[msg, chatbot, api_key],
175
+ outputs=[chatbot]
176
+ ).then(
177
+ lambda: "",
178
+ None,
179
+ [msg]
180
  )
181
 
182
  return demo
183
 
184
+ # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
185
  if __name__ == "__main__":
186
  demo = create_deepseek_interface()
187
  demo.launch(debug=True)