royajaygit commited on
Commit
a3d4bde
·
1 Parent(s): 404eb86

fix memory issue

Browse files
Files changed (1) hide show
  1. api/model_service.py +35 -57
api/model_service.py CHANGED
@@ -10,66 +10,36 @@ import onnxruntime as ort
10
  from scipy.special import softmax
11
  import google.generativeai as genai # updated import
12
 
13
- # ===================== 1. GLOBAL INITIALIZATION =====================
14
-
15
  BASE_DIR = Path(__file__).resolve().parent
16
 
 
 
17
  ONNX_MODEL_PATH = BASE_DIR / "ViT_PreProcessing-ops11-preprocessing-int-dynam_graph.onnx"
18
- METADATA_PATH = BASE_DIR / "train_metadata.csv"
19
- VENOM_DATA_PATH = BASE_DIR / "venomstatus_with_antivenom.csv"
20
- COUNTRY_DATA_PATH = BASE_DIR / "min-train_metadata.csv"
21
 
22
- # Load ONNX model globally
23
  try:
24
  session = ort.InferenceSession(str(ONNX_MODEL_PATH), providers=["CPUExecutionProvider"])
25
  except FileNotFoundError:
26
  raise SystemExit(f"ONNX model not found at {ONNX_MODEL_PATH}. Deployment cannot proceed.")
27
 
28
- # Load CSV metadata safely
29
- def load_csv(path):
30
- try:
31
- return pd.read_csv(path)
32
- except FileNotFoundError:
33
- raise SystemExit(f"Required CSV not found: {path}")
34
-
35
- metadata = load_csv(METADATA_PATH)
36
- venom_data = load_csv(VENOM_DATA_PATH)
37
- country_metadata = load_csv(COUNTRY_DATA_PATH)
38
-
39
- class_names = metadata['binomial'].unique()
40
-
41
- # ===================== 2. GEMINI API CONFIG =====================
42
-
43
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
44
- if GEMINI_API_KEY:
45
- genai.configure(api_key=GEMINI_API_KEY)
46
- else:
47
- print("WARNING: GEMINI_API_KEY not found. Image generation will fail.")
48
-
49
- # ===================== 3. IMAGE PREPROCESSING =====================
50
 
51
  def preprocess_image(image: Image.Image) -> np.ndarray:
52
  image = image.convert("RGB").resize((224, 224))
53
- input_array = np.array(image).astype(np.float32) / 255.0 # HWC
54
- input_array = np.transpose(input_array, (2, 0, 1)) # CHW
55
- input_array = np.expand_dims(input_array, axis=0) # Add batch dim
56
- return input_array
57
 
58
- # ===================== 4. PREDICTION FUNCTION =====================
59
 
60
  def predict_snake(image: Image.Image) -> dict:
61
- if not session:
62
- return {
63
- "error": "Model not loaded.",
64
- "species_name": "Unknown",
65
- "venom_status": -1,
66
- "antivenom_name": "N/A",
67
- "manufacturer": "N/A",
68
- "confidence": 0.0,
69
- "countries": "N/A"
70
- }
71
-
72
  try:
 
 
 
 
 
 
 
73
  input_tensor = preprocess_image(image)
74
  input_name = session.get_inputs()[0].name
75
  output_name = session.get_outputs()[0].name
@@ -99,28 +69,36 @@ def predict_snake(image: Image.Image) -> dict:
99
  except Exception as e:
100
  return {"error": "Prediction failed", "detail": str(e)}
101
 
102
- # ===================== 5. IMAGE GENERATION FUNCTION =====================
 
 
103
 
104
  def generate_image_from_text(prompt: str) -> tuple[bytes, Image.Image]:
105
  if not GEMINI_API_KEY:
106
- raise ConnectionError("Gemini API key not configured. Cannot generate image.")
 
 
 
107
 
108
  full_prompt = f"Highly detailed, scientifically accurate photo of a snake: {prompt}"
109
 
110
- result = genai.models.get("models/gemini-2.5-flash-preview-05-20").generate_content(
 
111
  prompt=full_prompt,
112
- temperature=1.0,
113
- max_output_tokens=1024
 
 
 
114
  )
115
 
116
- if not result or not result.output_text:
117
- raise RuntimeError("Gemini API returned no image or text.")
118
 
119
- # If your Gemini plan supports image output:
120
- # Use the proper image generation model
121
- # e.g., genai.models.get("imagen-3.0-generate-002").generate_content(...)
122
 
123
- # For now, return the text as a placeholder
124
- image_bytes = result.output_text.encode("utf-8")
125
- gen_image_pil = Image.new("RGB", (224, 224), color=(255, 255, 255))
126
  return image_bytes, gen_image_pil
 
10
  from scipy.special import softmax
11
  import google.generativeai as genai # updated import
12
 
 
 
13
  BASE_DIR = Path(__file__).resolve().parent
14
 
15
+ # ===================== 1. ONNX MODEL =====================
16
+
17
  ONNX_MODEL_PATH = BASE_DIR / "ViT_PreProcessing-ops11-preprocessing-int-dynam_graph.onnx"
 
 
 
18
 
 
19
  try:
20
  session = ort.InferenceSession(str(ONNX_MODEL_PATH), providers=["CPUExecutionProvider"])
21
  except FileNotFoundError:
22
  raise SystemExit(f"ONNX model not found at {ONNX_MODEL_PATH}. Deployment cannot proceed.")
23
 
24
+ # ===================== 2. IMAGE PREPROCESSING =====================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def preprocess_image(image: Image.Image) -> np.ndarray:
27
  image = image.convert("RGB").resize((224, 224))
28
+ arr = np.array(image, dtype=np.float32) / 255.0
29
+ arr = np.transpose(arr, (2, 0, 1))
30
+ return np.expand_dims(arr, axis=0)
 
31
 
32
+ # ===================== 3. PREDICTION FUNCTION =====================
33
 
34
  def predict_snake(image: Image.Image) -> dict:
 
 
 
 
 
 
 
 
 
 
 
35
  try:
36
+ # Lazy load CSVs to save memory
37
+ metadata = pd.read_csv(BASE_DIR / "train_metadata.csv")
38
+ venom_data = pd.read_csv(BASE_DIR / "venomstatus_with_antivenom.csv")
39
+ country_metadata = pd.read_csv(BASE_DIR / "min-train_metadata.csv")
40
+
41
+ class_names = metadata['binomial'].unique()
42
+
43
  input_tensor = preprocess_image(image)
44
  input_name = session.get_inputs()[0].name
45
  output_name = session.get_outputs()[0].name
 
69
  except Exception as e:
70
  return {"error": "Prediction failed", "detail": str(e)}
71
 
72
+ # ===================== 4. GEMINI IMAGE GENERATION =====================
73
+
74
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
75
 
76
  def generate_image_from_text(prompt: str) -> tuple[bytes, Image.Image]:
77
  if not GEMINI_API_KEY:
78
+ raise ConnectionError("Gemini API key not configured.")
79
+
80
+ # Configure Gemini on-demand
81
+ genai.configure(api_key=GEMINI_API_KEY)
82
 
83
  full_prompt = f"Highly detailed, scientifically accurate photo of a snake: {prompt}"
84
 
85
+ # Use the official image generation model
86
+ result = genai.models.get("imagen-3.0-generate-002").generate_content(
87
  prompt=full_prompt,
88
+ config=dict(
89
+ number_of_images=1,
90
+ output_mime_type="image/png",
91
+ aspect_ratio="1:1"
92
+ )
93
  )
94
 
95
+ if not result.generated_images:
96
+ raise RuntimeError("Gemini API returned no image.")
97
 
98
+ # Extract image bytes
99
+ image_bytes = result.generated_images[0].image.image_bytes
100
+ gen_image_pil = Image.open(io.BytesIO(image_bytes)).convert("RGB")
101
 
102
+ # Resize to 224x224 to match ONNX input
103
+ gen_image_pil = gen_image_pil.resize((224, 224))
 
104
  return image_bytes, gen_image_pil