"use client"; //import Hero1 from '@/components/Hero1' //IMPORT THE HERO1 FUNCTION TO MAKE THE TRANSCRIBE PAGE LOOK BETTER import React, { useState, useRef } from "react"; import axios from "axios"; const AudioTranscriber: React.FC = () => { const [file, setFile] = useState(null); const [transcription, setTranscription] = useState(null); const [loading, setLoading] = useState(false); const [recording, setRecording] = useState(false); const [error, setError] = useState(null); const mediaRecorderRef = useRef(null); const audioChunksRef = useRef([]); // Handle file selection const handleFileChange = (event: React.ChangeEvent) => { if (event.target.files && event.target.files.length > 0) { setFile(event.target.files[0]); console.log("File selected:", event.target.files[0].name); } }; // Handle file transcription const handleTranscription = async (audioFile: File) => { if (!audioFile) { alert("No audio file to transcribe!"); return; } console.log("Starting transcription for:", audioFile.name); const formData = new FormData(); formData.append("file", audioFile); setLoading(true); setError(null); // Clear previous errors try { const response = await axios.post("http://localhost:8000/transcribe", formData, { headers: { "Content-Type": "multipart/form-data", }, }); console.log("Transcription response:", response.data); if (response.data && response.data.transcription) { setTranscription(response.data.transcription); } else { setError("Unexpected response format. Check backend API."); console.error("Invalid response format:", response.data); } } catch (error) { console.error("Error transcribing audio:", error); setError("Failed to transcribe audio. Please try again."); } finally { setLoading(false); } }; // Start recording audio const startRecording = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); console.log("Microphone access granted."); mediaRecorderRef.current = new MediaRecorder(stream); audioChunksRef.current = []; // Reset audio chunks mediaRecorderRef.current.ondataavailable = (event) => { if (event.data.size > 0) { console.log("Audio chunk received:", event.data); audioChunksRef.current.push(event.data); } }; mediaRecorderRef.current.onstop = async () => { const audioBlob = new Blob(audioChunksRef.current, { type: "audio/mp3" }); const audioFile = new File([audioBlob], "recording.mp3", { type: "audio/mp3" }); console.log("Recording stopped. Blob created:", audioBlob); setFile(audioFile); // Save the recorded file setTranscription("Processing transcription for recorded audio..."); await handleTranscription(audioFile); // Automatically transcribe }; mediaRecorderRef.current.start(); console.log("Recording started."); setRecording(true); } catch (error) { console.error("Error starting recording:", error); setError("Failed to start recording. Please check microphone permissions."); } }; // Stop recording audio const stopRecording = () => { if (mediaRecorderRef.current) { console.log("Stopping recording..."); mediaRecorderRef.current.stop(); setRecording(false); } }; return (

Audio Transcription

Upload or Record Audio

Record Audio

{!recording ? ( ) : ( )}

Transcription:

{loading ? (

Processing transcription...

) : transcription ? (

{transcription}

) : (

No transcription available yet.

)}
{error && (
Error: {error}
)}
); }; export default AudioTranscriber; /* "use client" import type React from "react" import { useState, useRef } from "react" import axios from "axios" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Mic, Upload, StopCircle, FileAudio } from "lucide-react" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" const AudioTranscriber: React.FC = () => { const [file, setFile] = useState(null) const [transcription, setTranscription] = useState(null) const [loading, setLoading] = useState(false) const [recording, setRecording] = useState(false) const [error, setError] = useState(null) const mediaRecorderRef = useRef(null) const audioChunksRef = useRef([]) const handleFileChange = (event: React.ChangeEvent) => { if (event.target.files && event.target.files.length > 0) { setFile(event.target.files[0]) console.log("File selected:", event.target.files[0].name) } } const handleTranscription = async (audioFile: File) => { if (!audioFile) { setError("No audio file to transcribe!") return } console.log("Starting transcription for:", audioFile.name) const formData = new FormData() formData.append("file", audioFile) setLoading(true) setError(null) try { const response = await axios.post("http://localhost:8000/transcribe", formData, { headers: { "Content-Type": "multipart/form-data", }, }) console.log("Transcription response:", response.data) if (response.data && response.data.transcription) { setTranscription(response.data.transcription) } else { setError("Unexpected response format. Check backend API.") console.error("Invalid response format:", response.data) } } catch (error) { console.error("Error transcribing audio:", error) setError("Failed to transcribe audio. Please try again.") } finally { setLoading(false) } } const startRecording = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }) console.log("Microphone access granted.") mediaRecorderRef.current = new MediaRecorder(stream) audioChunksRef.current = [] mediaRecorderRef.current.ondataavailable = (event) => { if (event.data.size > 0) { console.log("Audio chunk received:", event.data) audioChunksRef.current.push(event.data) } } mediaRecorderRef.current.onstop = async () => { const audioBlob = new Blob(audioChunksRef.current, { type: "audio/mp3" }) const audioFile = new File([audioBlob], "recording.mp3", { type: "audio/mp3" }) console.log("Recording stopped. Blob created:", audioBlob) setFile(audioFile) setTranscription("Processing transcription for recorded audio...") await handleTranscription(audioFile) } mediaRecorderRef.current.start() console.log("Recording started.") setRecording(true) } catch (error) { console.error("Error starting recording:", error) setError("Failed to start recording. Please check microphone permissions.") } } const stopRecording = () => { if (mediaRecorderRef.current) { console.log("Stopping recording...") mediaRecorderRef.current.stop() setRecording(false) } } return (

Audio Transcriber

Transcribe Audio Upload an audio file or record directly to transcribe Upload Audio Record Audio
{!recording ? ( ) : ( )}
Transcription Result {loading ? "Processing transcription..." : transcription ? "Your transcription is ready" : "No transcription available yet"}