Spaces:
Running
Running
File size: 5,873 Bytes
392623c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import { useState } from 'react';
import Head from 'next/head';
import styles from '../styles/Home.module.css';
export default function Home() {
const [url, setUrl] = useState('');
const [loading, setLoading] = useState(false);
const [result, setResult] = useState(null);
const [error, setError] = useState(null);
const [debug, setDebug] = useState(null);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
setError(null);
setResult(null);
setDebug(null);
try {
// Ensure URL has a scheme
let formattedUrl = url;
if (!url.startsWith('http://') && !url.startsWith('https://')) {
formattedUrl = 'http://' + url;
}
console.log('Submitting URL:', formattedUrl);
// Use the ApiService instead of direct fetch
const data = await window.ApiService.predictUrl(formattedUrl);
console.log('Response data:', data);
if (data.status === 'error') {
throw new Error(data.message || 'Error analyzing URL');
}
setResult(data);
} catch (err) {
console.error('Error:', err);
setError(err.message || 'Failed to analyze URL. The service might be unavailable.');
} finally {
setLoading(false);
}
};
const getRiskColor = (score) => {
if (score < 30) return '#4caf50'; // Green
if (score < 70) return '#ff9800'; // Orange
return '#f44336'; // Red
};
return (
<div className={styles.container}>
<Head>
<title>URL Fraud Detection</title>
<meta name="description" content="Detect potential fraud in URLs" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>
URL Fraud Detection
</h1>
<p className={styles.description}>
Enter a URL to analyze for potential fraud
</p>
<form onSubmit={handleSubmit} className={styles.form}>
<input
type="text"
className={styles.input}
value={url}
onChange={(e) => setUrl(e.target.value)}
placeholder="https://example.com"
required
/>
<button
type="submit"
className={styles.button}
disabled={loading}
>
{loading ? 'Analyzing...' : 'Analyze URL'}
</button>
</form>
{error && (
<div className={styles.error}>
<p>{error}</p>
{debug && (
<details>
<summary>Debug Information</summary>
<pre>{JSON.stringify(debug, null, 2)}</pre>
</details>
)}
</div>
)}
{result && (
<div className={styles.result}>
<h2>Analysis Results</h2>
<div className={styles.scoreContainer}>
<div className={styles.scoreCircle} style={{
backgroundColor: getRiskColor(result.fraud_score),
color: '#fff'
}}>
<span className={styles.scoreValue}>{result.fraud_score}%</span>
</div>
<div className={styles.scoreLabel}>
<span>Risk Score</span>
<span className={styles.scoreDescription}>
{result.fraud_score < 30 ? 'Low Risk' :
result.fraud_score < 70 ? 'Medium Risk' : 'High Risk'}
</span>
</div>
</div>
<div className={styles.urlInfo}>
<h3>URL Information</h3>
<p><strong>Analyzed URL:</strong> {result.url}</p>
{result.is_trusted_domain && (
<p className={styles.trusted}>This appears to be a trusted domain</p>
)}
</div>
{result.suspicious_patterns && result.suspicious_patterns.length > 0 && (
<div className={styles.patternsList}>
<h3>Suspicious Patterns</h3>
<ul>
{result.suspicious_patterns.map((pattern, index) => (
<li key={index} className={styles.patternItem}>
{pattern}
</li>
))}
</ul>
</div>
)}
{result.feature_contributions && (
<div className={styles.featuresContainer}>
<h3>Risk Factors</h3>
{result.feature_contributions.map((feature, index) => (
<div key={index} className={styles.featureItem}>
<div className={styles.featureHeader}>
<span className={styles.featureName}>{feature.name}</span>
<span className={styles.featureValue}>
{feature.percentage}%
</span>
</div>
<div className={styles.progressBar}>
<div
className={`${styles.progressFill} ${feature.direction === 'decreases' ? styles.decreases : ''}`}
style={{ width: `${feature.percentage}%` }}
/>
</div>
</div>
))}
</div>
)}
{debug && (
<details>
<summary>Connection Information</summary>
<pre>{JSON.stringify(debug, null, 2)}</pre>
</details>
)}
</div>
)}
</main>
<footer className={styles.footer}>
<a
href="https://github.com/yourusername/fraud-detection-web"
target="_blank"
rel="noopener noreferrer"
>
Fraud Detection Project
</a>
</footer>
</div>
);
} |