File size: 3,017 Bytes
535f4a6
 
 
 
 
88ec0a4
535f4a6
 
893d74d
535f4a6
 
 
893d74d
 
afea127
 
5bfa797
88ec0a4
 
535f4a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88ec0a4
f2f0e4e
afea127
88ec0a4
 
afea127
535f4a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88ec0a4
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import google.generativeai as genai
from typing import Dict, Any
from dotenv import load_dotenv


load_dotenv()

GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
if not GEMINI_API_KEY:
    raise RuntimeError("GEMINI_API_KEY environment variable is not set. Please set it in your environment or .env file.")

# Set a working US proxy (test and replace; this one is from free lists as of Dec 2025)
# Find fresh ones at free-proxy-list.net or proxylister.com — filter for US, HTTP, high uptime
#os.environ['HTTP_PROXY'] = 'http://198.199.86.11:80'
#os.environ['HTTPS_PROXY'] = 'http://198.199.86.11:80'

# Get API key from environment variable
# Check GC LOL -molt
def build_prompt_from_counts(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> str:
    """
    Build a prompt for AI analysis based on detection results.
    
    Args:
        class_counts: Dictionary mapping class names to counts
        sample_type: Type of sample (default: "Blood smear")
        stain: Type of stain used (default: "Giemsa")
        magnification: Microscope magnification (default: "1000x")
    
    Returns:
        Formatted prompt string for AI analysis
    """
    summary = "Detected features in sample:\n"
    for cls, count in class_counts.items():
        summary += f"- {count} {cls.replace('_', ' ')}(s)\n"

    summary += f"Sample type: {sample_type}\nStain: {stain}\nMagnification: {magnification}\n"

    return f"""
You are a medical pathologist. Based on the following detection results from a microscopic image, write a concise diagnostic summary and suggest next steps.

{summary}

Output:
- Diagnostic Summary:
- Recommendation:
"""

def analyze_detections(class_counts: Dict[str, int], sample_type: str = "Blood smear", stain: str = "Giemsa", magnification: str = "1000x") -> Dict[str, Any]:
    """
    Analyze detection results using Gemini AI.
    
    Args:
        class_counts: Dictionary mapping class names to counts
        sample_type: Type of sample
        stain: Type of stain used
        magnification: Microscope magnification
    
    Returns:
        Dictionary containing analysis results
    """
    try:
        # Configure the API key
        genai.configure(api_key=GEMINI_API_KEY,transport='rest')
        #print("Gemini configured with proxy successfully.")
        
        # Initialize the Gemini model
        model = genai.GenerativeModel('gemini-2.5-flash')
        
        # Build the prompt
        prompt = build_prompt_from_counts(class_counts, sample_type, stain, magnification)
        
        # Generate content from the model
        response = model.generate_content(prompt)
        
        return {
            "success": True,
            "analysis": response.text,
            "detection_summary": class_counts
        }
        
    except Exception as e:
        return {
            "success": False,
            "error": str(e),
            "detection_summary": class_counts
        }