huduo commited on
Commit
7b77b37
·
verified ·
1 Parent(s): bd80a79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -1,12 +1,16 @@
 
1
  from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  import subprocess
4
 
 
 
 
5
  app = FastAPI()
6
 
7
- # 健康检查端点
8
  @app.get("/health")
9
  def health_check():
 
10
  return {"status": "ok"}
11
 
12
  class PromptRequest(BaseModel):
@@ -15,12 +19,14 @@ class PromptRequest(BaseModel):
15
  @app.post("/generate")
16
  async def generate_response(request: PromptRequest):
17
  try:
18
- # 调用 Ollama 模型生成响应
19
  result = subprocess.check_output(["ollama", "run", request.prompt], text=True)
 
20
  return {"response": result.strip()}
21
  except subprocess.CalledProcessError as e:
 
22
  raise HTTPException(status_code=500, detail=f"Error: {e}")
23
 
24
  @app.get("/")
25
  def read_root():
 
26
  return {"message": "Ollama API is running"}
 
1
+ import logging
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  import subprocess
5
 
6
+ # 配置日志
7
+ logging.basicConfig(level=logging.INFO)
8
+
9
  app = FastAPI()
10
 
 
11
  @app.get("/health")
12
  def health_check():
13
+ logging.info("Health check endpoint was called")
14
  return {"status": "ok"}
15
 
16
  class PromptRequest(BaseModel):
 
19
  @app.post("/generate")
20
  async def generate_response(request: PromptRequest):
21
  try:
 
22
  result = subprocess.check_output(["ollama", "run", request.prompt], text=True)
23
+ logging.info(f"Model response: {result.strip()}")
24
  return {"response": result.strip()}
25
  except subprocess.CalledProcessError as e:
26
+ logging.error(f"Error during model generation: {e}")
27
  raise HTTPException(status_code=500, detail=f"Error: {e}")
28
 
29
  @app.get("/")
30
  def read_root():
31
+ logging.info("Root endpoint was called")
32
  return {"message": "Ollama API is running"}