abanm commited on
Commit
f4ed706
·
verified ·
1 Parent(s): cc53a24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -1,18 +1,39 @@
1
  import os
2
  import subprocess
 
3
 
4
  from fastapi import FastAPI, Request, HTTPException
 
 
 
 
 
5
 
6
  app = FastAPI()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Load your API key from the environment (defaults to "change_me")
9
  OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY", "change_me")
10
 
11
- @app.post("/generate")
12
  async def generate(request: Request):
13
  """Endpoint that generates text based on the prompt."""
14
  # 1. Check API key
15
  auth_header = request.headers.get("Authorization")
 
 
16
  if not auth_header or not auth_header.startswith("Bearer "):
17
  raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
18
 
@@ -44,6 +65,7 @@ async def generate(request: Request):
44
  output, error = process.communicate(timeout=60) # Timeout after 60 seconds
45
 
46
  if process.returncode != 0:
 
47
  raise Exception(error.strip())
48
 
49
  except subprocess.TimeoutExpired:
@@ -51,15 +73,21 @@ async def generate(request: Request):
51
  raise HTTPException(status_code=500, detail="Ollama request timed out")
52
 
53
  except Exception as e:
 
54
  raise HTTPException(
55
  status_code=500,
56
  detail=f"Ollama error: {str(e)}"
57
  )
58
 
 
59
  return {"response": output.strip()}
60
 
 
 
 
 
 
61
  if __name__ == "__main__":
62
  import uvicorn
63
- uvicorn.run(app, host="0.0.0.0", port=8080) # Changed from 7860 to 8080
64
-
65
 
 
1
  import os
2
  import subprocess
3
+ import logging
4
 
5
  from fastapi import FastAPI, Request, HTTPException
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+
8
+ # Configure logging
9
+ logging.basicConfig(level=logging.INFO)
10
+ logger = logging.getLogger(__name__)
11
 
12
  app = FastAPI()
13
 
14
+ # Optional: Configure CORS if needed
15
+ origins = [
16
+ # Add allowed origins if you implement a frontend later
17
+ ]
18
+
19
+ app.add_middleware(
20
+ CORSMiddleware,
21
+ allow_origins=origins, # Adjust as needed
22
+ allow_credentials=True,
23
+ allow_methods=["*"],
24
+ allow_headers=["*"],
25
+ )
26
+
27
  # Load your API key from the environment (defaults to "change_me")
28
  OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY", "change_me")
29
 
30
+ @app.post("/api/generate")
31
  async def generate(request: Request):
32
  """Endpoint that generates text based on the prompt."""
33
  # 1. Check API key
34
  auth_header = request.headers.get("Authorization")
35
+ logger.info(f"Received Authorization header: {auth_header}")
36
+
37
  if not auth_header or not auth_header.startswith("Bearer "):
38
  raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
39
 
 
65
  output, error = process.communicate(timeout=60) # Timeout after 60 seconds
66
 
67
  if process.returncode != 0:
68
+ logger.error(f"Ollama Error: {error.strip()}")
69
  raise Exception(error.strip())
70
 
71
  except subprocess.TimeoutExpired:
 
73
  raise HTTPException(status_code=500, detail="Ollama request timed out")
74
 
75
  except Exception as e:
76
+ logger.error(f"Ollama Exception: {str(e)}")
77
  raise HTTPException(
78
  status_code=500,
79
  detail=f"Ollama error: {str(e)}"
80
  )
81
 
82
+ logger.info("Ollama response successfully generated.")
83
  return {"response": output.strip()}
84
 
85
+ @app.get("/health")
86
+ async def health():
87
+ """Health check endpoint."""
88
+ return {"status": "OK"}
89
+
90
  if __name__ == "__main__":
91
  import uvicorn
92
+ uvicorn.run(app, host="0.0.0.0", port=8080)
 
93