File size: 3,045 Bytes
a553748 c812f7e a553748 d69c555 a553748 |
1 2 3 4 5 6 7 8 9 10 11 12 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import gradio as gr
import requests
import soundfile as sf
import numpy as np
import tempfile
import os
ASR_URL = "https://bissakaai-spaceb.hf.space/gradio_api/call/transcribe_audio"
LLM_URL = "https://bissakaai-health1.hf.space/gradio_api/call/natlas_infer"
TTS_URL = "https://ayscript-text-to-speech.hf.space/gradio_api/call/synthesize_speech"
def call_asr(audio_path):
payload = {
"data": [audio_path]
}
r = requests.post(ASR_URL, json=payload, timeout=120)
return r.json()["data"][0]
def call_llm(text):
payload = {
"data": [text]
}
r = requests.post(LLM_URL, json=payload, timeout=120)
return r.json()["data"][0]
def call_tts(text, language):
payload = {
"data": [text, language]
}
r = requests.post(TTS_URL, json=payload, timeout=120)
print(r.json())
return r.json()["data"][0]
def healthatlas_pipeline(
mode,
text_input,
audio_input,
tts_language
):
if mode == "Text":
if not text_input.strip():
return "Please enter text", None
llm_response = call_llm(text_input)
audio_path = call_tts(llm_response, tts_language)
return llm_response, audio_path
else: # Audio mode
if audio_input is None:
return "Please upload audio", None
# Save uploaded audio
sr, audio = audio_input
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
sf.write(f.name, audio, sr)
audio_path = f.name
transcription = call_asr(audio_path)
llm_response = call_llm(transcription)
audio_out = call_tts(llm_response, tts_language)
return llm_response, audio_out
#gradio user interface
with gr.Blocks(title="HealthAtlas AI") as demo:
gr.Markdown(
"""
# 馃彞 HealthAtlas
Multilingual AI Health Triage Assistant
**Languages:** English 路 Yoruba 路 Hausa 路 Igbo
"""
)
mode = gr.Radio(
["Text", "Audio"],
value="Text",
label="Input Mode"
)
text_input = gr.Textbox(
label="Text Input",
visible=True
)
audio_input = gr.Audio(
label="Audio Input",
type="numpy",
visible=False
)
tts_language = gr.Dropdown(
choices=["yoruba", "hausa"],
value="yoruba",
label="Speech Output Language"
)
output_text = gr.Textbox(
label="HealthAtlas Response"
)
output_audio = gr.Audio(
label="Spoken Response"
)
submit = gr.Button("Submit")
def toggle_inputs(m):
return (
gr.update(visible=m == "Text"),
gr.update(visible=m == "Audio")
)
mode.change(
toggle_inputs,
inputs=mode,
outputs=[text_input, audio_input]
)
submit.click(
healthatlas_pipeline,
inputs=[mode, text_input, audio_input, tts_language],
outputs=[output_text, output_audio]
)
if __name__ == "__main__":
demo.launch() |