Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,61 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from pydub import AudioSegment
|
| 3 |
import librosa
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
# import your existing functions
|
| 10 |
from your_model_file import textonly, speechonly
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
def text_interface(text):
|
| 14 |
+
"""Process text input and return response"""
|
| 15 |
+
result = textonly(text)
|
| 16 |
+
return result
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def speech_interface(audio_file):
|
| 20 |
+
"""Process speech input and return LLM response and audio output"""
|
| 21 |
+
if audio_file is None:
|
| 22 |
+
return "Please provide an audio file", None
|
| 23 |
+
|
| 24 |
+
# audio_file is a tuple of (sample_rate, audio_data) from Gradio
|
| 25 |
+
sr, audio_data = audio_file
|
| 26 |
+
|
| 27 |
+
# Convert to mono if needed
|
| 28 |
+
if len(audio_data.shape) > 1:
|
| 29 |
+
audio_data = np.mean(audio_data, axis=1)
|
| 30 |
+
|
| 31 |
+
# Resample to 16000 Hz if necessary
|
| 32 |
+
if sr != 16000:
|
| 33 |
+
audio_data = librosa.resample(audio_data, orig_sr=sr, target_sr=16000)
|
| 34 |
+
|
| 35 |
+
# Call the speechonly function
|
| 36 |
+
llm_response, wav_path = speechonly(audio_data, output_wav_path="output.wav")
|
| 37 |
+
|
| 38 |
+
return llm_response, wav_path
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Create Gradio interface with tabs
|
| 42 |
+
with gr.Blocks(title="Hamid AI Speech API") as app:
|
| 43 |
+
gr.Markdown("# Hamid AI Speech Interface")
|
| 44 |
+
gr.Markdown("Choose between text-only or speech-based interaction")
|
| 45 |
+
|
| 46 |
+
with gr.Tab("Text Only"):
|
| 47 |
+
text_input = gr.Textbox(label="Enter your text", placeholder="Type something...")
|
| 48 |
+
text_output = gr.Textbox(label="Response", interactive=False)
|
| 49 |
+
text_button = gr.Button("Process Text")
|
| 50 |
+
text_button.click(fn=text_interface, inputs=text_input, outputs=text_output)
|
| 51 |
+
|
| 52 |
+
with gr.Tab("Speech Only"):
|
| 53 |
+
audio_input = gr.Audio(label="Upload or record audio", type="numpy")
|
| 54 |
+
speech_output = gr.Textbox(label="LLM Response", interactive=False)
|
| 55 |
+
audio_output = gr.Audio(label="Output Audio", type="filepath")
|
| 56 |
+
speech_button = gr.Button("Process Speech")
|
| 57 |
+
speech_button.click(fn=speech_interface, inputs=audio_input, outputs=[speech_output, audio_output])
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
app.launch(share=False)
|