Spaces:
Sleeping
Sleeping
Commit ·
d9899a8
1
Parent(s): 979ad70
Fix empty C++ code blocks in chat replies
Browse filesGradio 6.31's frontend lazy-loads the Prism cpp grammar without its c
dependency, so highlighting throws and the code block renders empty.
Rewrite C++ fence tags to c before display and use c in the example.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -33,6 +33,16 @@ except Exception:
|
|
| 33 |
return text
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# ---------------------------------------------------------------------------
|
| 37 |
# 設定
|
| 38 |
# ---------------------------------------------------------------------------
|
|
@@ -135,7 +145,7 @@ def respond(message, history, model, system_prompt, max_tokens, temperature):
|
|
| 135 |
for chunk in stream:
|
| 136 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 137 |
partial += chunk.choices[0].delta.content
|
| 138 |
-
yield _to_tw(partial)
|
| 139 |
if not partial:
|
| 140 |
yield "(模型沒有回傳內容,請再試一次)"
|
| 141 |
except Exception as e:
|
|
@@ -244,7 +254,7 @@ with gr.Blocks(title="程式設計小幫手") as demo:
|
|
| 244 |
],
|
| 245 |
examples=[
|
| 246 |
["我的 Python 程式出現 IndexError,為什麼?\n```python\nnums = [1, 2, 3]\nfor i in range(len(nums) + 1):\n print(nums[i])\n```"],
|
| 247 |
-
["這段 C++ 編譯不過,錯在哪裡?\n```
|
| 248 |
["寫一個 Python 函式,判斷字串是否為回文。"],
|
| 249 |
["請說明 C++ 的指標(pointer)和參照(reference)的差別。"],
|
| 250 |
],
|
|
|
|
| 33 |
return text
|
| 34 |
|
| 35 |
|
| 36 |
+
# Gradio 6 前端的 Prism 載入 cpp 語法時沒有先載入它依賴的 c 語法,會丟出 TypeError,
|
| 37 |
+
# 導致 ```cpp 程式碼區塊整段顯示成空白。把 C++ 系列的語言標籤改成 c(上色效果幾乎相同)。
|
| 38 |
+
_CPP_FENCE = re.compile(r"^([ \t]*(?:```|~~~)[ \t]*)(?:c\+\+|cpp|cxx|cc|hpp|hxx|h)(?=[ \t]*$|\s)",
|
| 39 |
+
re.IGNORECASE | re.MULTILINE)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _fix_code_fences(text):
|
| 43 |
+
return _CPP_FENCE.sub(r"\1c", text)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
# ---------------------------------------------------------------------------
|
| 47 |
# 設定
|
| 48 |
# ---------------------------------------------------------------------------
|
|
|
|
| 145 |
for chunk in stream:
|
| 146 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 147 |
partial += chunk.choices[0].delta.content
|
| 148 |
+
yield _fix_code_fences(_to_tw(partial))
|
| 149 |
if not partial:
|
| 150 |
yield "(模型沒有回傳內容,請再試一次)"
|
| 151 |
except Exception as e:
|
|
|
|
| 254 |
],
|
| 255 |
examples=[
|
| 256 |
["我的 Python 程式出現 IndexError,為什麼?\n```python\nnums = [1, 2, 3]\nfor i in range(len(nums) + 1):\n print(nums[i])\n```"],
|
| 257 |
+
["這段 C++ 編譯不過,錯在哪裡?\n```c\n#include <iostream>\nint main() {\n std::cout << \"hi\" << std::endl\n return 0;\n}\n```"],
|
| 258 |
["寫一個 Python 函式,判斷字串是否為回文。"],
|
| 259 |
["請說明 C++ 的指標(pointer)和參照(reference)的差別。"],
|
| 260 |
],
|