Spaces:
Sleeping
Sleeping
Commit ·
2e8618e
1
Parent(s): af16a7a
sixth commit
Browse files- dockerfile +22 -0
- question_api/bgnn_training_dataset.json +0 -0
- question_api/trainning_bgnn.py +0 -90
- requirements.txt +10 -2
dockerfile
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
build-essential \
|
| 10 |
+
gcc \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade pip \
|
| 16 |
+
&& pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
COPY . .
|
| 19 |
+
|
| 20 |
+
EXPOSE 8080
|
| 21 |
+
|
| 22 |
+
CMD ["gunicorn", "-b", "0.0.0.0:8080", "app:app"]
|
question_api/bgnn_training_dataset.json
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
question_api/trainning_bgnn.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn.functional as F
|
| 3 |
-
from torch_geometric.nn import GCNConv
|
| 4 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
-
from sklearn.model_selection import train_test_split
|
| 6 |
-
import json
|
| 7 |
-
import pickle
|
| 8 |
-
import os
|
| 9 |
-
import re
|
| 10 |
-
import string
|
| 11 |
-
|
| 12 |
-
# Clean text function
|
| 13 |
-
def clean_text(text):
|
| 14 |
-
text = text.lower()
|
| 15 |
-
text = re.sub(r'\[.*?\]', '', text)
|
| 16 |
-
text = re.sub(r'https?://\S+|www\.\S+', '', text)
|
| 17 |
-
text = re.sub(r'<.*?>+', '', text)
|
| 18 |
-
text = re.sub(f'[{re.escape(string.punctuation)}]', '', text)
|
| 19 |
-
text = re.sub(r'\n', ' ', text)
|
| 20 |
-
text = re.sub(r'\w*\d\w*', '', text)
|
| 21 |
-
text = re.sub(' +', ' ', text)
|
| 22 |
-
return text.strip()
|
| 23 |
-
|
| 24 |
-
# BiGNN model
|
| 25 |
-
class BiGNN(torch.nn.Module):
|
| 26 |
-
def __init__(self, input_dim, hidden_dim, output_dim):
|
| 27 |
-
super(BiGNN, self).__init__()
|
| 28 |
-
self.conv1 = GCNConv(input_dim, hidden_dim)
|
| 29 |
-
self.conv2 = GCNConv(hidden_dim, output_dim)
|
| 30 |
-
|
| 31 |
-
def forward(self, data):
|
| 32 |
-
x, edge_index = data.x, data.edge_index
|
| 33 |
-
x = F.relu(self.conv1(x, edge_index))
|
| 34 |
-
x = F.dropout(x, training=self.training)
|
| 35 |
-
x = self.conv2(x, edge_index)
|
| 36 |
-
return F.log_softmax(x, dim=1)
|
| 37 |
-
|
| 38 |
-
# Load data
|
| 39 |
-
with open("question_api/bgnn_training_dataset.json", "r", encoding="utf-8") as f:
|
| 40 |
-
data = json.load(f)
|
| 41 |
-
|
| 42 |
-
sentences = [clean_text(d["sentence"]) for d in data]
|
| 43 |
-
labels = [d["label"] for d in data]
|
| 44 |
-
|
| 45 |
-
# Vectorize
|
| 46 |
-
vectorizer = TfidfVectorizer()
|
| 47 |
-
X = vectorizer.fit_transform(sentences).toarray()
|
| 48 |
-
y = torch.tensor(labels)
|
| 49 |
-
|
| 50 |
-
# Build edge index (fake graph: each node is connected to itself)
|
| 51 |
-
edge_index = torch.tensor([[i for i in range(len(X))], [i for i in range(len(X))]], dtype=torch.long)
|
| 52 |
-
|
| 53 |
-
# Train/Test Split
|
| 54 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 55 |
-
|
| 56 |
-
# Convert to tensor
|
| 57 |
-
X_train = torch.tensor(X_train, dtype=torch.float32)
|
| 58 |
-
X_test = torch.tensor(X_test, dtype=torch.float32)
|
| 59 |
-
|
| 60 |
-
# Create dummy PyG data objects
|
| 61 |
-
train_data = type('Data', (object,), {})()
|
| 62 |
-
train_data.x = X_train
|
| 63 |
-
train_data.edge_index = edge_index[:, :X_train.shape[0]]
|
| 64 |
-
|
| 65 |
-
test_data = type('Data', (object,), {})()
|
| 66 |
-
test_data.x = X_test
|
| 67 |
-
test_data.edge_index = edge_index[:, :X_test.shape[0]]
|
| 68 |
-
|
| 69 |
-
# Train model
|
| 70 |
-
model = BiGNN(input_dim=X.shape[1], hidden_dim=16, output_dim=3)
|
| 71 |
-
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
|
| 72 |
-
epochs = 50
|
| 73 |
-
|
| 74 |
-
for epoch in range(epochs):
|
| 75 |
-
model.train()
|
| 76 |
-
optimizer.zero_grad()
|
| 77 |
-
out = model(train_data)
|
| 78 |
-
loss = F.nll_loss(out, y_train)
|
| 79 |
-
loss.backward()
|
| 80 |
-
optimizer.step()
|
| 81 |
-
print(f"Epoch {epoch+1}/{epochs}, Loss: {loss.item():.4f}")
|
| 82 |
-
|
| 83 |
-
# Save model and vectorizer
|
| 84 |
-
os.makedirs("question_api/artifacts", exist_ok=True)
|
| 85 |
-
torch.save(model.state_dict(), "question_api/artifacts/biggn_model.pt")
|
| 86 |
-
|
| 87 |
-
with open("question_api/artifacts/vectorizer.pkl", "wb") as f:
|
| 88 |
-
pickle.dump(vectorizer, f)
|
| 89 |
-
|
| 90 |
-
print("✅ Model and vectorizer saved.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -8,8 +8,16 @@ Flask-CORS==4.0.0
|
|
| 8 |
# Google AI
|
| 9 |
google-generativeai==0.3.2
|
| 10 |
|
| 11 |
-
|
| 12 |
-
torch==2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
transformers==4.35.2
|
| 14 |
scikit-learn==1.3.2
|
| 15 |
nltk==3.8.1
|
|
|
|
| 8 |
# Google AI
|
| 9 |
google-generativeai==0.3.2
|
| 10 |
|
| 11 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 12 |
+
torch==2.1.2+cpu
|
| 13 |
+
torchvision==0.16.2+cpu
|
| 14 |
+
torchaudio==2.1.2+cpu
|
| 15 |
+
|
| 16 |
+
torch-geometric==2.5.0
|
| 17 |
+
torch-scatter==2.1.2
|
| 18 |
+
torch-sparse==0.6.18
|
| 19 |
+
torch-cluster==1.6.3
|
| 20 |
+
torch-spline-conv==1.2.2
|
| 21 |
transformers==4.35.2
|
| 22 |
scikit-learn==1.3.2
|
| 23 |
nltk==3.8.1
|