Zenith Wang commited on
Commit
1cad6a0
·
1 Parent(s): 0c5a7f8

Fix conversation disappearing issue, change UI to English, remove model selection

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -27,7 +27,7 @@ def image_to_base64(image):
27
 
28
  return None
29
 
30
- def call_step_api_stream(message, history, model, temperature, max_tokens, image=None):
31
  """调用Step API进行流式对话"""
32
 
33
  if not message and not image:
@@ -35,7 +35,7 @@ def call_step_api_stream(message, history, model, temperature, max_tokens, image
35
  return
36
 
37
  if not STEP_API_KEY:
38
- history.append([message or "图片", "❌ API密钥未配置。请在 Settings 中添加 STEP_API_KEY"])
39
  yield history
40
  return
41
 
@@ -55,7 +55,7 @@ def call_step_api_stream(message, history, model, temperature, max_tokens, image
55
  try:
56
  base64_image = image_to_base64(image)
57
  if base64_image is None:
58
- history.append([message or "图片", "❌ 图片处理失败"])
59
  yield history
60
  return
61
 
@@ -66,9 +66,9 @@ def call_step_api_stream(message, history, model, temperature, max_tokens, image
66
  current_content.append({"type": "text", "text": message})
67
 
68
  messages.append({"role": "user", "content": current_content})
69
- display_message = f"[图片] {message}" if message else "[图片]"
70
  except Exception as e:
71
- history.append([message or "图片", f"❌ 图片处理错误: {str(e)}"])
72
  yield history
73
  return
74
  else:
@@ -83,14 +83,14 @@ def call_step_api_stream(message, history, model, temperature, max_tokens, image
83
  try:
84
  client = OpenAI(api_key=STEP_API_KEY, base_url=BASE_URL)
85
  except Exception as e:
86
- history[-1][1] = f"❌ 客户端初始化失败: {str(e)}"
87
  yield history
88
  return
89
 
90
  # 调用API
91
  try:
92
  response = client.chat.completions.create(
93
- model=model,
94
  messages=messages,
95
  temperature=temperature,
96
  max_tokens=max_tokens,
@@ -108,7 +108,7 @@ def call_step_api_stream(message, history, model, temperature, max_tokens, image
108
  yield history
109
 
110
  except Exception as e:
111
- history[-1][1] = f"❌ API请求失败: {str(e)}"
112
  yield history
113
 
114
  def user_input(message, history, image):
@@ -118,7 +118,7 @@ def user_input(message, history, image):
118
  return message, history, image
119
 
120
  def clear_history():
121
- """清空对话历史"""
122
  return [], None, ""
123
 
124
  # 创建Gradio界面
@@ -141,8 +141,8 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
141
  with gr.Row():
142
  with gr.Column(scale=8):
143
  msg = gr.Textbox(
144
- label="输入消息",
145
- placeholder="输入你的问题...",
146
  lines=1,
147
  max_lines=5,
148
  show_label=False,
@@ -150,14 +150,14 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
150
  container=False
151
  )
152
  with gr.Column(scale=1, min_width=100):
153
- submit_btn = gr.Button("发送", variant="primary")
154
  with gr.Column(scale=1, min_width=100):
155
- clear_btn = gr.Button("清空对话")
156
 
157
  # 图片上传
158
  with gr.Row():
159
  image_input = gr.Image(
160
- label="上传图片(可选)",
161
  type="pil",
162
  height=150,
163
  scale=1
@@ -165,14 +165,9 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
165
 
166
  with gr.Column(scale=1):
167
  # 设置面板
168
- gr.Markdown("### ⚙️ 设置")
169
 
170
- model_select = gr.Dropdown(
171
- choices=MODELS,
172
- value=MODELS[0],
173
- label="模型选择",
174
- interactive=True
175
- )
176
 
177
  temperature_slider = gr.Slider(
178
  minimum=0,
@@ -188,16 +183,16 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
188
  maximum=4000,
189
  value=2000,
190
  step=100,
191
- label="最大输出长度",
192
  interactive=True
193
  )
194
 
195
  gr.Markdown("""
196
- ### 📝 使用说明
197
- - 支持多轮对话
198
- - 可上传图片进行分析
199
- - 支持纯文本对话
200
- - 历史记录会保留上下文
201
  """)
202
 
203
  # 事件处理
@@ -208,7 +203,7 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
208
  queue=False
209
  ).then(
210
  call_step_api_stream,
211
- [msg, chatbot, model_select, temperature_slider, max_tokens_slider, image_input],
212
  chatbot
213
  )
214
 
@@ -219,7 +214,7 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
219
  queue=False
220
  ).then(
221
  call_step_api_stream,
222
- [msg, chatbot, model_select, temperature_slider, max_tokens_slider, image_input],
223
  chatbot
224
  )
225
 
@@ -242,5 +237,8 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
242
 
243
  # 启动应用
244
  if __name__ == "__main__":
245
- demo.queue()
246
- demo.launch()
 
 
 
 
27
 
28
  return None
29
 
30
+ def call_step_api_stream(message, history, temperature, max_tokens, image=None):
31
  """调用Step API进行流式对话"""
32
 
33
  if not message and not image:
 
35
  return
36
 
37
  if not STEP_API_KEY:
38
+ history.append([message or "[Image]", "❌ API key not configured. Please add STEP_API_KEY in Settings."])
39
  yield history
40
  return
41
 
 
55
  try:
56
  base64_image = image_to_base64(image)
57
  if base64_image is None:
58
+ history.append([message or "[Image]", "❌ Failed to process image"])
59
  yield history
60
  return
61
 
 
66
  current_content.append({"type": "text", "text": message})
67
 
68
  messages.append({"role": "user", "content": current_content})
69
+ display_message = f"[Image] {message}" if message else "[Image]"
70
  except Exception as e:
71
+ history.append([message or "[Image]", f"❌ Image processing error: {str(e)}"])
72
  yield history
73
  return
74
  else:
 
83
  try:
84
  client = OpenAI(api_key=STEP_API_KEY, base_url=BASE_URL)
85
  except Exception as e:
86
+ history[-1][1] = f"❌ Client initialization failed: {str(e)}"
87
  yield history
88
  return
89
 
90
  # 调用API
91
  try:
92
  response = client.chat.completions.create(
93
+ model="step-3",
94
  messages=messages,
95
  temperature=temperature,
96
  max_tokens=max_tokens,
 
108
  yield history
109
 
110
  except Exception as e:
111
+ history[-1][1] = f"❌ API request failed: {str(e)}"
112
  yield history
113
 
114
  def user_input(message, history, image):
 
118
  return message, history, image
119
 
120
  def clear_history():
121
+ """Clear conversation history"""
122
  return [], None, ""
123
 
124
  # 创建Gradio界面
 
141
  with gr.Row():
142
  with gr.Column(scale=8):
143
  msg = gr.Textbox(
144
+ label="Input message",
145
+ placeholder="Type your question here...",
146
  lines=1,
147
  max_lines=5,
148
  show_label=False,
 
150
  container=False
151
  )
152
  with gr.Column(scale=1, min_width=100):
153
+ submit_btn = gr.Button("Send", variant="primary")
154
  with gr.Column(scale=1, min_width=100):
155
+ clear_btn = gr.Button("Clear")
156
 
157
  # 图片上传
158
  with gr.Row():
159
  image_input = gr.Image(
160
+ label="Upload Image (Optional)",
161
  type="pil",
162
  height=150,
163
  scale=1
 
165
 
166
  with gr.Column(scale=1):
167
  # 设置面板
168
+ gr.Markdown("### ⚙️ Settings")
169
 
170
+
 
 
 
 
 
171
 
172
  temperature_slider = gr.Slider(
173
  minimum=0,
 
183
  maximum=4000,
184
  value=2000,
185
  step=100,
186
+ label="Max Output Length",
187
  interactive=True
188
  )
189
 
190
  gr.Markdown("""
191
+ ### 📝 Instructions
192
+ - Multi-turn conversation support
193
+ - Upload images for analysis
194
+ - Pure text conversation support
195
+ - Context preserved in history
196
  """)
197
 
198
  # 事件处理
 
203
  queue=False
204
  ).then(
205
  call_step_api_stream,
206
+ [msg, chatbot, temperature_slider, max_tokens_slider, image_input],
207
  chatbot
208
  )
209
 
 
214
  queue=False
215
  ).then(
216
  call_step_api_stream,
217
+ [msg, chatbot, temperature_slider, max_tokens_slider, image_input],
218
  chatbot
219
  )
220
 
 
237
 
238
  # 启动应用
239
  if __name__ == "__main__":
240
+ demo.queue(max_size=10)
241
+ demo.launch(
242
+ share=False,
243
+ debug=True
244
+ )