File size: 1,376 Bytes
ed85fe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Any, List

def create_model() -> Any:
    try:
        import sciann as sn
        import numpy as np

        x = sn.Variable('x')
        y = sn.Variable('y')
        net = sn.Functional('u')(x, y)

        class SciannWrapper:
            def __init__(self, func):
                self.func = func

            def predict(self, inputs: List[List[float]]):
                arr = np.array(inputs)
                x_in = arr[:, 0]
                y_in = arr[:, 1] if arr.shape[1] > 1 else np.zeros_like(x_in)
                out = self.func.predict(x_in, y_in)
                return out

            def load_weights(self, path: str):
                try:
                    self.func.load_weights(path)
                except Exception:
                    pass

        return SciannWrapper(net)

    except Exception:
        class FallbackModel:
            def predict(self, inputs):
                out = []
                for row in inputs:
                    p1 = float(row[0]) if len(row) > 0 else 0.0
                    p2 = float(row[1]) if len(row) > 1 else 0.0
                    confidence = max(0.0, min(1.0, 0.5 + 0.01 * p1 - 0.001 * p2))
                    stress = p1 * 1.5
                    strain = p2 * 0.2
                    out.append([confidence, stress, strain])
                return out
        return FallbackModel()