Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- backend/app.py +29 -0
- backend/gtts_utils.py +10 -0
- backend/llm_utils.py +8 -0
- backend/requirements.txt +5 -0
- backend/whisper_utils.py +7 -0
backend/app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from backend.whisper_utils import transcribe_audio
|
| 4 |
+
from backend.gtts_utils import generate_speech
|
| 5 |
+
from backend.llm_utils import get_llm_response
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
@app.post("/transcribe/")
|
| 11 |
+
async def transcribe(file: UploadFile):
|
| 12 |
+
file_path = f"audio/{file.filename}"
|
| 13 |
+
with open(file_path, "wb") as audio:
|
| 14 |
+
audio.write(await file.read())
|
| 15 |
+
|
| 16 |
+
text = transcribe_audio(file_path)
|
| 17 |
+
os.remove(file_path) # Cleanup audio file
|
| 18 |
+
return {"transcription": text}
|
| 19 |
+
|
| 20 |
+
@app.post("/response/")
|
| 21 |
+
async def get_response(input_text: str):
|
| 22 |
+
llm_response = get_llm_response(input_text)
|
| 23 |
+
audio_path = generate_speech(llm_response)
|
| 24 |
+
return {"response": llm_response, "audio_url": audio_path}
|
| 25 |
+
|
| 26 |
+
@app.get("/audio/{file_name}")
|
| 27 |
+
async def serve_audio(file_name: str):
|
| 28 |
+
file_path = f"audio/{file_name}"
|
| 29 |
+
return FileResponse(file_path)
|
backend/gtts_utils.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gtts import gTTS
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
+
def generate_speech(text: str) -> str:
|
| 6 |
+
file_name = f"{uuid.uuid4()}.mp3"
|
| 7 |
+
file_path = f"audio/{file_name}"
|
| 8 |
+
tts = gTTS(text)
|
| 9 |
+
tts.save(file_path)
|
| 10 |
+
return file_path
|
backend/llm_utils.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Load the Hugging Face LLM
|
| 4 |
+
llm = pipeline("text-generation", model="gpt2", max_length=100)
|
| 5 |
+
|
| 6 |
+
def get_llm_response(prompt: str) -> str:
|
| 7 |
+
response = llm(prompt)
|
| 8 |
+
return response[0]["generated_text"]
|
backend/requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
openai-whisper
|
| 4 |
+
transformers
|
| 5 |
+
gtts
|
backend/whisper_utils.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import whisper
|
| 2 |
+
|
| 3 |
+
model = whisper.load_model("base")
|
| 4 |
+
|
| 5 |
+
def transcribe_audio(file_path: str) -> str:
|
| 6 |
+
result = model.transcribe(file_path)
|
| 7 |
+
return result["text"]
|