Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import torch
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 6 |
|
| 7 |
# 1. Model Architecture
|
| 8 |
class SourceCodeAuthorCheck(nn.Module):
|
|
@@ -32,22 +33,24 @@ class SourceCodeAuthorCheck(nn.Module):
|
|
| 32 |
return self.fc(pooled)
|
| 33 |
|
| 34 |
# 2. Device and Loading Initialization
|
| 35 |
-
# Note: HF Free Spaces use CPU. We check for CUDA in case you upgrade the Space hardware.
|
| 36 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 38 |
tokenizer.pad_token = tokenizer.eos_token
|
| 39 |
|
| 40 |
-
model
|
| 41 |
-
|
| 42 |
-
# Download weights securely from your model repository
|
| 43 |
model_path = hf_hub_download(repo_id="assix-research/SourceCodeAuthorCheck-SLM-10M", filename="source_code_classifier.pth")
|
| 44 |
-
model.load_state_dict(torch.load(model_path, map_location=
|
| 45 |
model.eval()
|
| 46 |
|
| 47 |
-
# 3. Inference Logic
|
|
|
|
| 48 |
def predict_author(code_snippet):
|
| 49 |
if not code_snippet or not code_snippet.strip():
|
| 50 |
return "Please paste valid code.", "0.0%"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
inputs = tokenizer(
|
| 53 |
code_snippet,
|
|
@@ -58,7 +61,6 @@ def predict_author(code_snippet):
|
|
| 58 |
).to(device)
|
| 59 |
|
| 60 |
with torch.no_grad():
|
| 61 |
-
# Handle mixed precision safely based on available hardware
|
| 62 |
if torch.cuda.is_available():
|
| 63 |
with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
|
| 64 |
logits = model(inputs['input_ids'], inputs['attention_mask'])
|
|
@@ -70,6 +72,9 @@ def predict_author(code_snippet):
|
|
| 70 |
score = round(prob * 100, 2)
|
| 71 |
verdict = "π€ AI Generated" if prob > 0.5 else "π¨βπ» Human Written"
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
return verdict, f"{score}%"
|
| 74 |
|
| 75 |
# 4. Gradio Interface Construction
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
from transformers import AutoTokenizer
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
+
import spaces
|
| 7 |
|
| 8 |
# 1. Model Architecture
|
| 9 |
class SourceCodeAuthorCheck(nn.Module):
|
|
|
|
| 33 |
return self.fc(pooled)
|
| 34 |
|
| 35 |
# 2. Device and Loading Initialization
|
|
|
|
|
|
|
| 36 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 37 |
tokenizer.pad_token = tokenizer.eos_token
|
| 38 |
|
| 39 |
+
# Load model globally on CPU first
|
| 40 |
+
model = SourceCodeAuthorCheck()
|
|
|
|
| 41 |
model_path = hf_hub_download(repo_id="assix-research/SourceCodeAuthorCheck-SLM-10M", filename="source_code_classifier.pth")
|
| 42 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu", weights_only=True))
|
| 43 |
model.eval()
|
| 44 |
|
| 45 |
+
# 3. Inference Logic with ZeroGPU Decorator
|
| 46 |
+
@spaces.GPU
|
| 47 |
def predict_author(code_snippet):
|
| 48 |
if not code_snippet or not code_snippet.strip():
|
| 49 |
return "Please paste valid code.", "0.0%"
|
| 50 |
+
|
| 51 |
+
# ZeroGPU dynamically provides CUDA access inside this decorated function
|
| 52 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 53 |
+
model.to(device)
|
| 54 |
|
| 55 |
inputs = tokenizer(
|
| 56 |
code_snippet,
|
|
|
|
| 61 |
).to(device)
|
| 62 |
|
| 63 |
with torch.no_grad():
|
|
|
|
| 64 |
if torch.cuda.is_available():
|
| 65 |
with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
|
| 66 |
logits = model(inputs['input_ids'], inputs['attention_mask'])
|
|
|
|
| 72 |
score = round(prob * 100, 2)
|
| 73 |
verdict = "π€ AI Generated" if prob > 0.5 else "π¨βπ» Human Written"
|
| 74 |
|
| 75 |
+
# Move model back to CPU to free up ZeroGPU vRAM for other users
|
| 76 |
+
model.to("cpu")
|
| 77 |
+
|
| 78 |
return verdict, f"{score}%"
|
| 79 |
|
| 80 |
# 4. Gradio Interface Construction
|