Spaces:
Runtime error
Runtime error
| 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() | |