akhaliq HF Staff commited on
Commit
62beaf6
·
1 Parent(s): 2e50f11

Remove credentials field for 100% secure server-side key loading

Browse files
Files changed (3) hide show
  1. app.py +4 -5
  2. static/app.js +3 -34
  3. static/index.html +1 -12
app.py CHANGED
@@ -15,26 +15,25 @@ os.makedirs("static", exist_ok=True)
15
  @app.api(name="chat_with_step")
16
  def chat_with_step(
17
  messages_json: str,
18
- api_key: str,
19
  reasoning_effort: str = "medium",
20
  max_tokens: int = 2048,
21
  temperature: float = 0.7
22
  ) -> str:
23
  """
24
  API endpoint to call Step 3.7 Flash model via OpenAI-compatible API.
25
- Takes conversation messages as a JSON-serialized string, API Key, and parameters.
26
  Returns the assistant response along with any reasoning details.
27
  """
28
  try:
29
  # Load messages from JSON string
30
  messages = json.loads(messages_json)
31
 
32
- # Use user-supplied key or fallback to server environment variable
33
- key = api_key.strip() if api_key and api_key.strip() else os.environ.get("STEP_API_KEY", "")
34
  if not key:
35
  return json.dumps({
36
  "status": "error",
37
- "message": "StepFun API Key is required. Please enter your API Key in the settings sidebar."
38
  })
39
 
40
  # Initialize OpenAI client configured for StepFun
 
15
  @app.api(name="chat_with_step")
16
  def chat_with_step(
17
  messages_json: str,
 
18
  reasoning_effort: str = "medium",
19
  max_tokens: int = 2048,
20
  temperature: float = 0.7
21
  ) -> str:
22
  """
23
  API endpoint to call Step 3.7 Flash model via OpenAI-compatible API.
24
+ Takes conversation messages as a JSON-serialized string, and parameters.
25
  Returns the assistant response along with any reasoning details.
26
  """
27
  try:
28
  # Load messages from JSON string
29
  messages = json.loads(messages_json)
30
 
31
+ # Load key from secure server-side environment variable
32
+ key = os.environ.get("STEP_API_KEY", "").strip()
33
  if not key:
34
  return json.dumps({
35
  "status": "error",
36
+ "message": "STEP_API_KEY environment variable is not configured on the server."
37
  })
38
 
39
  # Initialize OpenAI client configured for StepFun
static/app.js CHANGED
@@ -2,7 +2,6 @@ import { client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.m
2
 
3
  // Global Application State Management
4
  const STATE = {
5
- apiKey: localStorage.getItem("step_api_key") || "",
6
  reasoningEffort: "medium",
7
  maxTokens: 2048,
8
  temperature: 0.7,
@@ -14,8 +13,6 @@ const STATE = {
14
 
15
  // DOM Elements hooks
16
  const dom = {
17
- apiKeyInput: document.getElementById("api-key-input"),
18
- toggleKeyVisibility: document.getElementById("toggle-key-visibility"),
19
  effortRadioButtons: document.querySelectorAll('input[name="reasoning-effort"]'),
20
  maxTokensSlider: document.getElementById("max-tokens-slider"),
21
  maxTokensVal: document.getElementById("max-tokens-val"),
@@ -71,12 +68,7 @@ marked.setOptions({
71
 
72
  // Setup Initial State & Event Handlers
73
  async function initializeApp() {
74
- // 1. Restore Credentials
75
- if (STATE.apiKey && dom.apiKeyInput) {
76
- dom.apiKeyInput.value = STATE.apiKey;
77
- }
78
-
79
- // 2. Connect Gradio Client in background (Non-blocking)
80
  client(window.location.origin)
81
  .then(app => {
82
  STATE.gradioClient = app;
@@ -86,27 +78,12 @@ async function initializeApp() {
86
  console.error("Gradio Client Connection Failed:", e);
87
  });
88
 
89
- // 3. Register Sidebar Drawer Slide Events (Drawer + Overlay)
90
  if (dom.btnToggleRight) dom.btnToggleRight.addEventListener("click", () => toggleSettingsDrawer(true));
91
  if (dom.btnCloseDrawer) dom.btnCloseDrawer.addEventListener("click", () => toggleSettingsDrawer(false));
92
  if (dom.sidebarOverlay) dom.sidebarOverlay.addEventListener("click", () => toggleSettingsDrawer(false));
93
 
94
- // 4. Register Settings Listeners
95
- if (dom.apiKeyInput) {
96
- dom.apiKeyInput.addEventListener("input", (e) => {
97
- STATE.apiKey = e.target.value;
98
- localStorage.setItem("step_api_key", STATE.apiKey);
99
- });
100
- }
101
-
102
- if (dom.toggleKeyVisibility) {
103
- dom.toggleKeyVisibility.addEventListener("click", () => {
104
- if (dom.apiKeyInput) {
105
- const type = dom.apiKeyInput.type === "password" ? "text" : "password";
106
- dom.apiKeyInput.type = type;
107
- }
108
- });
109
- }
110
 
111
  if (dom.effortRadioButtons) {
112
  dom.effortRadioButtons.forEach(radio => {
@@ -370,13 +347,6 @@ async function triggerPromptSubmission(inputElement) {
370
  const promptText = inputElement.value.trim();
371
  if (!promptText && STATE.uploadedFiles.length === 0) return;
372
 
373
- // Check credentials
374
- if (!STATE.apiKey && !dom.apiKeyInput.placeholder.includes("server env")) {
375
- appendSystemLog("StepFun API Key is required. Please set it in the settings card drawer.", true);
376
- toggleSettingsDrawer(true);
377
- return;
378
- }
379
-
380
  setLoadingState(true);
381
 
382
  // 1. Format user message contents
@@ -445,7 +415,6 @@ async function triggerPromptSubmission(inputElement) {
445
  // Call our gradio.Server api endpoint using named object parameters matching Python arguments
446
  const result = await STATE.gradioClient.predict("/chat_with_step", {
447
  messages_json: JSON.stringify(STATE.conversationHistory),
448
- api_key: STATE.apiKey,
449
  reasoning_effort: STATE.reasoningEffort,
450
  max_tokens: STATE.maxTokens,
451
  temperature: STATE.temperature
 
2
 
3
  // Global Application State Management
4
  const STATE = {
 
5
  reasoningEffort: "medium",
6
  maxTokens: 2048,
7
  temperature: 0.7,
 
13
 
14
  // DOM Elements hooks
15
  const dom = {
 
 
16
  effortRadioButtons: document.querySelectorAll('input[name="reasoning-effort"]'),
17
  maxTokensSlider: document.getElementById("max-tokens-slider"),
18
  maxTokensVal: document.getElementById("max-tokens-val"),
 
68
 
69
  // Setup Initial State & Event Handlers
70
  async function initializeApp() {
71
+ // 1. Connect Gradio Client in background (Non-blocking)
 
 
 
 
 
72
  client(window.location.origin)
73
  .then(app => {
74
  STATE.gradioClient = app;
 
78
  console.error("Gradio Client Connection Failed:", e);
79
  });
80
 
81
+ // 2. Register Sidebar Drawer Slide Events (Drawer + Overlay)
82
  if (dom.btnToggleRight) dom.btnToggleRight.addEventListener("click", () => toggleSettingsDrawer(true));
83
  if (dom.btnCloseDrawer) dom.btnCloseDrawer.addEventListener("click", () => toggleSettingsDrawer(false));
84
  if (dom.sidebarOverlay) dom.sidebarOverlay.addEventListener("click", () => toggleSettingsDrawer(false));
85
 
86
+ // 3. Register Settings Listeners
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if (dom.effortRadioButtons) {
89
  dom.effortRadioButtons.forEach(radio => {
 
347
  const promptText = inputElement.value.trim();
348
  if (!promptText && STATE.uploadedFiles.length === 0) return;
349
 
 
 
 
 
 
 
 
350
  setLoadingState(true);
351
 
352
  // 1. Format user message contents
 
415
  // Call our gradio.Server api endpoint using named object parameters matching Python arguments
416
  const result = await STATE.gradioClient.predict("/chat_with_step", {
417
  messages_json: JSON.stringify(STATE.conversationHistory),
 
418
  reasoning_effort: STATE.reasoningEffort,
419
  max_tokens: STATE.maxTokens,
420
  temperature: STATE.temperature
static/index.html CHANGED
@@ -125,18 +125,7 @@
125
  </div>
126
 
127
  <div class="sidebar-right-content">
128
- <!-- Credentials -->
129
- <div class="right-block-card">
130
- <div class="card-block-header">
131
- <span class="block-title">Credentials</span>
132
- </div>
133
- <div class="password-wrapper">
134
- <input type="password" id="api-key-input" placeholder="Enter key or use server env..." autocomplete="off">
135
- <button type="button" id="toggle-key-visibility" class="visibility-toggle" title="Show/Hide">
136
- <svg viewBox="0 0 24 24" width="14" height="14" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path><circle cx="12" cy="12" r="3"></circle></svg>
137
- </button>
138
- </div>
139
- </div>
140
 
141
  <!-- Reasoning mode -->
142
  <div class="right-block-card">
 
125
  </div>
126
 
127
  <div class="sidebar-right-content">
128
+
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  <!-- Reasoning mode -->
131
  <div class="right-block-card">