File size: 2,255 Bytes
b81412e
 
 
 
 
 
 
 
 
 
 
 
 
 
ca8e3c7
 
b81412e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4897b58
 
 
5344190
 
 
4ef0526
5344190
 
4ef0526
5344190
4897b58
 
b81412e
4897b58
 
 
 
 
 
 
 
 
 
b81412e
 
 
 
 
 
83d5a57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
import os
from model_inference import predict_image_int32, get_snake_info
from GPT_client import get_snake_description, LLM_snake_identifier
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(title="Snake Identification API")

UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
from fastapi.middleware.cors import CORSMiddleware

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"]
    ,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
@app.get("/")
async def helper():
    return "snake"

@app.post("/identify-snake")
async def identify_snake(image: UploadFile = File(...)):
    """Endpoint 1: Identify snake species from image"""
    image_path = os.path.join(UPLOAD_DIR, image.filename)
    with open(image_path, "wb") as f:
        f.write(await image.read())

    pred_class, species_name, confidence = predict_image_int32(image_path)
    venom_status, antivenom, habitat = get_snake_info(pred_class)
    about_snake = get_snake_description(species_name)
    result = None
    if confidence <= 65:
        result = {
            "Image": "Not available",
            "Identification Confidence": "Not Applicable",
            "Predicted Species": "Not Applicable",
            "Venomous status": "Non-Venomous",
            "About This Snake": "Not Applicable",
            "Habitat & Distribution": "Not Applicable",
            "Danger Level": "Low",
            "Recommended Antivenom": "Not Applicable",
        }
    else:

        result = {
            "Image": image.filename,
            "Identification Confidence": f"{confidence:.2f}%",
            "Predicted Species": species_name,
            "Venomous status": venom_status,
            "About This Snake": about_snake,
            "Habitat & Distribution": habitat,
            "Danger Level": "High" if venom_status == "Venomous" else "Low",
            "Recommended Antivenom": antivenom,
        }

    return JSONResponse(result)

@app.post("/describe-snake")
async def description_base_snake_identify(text: str = Form(...)):
    """Endpoint 2: Describe snake by name or text"""
    return LLM_snake_identifier(text)