Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,106 +3,49 @@ import torch
|
|
3 |
import torchaudio
|
4 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
from huggingface_hub import InferenceClient
|
6 |
-
from ttsmms import download, TTS
|
7 |
-
from langdetect import detect
|
8 |
-
import os
|
9 |
-
import wave
|
10 |
-
import numpy as np
|
11 |
|
12 |
-
#
|
13 |
asr_model_name = "Futuresony/Future-sw_ASR-24-02-2025"
|
14 |
processor = Wav2Vec2Processor.from_pretrained(asr_model_name)
|
15 |
asr_model = Wav2Vec2ForCTC.from_pretrained(asr_model_name)
|
16 |
|
17 |
-
#
|
18 |
client = InferenceClient("unsloth/gemma-3-1b-it")
|
19 |
-
def format_prompt(user_input):
|
20 |
-
return f"{user_input}"
|
21 |
|
22 |
-
#
|
23 |
-
swahili_dir = download("swh", "./data/swahili")
|
24 |
-
english_dir = download("eng", "./data/english")
|
25 |
-
swahili_tts = TTS(swahili_dir)
|
26 |
-
english_tts = TTS(english_dir)
|
27 |
-
|
28 |
-
# === Step 4: Generate silent fallback audio ===
|
29 |
-
def create_silent_wav(filename="./error.wav", duration_sec=1.0, sample_rate=16000):
|
30 |
-
if not os.path.exists(filename):
|
31 |
-
silence = np.zeros(int(sample_rate * duration_sec), dtype=np.int16)
|
32 |
-
with wave.open(filename, 'w') as wf:
|
33 |
-
wf.setnchannels(1)
|
34 |
-
wf.setsampwidth(2)
|
35 |
-
wf.setframerate(sample_rate)
|
36 |
-
wf.writeframes(silence.tobytes())
|
37 |
-
|
38 |
-
create_silent_wav() # Call once at startup
|
39 |
-
|
40 |
-
# === Step 5: Transcription Function ===
|
41 |
def transcribe(audio_file):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
print("ASR Error:", e)
|
54 |
-
return "[ASR Failed]"
|
55 |
-
|
56 |
-
# === Step 6: Text Generation Function ===
|
57 |
def generate_text(prompt):
|
58 |
-
|
59 |
-
|
60 |
-
response = client.text_generation(formatted_prompt, max_new_tokens=250, temperature=0.7, top_p=0.95)
|
61 |
-
return response.strip()
|
62 |
-
except Exception as e:
|
63 |
-
print("Text Generation Error:", e)
|
64 |
-
return "[Text Generation Failed]"
|
65 |
|
66 |
-
#
|
67 |
-
def
|
68 |
-
|
69 |
-
|
70 |
-
try:
|
71 |
-
if lang == "sw":
|
72 |
-
swahili_tts.synthesis(text, wav_path=wav_path)
|
73 |
-
else:
|
74 |
-
english_tts.synthesis(text, wav_path=wav_path)
|
75 |
-
return wav_path
|
76 |
-
except Exception as e:
|
77 |
-
print("TTS Error:", e)
|
78 |
-
return "./error.wav" # Use fallback silent audio
|
79 |
-
|
80 |
-
# === Step 8: Combined Logic ===
|
81 |
-
def process_audio(audio):
|
82 |
transcription = transcribe(audio)
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
audio_output = gr.Audio(label="π Output Speech")
|
99 |
-
submit_btn = gr.Button("Submit")
|
100 |
-
|
101 |
-
submit_btn.click(
|
102 |
-
fn=process_audio,
|
103 |
-
inputs=audio_input,
|
104 |
-
outputs=[text_output, generated_text_output, audio_output]
|
105 |
-
)
|
106 |
-
|
107 |
-
if __name__ == "__main__":
|
108 |
-
demo.launch()
|
|
|
3 |
import torchaudio
|
4 |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
5 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
# Load ASR model
|
8 |
asr_model_name = "Futuresony/Future-sw_ASR-24-02-2025"
|
9 |
processor = Wav2Vec2Processor.from_pretrained(asr_model_name)
|
10 |
asr_model = Wav2Vec2ForCTC.from_pretrained(asr_model_name)
|
11 |
|
12 |
+
# Load text generation client
|
13 |
client = InferenceClient("unsloth/gemma-3-1b-it")
|
|
|
|
|
14 |
|
15 |
+
# Function: Transcribe audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def transcribe(audio_file):
|
17 |
+
waveform, sample_rate = torchaudio.load(audio_file)
|
18 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
|
19 |
+
waveform = resampler(waveform).squeeze().numpy()
|
20 |
+
inputs = processor(waveform, sampling_rate=16000, return_tensors="pt")
|
21 |
+
with torch.no_grad():
|
22 |
+
logits = asr_model(inputs.input_values).logits
|
23 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
24 |
+
transcription = processor.batch_decode(predicted_ids)[0]
|
25 |
+
return transcription
|
26 |
+
|
27 |
+
# Function: Generate response based on transcription
|
|
|
|
|
|
|
|
|
28 |
def generate_text(prompt):
|
29 |
+
response = client.text_generation(prompt, max_new_tokens=150, temperature=0.7)
|
30 |
+
return response.strip()
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# Gradio interface
|
33 |
+
def asr_and_generate(audio):
|
34 |
+
if not audio:
|
35 |
+
return "No audio provided.", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
transcription = transcribe(audio)
|
37 |
+
generated = generate_text(transcription)
|
38 |
+
return transcription, generated
|
39 |
+
|
40 |
+
demo = gr.Interface(
|
41 |
+
fn=asr_and_generate,
|
42 |
+
inputs=gr.Audio(label="Upload or Record Audio", type="filepath"),
|
43 |
+
outputs=[
|
44 |
+
gr.Textbox(label="Transcription"),
|
45 |
+
gr.Textbox(label="AI Response")
|
46 |
+
],
|
47 |
+
title="ASR to Text Generation",
|
48 |
+
description="Upload audio. The model will transcribe speech to text and generate a response using a fine-tuned text model."
|
49 |
+
)
|
50 |
+
|
51 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|