Spaces:
Runtime error
Runtime error
File size: 1,093 Bytes
807c6ce dd8a3aa 7b77b37 807c6ce dd8a3aa bd80a79 807c6ce dd8a3aa 807c6ce dd8a3aa 807c6ce dd8a3aa 807c6ce dd8a3aa 807c6ce dd8a3aa 807c6ce dd8a3aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import subprocess
import logging
app = FastAPI()
# 这里设置日志级别为 DEBUG
logging.basicConfig(level=logging.DEBUG)
class PromptRequest(BaseModel):
prompt: str
@app.on_event("startup")
async def startup_event():
logging.debug("Starting application...")
@app.post("/generate")
async def generate_response(request: PromptRequest):
try:
logging.debug(f"Received prompt: {request.prompt}")
result = subprocess.check_output(["ollama", "run", request.prompt], text=True)
logging.debug(f"Model response: {result.strip()}")
return {"response": result.strip()}
except subprocess.CalledProcessError as e:
logging.error(f"Error calling ollama: {e}")
raise HTTPException(status_code=500, detail=f"Error: {e}")
@app.get("/")
def read_root():
logging.debug("Received GET request on root")
return {"message": "Ollama API is running"}
@app.get("/health")
def health_check():
logging.debug("Health check received")
return {"status": "ok"}
|