File size: 1,330 Bytes
58bd26a
845dc86
58bd26a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from pathlib import Path

from agents.agent import Agent
from agents.deep_neural_network import DeepNeuralNetworkInference

WEIGHTS_PATH = Path("deep_neural_network.pth")


class NeuralNetworkAgent(Agent):
    name = "Neural Network Agent"
    color = Agent.MAGENTA

    def __init__(self):
        self.neural_network = None
        self.log("Neural Network Agent is initializing")
        if not WEIGHTS_PATH.exists():
            self.log(
                "Neural network weights not found (deep_neural_network.pth) — agent disabled. "
                "Fine for Hugging Face Spaces under 1GB storage limit."
            )
            return
        self.neural_network = DeepNeuralNetworkInference()
        self.neural_network.setup()
        self.neural_network.load(str(WEIGHTS_PATH))
        self.log("Neural Network Agent is ready and weights are loaded")

    @property
    def available(self) -> bool:
        return self.neural_network is not None

    def price(self, description: str) -> float:
        if not self.available:
            return 0.0
        self.log("Neural Network Agent is starting a prediction")
        result = self.neural_network.inference(description)
        self.log(f"Neural Network Agent completed - predicting ${result:.2f}")
        return result