Predict_Snake_Antivenom / api /model_service.py
royajaygit
updated download_files.py
def435c
Raw History Blame Contribute Delete
4.64 kB
import os
import io
from pathlib import Path
import numpy as np
import pandas as pd
from PIL import Image
import onnxruntime as ort
from scipy.special import softmax
import google.generativeai as genai
from dotenv import load_dotenv
# ===================== 0. LOAD ENV =====================
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
if GEMINI_API_KEY:
genai.configure(api_key=GEMINI_API_KEY)
else:
print("⚠️ WARNING: GEMINI_API_KEY not found. Image generation will be disabled.")
# ===================== 1. PATHS =====================
# Ensure paths point to the assets folder (used by download_files.py)
BASE_DIR = Path(__file__).resolve().parent / "assets"
ONNX_MODEL_PATH = BASE_DIR / "ViT_PreProcessing-ops11-preprocessing-int-dynam_graph.onnx"
TRAIN_METADATA_PATH = BASE_DIR / "train_metadata.csv"
VENOM_XLSX_PATH = BASE_DIR / "venomstatus_with_antivenom.xlsx"
COUNTRY_METADATA_PATH = BASE_DIR / "min-train_metadata.csv"
# ===================== 2. SAFE MODEL LOADING =====================
session = None
def load_model():
global session
if session is None:
if not ONNX_MODEL_PATH.exists():
raise FileNotFoundError(f"❌ ONNX model not found at {ONNX_MODEL_PATH}")
session = ort.InferenceSession(str(ONNX_MODEL_PATH), providers=["CPUExecutionProvider"])
print("✅ Model loaded successfully!")
return session
# ===================== 3. IMAGE PREPROCESSING =====================
def preprocess_image(image: Image.Image) -> np.ndarray:
image = image.convert("RGB").resize((224, 224))
arr = np.array(image, dtype=np.float32) / 255.0
arr = np.transpose(arr, (2, 0, 1))
return np.expand_dims(arr, axis=0)
# ===================== 4. PREDICTION =====================
def predict_snake(image: Image.Image) -> dict:
try:
# Ensure all required files exist
required_files = [TRAIN_METADATA_PATH, VENOM_XLSX_PATH, COUNTRY_METADATA_PATH]
for path in required_files:
if not path.exists():
raise FileNotFoundError(f"Missing file: {path}")
metadata = pd.read_csv(TRAIN_METADATA_PATH)
venom_data = pd.read_excel(VENOM_XLSX_PATH)
country_metadata = pd.read_csv(COUNTRY_METADATA_PATH)
class_names = metadata['binomial'].unique().tolist()
input_tensor = preprocess_image(image)
sess = load_model()
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
logits = sess.run([output_name], {input_name: input_tensor})[0][0]
probabilities = softmax(logits)
predicted_class_id = int(np.argmax(probabilities))
species_name = class_names[predicted_class_id]
confidence = float(np.max(probabilities))
# Lookup venom + antivenom data
species_rows = venom_data[venom_data['species'] == species_name]
species_row = species_rows.iloc[0].to_dict() if not species_rows.empty else {}
# Country info
country_list = country_metadata[country_metadata['binomial'] == species_name]['country'].unique()
countries = [c for c in country_list if c.lower() not in ('unknown', 'unidentified', 'n/a')]
country_output = ", ".join(countries) if countries else "Distribution data unavailable"
return {
"species_name": species_name,
"venom_status": int(species_row.get('venom_status', -1)),
"antivenom_name": species_row.get('antivenom Name', 'N/A'),
"manufacturer": species_row.get('manufacturer', 'N/A'),
"confidence": round(confidence, 4),
"countries": country_output
}
except Exception as e:
return {"error": "Prediction failed", "detail": str(e)}
# ===================== 5. GEMINI IMAGE GENERATION =====================
def generate_image_from_text(prompt: str):
if not GEMINI_API_KEY:
raise RuntimeError("❌ Gemini API key not configured in environment.")
full_prompt = f"Highly detailed, scientifically accurate photo of a snake: {prompt}"
model = genai.GenerativeModel("imagen-3.0-generate-002")
result = model.generate_content(
prompt=full_prompt,
generation_config={
"number_of_images": 1,
"output_mime_type": "image/png",
"aspect_ratio": "1:1"
}
)
if not result.generated_images:
raise RuntimeError("Gemini API returned no image.")
image_bytes = result.generated_images[0].image.image_bytes
gen_image_pil = Image.open(io.BytesIO(image_bytes)).convert("RGB").resize((224, 224))
return image_bytes, gen_image_pil