Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,66 +2,60 @@
|
|
2 |
import os
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
-
from huggingface_hub import hf_hub_download
|
6 |
-
from
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Download checkpoint
|
19 |
-
checkpoint_path = hf_hub_download(
|
20 |
-
repo_id="VLabTech/cognitive_net",
|
21 |
-
filename="model.pt"
|
22 |
-
)
|
23 |
-
|
24 |
-
# Load weights
|
25 |
-
model.load_state_dict(torch.load(checkpoint_path))
|
26 |
-
model.eval()
|
27 |
-
return model
|
28 |
-
except Exception as e:
|
29 |
-
print(f"Error loading model: {e}")
|
30 |
-
return None
|
31 |
-
|
32 |
-
def predict(self, input_text):
|
33 |
-
"""Proses input dan generate prediksi"""
|
34 |
-
try:
|
35 |
-
# Parse input
|
36 |
-
values = [float(x.strip()) for x in input_text.split(",")]
|
37 |
-
if len(values) != 5:
|
38 |
-
return f"Error: Masukkan tepat 5 nilai (dipisahkan koma). Anda memasukkan {len(values)} nilai."
|
39 |
-
|
40 |
-
# Convert ke tensor
|
41 |
-
input_tensor = torch.tensor(values, dtype=torch.float32)
|
42 |
-
|
43 |
-
# Generate prediksi
|
44 |
-
with torch.no_grad():
|
45 |
-
output = self.model(input_tensor)
|
46 |
-
|
47 |
-
# Format output
|
48 |
-
result = f"Hasil Prediksi:\n"
|
49 |
-
result += f"Output 1: {output[0]:.4f}\n"
|
50 |
-
result += f"Output 2: {output[1]:.4f}"
|
51 |
-
|
52 |
-
return result
|
53 |
-
|
54 |
-
except ValueError:
|
55 |
-
return "Error: Pastikan semua input adalah angka valid"
|
56 |
-
except Exception as e:
|
57 |
-
return f"Error: {str(e)}"
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Setup Gradio Interface
|
63 |
demo = gr.Interface(
|
64 |
-
fn=
|
65 |
inputs=gr.Textbox(
|
66 |
label="Input Values",
|
67 |
placeholder="Masukkan 5 nilai numerik (pisahkan dengan koma). Contoh: 1.0, 2.0, 3.0, 4.0, 5.0"
|
@@ -71,16 +65,10 @@ demo = gr.Interface(
|
|
71 |
description="""
|
72 |
## Cognitive Network Inference Demo
|
73 |
|
74 |
-
Model ini menerima 5 input numerik dan menghasilkan 2 output numerik
|
75 |
-
|
76 |
-
### Cara Penggunaan:
|
77 |
-
1. Masukkan 5 angka yang dipisahkan dengan koma
|
78 |
-
2. Klik Submit untuk melihat hasil prediksi
|
79 |
|
80 |
-
|
81 |
-
- Masukkan tepat 5 nilai numerik
|
82 |
-
- Pisahkan nilai dengan koma
|
83 |
-
- Gunakan titik untuk desimal
|
84 |
""",
|
85 |
examples=[
|
86 |
["1.0, 2.0, 3.0, 4.0, 5.0"],
|
|
|
2 |
import os
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
6 |
+
from importlib.util import spec_from_file_location, module_from_spec
|
7 |
+
import sys
|
8 |
|
9 |
+
def setup_cognitive_net():
|
10 |
+
"""Setup cognitive_net module from HuggingFace"""
|
11 |
+
# Download repository content
|
12 |
+
repo_path = snapshot_download(repo_id="VLabTech/cognitive_net")
|
13 |
+
|
14 |
+
# Import module from downloaded path
|
15 |
+
sys.path.append(repo_path)
|
16 |
+
from cognitive_net import DynamicCognitiveNet
|
17 |
+
return DynamicCognitiveNet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def predict(input_text):
|
20 |
+
"""Process input and return prediction"""
|
21 |
+
try:
|
22 |
+
# Parse input
|
23 |
+
values = [float(x.strip()) for x in input_text.split(",")]
|
24 |
+
if len(values) != 5:
|
25 |
+
return f"Error: Masukkan tepat 5 nilai (dipisahkan koma). Anda memasukkan {len(values)} nilai."
|
26 |
+
|
27 |
+
# Setup model
|
28 |
+
DynamicCognitiveNet = setup_cognitive_net()
|
29 |
+
model = DynamicCognitiveNet(input_size=5, output_size=2)
|
30 |
+
|
31 |
+
# Load weights
|
32 |
+
checkpoint_path = hf_hub_download(
|
33 |
+
repo_id="VLabTech/cognitive_net",
|
34 |
+
filename="model.pt"
|
35 |
+
)
|
36 |
+
model.load_state_dict(torch.load(checkpoint_path))
|
37 |
+
model.eval()
|
38 |
+
|
39 |
+
# Generate prediction
|
40 |
+
input_tensor = torch.tensor(values, dtype=torch.float32)
|
41 |
+
with torch.no_grad():
|
42 |
+
output = model(input_tensor)
|
43 |
+
|
44 |
+
# Format output
|
45 |
+
result = f"Hasil Prediksi:\n"
|
46 |
+
result += f"Output 1: {output[0]:.4f}\n"
|
47 |
+
result += f"Output 2: {output[1]:.4f}"
|
48 |
+
|
49 |
+
return result
|
50 |
+
|
51 |
+
except ValueError:
|
52 |
+
return "Error: Pastikan semua input adalah angka valid"
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error: {str(e)}"
|
55 |
|
56 |
# Setup Gradio Interface
|
57 |
demo = gr.Interface(
|
58 |
+
fn=predict,
|
59 |
inputs=gr.Textbox(
|
60 |
label="Input Values",
|
61 |
placeholder="Masukkan 5 nilai numerik (pisahkan dengan koma). Contoh: 1.0, 2.0, 3.0, 4.0, 5.0"
|
|
|
65 |
description="""
|
66 |
## Cognitive Network Inference Demo
|
67 |
|
68 |
+
Model ini menerima 5 input numerik dan menghasilkan 2 output numerik menggunakan
|
69 |
+
arsitektur Cognitive Network yang terinspirasi dari cara kerja otak biologis.
|
|
|
|
|
|
|
70 |
|
71 |
+
Model source: https://huggingface.co/VLabTech/cognitive_net
|
|
|
|
|
|
|
72 |
""",
|
73 |
examples=[
|
74 |
["1.0, 2.0, 3.0, 4.0, 5.0"],
|