broccoli0510 Claude Opus 5.5 commited on
Commit
8204a67
·
1 Parent(s): cce5088

Extract URL from LLM_BASE_URL when pasted as a Markdown link

Browse files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +8 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
 
3
  import gradio as gr
4
  from openai import OpenAI
@@ -23,7 +24,13 @@ def _env(name, default=""):
23
 
24
 
25
  # 讀取 Space 的環境變數;API Key 請設在 Space Settings → Variables and secrets 的 Secret(勿寫在程式碼裡)
26
- BASE_URL = _env("LLM_BASE_URL", "https://api02.webinfo.tw/v1")
 
 
 
 
 
 
27
  API_KEY = _env("LLM_API_KEY")
28
  _RAW_KEY_LEN = len(os.getenv("LLM_API_KEY") or "") # 診斷用:只記長度,不記內容
29
  DEFAULT_MODEL = _env("LLM_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct-AWQ")
 
1
  import os
2
+ import re
3
 
4
  import gradio as gr
5
  from openai import OpenAI
 
24
 
25
 
26
  # 讀取 Space 的環境變數;API Key 請設在 Space Settings → Variables and secrets 的 Secret(勿寫在程式碼裡)
27
+ def _clean_url(value, default):
28
+ """從設定值抽出第一個網址;可處理從聊天軟體複製來的 Markdown 連結,例如 [https://x/v1](https://x/v1)。"""
29
+ m = re.search(r"https?://[^\s\[\]()<>\"']+", value or "")
30
+ return m.group(0).rstrip("/") if m else default
31
+
32
+
33
+ BASE_URL = _clean_url(_env("LLM_BASE_URL"), "https://api02.webinfo.tw/v1")
34
  API_KEY = _env("LLM_API_KEY")
35
  _RAW_KEY_LEN = len(os.getenv("LLM_API_KEY") or "") # 診斷用:只記長度,不記內容
36
  DEFAULT_MODEL = _env("LLM_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct-AWQ")