Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# 老師的後端設定
|
| 6 |
+
BASE_URL = os.getenv("LLM_BASE_URL", "https://api01.webinfo.tw/v1")
|
| 7 |
+
API_KEY = os.getenv("LLM_API_KEY", "2ea4b45b49572d2c3ebd1be5fe2331a2e5599e5e1bdc4ddc172e9c9978fcc86b")
|
| 8 |
+
|
| 9 |
+
client = OpenAI(base_url=BASE_URL, api_key=API_KEY or "missing-key", timeout=120)
|
| 10 |
+
|
| 11 |
+
def predict(message, history, model, system_prompt, max_tokens, temperature):
|
| 12 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 13 |
+
for human, assistant in history:
|
| 14 |
+
messages.append({"role": "user", "content": human})
|
| 15 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 16 |
+
messages.append({"role": "user", "content": message})
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model=model,
|
| 21 |
+
messages=messages,
|
| 22 |
+
max_tokens=int(max_tokens),
|
| 23 |
+
temperature=float(temperature),
|
| 24 |
+
stream=True
|
| 25 |
+
)
|
| 26 |
+
partial_message = ""
|
| 27 |
+
for chunk in response:
|
| 28 |
+
if chunk.choices[0].delta.content:
|
| 29 |
+
partial_message += chunk.choices[0].delta.content
|
| 30 |
+
yield partial_message
|
| 31 |
+
except Exception as e:
|
| 32 |
+
yield f"連線錯誤或後端異常:{str(e)}"
|
| 33 |
+
|
| 34 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="程式設計小幫手") as demo:
|
| 35 |
+
gr.Markdown("## 程式設計小幫手\n提出程式設計相關的問題,回答由自架的開源大型語言模型產生。")
|
| 36 |
+
|
| 37 |
+
with gr.Tabs():
|
| 38 |
+
with gr.TabItem("對話"):
|
| 39 |
+
with gr.Row():
|
| 40 |
+
model_dropdown = gr.Dropdown(
|
| 41 |
+
choices=[
|
| 42 |
+
"Qwen/Qwen2.5-Coder-7B-Instruct-AWQ",
|
| 43 |
+
"gemma-3-taide-12b-chat",
|
| 44 |
+
"gemma-3-4b-it"
|
| 45 |
+
],
|
| 46 |
+
value="Qwen/Qwen2.5-Coder-7B-Instruct-AWQ",
|
| 47 |
+
label="模型"
|
| 48 |
+
)
|
| 49 |
+
system_prompt_input = gr.Textbox(
|
| 50 |
+
label="系統提示詞",
|
| 51 |
+
value="你是一位樂於助人的程式設計助理。請一律使用繁體中文回答,清楚說明概念,撰寫正確且易讀的程式碼,並指出錯誤或邊界情況。程式碼請放在附上語言名稱的 fenced code block 中。",
|
| 52 |
+
lines=2
|
| 53 |
+
)
|
| 54 |
+
with gr.Row():
|
| 55 |
+
max_tokens_slider = gr.Slider(minimum=32, maximum=2048, value=512, step=1, label="最大輸出 token 數")
|
| 56 |
+
temp_slider = gr.Slider(minimum=0.0, maximum=1.5, value=0.2, step=0.05, label="溫度")
|
| 57 |
+
|
| 58 |
+
gr.ChatInterface(
|
| 59 |
+
fn=predict,
|
| 60 |
+
additional_inputs=[model_dropdown, system_prompt_input, max_tokens_slider, temp_slider],
|
| 61 |
+
chatbot=gr.Chatbot(type="messages", height=500),
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.TabItem("伺服器狀態"):
|
| 65 |
+
gr.Markdown("### 實驗室 LLM 伺服器狀態\n- **Base URL**: `https://api01.webinfo.tw/v1`\n- **狀態**: 連線正常 (使用實驗室顯卡 GPU 進行推論運算)")
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|