Spaces:
Sleeping
Sleeping
Commit ·
b452183
1
Parent(s): db53725
Final setup: Ollama API with gemma3:1b and updated generate method
Browse files- app/main.py +6 -2
app/main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI, Header, HTTPException
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
|
@@ -6,17 +7,20 @@ app = FastAPI()
|
|
| 6 |
|
| 7 |
API_KEY = os.getenv("API_KEY", "default_key") # change this!
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
@app.get("/")
|
| 10 |
def root():
|
| 11 |
return {"status": "Ollama API is running"}
|
| 12 |
|
| 13 |
@app.post("/generate")
|
| 14 |
-
def generate(
|
| 15 |
if authorization != f"Bearer {API_KEY}":
|
| 16 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 17 |
|
| 18 |
r = requests.post(
|
| 19 |
"http://localhost:11434/api/generate",
|
| 20 |
-
json={"model": "gemma3:1b", "prompt": prompt},
|
| 21 |
)
|
| 22 |
return r.json()
|
|
|
|
| 1 |
from fastapi import FastAPI, Header, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import os
|
| 4 |
import requests
|
| 5 |
|
|
|
|
| 7 |
|
| 8 |
API_KEY = os.getenv("API_KEY", "default_key") # change this!
|
| 9 |
|
| 10 |
+
class PromptRequest(BaseModel):
|
| 11 |
+
prompt: str
|
| 12 |
+
|
| 13 |
@app.get("/")
|
| 14 |
def root():
|
| 15 |
return {"status": "Ollama API is running"}
|
| 16 |
|
| 17 |
@app.post("/generate")
|
| 18 |
+
def generate(request: PromptRequest, authorization: str = Header(None)):
|
| 19 |
if authorization != f"Bearer {API_KEY}":
|
| 20 |
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 21 |
|
| 22 |
r = requests.post(
|
| 23 |
"http://localhost:11434/api/generate",
|
| 24 |
+
json={"model": "gemma3:1b", "prompt": request.prompt},
|
| 25 |
)
|
| 26 |
return r.json()
|