KRISH09bha commited on
Commit
e414d7e
·
verified ·
1 Parent(s): 1026a0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -29
app.py CHANGED
@@ -1,33 +1,17 @@
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)
 
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}