Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,17 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from ollama import Ollama
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
-
app = FastAPI(
|
| 8 |
-
|
| 9 |
-
# Initialize Ollama client
|
| 10 |
-
client = Ollama()
|
| 11 |
|
| 12 |
@app.post("/analyze")
|
| 13 |
-
async def analyze(file: UploadFile = File(...)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 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)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import base64
|
| 4 |
+
import ollama
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
client = ollama.Client()
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@app.post("/analyze")
|
| 10 |
+
async def analyze(file: UploadFile = File(...)):
|
| 11 |
+
image_bytes = await file.read()
|
| 12 |
+
image_b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 13 |
+
|
| 14 |
+
prompt = "Classify the civic issue in this image as JSON with keys: issue_type, severity, department."
|
| 15 |
+
response = client.run("llava", f"{prompt}", image=image_b64)
|
| 16 |
+
|
| 17 |
+
return {"analysis": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|