Merge remote-tracking branch 'refs/remotes/origin/main'
This commit is contained in:
10
src/app/(panels)/suite/patient/chat/page.tsx
Normal file
10
src/app/(panels)/suite/patient/chat/page.tsx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
export default function Chat() {
|
||||||
|
return (
|
||||||
|
<div className="container mx-auto">
|
||||||
|
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
export const metadata = {
|
|
||||||
title: 'Next.js',
|
|
||||||
description: 'Generated by Next.js',
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function RootLayout({
|
|
||||||
children,
|
|
||||||
}: {
|
|
||||||
children: React.ReactNode
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<html lang="en">
|
|
||||||
<body>{children}</body>
|
|
||||||
</html>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,56 +1,122 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import React, { useState } from "react";
|
import React, { useState, useRef } from "react";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
const AudioTranscriber: React.FC = () => {
|
const AudioTranscriber: React.FC = () => {
|
||||||
const [file, setFile] = useState<File | null>(null);
|
const [file, setFile] = useState<File | null>(null);
|
||||||
const [transcription, setTranscription] = useState<string | null>(null);
|
const [transcription, setTranscription] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [recording, setRecording] = useState(false);
|
||||||
|
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
|
||||||
|
const audioChunksRef = useRef<Blob[]>([]);
|
||||||
|
|
||||||
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
// Handle file selection
|
||||||
if (event.target.files && event.target.files.length > 0) {
|
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setFile(event.target.files[0]);
|
if (event.target.files && event.target.files.length > 0) {
|
||||||
}
|
setFile(event.target.files[0]);
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleTranscription = async () => {
|
// Handle file transcription
|
||||||
if (!file) return alert("Please select an audio file to transcribe!");
|
const handleTranscription = async (audioFile: File) => {
|
||||||
|
if (!audioFile) return alert("No audio file to transcribe!");
|
||||||
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("file", file);
|
formData.append("file", audioFile);
|
||||||
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const response = await axios.post("http://localhost:8000/transcribe", formData, {
|
const response = await axios.post("http://localhost:8000/transcribe", formData, {
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "multipart/form-data",
|
"Content-Type": "multipart/form-data",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
setTranscription(response.data.transcription);
|
setTranscription(response.data.transcription);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error transcribing audio:", error);
|
console.error("Error transcribing audio:", error);
|
||||||
alert("Failed to transcribe audio. Please try again.");
|
alert("Failed to transcribe audio. Please try again.");
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
// Start recording audio
|
||||||
<div>
|
const startRecording = async () => {
|
||||||
<h1>Audio Transcription</h1>
|
try {
|
||||||
<input type="file" accept="audio/*" onChange={handleFileChange} />
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
<button onClick={handleTranscription} disabled={loading}>
|
mediaRecorderRef.current = new MediaRecorder(stream);
|
||||||
{loading ? "Transcribing..." : "Transcribe"}
|
|
||||||
</button>
|
audioChunksRef.current = []; // Reset audio chunks
|
||||||
{transcription && (
|
mediaRecorderRef.current.ondataavailable = (event) => {
|
||||||
<div>
|
if (event.data.size > 0) {
|
||||||
<h2>Transcription:</h2>
|
audioChunksRef.current.push(event.data);
|
||||||
<p>{transcription}</p>
|
}
|
||||||
</div>
|
};
|
||||||
)}
|
|
||||||
</div>
|
mediaRecorderRef.current.onstop = async () => {
|
||||||
);
|
const audioBlob = new Blob(audioChunksRef.current, { type: "audio/mp3" });
|
||||||
|
const audioFile = new File([audioBlob], "recording.mp3", { type: "audio/mp3" });
|
||||||
|
setFile(audioFile); // Save the recorded file
|
||||||
|
|
||||||
|
// Transcribe the recorded audio
|
||||||
|
setTranscription("Transcribing the recorded audio...");
|
||||||
|
await handleTranscription(audioFile);
|
||||||
|
};
|
||||||
|
|
||||||
|
mediaRecorderRef.current.start();
|
||||||
|
setRecording(true);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error starting recording:", error);
|
||||||
|
alert("Failed to start recording. Please check microphone permissions.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Stop recording audio
|
||||||
|
const stopRecording = () => {
|
||||||
|
if (mediaRecorderRef.current) {
|
||||||
|
mediaRecorderRef.current.stop();
|
||||||
|
setRecording(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Audio Transcription</h1>
|
||||||
|
<div>
|
||||||
|
<h2>Upload or Record Audio</h2>
|
||||||
|
{/* File Upload */}
|
||||||
|
<input type="file" accept="audio/*" onChange={handleFileChange} />
|
||||||
|
<button onClick={() => file && handleTranscription(file)} disabled={loading || !file}>
|
||||||
|
{loading ? "Transcribing..." : "Transcribe"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Recording Controls */}
|
||||||
|
<div>
|
||||||
|
<h2>Record Audio</h2>
|
||||||
|
{!recording ? (
|
||||||
|
<button onClick={startRecording}>Start Recording</button>
|
||||||
|
) : (
|
||||||
|
<button onClick={stopRecording} disabled={!recording}>
|
||||||
|
Stop Recording
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Transcription Result */}
|
||||||
|
<div>
|
||||||
|
<h2>Transcription:</h2>
|
||||||
|
{loading ? (
|
||||||
|
<p>Processing transcription...</p>
|
||||||
|
) : transcription ? (
|
||||||
|
<p>{transcription}</p>
|
||||||
|
) : (
|
||||||
|
<p>No transcription available yet.</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default AudioTranscriber;
|
export default AudioTranscriber;
|
||||||
|
|||||||
Binary file not shown.
@@ -24,9 +24,8 @@ async def transcribe_audio(file: UploadFile = File(...)):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Transcribe the audio
|
# Transcribe the audio
|
||||||
result = model.transcribe("inputs/test.mp3")
|
result = model.transcribe(temp_path)
|
||||||
transcription = result["text"]
|
transcription = result["text"]
|
||||||
print(transcription)
|
|
||||||
finally:
|
finally:
|
||||||
# Clean up temporary file
|
# Clean up temporary file
|
||||||
os.remove(temp_path)
|
os.remove(temp_path)
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import multer from "multer";
|
|
||||||
import fs from "fs/promises";
|
|
||||||
import whisper from "openai-whisper";
|
|
||||||
|
|
||||||
const upload = multer({ dest: "uploads/" });
|
|
||||||
|
|
||||||
export const config = {
|
|
||||||
api: {
|
|
||||||
bodyParser: false, // Disable Next.js's body parsing for file uploads
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Whisper model (initialize once for efficiency)
|
|
||||||
const model = whisper.load_model("base");
|
|
||||||
|
|
||||||
// Utility to transcribe audio
|
|
||||||
async function transcribeAudio(filePath: string): Promise<string> {
|
|
||||||
const transcription = await model.transcribe(filePath);
|
|
||||||
return transcription.text;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function parseMultipartForm(req: NextRequest): Promise<File> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const multerMiddleware = upload.single("audio");
|
|
||||||
multerMiddleware(req as any, {} as any, (error: any) => {
|
|
||||||
if (error) return reject(error);
|
|
||||||
resolve(req.file);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
|
||||||
try {
|
|
||||||
// Parse the incoming multipart form data
|
|
||||||
const file = await parseMultipartForm(req);
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
return NextResponse.json({ error: "No audio file provided" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const filePath = file.path;
|
|
||||||
|
|
||||||
// Transcribe the audio
|
|
||||||
const transcription = await transcribeAudio(filePath);
|
|
||||||
|
|
||||||
// Clean up the uploaded file
|
|
||||||
await fs.unlink(filePath);
|
|
||||||
|
|
||||||
return NextResponse.json({ transcription });
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error during transcription:", error);
|
|
||||||
return NextResponse.json({ error: "Transcription failed" }, { status: 500 });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user