Spaces:
Running
Running
Zenith Wang
commited on
Commit
·
560ce7e
1
Parent(s):
1cad6a0
Add debug logging and remove instructions section
Browse files
app.py
CHANGED
|
@@ -29,16 +29,21 @@ def image_to_base64(image):
|
|
| 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:
|
|
|
|
| 34 |
yield history
|
| 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 |
|
|
|
|
|
|
|
| 42 |
# 构造消息历史
|
| 43 |
messages = []
|
| 44 |
|
|
@@ -78,17 +83,23 @@ def call_step_api_stream(message, history, temperature, max_tokens, image=None):
|
|
| 78 |
|
| 79 |
# 添加到历史记录
|
| 80 |
history.append([display_message, ""])
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# 创建客户端
|
| 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,
|
|
@@ -96,18 +107,32 @@ def call_step_api_stream(message, history, temperature, max_tokens, image=None):
|
|
| 96 |
max_tokens=max_tokens,
|
| 97 |
stream=True
|
| 98 |
)
|
|
|
|
| 99 |
|
| 100 |
# 处理流式响应
|
| 101 |
full_response = ""
|
|
|
|
| 102 |
for chunk in response:
|
|
|
|
| 103 |
if chunk.choices and len(chunk.choices) > 0:
|
| 104 |
delta = chunk.choices[0].delta
|
| 105 |
if hasattr(delta, 'content') and delta.content:
|
| 106 |
full_response += delta.content
|
| 107 |
history[-1][1] = full_response
|
|
|
|
| 108 |
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
| 111 |
history[-1][1] = f"❌ API request failed: {str(e)}"
|
| 112 |
yield history
|
| 113 |
|
|
@@ -187,13 +212,7 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
|
| 187 |
interactive=True
|
| 188 |
)
|
| 189 |
|
| 190 |
-
|
| 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 |
# 事件处理
|
| 199 |
msg.submit(
|
|
@@ -237,6 +256,8 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
|
| 237 |
|
| 238 |
# 启动应用
|
| 239 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 240 |
demo.queue(max_size=10)
|
| 241 |
demo.launch(
|
| 242 |
share=False,
|
|
|
|
| 29 |
|
| 30 |
def call_step_api_stream(message, history, temperature, max_tokens, image=None):
|
| 31 |
"""调用Step API进行流式对话"""
|
| 32 |
+
print(f"[DEBUG] Starting API call - Message: {message}, Has Image: {image is not None}")
|
| 33 |
|
| 34 |
if not message and not image:
|
| 35 |
+
print("[DEBUG] No message or image provided")
|
| 36 |
yield history
|
| 37 |
return
|
| 38 |
|
| 39 |
if not STEP_API_KEY:
|
| 40 |
+
print("[DEBUG] API key not configured")
|
| 41 |
history.append([message or "[Image]", "❌ API key not configured. Please add STEP_API_KEY in Settings."])
|
| 42 |
yield history
|
| 43 |
return
|
| 44 |
|
| 45 |
+
print(f"[DEBUG] API Key exists: {bool(STEP_API_KEY)}")
|
| 46 |
+
|
| 47 |
# 构造消息历史
|
| 48 |
messages = []
|
| 49 |
|
|
|
|
| 83 |
|
| 84 |
# 添加到历史记录
|
| 85 |
history.append([display_message, ""])
|
| 86 |
+
yield history # 立即显示用户消息
|
| 87 |
+
|
| 88 |
+
print(f"[DEBUG] Messages to send: {messages}")
|
| 89 |
|
| 90 |
# 创建客户端
|
| 91 |
try:
|
| 92 |
client = OpenAI(api_key=STEP_API_KEY, base_url=BASE_URL)
|
| 93 |
+
print("[DEBUG] Client created successfully")
|
| 94 |
except Exception as e:
|
| 95 |
+
print(f"[DEBUG] Client initialization failed: {e}")
|
| 96 |
history[-1][1] = f"❌ Client initialization failed: {str(e)}"
|
| 97 |
yield history
|
| 98 |
return
|
| 99 |
|
| 100 |
# 调用API
|
| 101 |
try:
|
| 102 |
+
print("[DEBUG] Calling API...")
|
| 103 |
response = client.chat.completions.create(
|
| 104 |
model="step-3",
|
| 105 |
messages=messages,
|
|
|
|
| 107 |
max_tokens=max_tokens,
|
| 108 |
stream=True
|
| 109 |
)
|
| 110 |
+
print("[DEBUG] API call successful, processing stream...")
|
| 111 |
|
| 112 |
# 处理流式响应
|
| 113 |
full_response = ""
|
| 114 |
+
chunk_count = 0
|
| 115 |
for chunk in response:
|
| 116 |
+
chunk_count += 1
|
| 117 |
if chunk.choices and len(chunk.choices) > 0:
|
| 118 |
delta = chunk.choices[0].delta
|
| 119 |
if hasattr(delta, 'content') and delta.content:
|
| 120 |
full_response += delta.content
|
| 121 |
history[-1][1] = full_response
|
| 122 |
+
print(f"[DEBUG] Chunk {chunk_count}: {delta.content[:50] if delta.content else 'None'}...")
|
| 123 |
yield history
|
| 124 |
+
|
| 125 |
+
if not full_response:
|
| 126 |
+
print("[DEBUG] No response content received")
|
| 127 |
+
history[-1][1] = "⚠️ No response received from API"
|
| 128 |
+
yield history
|
| 129 |
+
else:
|
| 130 |
+
print(f"[DEBUG] Final response length: {len(full_response)} chars")
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
+
print(f"[DEBUG] API request failed: {e}")
|
| 134 |
+
import traceback
|
| 135 |
+
traceback.print_exc()
|
| 136 |
history[-1][1] = f"❌ API request failed: {str(e)}"
|
| 137 |
yield history
|
| 138 |
|
|
|
|
| 212 |
interactive=True
|
| 213 |
)
|
| 214 |
|
| 215 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# 事件处理
|
| 218 |
msg.submit(
|
|
|
|
| 256 |
|
| 257 |
# 启动应用
|
| 258 |
if __name__ == "__main__":
|
| 259 |
+
print(f"[DEBUG] Starting app with API key: {'Set' if STEP_API_KEY else 'Not set'}")
|
| 260 |
+
print(f"[DEBUG] Base URL: {BASE_URL}")
|
| 261 |
demo.queue(max_size=10)
|
| 262 |
demo.launch(
|
| 263 |
share=False,
|