Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,19 @@
|
|
| 1 |
-
#
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
from openai import OpenAI
|
| 5 |
|
| 6 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
api_key="sk-local", # llama.cpp 不檢查內容,只要有就行
|
| 23 |
-
timeout=600
|
| 24 |
-
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
try:
|
| 33 |
-
stream = client.chat.completions.create(
|
| 34 |
-
model="qwen3",
|
| 35 |
-
messages=messages,
|
| 36 |
-
max_tokens=max_tokens,
|
| 37 |
-
temperature=temperature,
|
| 38 |
-
top_p=top_p,
|
| 39 |
-
reasoning_effort="low",
|
| 40 |
-
stream=True,
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
output = ""
|
| 44 |
-
skip_tokens = ['<|channel|>', 'analysis']
|
| 45 |
-
skipped = {token: False for token in skip_tokens} # 追蹤每個 token 是否已忽略過
|
| 46 |
-
|
| 47 |
-
for chunk in stream:
|
| 48 |
-
print("[DEBUG] chunk:", chunk)
|
| 49 |
-
delta = chunk.choices[0].delta
|
| 50 |
-
if delta and delta.content:
|
| 51 |
-
content = delta.content.strip()
|
| 52 |
-
# 如果這個 token 是要跳過的,且還沒被跳過過
|
| 53 |
-
if content in skip_tokens and not skipped[content]:
|
| 54 |
-
skipped[content] = True
|
| 55 |
-
continue
|
| 56 |
-
output += delta.content # 正常加入輸出
|
| 57 |
-
|
| 58 |
-
yield {"role": "assistant", "content": output}
|
| 59 |
-
|
| 60 |
-
except Exception as e:
|
| 61 |
-
print(f"[Error] {e}")
|
| 62 |
-
yield {"role": "assistant", "content": "⚠️ Llama.cpp server 沒有回應,請稍後再試。"}
|
| 63 |
-
|
| 64 |
-
# ✅ Gradio 介面 (新版必須用 type="messages")
|
| 65 |
-
demo = gr.ChatInterface(
|
| 66 |
-
respond,
|
| 67 |
-
type="messages", # 🔑 使用 OpenAI 風格訊息格式
|
| 68 |
-
additional_inputs=[
|
| 69 |
-
gr.Textbox(value="You are a friendly assistant.", label="System message"),
|
| 70 |
-
gr.Slider(minimum=1, maximum=16383, value=4096, step=1, label="Max new tokens"),
|
| 71 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 72 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 73 |
-
],
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
if __name__ == "__main__":
|
| 77 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
|
|
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# 定義輸入/輸出資料格式
|
| 8 |
+
class Item(BaseModel):
|
| 9 |
+
text: str
|
| 10 |
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def root():
|
| 13 |
+
return {"message": "Hello from FastAPI on HF Space!"}
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@app.post("/predict")
|
| 16 |
+
def predict(item: Item):
|
| 17 |
+
# 模擬一個簡單推論邏輯
|
| 18 |
+
result = item.text.upper()
|
| 19 |
+
return {"input": item.text, "result": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|