Turkcell / frontend /src /VerificationPage.jsx
huseyinemretech's picture
Lite Deploy: Clean start without large files
9f48b33
Raw History Blame Contribute Delete
6.9 kB
import React, { useState, useRef } from 'react';
import './App.css';
import toWav from 'audiobuffer-to-wav';
import AudioVisualizer from './AudioVisualizer.jsx';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faMicrophone, faStop } from '@fortawesome/free-solid-svg-icons';
import logo from './DualMind_Logo.png';
import { API_CONFIG } from './config.js';
const VerificationPage = ({ onVerified }) => {
const [status, setStatus] = useState('Hazır');
const [isRecording, setIsRecording] = useState(false);
const mediaRecorderRef = useRef(null);
const [audioData, setAudioData] = useState(null);
const [speakerName, setSpeakerName] = useState('');
const [role, setRole] = useState('owner'); // 'owner' (Hat Sahibi) | 'user' (Kullanıcı)
const API_URL = API_CONFIG.SES_API;
const sendAudioToApi = async (audioBlob) => {
if (!speakerName) {
setStatus('Lütfen doğrulanacak numarayı girin.');
return;
}
setStatus('Ses analizi için API\'ye gönderiliyor...');
const formData = new FormData();
const fileExtension = audioBlob.type.split('/')[1] || 'wav';
formData.append('audio_file', audioBlob, `audio.${fileExtension}`);
try {
const response = await fetch(`${API_URL}/recognize/?role=${role}`, { method: 'POST', body: formData });
const data = await response.json();
const confidencePct = (data?.confidence ? (data.confidence * 100) : 0).toFixed(2);
const suffix = role === 'owner' ? 'HatSahibi' : 'Kullanici';
const expectedName = `${speakerName}${suffix}`;
const isVerified = response.ok && data?.speaker === expectedName;
onVerified({
inputName: speakerName,
apiSpeaker: data?.speaker || null,
confidence: confidencePct,
isVerified,
message: isVerified ? `Doğrulandı (%${confidencePct})` : `Doğrulanamadı (%${confidencePct})`,
role,
expectedName
});
} catch (error) {
console.error('API çağrısı sırasında hata:', error);
const suffix = role === 'owner' ? 'HatSahibi' : 'Kullanici';
const expectedName = `${speakerName}${suffix}`;
onVerified({ inputName: speakerName, apiSpeaker: null, confidence: '0.00', isVerified: false, message: 'Sunucu hatası', role, expectedName });
}
};
const handleRecordStart = async () => {
if (isRecording) return;
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
setAudioData(stream);
const chunks = [];
const mimeType = MediaRecorder.isTypeSupported('audio/webm;codecs=opus') ? 'audio/webm;codecs=opus' : 'audio/wav';
mediaRecorderRef.current = new MediaRecorder(stream, { mimeType });
mediaRecorderRef.current.ondataavailable = (event) => { chunks.push(event.data); };
mediaRecorderRef.current.onstop = async () => {
const audioBlob = new Blob(chunks, { type: mediaRecorderRef.current.mimeType });
const arrayBuffer = await audioBlob.arrayBuffer();
const audioCtx = new AudioContext();
const audioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
const wavBuffer = toWav(audioBuffer);
const wavBlob = new Blob([wavBuffer], { type: 'audio/wav' });
const wavFile = new File([wavBlob], 'recorded_audio.wav', { type: 'audio/wav' });
stream.getTracks().forEach(track => track.stop());
setAudioData(null);
sendAudioToApi(wavFile);
};
mediaRecorderRef.current.start();
setIsRecording(true);
setStatus('Kayıt başlatıldı...');
} catch (err) {
console.error('Mikrofon erişim hatası:', err);
setStatus('Mikrofon erişim hatası!');
}
};
const handleRecordStop = () => {
if (mediaRecorderRef.current && mediaRecorderRef.current.state === 'recording') {
mediaRecorderRef.current.stop();
setIsRecording(false);
setStatus('Kayıt durduruldu.');
}
};
const handleFileUpload = (event) => {
const file = event.target.files[0];
if (file) {
sendAudioToApi(file);
}
};
return (
<div className="App">
<div className="container">
<img src={logo} alt="Sesli AI Logosu" className="logo" />
<h1>Kimlik Doğrulama</h1>
<p className="subtitle">Sadece numara girin ve ses yükleyin ya da kaydedin</p>
<div className="verification-form">
{/* Input Section */}
<div className="form-section">
<div className="input-group">
<label className="input-label">Telefon Numarası</label>
<input
type="text"
placeholder="0532 XXX XX XX"
value={speakerName}
onChange={(e) => setSpeakerName(e.target.value)}
className="speaker-input modern-input"
/>
</div>
<div className="input-group">
<label className="input-label">Doğrulama Türü</label>
<div className="role-toggle">
<button
className={`role-option ${role === 'owner' ? 'active' : ''}`}
onClick={() => setRole('owner')}
type="button"
>
<span className="role-icon">👤</span>
Hat Sahibi
</button>
<button
className={`role-option ${role === 'user' ? 'active' : ''}`}
onClick={() => setRole('user')}
type="button"
>
<span className="role-icon">👥</span>
Kullanıcı
</button>
</div>
</div>
</div>
{/* Action Buttons */}
<div className="action-buttons">
{!isRecording ? (
<button className="button action-button record-btn" onClick={handleRecordStart}>
<FontAwesomeIcon icon={faMicrophone} />
<span>Ses Kaydet</span>
</button>
) : (
<button className="button action-button stop-btn recording" onClick={handleRecordStop}>
<FontAwesomeIcon icon={faStop} />
<span>Kaydı Durdur</span>
</button>
)}
<div className="divider-text">veya</div>
<input type="file" id="audio-upload" accept="audio/*" onChange={handleFileUpload} style={{ display: 'none' }} />
<label htmlFor="audio-upload" className="button action-button upload-btn">
<span>📁</span>
<span>Dosya Yükle</span>
</label>
</div>
</div>
{isRecording && audioData && <AudioVisualizer audioData={audioData} />}
<div className="status">Durum: <span>{status}</span></div>
</div>
</div>
);
};
export default VerificationPage;