vincentiusyoshuac commited on
Commit
ab00162
·
verified ·
1 Parent(s): 20e7a00

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -63
app.py CHANGED
@@ -3,72 +3,90 @@ import sys
3
  import torch
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download, snapshot_download
 
6
 
7
- def setup_model():
8
- # Download repository content
9
- repo_path = snapshot_download(
10
- repo_id="VLabTech/cognitive_net",
11
- repo_type="model",
12
- local_dir="./model_repo"
13
- )
14
-
15
- # Add to Python path
16
- sys.path.append(repo_path)
17
-
18
- # Now we can import from the downloaded modules
19
- from memory import CognitiveMemory
20
- from node import CognitiveNode
21
- from network import DynamicCognitiveNet
22
-
23
- return DynamicCognitiveNet(input_size=5, output_size=2)
24
-
25
- def predict(input_text):
26
- try:
27
- # Parse input
28
- values = [float(x.strip()) for x in input_text.split(",")]
29
- if len(values) != 5:
30
- return f"Error: Masukkan tepat 5 nilai (dipisahkan koma). Anda memasukkan {len(values)} nilai."
31
-
32
- # Setup model
33
- model = setup_model()
34
 
35
- # Generate prediction
36
- input_tensor = torch.tensor(values, dtype=torch.float32)
37
- output = model(input_tensor)
 
 
 
 
 
38
 
39
- # Format output
40
- result = "Hasil Prediksi:\n"
41
- result += f"Output 1: {output[0]:.4f}\n"
42
- result += f"Output 2: {output[1]:.4f}"
43
-
44
- return result
45
-
46
- except ValueError as e:
47
- return f"Error dalam format input: {str(e)}"
48
- except Exception as e:
49
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Setup Gradio Interface
52
- demo = gr.Interface(
53
- fn=predict,
54
- inputs=gr.Textbox(
55
- label="Input Values",
56
- placeholder="Masukkan 5 nilai numerik (pisahkan dengan koma). Contoh: 1.0, 2.0, 3.0, 4.0, 5.0"
57
- ),
58
- outputs=gr.Textbox(label="Hasil Prediksi"),
59
- title="Cognitive Network Demo",
60
- description="""
61
- ## Cognitive Network Inference Demo
62
- Model ini menerima 5 input numerik dan menghasilkan 2 output numerik menggunakan
63
- arsitektur Cognitive Network yang terinspirasi dari cara kerja otak biologis.
64
- Model diambil dari VLabTech/cognitive_net.
65
- """,
66
- examples=[
67
- ["1.0, 2.0, 3.0, 4.0, 5.0"],
68
- ["0.5, -1.0, 2.5, 1.5, -0.5"],
69
- ["0.1, 0.2, 0.3, 0.4, 0.5"]
70
- ]
71
- )
 
 
 
 
 
 
72
 
73
  if __name__ == "__main__":
74
- demo.launch()
 
3
  import torch
4
  import gradio as gr
5
  from huggingface_hub import hf_hub_download, snapshot_download
6
+ from pathlib import Path
7
 
8
+ class CognitiveNetworkDemo:
9
+ def __init__(self):
10
+ self.model = None
11
+ self.repo_path = None
12
+ self.setup_environment()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def setup_environment(self):
15
+ """Setup the environment and download the model"""
16
+ # Download repository content
17
+ self.repo_path = Path(snapshot_download(
18
+ repo_id="VLabTech/cognitive_net",
19
+ repo_type="model",
20
+ local_dir="./model_repo"
21
+ ))
22
 
23
+ # Add model directory to Python path
24
+ if str(self.repo_path) not in sys.path:
25
+ sys.path.insert(0, str(self.repo_path))
26
+
27
+ def load_model(self):
28
+ """Load the model if not already loaded"""
29
+ if self.model is None:
30
+ try:
31
+ # Import here to ensure path is set up
32
+ from model_repo.cognitive_net.network import DynamicCognitiveNet
33
+ self.model = DynamicCognitiveNet(input_size=5, output_size=2)
34
+ except ImportError as e:
35
+ raise ImportError(f"Gagal mengimpor model: {str(e)}")
36
+ return self.model
37
+
38
+ def predict(self, input_text):
39
+ """Make predictions using the model"""
40
+ try:
41
+ # Parse input
42
+ values = [float(x.strip()) for x in input_text.split(",")]
43
+ if len(values) != 5:
44
+ return f"Error: Masukkan tepat 5 nilai (dipisahkan koma). Anda memasukkan {len(values)} nilai."
45
+
46
+ # Load model and generate prediction
47
+ model = self.load_model()
48
+ input_tensor = torch.tensor(values, dtype=torch.float32)
49
+ output = model(input_tensor)
50
+
51
+ # Format output
52
+ result = "Hasil Prediksi:\n"
53
+ result += f"Output 1: {output[0]:.4f}\n"
54
+ result += f"Output 2: {output[1]:.4f}"
55
+
56
+ return result
57
+
58
+ except ValueError as e:
59
+ return f"Error dalam format input: {str(e)}"
60
+ except Exception as e:
61
+ return f"Error: {str(e)}"
62
 
63
+ def main():
64
+ # Initialize the demo
65
+ demo_app = CognitiveNetworkDemo()
66
+
67
+ # Setup Gradio Interface
68
+ demo = gr.Interface(
69
+ fn=demo_app.predict,
70
+ inputs=gr.Textbox(
71
+ label="Input Values",
72
+ placeholder="Masukkan 5 nilai numerik (pisahkan dengan koma). Contoh: 1.0, 2.0, 3.0, 4.0, 5.0"
73
+ ),
74
+ outputs=gr.Textbox(label="Hasil Prediksi"),
75
+ title="Cognitive Network Demo",
76
+ description="""
77
+ ## Cognitive Network Inference Demo
78
+ Model ini menerima 5 input numerik dan menghasilkan 2 output numerik menggunakan
79
+ arsitektur Cognitive Network yang terinspirasi dari cara kerja otak biologis.
80
+ Model diambil dari VLabTech/cognitive_net.
81
+ """,
82
+ examples=[
83
+ ["1.0, 2.0, 3.0, 4.0, 5.0"],
84
+ ["0.5, -1.0, 2.5, 1.5, -0.5"],
85
+ ["0.1, 0.2, 0.3, 0.4, 0.5"]
86
+ ]
87
+ )
88
+
89
+ demo.launch()
90
 
91
  if __name__ == "__main__":
92
+ main()