Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from ollama import Ollama
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
app = FastAPI(title="Civic Image Analysis API - LLaVA")
|
8 |
+
|
9 |
+
# Initialize Ollama client
|
10 |
+
client = Ollama()
|
11 |
+
|
12 |
+
@app.post("/analyze")
|
13 |
+
async def analyze(file: UploadFile = File(...), prompt: str = "Classify the civic issue in this image"):
|
14 |
+
try:
|
15 |
+
# Read image bytes
|
16 |
+
image_bytes = await file.read()
|
17 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
18 |
+
|
19 |
+
# Save temporarily to pass path to Ollama (if required)
|
20 |
+
image_path = f"/tmp/{file.filename}"
|
21 |
+
image.save(image_path)
|
22 |
+
|
23 |
+
# Call Ollama model
|
24 |
+
response = client.call(
|
25 |
+
model="llava",
|
26 |
+
prompt=prompt,
|
27 |
+
image_path=image_path
|
28 |
+
)
|
29 |
+
|
30 |
+
return JSONResponse({"analysis": response})
|
31 |
+
|
32 |
+
except Exception as e:
|
33 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|