File size: 4,206 Bytes
e93bfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os

import questionary
from rich.console import Console
from rich.panel import Panel
from rich.table import Table

import capture
from optimize import (
    clear_memory,
    get_device,
    get_memory_usage,
    infer_blip,
    infer_fusion,
    set_cpu_threads,
)

console = Console()

def run_vision(image_path):
    console.print("[cyan]Running Vision analysis...[/cyan]")
    try:
        caption = infer_blip(image_path)
    except Exception as e:
        console.print(f"[red]Error: {e}[/red]")
        return

    console.print()
    console.print(Panel(f"[bold green]{caption}[/bold green]", title="Generated Radiology Caption"))
    return caption

def run_symptom_check(image_path):
    symptoms = questionary.text("Enter patient symptoms / clinical indication:").ask()
    if not symptoms:
        symptoms = "No symptoms provided"

    try:
        diagnosis, confidence = infer_fusion(image_path, symptoms)
    except Exception as e:
        console.print(f"[red]Error: {e}[/red]")
        return

    if diagnosis is None:
        console.print(f"[red]{confidence}[/red]")
        return

    table = Table(title="Diagnosis Result")
    table.add_column("Prediction", style="cyan")
    table.add_column("Confidence", style="green")
    table.add_row(diagnosis, f"{confidence:.1%}")
    console.print(table)

    if confidence < 0.75:
        console.print("[yellow]Low confidence — consider follow-up.[/yellow]")

    return diagnosis, confidence

def show_status():
    from training import info as training_info

    console.print("[bold cyan]--- System Status ---[/bold cyan]")
    mem = get_memory_usage()
    console.print(f"Device:        [green]{get_device().upper()}[/green]")
    console.print(f"Process RAM:   {mem['rss_mb']:.0f} MB")
    console.print()

    console.print("[bold cyan]--- Fusion Model Data ---[/bold cyan]")
    training_info()
    console.print()

    cap_dir = capture.IMAGES_DIR
    count = len(list(cap_dir.glob("*"))) if cap_dir.exists() else 0
    console.print(f"Captured images: {count} in {cap_dir}")

def install_deps():
    deps = []
    try:
        import cv2
    except ImportError:
        deps.append("opencv-python")
    try:
        import pydicom
    except ImportError:
        deps.append("pydicom")
    try:
        import nltk
    except ImportError:
        deps.append("nltk")

    if not deps:
        console.print("[green]All optional dependencies are already installed.[/green]")
        return

    import subprocess
    import sys
    console.print(f"[yellow]Installing: {' '.join(deps)}[/yellow]")
    subprocess.check_call([sys.executable, "-m", "pip", "install", *deps])
    console.print("[green]Done.[/green]")

def main():
    set_cpu_threads()

    console.print(Panel.fit("[bold cyan]MedicalAI - Light Weight[/bold cyan]"))
    console.print()

    while True:
        choice = questionary.select(
            "What would you like to do?",
            choices=[
                "Vision — Generate report from X-ray",
                "Symptom Check — Diagnose from X-ray + symptoms",
                "System Status & Data Info",
                "Install optional deps (camera, DICOM, BLEU)",
                "Exit",
            ],
            pointer=">",
        ).ask()

        if choice == "Exit":
            console.print("[bold red]Exiting...[/bold red]")
            clear_memory()
            break

        if choice == "Install optional deps (camera, DICOM, BLEU)":
            install_deps()
            continue

        if choice == "System Status & Data Info":
            show_status()
            console.print()
            continue

        image_path, msg = capture.pick_image()
        if image_path is None:
            console.print(f"[red]{msg}[/red]")
            continue
        console.print(f"[dim]{msg}[/dim]")

        if choice.startswith("Vision"):
            run_vision(image_path)
        elif choice.startswith("Symptom Check"):
            run_symptom_check(image_path)

        clear_memory()
        console.print()
        again = questionary.confirm("Do another?").ask()
        if not again:
            break

    clear_memory()

if __name__ == "__main__":
    main()