from flask import Flask, request, jsonify, render_template_string from flask_cors import CORS import torch import torch.nn as nn import time import os # Force PyTorch to use single thread (fixes slow inference on throttled CPUs) torch.set_num_threads(1) torch.set_num_interop_threads(1) os.environ['OMP_NUM_THREADS'] = '1' os.environ['MKL_NUM_THREADS'] = '1' # Import our model classes class CharTokenizer: def __init__(self): chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ " chars += "0123456789.,!?¿áéíóúñÁÉÍÓÚÑ" self.char_to_idx = {c: i+1 for i, c in enumerate(chars)} self.idx_to_char = {i+1: c for i, c in enumerate(chars)} self.vocab_size = len(self.char_to_idx) + 1 def encode(self, text, max_len=100): indices = [self.char_to_idx.get(c, 0) for c in text[:max_len]] indices += [0] * (max_len - len(indices)) return torch.tensor(indices, dtype=torch.long) class AtacamaWeatherOracle(nn.Module): def __init__(self, vocab_size=100, embed_dim=16, hidden_dim=32): super().__init__() self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=0) self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True) self.classifier = nn.Linear(hidden_dim, 2) def forward(self, x): embedded = self.embedding(x) _, (hidden, _) = self.lstm(embedded) logits = self.classifier(hidden.squeeze(0)) return logits # Initialize Flask app app = Flask(__name__) CORS(app) # Load the trained model print("Loading Atacama Weather Oracle...") load_start = time.time() tokenizer = CharTokenizer() model = AtacamaWeatherOracle(vocab_size=tokenizer.vocab_size) checkpoint = torch.load('atacama_weather_oracle.pth', map_location='cpu') model.load_state_dict(checkpoint['model_state_dict']) model.eval() load_time = time.time() - load_start print(f"✅ Oracle loaded and ready! (took {load_time:.3f}s)") # HTML template for the web interface HTML_TEMPLATE = """
An ultra-small language model 7,762 parameters 30KB 99.9% certain