Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -42,16 +42,13 @@ except (ImportError, RuntimeError, OSError):
|
|
| 42 |
from llama_cpp import Llama
|
| 43 |
|
| 44 |
import spaces
|
| 45 |
-
from typing import List
|
| 46 |
-
from pydantic import BaseModel, Field
|
| 47 |
-
from fastapi import HTTPException, Header
|
| 48 |
import gradio as gr
|
| 49 |
|
| 50 |
# =============================================================
|
| 51 |
# 2. Load GGUF Model
|
| 52 |
# =============================================================
|
| 53 |
MODEL_PATH = "Qwen3.5-0.8B.Q4_K_M.gguf"
|
| 54 |
-
API_KEY = os.getenv("API_KEY", "commitcon-secret-2026")
|
| 55 |
|
| 56 |
print(f"Loading {MODEL_PATH} via llama-cpp-python ...")
|
| 57 |
llm = Llama(
|
|
@@ -69,7 +66,6 @@ SYSTEM_PROMPT = (
|
|
| 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}]
|
| 75 |
if history:
|
|
@@ -77,60 +73,36 @@ def run_inference(prompt: str, history: List[dict] = None) -> str:
|
|
| 77 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 78 |
messages.append({"role": "user", "content": prompt})
|
| 79 |
response = llm.create_chat_completion(
|
| 80 |
-
messages=messages, temperature=0.7, max_tokens=
|
| 81 |
)
|
| 82 |
return response["choices"][0]["message"]["content"]
|
| 83 |
|
| 84 |
# =============================================================
|
| 85 |
-
# 3. Gradio Interface (
|
| 86 |
# =============================================================
|
| 87 |
-
@spaces.GPU
|
| 88 |
def gradio_chat(user_message, history):
|
| 89 |
hist_formatted = []
|
| 90 |
if history:
|
| 91 |
-
for
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
hist_formatted.append({"role": "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 105 |
-
# =============================================================
|
| 106 |
-
class HistoryMessage(BaseModel):
|
| 107 |
-
role: Literal["user", "assistant"]
|
| 108 |
-
content: str = Field(min_length=1, max_length=4000)
|
| 109 |
-
|
| 110 |
-
class ChatRequest(BaseModel):
|
| 111 |
-
driver_id: Optional[str] = "DRV_DEFAULT"
|
| 112 |
-
driver_prompt: str = Field(min_length=1, max_length=2000)
|
| 113 |
-
recommendation_data: Optional[dict[str, Any]] = None
|
| 114 |
-
current_zone_id: Optional[str] = None
|
| 115 |
-
history: List[HistoryMessage] = Field(default_factory=list)
|
| 116 |
-
|
| 117 |
-
def chat_endpoint(payload: ChatRequest, x_api_key: Optional[str] = Header(None, alias="X-API-Key")):
|
| 118 |
-
if API_KEY and x_api_key != API_KEY:
|
| 119 |
-
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API Key")
|
| 120 |
-
formatted_history = [{"role": m.role, "content": m.content} for m in payload.history]
|
| 121 |
-
answer = run_inference(payload.driver_prompt, formatted_history)
|
| 122 |
-
return {
|
| 123 |
-
"status": "success",
|
| 124 |
-
"answer": answer,
|
| 125 |
-
"model": "project-driver-qwen-gguf",
|
| 126 |
-
"zone_context": payload.current_zone_id
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
# Register the POST /chat route directly into Gradio's underlying FastAPI router
|
| 130 |
-
demo.app.add_api_route("/chat", chat_endpoint, methods=["POST"])
|
| 131 |
-
|
| 132 |
-
# =============================================================
|
| 133 |
-
# 5. Native ZeroGPU Launch
|
| 134 |
# =============================================================
|
| 135 |
if __name__ == "__main__":
|
| 136 |
demo.launch()
|
|
|
|
| 42 |
from llama_cpp import Llama
|
| 43 |
|
| 44 |
import spaces
|
| 45 |
+
from typing import List
|
|
|
|
|
|
|
| 46 |
import gradio as gr
|
| 47 |
|
| 48 |
# =============================================================
|
| 49 |
# 2. Load GGUF Model
|
| 50 |
# =============================================================
|
| 51 |
MODEL_PATH = "Qwen3.5-0.8B.Q4_K_M.gguf"
|
|
|
|
| 52 |
|
| 53 |
print(f"Loading {MODEL_PATH} via llama-cpp-python ...")
|
| 54 |
llm = Llama(
|
|
|
|
| 66 |
"earnings, and low traffic delays."
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
def run_inference(prompt: str, history: List[dict] = None) -> str:
|
| 70 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 71 |
if history:
|
|
|
|
| 73 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 74 |
messages.append({"role": "user", "content": prompt})
|
| 75 |
response = llm.create_chat_completion(
|
| 76 |
+
messages=messages, temperature=0.7, max_tokens=256
|
| 77 |
)
|
| 78 |
return response["choices"][0]["message"]["content"]
|
| 79 |
|
| 80 |
# =============================================================
|
| 81 |
+
# 3. Gradio Interface (duration=15 ensures ZeroGPU quota is preserved)
|
| 82 |
# =============================================================
|
| 83 |
+
@spaces.GPU(duration=15)
|
| 84 |
def gradio_chat(user_message, history):
|
| 85 |
hist_formatted = []
|
| 86 |
if history:
|
| 87 |
+
for item in history:
|
| 88 |
+
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 89 |
+
u, b = item
|
| 90 |
+
hist_formatted.append({"role": "user", "content": str(u)})
|
| 91 |
+
if b:
|
| 92 |
+
hist_formatted.append({"role": "assistant", "content": str(b)})
|
| 93 |
+
elif isinstance(item, dict):
|
| 94 |
+
hist_formatted.append(item)
|
| 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 |
+
api_name="chat"
|
| 102 |
)
|
| 103 |
|
| 104 |
# =============================================================
|
| 105 |
+
# 4. Native ZeroGPU Launch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# =============================================================
|
| 107 |
if __name__ == "__main__":
|
| 108 |
demo.launch()
|