File size: 2,535 Bytes
6a2403a
57b5789
bd035eb
6a2403a
d571264
bd035eb
5b766e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d571264
5b766e8
 
 
d571264
5b766e8
 
 
 
d571264
5b766e8
 
d571264
5b766e8
 
 
 
 
 
 
 
 
 
 
 
 
57b5789
5b766e8
 
 
 
57b5789
5b766e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd035eb
6dd5d74
6a2403a
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
import os
import sys
import torch
import gradio as gr
from huggingface_hub import hf_hub_download, snapshot_download

def predict(input_text: str) -> str:
    """
    Memproses input dan menghasilkan prediksi
    """
    try:
        # Parse input
        values = [float(x.strip()) for x in input_text.split(",")]
        if len(values) != 5:
            return f"Error: Masukkan tepat 5 nilai (dipisahkan koma). Anda memasukkan {len(values)} nilai."

        # Download dan load kode model
        repo_path = snapshot_download(
            repo_id="VLabTech/cognitive_net",
            local_dir="./model_repo"
        )
        
        # Import files secara langsung
        import sys
        sys.path.append("./model_repo")
        
        # Import komponen model
        from memory import CognitiveMemory
        from node import CognitiveNode
        from network import DynamicCognitiveNet
        
        # Setup model
        model = DynamicCognitiveNet(input_size=5, output_size=2)
        
        # Load weights
        checkpoint_path = hf_hub_download(
            repo_id="VLabTech/cognitive_net",
            filename="model.pt",
            local_dir="./model_weights"
        )
        model.load_state_dict(torch.load(checkpoint_path))
        model.eval()
        
        # Generate prediction
        input_tensor = torch.tensor(values, dtype=torch.float32)
        with torch.no_grad():
            output = model(input_tensor)
        
        # Format output
        result = "Hasil Prediksi:\n"
        result += f"Output 1: {output[0]:.4f}\n"
        result += f"Output 2: {output[1]:.4f}"
        
        return result
            
    except ValueError as e:
        return f"Error dalam format input: {str(e)}"
    except Exception as e:
        return f"Error: {str(e)}"

# Setup Gradio Interface
demo = gr.Interface(
    fn=predict,
    inputs=gr.Textbox(
        label="Input Values",
        placeholder="Masukkan 5 nilai numerik (pisahkan dengan koma). Contoh: 1.0, 2.0, 3.0, 4.0, 5.0"
    ),
    outputs=gr.Textbox(label="Hasil Prediksi"),
    title="Cognitive Network Demo",
    description="""
    ## Cognitive Network Inference Demo
    Model ini menerima 5 input numerik dan menghasilkan 2 output numerik menggunakan
    arsitektur Cognitive Network yang terinspirasi dari cara kerja otak biologis.
    """,
    examples=[
        ["1.0, 2.0, 3.0, 4.0, 5.0"],
        ["0.5, -1.0, 2.5, 1.5, -0.5"],
        ["0.1, 0.2, 0.3, 0.4, 0.5"]
    ]
)

if __name__ == "__main__":
    demo.launch()