File size: 3,679 Bytes
6f5abfa
 
 
 
 
 
 
 
 
 
 
edae06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5abfa
 
 
 
edae06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f5abfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import sys
import os
import glob

# Add parent directory to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from logos.connectors import get_connector

def analyze_uploads():
    import argparse
    parser = argparse.ArgumentParser(description="Analyze images from multiple folders.")
    parser.add_argument("paths", nargs='*', help="File paths or glob patterns")
    parser.add_argument("--recursive", "-r", action="store_true", help="Recursive search")
    args = parser.parse_args()

    images = []
    if not args.paths:
        # Default behavior: Search current dir
        print("No paths provided. Searching current directory.")
        images = glob.glob("*.png")
    else:
        for pattern in args.paths:
            # Handle recursive globs (e.g. **/*.png)
            if args.recursive and "**" not in pattern:
                pattern = os.path.join(pattern, "**")
            
            # If pattern is a dir, add default extension
            if os.path.isdir(pattern):
                pattern = os.path.join(pattern, "*.png")
                
            found = glob.glob(pattern, recursive=args.recursive)
            images.extend(found)
            
    # Remove duplicates
    images = list(set(images))
    
    print(f"Found {len(images)} images to analyze.")
    
    ocr = get_connector('ocr')
    ocr = get_connector('ocr')
    
    # Try Local Stack first (Nano Swarm), fallback to Dolphin Cloud
    try:
        from logos.connectors import LocalLLMConnector
        # Quick check if port 1234 is open
        import socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex(('localhost', 1234))
        sock.close()
        
        if result == 0:
            print("[INFO] Local Nano Swarm detected (Port 1234). Using Local Stack.")
            dolphin = get_connector('local', model="local-model")
        else:
            print("[INFO] Local Stack offline. Falling back to Cloud Dolphin.")
            dolphin = get_connector('dolphin')
    except Exception as e:
        print(f"[WARN] Local check failed ({e}). Defaulting to Cloud.")
        dolphin = get_connector('dolphin')
    
    for img_path in images:
        print(f"\n--- Analyzing {os.path.basename(img_path)} ---")
        
        # 1. OCR Extraction
        print("Running OCR...")
        try:
            ocr_result = ocr.extract_text(img_path)
            raw_text = ocr_result['full_text']
            print(f"Extracted Text: {raw_text[:200]}...")
        except Exception as e:
            print(f"OCR Failed: {e}")
            raw_text = ""
            
        # 2. Dolphin Analysis (using OCR context if Vision is weak)
        print("Running Dolphin Analyst...")
        try:
            # We feed the OCR text to Dolphin to interpret the matrices
            prompt = f"""
            The user uploaded a diagram containing the following text (extracted via OCR):
            "{raw_text}"
            
            This appears to be a matrix multiplication table or state machine logic (GF4/Galois Field).
            Analyze the logic presented. Specifically look for:
            - Input vectors (i1..i4)
            - State vectors (S1..S4)
            - Output results
            - Binary/Hex relationships
            
            Explain the mathematical relationship shown.
            """
            analysis = dolphin.chat(prompt, system_prompt="You are a Cryptographic Analyst specializing in Finite Fields.")
            print(f"Analyst Report:\n{analysis}\n")
        except Exception as e:
            print(f"Dolphin Failed: {e}")

if __name__ == "__main__":
    analyze_uploads()