Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,71 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from openai import OpenAI
|
| 5 |
-
|
| 6 |
-
def get_local_ip():
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
ip =
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
additional_inputs=[
|
| 73 |
-
gr.Textbox(value="You are a friendly assistant.", label="System message"),
|
| 74 |
-
gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"),
|
| 75 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 76 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 77 |
-
],
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
if __name__ == "__main__":
|
| 81 |
-
demo.launch()
|
|
|
|
| 1 |
+
### app.py
|
| 2 |
+
import socket
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
def get_local_ip():
|
| 7 |
+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
| 8 |
+
try:
|
| 9 |
+
s.connect(("8.8.8.8", 80))
|
| 10 |
+
ip = s.getsockname()[0]
|
| 11 |
+
except Exception:
|
| 12 |
+
ip = "127.0.0.1"
|
| 13 |
+
finally:
|
| 14 |
+
s.close()
|
| 15 |
+
return ip
|
| 16 |
+
|
| 17 |
+
print("本機 IP:", get_local_ip())
|
| 18 |
+
|
| 19 |
+
# ✅ 設定 base URL 連接本地 llama.cpp API
|
| 20 |
+
client = OpenAI(
|
| 21 |
+
base_url="http://0.0.0.0:8000/v1",
|
| 22 |
+
api_key="sk-local", # llama.cpp 不檢查內容,只要有就行
|
| 23 |
+
timeout=600
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ✅ 回應函式 (流式 generator)
|
| 27 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 28 |
+
# history 是 list of dict: [{"role": "user"/"assistant", "content": "..."}]
|
| 29 |
+
messages = [{"role": "system", "content": system_message}]
|
| 30 |
+
messages.extend(history) # 直接加入舊對話
|
| 31 |
+
messages.append({"role": "user", "content": message})
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
stream = client.chat.completions.create(
|
| 35 |
+
model="qwen3", # ⚠️ 替換成你 llama.cpp 載入的模型 general.name
|
| 36 |
+
messages=messages,
|
| 37 |
+
max_tokens=max_tokens,
|
| 38 |
+
temperature=temperature,
|
| 39 |
+
top_p=top_p,
|
| 40 |
+
stream=True,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
output = ""
|
| 44 |
+
for chunk in stream:
|
| 45 |
+
# 🔍 Debug log
|
| 46 |
+
# print("[DEBUG] chunk:", chunk)
|
| 47 |
+
|
| 48 |
+
if chunk.choices:
|
| 49 |
+
delta = chunk.choices[0].delta
|
| 50 |
+
if delta and delta.content:
|
| 51 |
+
output += delta.content
|
| 52 |
+
yield {"role": "assistant", "content": output}
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"[Error] {e}")
|
| 56 |
+
yield {"role": "assistant", "content": "⚠️ Llama.cpp server 沒有回應,請稍後再試。"}
|
| 57 |
+
|
| 58 |
+
# ✅ Gradio 介面 (新版必須用 type="messages")
|
| 59 |
+
demo = gr.ChatInterface(
|
| 60 |
+
respond,
|
| 61 |
+
type="messages", # 🔑 使用 OpenAI 風格訊息格式
|
| 62 |
+
additional_inputs=[
|
| 63 |
+
gr.Textbox(value="You are a friendly assistant.", label="System message"),
|
| 64 |
+
gr.Slider(minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens"),
|
| 65 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 66 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 67 |
+
],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|