Spaces:
Running on Zero
Running on Zero
Commit ·
316f370
1
Parent(s): fb2297e
fixing
Browse files
app.py
CHANGED
|
@@ -44,7 +44,7 @@ except (ImportError, RuntimeError, OSError):
|
|
| 44 |
import spaces
|
| 45 |
from typing import List, Literal, Optional
|
| 46 |
from pydantic import BaseModel, Field
|
| 47 |
-
from fastapi import
|
| 48 |
import gradio as gr
|
| 49 |
|
| 50 |
# =============================================================
|
|
@@ -69,11 +69,14 @@ SYSTEM_PROMPT = (
|
|
| 69 |
"earnings, and low traffic delays."
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
@spaces.GPU
|
| 73 |
-
def
|
| 74 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 75 |
-
if
|
| 76 |
-
for m in
|
| 77 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 78 |
messages.append({"role": "user", "content": prompt})
|
| 79 |
response = llm.create_chat_completion(
|
|
@@ -82,10 +85,27 @@ def run_inference(prompt: str, history: List[dict] = None) -> str:
|
|
| 82 |
return response["choices"][0]["message"]["content"]
|
| 83 |
|
| 84 |
# =============================================================
|
| 85 |
-
#
|
| 86 |
# =============================================================
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
class HistoryMessage(BaseModel):
|
| 90 |
role: Literal["user", "assistant"]
|
| 91 |
content: str = Field(min_length=1, max_length=4000)
|
|
@@ -96,12 +116,12 @@ class ChatRequest(BaseModel):
|
|
| 96 |
current_zone_id: Optional[str] = None
|
| 97 |
history: List[HistoryMessage] = Field(default_factory=list)
|
| 98 |
|
| 99 |
-
@app.post("/chat")
|
| 100 |
-
def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None)):
|
| 101 |
if API_KEY and x_api_key != API_KEY:
|
| 102 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API Key")
|
| 103 |
formatted_history = [{"role": m.role, "content": m.content} for m in payload.history]
|
| 104 |
-
answer =
|
| 105 |
return {
|
| 106 |
"status": "success",
|
| 107 |
"answer": answer,
|
|
@@ -110,30 +130,7 @@ def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None))
|
|
| 110 |
}
|
| 111 |
|
| 112 |
# =============================================================
|
| 113 |
-
#
|
| 114 |
-
# =============================================================
|
| 115 |
-
def gradio_chat(user_message, history):
|
| 116 |
-
hist_formatted = []
|
| 117 |
-
if history:
|
| 118 |
-
for user_msg, bot_msg in history:
|
| 119 |
-
hist_formatted.append({"role": "user", "content": user_msg})
|
| 120 |
-
if bot_msg:
|
| 121 |
-
hist_formatted.append({"role": "assistant", "content": bot_msg})
|
| 122 |
-
return run_inference(user_message, hist_formatted)
|
| 123 |
-
|
| 124 |
-
demo = gr.ChatInterface(
|
| 125 |
-
fn=gradio_chat,
|
| 126 |
-
title="🧭 CommitCon Sherpa Driver Copilot",
|
| 127 |
-
description="Fine-tuned Qwen 0.8B Q4_K_M — llama-cpp-python inference"
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
# Mount Gradio UI on the FastAPI app
|
| 131 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 132 |
-
|
| 133 |
-
# =============================================================
|
| 134 |
-
# 5. Uvicorn Runner on Environment PORT
|
| 135 |
# =============================================================
|
| 136 |
if __name__ == "__main__":
|
| 137 |
-
|
| 138 |
-
port = int(os.environ.get("PORT", 7860))
|
| 139 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 44 |
import spaces
|
| 45 |
from typing import List, Literal, Optional
|
| 46 |
from pydantic import BaseModel, Field
|
| 47 |
+
from fastapi import HTTPException, Header, Request
|
| 48 |
import gradio as gr
|
| 49 |
|
| 50 |
# =============================================================
|
|
|
|
| 69 |
"earnings, and low traffic delays."
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# =============================================================
|
| 73 |
+
# 3. Core ZeroGPU Inference function
|
| 74 |
+
# =============================================================
|
| 75 |
@spaces.GPU
|
| 76 |
+
def generate_response(prompt: str, history_messages: list = None) -> str:
|
| 77 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 78 |
+
if history_messages:
|
| 79 |
+
for m in history_messages:
|
| 80 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 81 |
messages.append({"role": "user", "content": prompt})
|
| 82 |
response = llm.create_chat_completion(
|
|
|
|
| 85 |
return response["choices"][0]["message"]["content"]
|
| 86 |
|
| 87 |
# =============================================================
|
| 88 |
+
# 4. Gradio Chat UI with @spaces.GPU
|
| 89 |
# =============================================================
|
| 90 |
+
@spaces.GPU
|
| 91 |
+
def gradio_chat(user_message, history):
|
| 92 |
+
hist_formatted = []
|
| 93 |
+
if history:
|
| 94 |
+
for user_msg, bot_msg in history:
|
| 95 |
+
hist_formatted.append({"role": "user", "content": user_msg})
|
| 96 |
+
if bot_msg:
|
| 97 |
+
hist_formatted.append({"role": "assistant", "content": bot_msg})
|
| 98 |
+
return generate_response(user_message, hist_formatted)
|
| 99 |
|
| 100 |
+
demo = gr.ChatInterface(
|
| 101 |
+
fn=gradio_chat,
|
| 102 |
+
title="🧭 CommitCon Sherpa Driver Copilot",
|
| 103 |
+
description="Fine-tuned Qwen 0.8B Q4_K_M — llama-cpp-python inference"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# =============================================================
|
| 107 |
+
# 5. Attach POST /chat to demo.app using Gradio's native router
|
| 108 |
+
# =============================================================
|
| 109 |
class HistoryMessage(BaseModel):
|
| 110 |
role: Literal["user", "assistant"]
|
| 111 |
content: str = Field(min_length=1, max_length=4000)
|
|
|
|
| 116 |
current_zone_id: Optional[str] = None
|
| 117 |
history: List[HistoryMessage] = Field(default_factory=list)
|
| 118 |
|
| 119 |
+
@demo.app.post("/chat")
|
| 120 |
+
def chat_endpoint(payload: ChatRequest, request: Request, x_api_key: Optional[str] = Header(None)):
|
| 121 |
if API_KEY and x_api_key != API_KEY:
|
| 122 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API Key")
|
| 123 |
formatted_history = [{"role": m.role, "content": m.content} for m in payload.history]
|
| 124 |
+
answer = generate_response(payload.driver_prompt, formatted_history)
|
| 125 |
return {
|
| 126 |
"status": "success",
|
| 127 |
"answer": answer,
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
# =============================================================
|
| 133 |
+
# 6. Launch with demo.launch() (DO NOT USE UVICORN DIRECTLY ON ZEROGPU)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# =============================================================
|
| 135 |
if __name__ == "__main__":
|
| 136 |
+
demo.launch()
|
|
|
|
|
|