Commit ·
2419d6a
1
Parent(s): 2c0a1bd
fixed
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 HTTPException, Header
|
| 48 |
import gradio as gr
|
| 49 |
|
| 50 |
# =============================================================
|
|
@@ -69,7 +69,6 @@ SYSTEM_PROMPT = (
|
|
| 69 |
"earnings, and low traffic delays."
|
| 70 |
)
|
| 71 |
|
| 72 |
-
# ZeroGPU decorator satisfies HF's startup check
|
| 73 |
@spaces.GPU
|
| 74 |
def run_inference(prompt: str, history: List[dict] = None) -> str:
|
| 75 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
@@ -83,26 +82,10 @@ def run_inference(prompt: str, history: List[dict] = None) -> str:
|
|
| 83 |
return response["choices"][0]["message"]["content"]
|
| 84 |
|
| 85 |
# =============================================================
|
| 86 |
-
# 3.
|
| 87 |
# =============================================================
|
| 88 |
-
|
| 89 |
-
hist_formatted = []
|
| 90 |
-
if history:
|
| 91 |
-
for user_msg, bot_msg in history:
|
| 92 |
-
hist_formatted.append({"role": "user", "content": user_msg})
|
| 93 |
-
if bot_msg:
|
| 94 |
-
hist_formatted.append({"role": "assistant", "content": bot_msg})
|
| 95 |
-
return run_inference(user_message, hist_formatted)
|
| 96 |
|
| 97 |
-
demo = gr.ChatInterface(
|
| 98 |
-
fn=gradio_chat,
|
| 99 |
-
title="🧭 CommitCon Sherpa Driver Copilot",
|
| 100 |
-
description="Fine-tuned Qwen 0.8B Q4_K_M — llama-cpp-python inference"
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
# =============================================================
|
| 104 |
-
# 4. Attach FastAPI Endpoints to demo.app
|
| 105 |
-
# =============================================================
|
| 106 |
class HistoryMessage(BaseModel):
|
| 107 |
role: Literal["user", "assistant"]
|
| 108 |
content: str = Field(min_length=1, max_length=4000)
|
|
@@ -113,7 +96,7 @@ class ChatRequest(BaseModel):
|
|
| 113 |
current_zone_id: Optional[str] = None
|
| 114 |
history: List[HistoryMessage] = Field(default_factory=list)
|
| 115 |
|
| 116 |
-
@
|
| 117 |
def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None)):
|
| 118 |
if API_KEY and x_api_key != API_KEY:
|
| 119 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API Key")
|
|
@@ -127,7 +110,28 @@ def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None))
|
|
| 127 |
}
|
| 128 |
|
| 129 |
# =============================================================
|
| 130 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# =============================================================
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
demo.launch()
|
|
|
|
| 44 |
import spaces
|
| 45 |
from typing import List, Literal, Optional
|
| 46 |
from pydantic import BaseModel, Field
|
| 47 |
+
from fastapi import FastAPI, HTTPException, Header
|
| 48 |
import gradio as gr
|
| 49 |
|
| 50 |
# =============================================================
|
|
|
|
| 69 |
"earnings, and low traffic delays."
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
@spaces.GPU
|
| 73 |
def run_inference(prompt: str, history: List[dict] = None) -> str:
|
| 74 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 82 |
return response["choices"][0]["message"]["content"]
|
| 83 |
|
| 84 |
# =============================================================
|
| 85 |
+
# 3. Create FastAPI app & define POST /chat endpoint
|
| 86 |
# =============================================================
|
| 87 |
+
app = FastAPI(title="CommitCon Sherpa AI Copilot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
class HistoryMessage(BaseModel):
|
| 90 |
role: Literal["user", "assistant"]
|
| 91 |
content: str = Field(min_length=1, max_length=4000)
|
|
|
|
| 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")
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
# =============================================================
|
| 113 |
+
# 4. Gradio Chat UI mounted at /ui (avoiding root conflict)
|
| 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 cleanly without hijacking API endpoints
|
| 131 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 132 |
+
|
| 133 |
+
# =============================================================
|
| 134 |
+
# 5. Native Launch using demo.launch(app=...)
|
| 135 |
# =============================================================
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
demo.launch(app=app)
|