Arghya Ghosh commited on
Commit
ec635f4
·
verified ·
1 Parent(s): 1bca716

Update core/utils.py

Browse files
Files changed (1) hide show
  1. core/utils.py +71 -58
core/utils.py CHANGED
@@ -1,58 +1,71 @@
1
- # app/core/utils.py
2
- import numpy as np
3
- import tensorflow as tf
4
- import io
5
- from PIL import Image
6
- from .config import GEMINI_MODEL
7
-
8
-
9
- def preprocess_image_tf(image_bytes):
10
- img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
11
- img = img.resize((224, 224))
12
- img_array = tf.keras.preprocessing.image.img_to_array(img) # type: ignore
13
- return np.expand_dims(img_array / 255.0, axis=0)
14
-
15
-
16
- def preprocess_image_pil(image_bytes):
17
- img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
18
- return img
19
-
20
-
21
- async def generate_gemini_insights(label, confidences, mode="general"):
22
- confidence = confidences[label] * 100
23
-
24
- if mode == "tumor":
25
- prompt = f"""
26
- You are a medical assistant. Generate a tumor analysis report for diagnosis **{label}** with confidence {confidence:.1f}%.
27
- Include:
28
- - Description
29
- - Common MRI findings
30
- - Recommended medical steps
31
- - Lifestyle/care suggestions
32
- Avoid markdown or HTML.
33
- """
34
- elif mode == "alzheimers":
35
- prompt = f"""
36
- Generate Alzheimer’s medical report for type '{label}' with {confidence:.1f}% confidence.
37
- Explain:
38
- - Symptoms
39
- - MRI observations
40
- - Next steps
41
- - Care strategies
42
- Format as plain text only.
43
- """
44
- elif mode == "pneumonia":
45
- prompt = f"""
46
- Explain in layman terms the medical condition '{label}' with {confidence:.1f}% confidence.
47
- Focus on:
48
- - What it is
49
- - Symptoms
50
- - What the patient should do
51
- Format in markdown.
52
- """
53
- else:
54
- prompt = f"Explain medical condition '{label}' with {confidence:.1f}% confidence."
55
-
56
- response = await GEMINI_MODEL.generate_content_async(prompt)
57
- return response.text
58
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/core/utils.py
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ import io
5
+ from PIL import Image
6
+ from .config import GEMINI_MODEL
7
+
8
+
9
+ def preprocess_image_tf(image_bytes):
10
+ img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
11
+ img = img.resize((224, 224))
12
+ img_array = tf.keras.preprocessing.image.img_to_array(img) # type: ignore
13
+ return np.expand_dims(img_array / 255.0, axis=0)
14
+
15
+
16
+ def preprocess_image_pil(image_bytes):
17
+ img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
18
+ return img
19
+
20
+
21
+ async def generate_gemini_insights(label, confidences, mode="general"):
22
+ confidence = confidences[label] * 100
23
+
24
+ if mode == "tumor":
25
+ prompt = f"""
26
+ You are a medical assistant. Generate a tumor analysis report for diagnosis **{label}** with confidence {confidence:.1f}%.
27
+ Include:
28
+ - Description
29
+ - Common MRI findings
30
+ - Recommended medical steps
31
+ - Lifestyle/care suggestions
32
+ Avoid markdown or HTML.
33
+ """
34
+ elif mode == "alzheimers":
35
+ prompt = f"""
36
+ Generate Alzheimer’s medical report for type '{label}' with {confidence:.1f}% confidence.
37
+ Explain:
38
+ - Symptoms
39
+ - MRI observations
40
+ - Next steps
41
+ - Care strategies
42
+
43
+ Format the output strictly using Markdown. Output only the following sections and nothing else. No introduction, no headings, no extra markdown syntax beyond what is shown below. Use this exact format:
44
+
45
+ ## Symptoms
46
+ [description]
47
+
48
+ ## MRI observations
49
+ [description]
50
+
51
+ ## Next steps
52
+ [description]
53
+
54
+ ## Care strategies
55
+ [description]
56
+ """
57
+ elif mode == "pneumonia":
58
+ prompt = f"""
59
+ Explain in layman terms the medical condition '{label}' with {confidence:.1f}% confidence.
60
+ Focus on:
61
+ - What it is
62
+ - Symptoms
63
+ - What the patient should do
64
+ Format in markdown.
65
+ """
66
+ else:
67
+ prompt = f"Explain medical condition '{label}' with {confidence:.1f}% confidence."
68
+
69
+ response = await GEMINI_MODEL.generate_content_async(prompt)
70
+ return response.text
71
+