Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,47 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
import librosa
|
| 5 |
-
import uvicorn
|
| 6 |
-
import torch
|
| 7 |
-
import soundfile as sf
|
| 8 |
-
|
| 9 |
-
# import your existing functions
|
| 10 |
-
from your_model_file import textonly, speechonly
|
| 11 |
-
|
| 12 |
-
app = FastAPI(title="Hamid Speech API", version="1.0.0")
|
| 13 |
-
|
| 14 |
-
@app.get("/")
|
| 15 |
-
def root():
|
| 16 |
-
return {"message": "Welcome to Hamid AI Speech API"}
|
| 17 |
-
|
| 18 |
-
class TextRequest(BaseModel):
|
| 19 |
-
text: str
|
| 20 |
-
|
| 21 |
-
class SpeechRequest(BaseModel):
|
| 22 |
-
input_audio_path: str
|
| 23 |
-
wav_output_path: str
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
@app.post("/textonly")
|
| 27 |
-
def run_text(req: TextRequest):
|
| 28 |
-
result = textonly(req.text)
|
| 29 |
-
return {"response": result}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@app.post("/speechonly")
|
| 33 |
-
def run_speech(req: SpeechRequest):
|
| 34 |
-
# Convert input audio to WAV
|
| 35 |
-
audio = AudioSegment.from_file(req.input_audio_path)
|
| 36 |
-
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 37 |
-
audio.export(req.wav_output_path, format="wav")
|
| 38 |
-
|
| 39 |
-
# Load WAV
|
| 40 |
-
speech, sr = librosa.load(req.wav_output_path, sr=16000)
|
| 41 |
-
|
| 42 |
-
llm_response, wav_path = speechonly(speech, output_wav_path=req.wav_output_path)
|
| 43 |
-
|
| 44 |
-
return {
|
| 45 |
-
"response": llm_response,
|
| 46 |
-
"wav_saved": wav_path
|
| 47 |
-
}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
import librosa
|
| 5 |
+
import uvicorn
|
| 6 |
+
import torch
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
|
| 9 |
+
# import your existing functions
|
| 10 |
+
from your_model_file import textonly, speechonly
|
| 11 |
+
|
| 12 |
+
app = FastAPI(title="Hamid Speech API", version="1.0.0")
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def root():
|
| 16 |
+
return {"message": "Welcome to Hamid AI Speech API"}
|
| 17 |
+
|
| 18 |
+
class TextRequest(BaseModel):
|
| 19 |
+
text: str
|
| 20 |
+
|
| 21 |
+
class SpeechRequest(BaseModel):
|
| 22 |
+
input_audio_path: str
|
| 23 |
+
wav_output_path: str
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@app.post("/textonly")
|
| 27 |
+
def run_text(req: TextRequest):
|
| 28 |
+
result = textonly(req.text)
|
| 29 |
+
return {"response": result}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.post("/speechonly")
|
| 33 |
+
def run_speech(req: SpeechRequest):
|
| 34 |
+
# Convert input audio to WAV
|
| 35 |
+
audio = AudioSegment.from_file(req.input_audio_path)
|
| 36 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 37 |
+
audio.export(req.wav_output_path, format="wav")
|
| 38 |
+
|
| 39 |
+
# Load WAV
|
| 40 |
+
speech, sr = librosa.load(req.wav_output_path, sr=16000)
|
| 41 |
+
|
| 42 |
+
llm_response, wav_path = speechonly(speech, output_wav_path=req.wav_output_path)
|
| 43 |
+
|
| 44 |
+
return {
|
| 45 |
+
"response": llm_response,
|
| 46 |
+
"wav_saved": wav_path
|
| 47 |
+
}
|