heartwood / server.py
vivek gangadharan
switch to ZeroGPU: transformers pipeline, drop llama-cpp-python
5ec760d
Raw
History Blame Contribute Delete
1.45 kB
from __future__ import annotations
import os
from gradio import Server
from fastapi.responses import HTMLResponse, FileResponse
import ai
app = Server()
HERE = os.path.dirname(os.path.abspath(__file__))
try:
from fastapi.staticfiles import StaticFiles
app.mount("/nasset", StaticFiles(directory=os.path.join(HERE, "nasset")), name="nasset")
except Exception as exc:
print(f"[heartwood] could not mount /nasset: {exc}")
@app.get("/quest")
def quest(level: int = 1):
return ai.new_quest(level)
@app.get("/challenge")
def challenge(npc: str = ""):
return ai.challenge_for(npc)
@app.get("/judge")
def judge(npc: str = "", mood: str = "", text: str = ""):
return ai.judge(npc, mood, text)
@app.get("/chat")
def chat(npc: str = "", text: str = ""):
return {"npc": npc, "line": ai.chat_line(npc, text)}
@app.get("/main.js")
def mainjs():
return FileResponse(os.path.join(HERE, "main.js"), media_type="application/javascript")
@app.get("/", response_class=HTMLResponse)
async def homepage():
with open(os.path.join(HERE, "index.html"), "r", encoding="utf-8") as f:
return f.read()
if os.environ.get("SS_LLM", "1") == "1":
import threading
import llm
threading.Thread(target=llm.warmup, daemon=True).start()
if __name__ == "__main__":
os.environ.setdefault("GRADIO_SERVER_NAME", "0.0.0.0")
os.environ.setdefault("GRADIO_SERVER_PORT", "7860")
app.launch(show_error=True)