File size: 2,143 Bytes
7aa3251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
# app/core/utils.py
import numpy as np
import tensorflow as tf
import io
from PIL import Image
from .config import GEMINI_MODEL


def preprocess_image_tf(image_bytes):
    img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
    img = img.resize((224, 224))
    img_array = tf.keras.preprocessing.image.img_to_array(img) # type: ignore
    return np.expand_dims(img_array / 255.0, axis=0)


def preprocess_image_pil(image_bytes):
    img = Image.open(io.BytesIO(image_bytes)).convert("RGB")
    return img


async def generate_gemini_insights(label, confidences, mode="general"):
    confidence = confidences[label] * 100

    if mode == "tumor":
        prompt = f"""
        You are a medical assistant. Generate a tumor analysis report for diagnosis **{label}** with confidence {confidence:.1f}%.
        Include:
        - Description
        - Common MRI findings
        - Recommended medical steps
        - Lifestyle/care suggestions
        Avoid markdown or HTML.
        """
    elif mode == "alzheimers":
        prompt = f"""
        Generate Alzheimer’s medical report for type '{label}' with {confidence:.1f}% confidence.
        Explain:
        - Symptoms
        - MRI observations
        - Next steps
        - Care strategies

        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:

        ## Symptoms
        [description]

        ## MRI observations
        [description]

        ## Next steps
        [description]

        ## Care strategies
        [description]
        """
    elif mode == "pneumonia":
        prompt = f"""
        Explain in layman terms the medical condition '{label}' with {confidence:.1f}% confidence.
        Focus on:
        - What it is
        - Symptoms
        - What the patient should do
        Format in markdown.
        """
    else:
        prompt = f"Explain medical condition '{label}' with {confidence:.1f}% confidence."

    response = await GEMINI_MODEL.generate_content_async(prompt)
    return response.text