redfernstech commited on
Commit
e8a4c92
·
verified ·
1 Parent(s): 73f703c

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +24 -0
  2. app.py +29 -0
  3. gtts_utils.py +10 -0
  4. llm_utils.py +8 -0
  5. requirements.txt +5 -0
  6. whisper_utils.py +7 -0
Dockerfile ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python image as the base image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory
5
+ WORKDIR /app
6
+
7
+ # Copy the backend files into the container
8
+ COPY backend/ /app
9
+
10
+ # Install system dependencies
11
+ RUN apt-get update && apt-get install -y \
12
+ gcc \
13
+ libsndfile1 \
14
+ && rm -rf /var/lib/apt/lists/*
15
+
16
+ # Install Python dependencies
17
+ RUN pip install --upgrade pip
18
+ RUN pip install -r requirements.txt
19
+
20
+ # Expose the FastAPI server port
21
+ EXPOSE 8000
22
+
23
+ # Run the FastAPI app with uvicorn
24
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
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)
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
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"]
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ openai-whisper
4
+ transformers
5
+ gtts
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"]