abanm commited on
Commit
dc0cbdf
·
verified ·
1 Parent(s): 77a34d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -47
app.py CHANGED
@@ -1,47 +1,61 @@
1
- import os
2
- import subprocess
3
-
4
- from fastapi import FastAPI, Request, HTTPException
5
- import uvicorn
6
-
7
- app = FastAPI()
8
-
9
- # Load your API key from environment (set this in HF Secrets for security)
10
- OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY", "change_me")
11
-
12
- @app.post("/generate")
13
- async def generate(request: Request):
14
- """Endpoint that generates text based on the prompt."""
15
- # 1. Check API key
16
- auth_header = request.headers.get("Authorization")
17
- if not auth_header or not auth_header.startswith("Bearer "):
18
- raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
19
-
20
- token = auth_header.split(" ")[1]
21
- if token != OLLAMA_API_KEY:
22
- raise HTTPException(status_code=401, detail="Invalid API key")
23
-
24
- # 2. Parse the request JSON
25
- body = await request.json()
26
- prompt_text = body.get("prompt", "")
27
- if not prompt_text:
28
- raise HTTPException(status_code=400, detail="No prompt provided")
29
-
30
- # 3. Option A: Call Ollama via its CLI directly
31
- # (The model is served in the background via `ollama serve`)
32
- process = subprocess.Popen(
33
- ["ollama", "run", prompt_text],
34
- stdout=subprocess.PIPE,
35
- stderr=subprocess.PIPE,
36
- text=True
37
- )
38
- output, error = process.communicate()
39
-
40
- if process.returncode != 0 or error:
41
- raise HTTPException(status_code=500, detail=f"Ollama error: {error.strip()}")
42
-
43
- return {"response": output.strip()}
44
-
45
-
46
- if __name__ == "__main__":
47
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+
4
+ from fastapi import FastAPI, Request, HTTPException
5
+ import uvicorn
6
+
7
+ app = FastAPI()
8
+
9
+ # Load your API key from the environment (defaults to "change_me")
10
+ OLLAMA_API_KEY = os.environ.get("OLLAMA_API_KEY", "change_me")
11
+
12
+ @app.post("/generate")
13
+ async def generate(request: Request):
14
+ """Endpoint that generates text based on the prompt."""
15
+ # 1. Check API key
16
+ auth_header = request.headers.get("Authorization")
17
+ if not auth_header or not auth_header.startswith("Bearer "):
18
+ raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
19
+
20
+ token = auth_header.split(" ")[1]
21
+ if token != OLLAMA_API_KEY:
22
+ raise HTTPException(status_code=401, detail="Invalid API key")
23
+
24
+ # 2. Parse the request JSON
25
+ body = await request.json()
26
+ prompt_text = body.get("prompt", "")
27
+ if not prompt_text:
28
+ raise HTTPException(status_code=400, detail="No prompt provided")
29
+
30
+ # 3. Call Ollama via CLI (example: using a HF model)
31
+ # If you want a default model, you can use just "ollama run" with the prompt.
32
+ # If you want a specific HF model, specify it here:
33
+ # "ollama run hf.co/abanm/Dubs-Q8_0-GGUF prompt_text"
34
+ # For now, let's illustrate passing two args: model + the user prompt
35
+ command = [
36
+ "ollama",
37
+ "run",
38
+ prompt_text # or "hf.co/abanm/Dubs-Q8_0-GGUF", prompt_text
39
+ ]
40
+
41
+ process = subprocess.Popen(
42
+ command,
43
+ stdout=subprocess.PIPE,
44
+ stderr=subprocess.PIPE,
45
+ text=True
46
+ )
47
+ output, error = process.communicate()
48
+
49
+ if process.returncode != 0 or error:
50
+ raise HTTPException(
51
+ status_code=500,
52
+ detail=f"Ollama error: {error.strip()}"
53
+ )
54
+
55
+ return {"response": output.strip()}
56
+
57
+
58
+ if __name__ == "__main__":
59
+ # For local debugging (outside Docker)
60
+ uvicorn.run(app, host="0.0.0.0", port=7860)
61
+