Futuresony commited on
Commit
ebb2736
·
verified ·
1 Parent(s): ef13b0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
+ # from huggingface_hub import InferenceClient # Removed
6
+ from ttsmms import download, TTS
7
+ from langdetect import detect
8
+ from gradio_client import Client # Added
9
+
10
+ # Load ASR Model
11
+ asr_model_name = "Futuresony/Future-sw_ASR-24-02-2025"
12
+ processor = Wav2Vec2Processor.from_pretrained(asr_model_name)
13
+ asr_model = Wav2Vec2ForCTC.from_pretrained(asr_model_name)
14
+
15
+ # Load Text Generation Model - Using Gradio Client
16
+ # client = InferenceClient("unsloth/gemma-3-1b-it") # Removed
17
+ llm_client = Client("Futuresony/Mr.Events") # Added
18
+
19
+ # def format_prompt(user_input): # Removed
20
+ # return f"{user_input}" # Removed
21
+
22
+ # Load TTS Models
23
+ swahili_dir = download("swh", "./data/swahili")
24
+ english_dir = download("eng", "./data/english")
25
+
26
+ swahili_tts = TTS(swahili_dir)
27
+ english_tts = TTS(english_dir)
28
+
29
+ # ASR Function
30
+ def transcribe(audio_file):
31
+ speech_array, sample_rate = torchaudio.load(audio_file)
32
+ resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
33
+ speech_array = resampler(speech_array).squeeze().numpy()
34
+ input_values = processor(speech_array, sampling_rate=16000, return_tensors="pt").input_values
35
+ with torch.no_grad():
36
+ logits = asr_model(input_values).logits
37
+ predicted_ids = torch.argmax(logits, dim=-1)
38
+ transcription = processor.batch_decode(predicted_ids)[0]
39
+ return transcription
40
+
41
+ # Text Generation Function - Using Gradio Client
42
+ def generate_text(prompt):
43
+ # formatted_prompt = format_prompt(prompt) # Removed
44
+ # response = client.text_generation(formatted_prompt, max_new_tokens=250, temperature=0.7, top_p=0.95) # Removed
45
+ result = llm_client.predict(query=prompt, api_name="/chat") # Added
46
+ return result.strip() # Modified to return the result from the Gradio Client
47
+
48
+ # TTS Function
49
+ def text_to_speech(text):
50
+ lang = detect(text)
51
+ wav_path = "./output.wav"
52
+ if lang == "sw":
53
+ swahili_tts.synthesis(text, wav_path=wav_path)
54
+ else:
55
+ english_tts.synthesis(text, wav_path=wav_path)
56
+ return wav_path
57
+
58
+ # Combined Processing Function
59
+ def process_audio(audio):
60
+ transcription = transcribe(audio)
61
+ generated_text = generate_text(transcription)
62
+ speech = text_to_speech(generated_text)
63
+ return transcription, generated_text, speech
64
+
65
+ # Gradio Interface
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("<p align='center' style='font-size: 20px;'>End-to-End ASR, Text Generation, and TTS</p>")
68
+ gr.HTML("<center>Upload or record audio. The model will transcribe, generate a response, and read it out.</center>")
69
+
70
+ audio_input = gr.Audio(label="Input Audio", type="filepath")
71
+ text_output = gr.Textbox(label="Transcription")
72
+ generated_text_output = gr.Textbox(label="Generated Text")
73
+ audio_output = gr.Audio(label="Output Speech")
74
+ submit_btn = gr.Button("Submit")
75
+
76
+ submit_btn.click(
77
+ fn=process_audio,
78
+ inputs=audio_input,
79
+ outputs=[text_output, generated_text_output, audio_output]
80
+ )
81
+
82
+ if __name__ == "__main__":
83
+ demo.launch()