Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- app.py +18 -0
- best_pr_testcase_model.pt +3 -0
- model.py +69 -0
- requirements.txt +5 -0
- start.sh +2 -0
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from model import predict_testcases
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="PR Test Case Prioritization API")
|
| 6 |
+
|
| 7 |
+
class PRInput(BaseModel):
|
| 8 |
+
pr_title: str
|
| 9 |
+
top_k: int = 5
|
| 10 |
+
|
| 11 |
+
@app.get("/")
|
| 12 |
+
def root():
|
| 13 |
+
return {"message": "ML API is running"}
|
| 14 |
+
|
| 15 |
+
@app.post("/predict")
|
| 16 |
+
def predict(pr: PRInput):
|
| 17 |
+
results = predict_testcases(pr.pr_title, pr.top_k)
|
| 18 |
+
return {"predictions": results}
|
best_pr_testcase_model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ce988154757b55dbd60ab39e941e815c83f467410399a4938e1d8641a9b27e6d
|
| 3 |
+
size 318023
|
model.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# Config
|
| 7 |
+
# =========================
|
| 8 |
+
MAX_LEN = 50
|
| 9 |
+
VOCAB_SIZE = 1000
|
| 10 |
+
NUM_TCS = 50
|
| 11 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# Test case list
|
| 15 |
+
# =========================
|
| 16 |
+
all_tcs = [f"TC_{i}" for i in range(NUM_TCS)]
|
| 17 |
+
|
| 18 |
+
# =========================
|
| 19 |
+
# Model Architecture
|
| 20 |
+
# =========================
|
| 21 |
+
class SmallPRClassifier(torch.nn.Module):
|
| 22 |
+
def __init__(self, vocab_size=VOCAB_SIZE, embed_dim=64, num_labels=NUM_TCS):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.embedding = torch.nn.EmbeddingBag(vocab_size, embed_dim)
|
| 25 |
+
self.fc1 = torch.nn.Linear(embed_dim, 128)
|
| 26 |
+
self.relu = torch.nn.ReLU()
|
| 27 |
+
self.dropout = torch.nn.Dropout(0.3)
|
| 28 |
+
self.classifier = torch.nn.Linear(128, num_labels)
|
| 29 |
+
|
| 30 |
+
def forward(self, x):
|
| 31 |
+
embedded = self.embedding(x)
|
| 32 |
+
hidden = self.relu(self.fc1(embedded))
|
| 33 |
+
logits = self.classifier(self.dropout(hidden))
|
| 34 |
+
return logits
|
| 35 |
+
|
| 36 |
+
# =========================
|
| 37 |
+
# Tokenizer (dummy)
|
| 38 |
+
# =========================
|
| 39 |
+
def text_to_indices(text, max_len=MAX_LEN, vocab_size=VOCAB_SIZE):
|
| 40 |
+
idxs = [random.randint(2, vocab_size-1) for _ in text.split()]
|
| 41 |
+
if len(idxs) < max_len:
|
| 42 |
+
idxs += [0]*(max_len - len(idxs))
|
| 43 |
+
else:
|
| 44 |
+
idxs = idxs[:max_len]
|
| 45 |
+
return idxs
|
| 46 |
+
|
| 47 |
+
# =========================
|
| 48 |
+
# Load model
|
| 49 |
+
# =========================
|
| 50 |
+
model = SmallPRClassifier().to(DEVICE)
|
| 51 |
+
model.load_state_dict(torch.load("best_pr_testcase_model.pt", map_location=DEVICE))
|
| 52 |
+
model.eval()
|
| 53 |
+
|
| 54 |
+
# =========================
|
| 55 |
+
# Prediction function
|
| 56 |
+
# =========================
|
| 57 |
+
def predict_testcases(pr_title, top_k=5):
|
| 58 |
+
idxs = torch.tensor([text_to_indices(pr_title)]).to(DEVICE)
|
| 59 |
+
with torch.no_grad():
|
| 60 |
+
logits = model(idxs)
|
| 61 |
+
scores = torch.sigmoid(logits).cpu().numpy().flatten()
|
| 62 |
+
ranked_indices = np.argsort(scores)[::-1][:top_k]
|
| 63 |
+
results = []
|
| 64 |
+
for idx in ranked_indices:
|
| 65 |
+
results.append({
|
| 66 |
+
"Test_Case_ID": all_tcs[idx],
|
| 67 |
+
"Priority_Score": float(scores[idx])
|
| 68 |
+
})
|
| 69 |
+
return results
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
numpy
|
| 5 |
+
pandas
|
start.sh
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
uvicorn app:app --host 0.0.0.0 --port 7860
|