Habiba A. Elbehairy commited on
Commit ·
b19ac71
1
Parent(s): 535649b
edit app
Browse files
app.py
CHANGED
|
@@ -2,11 +2,16 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
from typing import Dict
|
|
|
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
model_name = "HabibaElbehairy/codebert-multitask-similarity"
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 12 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
@@ -31,10 +36,11 @@ class SimilarityInput(BaseModel):
|
|
| 31 |
source_code: SourceCode
|
| 32 |
test_case_1: TestCase
|
| 33 |
test_case_2: TestCase
|
| 34 |
-
|
| 35 |
-
@app.post("/predict")
|
| 36 |
async def predict(data: SimilarityInput):
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
combined_input = (
|
| 39 |
f"[SOURCE CLASS]: {data.source_code.class_name}\n"
|
| 40 |
f"[SOURCE CODE]: {data.source_code.code}\n"
|
|
@@ -42,17 +48,14 @@ async def predict(data: SimilarityInput):
|
|
| 42 |
f"[TEST 2]: {data.test_case_2.code}"
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# Tokenize
|
| 46 |
inputs = tokenizer(combined_input, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 47 |
|
| 48 |
-
# Predict
|
| 49 |
with torch.no_grad():
|
| 50 |
outputs = model(**inputs)
|
| 51 |
|
| 52 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 53 |
score = torch.argmax(probs, dim=-1).item()
|
| 54 |
|
| 55 |
-
# Mapping index to label and explanation
|
| 56 |
label_map = {
|
| 57 |
0: ("Duplicate", "Tests cover the same logic with similar inputs."),
|
| 58 |
1: ("Redundant", "Tests validate similar behavior but with slightly varied input."),
|
|
@@ -63,6 +66,8 @@ async def predict(data: SimilarityInput):
|
|
| 63 |
|
| 64 |
return {
|
| 65 |
"pair_id": data.pair_id,
|
|
|
|
|
|
|
| 66 |
"similarity": {
|
| 67 |
"score": score,
|
| 68 |
"classification": label,
|
|
@@ -70,3 +75,8 @@ async def predict(data: SimilarityInput):
|
|
| 70 |
},
|
| 71 |
"probabilities": probs[0].tolist()
|
| 72 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
from typing import Dict
|
| 5 |
+
import uvicorn
|
| 6 |
import torch
|
| 7 |
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
title="CodeBERT Multitask Similarity API",
|
| 10 |
+
description="Compare test case similarity using a fine-tuned CodeBERT model.",
|
| 11 |
+
version="1.0.0"
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
# Load model and tokenizer
|
| 15 |
model_name = "HabibaElbehairy/codebert-multitask-similarity"
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
|
|
|
| 36 |
source_code: SourceCode
|
| 37 |
test_case_1: TestCase
|
| 38 |
test_case_2: TestCase
|
| 39 |
+
@app.post("/predict", tags=["Prediction"])
|
|
|
|
| 40 |
async def predict(data: SimilarityInput):
|
| 41 |
+
"""
|
| 42 |
+
Predict similarity class between two test cases for a given source class.
|
| 43 |
+
"""
|
| 44 |
combined_input = (
|
| 45 |
f"[SOURCE CLASS]: {data.source_code.class_name}\n"
|
| 46 |
f"[SOURCE CODE]: {data.source_code.code}\n"
|
|
|
|
| 48 |
f"[TEST 2]: {data.test_case_2.code}"
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
inputs = tokenizer(combined_input, return_tensors="pt", padding=True, truncation=True).to(device)
|
| 52 |
|
|
|
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model(**inputs)
|
| 55 |
|
| 56 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 57 |
score = torch.argmax(probs, dim=-1).item()
|
| 58 |
|
|
|
|
| 59 |
label_map = {
|
| 60 |
0: ("Duplicate", "Tests cover the same logic with similar inputs."),
|
| 61 |
1: ("Redundant", "Tests validate similar behavior but with slightly varied input."),
|
|
|
|
| 66 |
|
| 67 |
return {
|
| 68 |
"pair_id": data.pair_id,
|
| 69 |
+
"test_case_1_name": data.test_case_1.name,
|
| 70 |
+
"test_case_2_name": data.test_case_2.name,
|
| 71 |
"similarity": {
|
| 72 |
"score": score,
|
| 73 |
"classification": label,
|
|
|
|
| 75 |
},
|
| 76 |
"probabilities": probs[0].tolist()
|
| 77 |
}
|
| 78 |
+
|
| 79 |
+
# This allows the app to run locally or in HF Spaces
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|