Spaces:
Running
Running
Download backend/app/models/feedback.py from HeshamXOR/MalwareGuard: direct link, hf CLI and curl.
- Browser
- Download file 4.06 kB
-
https://huggingface.co/spaces/HeshamXOR/MalwareGuard/resolve/main/backend/app/models/feedback.py
- Command line
-
hf download hf://spaces/HeshamXOR/MalwareGuard/backend/app/models/feedback.py
-
curl -L -o feedback.py https://huggingface.co/spaces/HeshamXOR/MalwareGuard/resolve/main/backend/app/models/feedback.py
4.06 kB
| """ | |
| Pydantic models for Reinforcement Learning feedback. | |
| Two feedback channels: | |
| 1. Malware analysis: auto-reward based on VirusTotal agreement (+1 agree, -1 disagree) | |
| 2. URL analysis: user-submitted reward via popup ("Was this correct?" Yes β +1, No β -1) | |
| """ | |
| from pydantic import BaseModel, Field | |
| from typing import Optional, List, Dict | |
| from enum import Enum | |
| class FeedbackType(str, Enum): | |
| MALWARE = "malware" | |
| URL = "url" | |
| # ββ Malware RL Feedback ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class MalwareFeedbackRequest(BaseModel): | |
| """ | |
| Auto-generated feedback when VirusTotal results are compared with | |
| XGBoost prediction. Reward is computed server-side. | |
| """ | |
| features: List[float] = Field( | |
| ..., | |
| min_length=2381, | |
| max_length=2381, | |
| description="EMBER 2018 v2 feature vector", | |
| ) | |
| sha256: str = Field( | |
| ..., | |
| pattern=r"^[a-fA-F0-9]{64}$", | |
| description="SHA256 hash of the analysed file", | |
| ) | |
| predicted_label: str = Field( | |
| ..., | |
| description="XGBoost prediction: 'Malicious' or 'Benign'", | |
| ) | |
| predicted_probability: float = Field(..., ge=0.0, le=1.0) | |
| vt_malicious_count: int = Field(..., ge=0) | |
| vt_total_engines: int = Field(..., ge=0) | |
| vt_detection_ratio: float = Field(..., ge=0.0, le=1.0) | |
| # ββ URL RL Feedback ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class URLFeedbackRequest(BaseModel): | |
| """ | |
| User-submitted feedback for URL predictions. | |
| The user confirms or denies the model's verdict. | |
| """ | |
| url: str = Field(..., min_length=3, description="Analysed URL") | |
| predicted_label: str = Field( | |
| ..., | |
| description="Model prediction: 'Malicious' or 'Benign'", | |
| ) | |
| malicious_score: float = Field(..., ge=0.0, le=1.0) | |
| is_correct: bool = Field( | |
| ..., | |
| description="True if user confirms prediction is correct, False otherwise", | |
| ) | |
| base_model_scores: Dict[str, float] = Field( | |
| default_factory=dict, | |
| description="Base model malicious probabilities for meta-feature reconstruction", | |
| ) | |
| # ββ Responses ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class FeedbackResponse(BaseModel): | |
| """Response after submitting feedback.""" | |
| success: bool | |
| reward: int | |
| message: str | |
| total_feedback_count: int | |
| class FeedbackStats(BaseModel): | |
| """Aggregated RL statistics.""" | |
| malware_total: int | |
| malware_agree: int | |
| malware_disagree: int | |
| malware_agreement_rate: float | |
| url_total: int | |
| url_correct: int | |
| url_incorrect: int | |
| url_accuracy_rate: float | |
| recent_malware: List[dict] = [] | |
| recent_url: List[dict] = [] | |
| model_updates: List[dict] = [] | |
| class ModelUpdateRequest(BaseModel): | |
| """Request to trigger incremental model update.""" | |
| model_type: str = Field( | |
| ..., | |
| description="Which model to update: 'malware' or 'url'", | |
| pattern=r"^(malware|url)$", | |
| ) | |
| min_samples: int = Field( | |
| 10, | |
| ge=1, | |
| description="Minimum feedback samples required to proceed", | |
| ) | |
| learning_rate: float = Field( | |
| 0.01, | |
| ge=0.001, | |
| le=0.1, | |
| description="Learning rate for incremental training", | |
| ) | |
| num_boost_rounds: int = Field( | |
| 5, | |
| ge=1, | |
| le=50, | |
| description="Number of additional boosting rounds", | |
| ) | |
| class ModelUpdateResponse(BaseModel): | |
| """Response from model update trigger.""" | |
| success: bool | |
| model_type: str | |
| samples_used: int | |
| message: str | |