Spaces:
Sleeping
Sleeping
# voicepulse.py (multilingual feedback transcriber and word cloud generator with export) | |
import gradio as gr | |
import numpy as np | |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM | |
import matplotlib.pyplot as plt | |
from wordcloud import WordCloud | |
import nltk | |
import io | |
import torch | |
import csv | |
from gtts import gTTS | |
nltk.download("stopwords") | |
stop_words = set(nltk.corpus.stopwords.words("english")) | |
# Translation model for multilingual -> English | |
translation_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M") | |
translation_tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M") | |
device = 0 if torch.cuda.is_available() else -1 | |
# In-memory feedback word list and archive | |
feedback_words = [] | |
all_feedback = [] | |
# Sample audios | |
sample_texts = { | |
"Telugu Sample": "మీ సేవలు చాలా బాగున్నాయి. మేము చాలా సంతృప్తిగా ఉన్నాము.", | |
"Hindi Sample": "आपकी सेवा बहुत अच्छी थी और हम संतुष्ट हैं।", | |
"Tamil Sample": "உங்கள் சேவை மிகவும் சிறந்ததாக இருந்தது. நாங்கள் திருப்தி அடைந்தோம்.", | |
"English Sample": "Your support team was helpful and responsive." | |
} | |
def generate_sample_audio(text, lang_code): | |
tts = gTTS(text, lang=lang_code) | |
tts.save("sample_full.mp3") | |
from pydub import AudioSegment | |
full_audio = AudioSegment.from_mp3("sample_full.mp3") | |
short_audio = full_audio[:3000] # first 3 seconds | |
short_audio.export("sample.mp3", format="mp3") | |
return "sample.mp3" | |
def translate(text, src_lang, tgt_lang="eng_Latn"): | |
translation_pipeline = pipeline( | |
"translation", | |
model=translation_model, | |
tokenizer=translation_tokenizer, | |
src_lang=src_lang, | |
tgt_lang=tgt_lang, | |
max_length=400, | |
device=device | |
) | |
result = translation_pipeline(text) | |
return result[0]['translation_text'] | |
def get_transcription(audio, language): | |
sr, y = audio | |
y = y.astype(np.float32) | |
y /= np.max(np.abs(y)) | |
if language == "English": | |
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en") | |
return transcriber({"sampling_rate": sr, "raw": y})["text"] | |
model_map = { | |
"Hindi": ("theainerd/Wav2Vec2-large-xlsr-hindi", "hin_Deva"), | |
"Telugu": ("anuragshas/wav2vec2-large-xlsr-53-telugu", "tel_Telu"), | |
"Tamil": ("Harveenchadha/vakyansh-wav2vec2-tamil-tam-250", "tam_Taml"), | |
"Kannada": ("vasista22/whisper-kannada-medium", "kan_Knda") | |
} | |
model_name, src_lang = model_map[language] | |
transcriber = pipeline("automatic-speech-recognition", model=model_name) | |
text = transcriber({"sampling_rate": sr, "raw": y})["text"] | |
return translate(text, src_lang) | |
def process_feedback(audio, language): | |
transcription = get_transcription(audio, language) | |
# Use summarization to extract core feedback idea | |
summarizer = pipeline("summarization", model="mrm8488/distilbart2cnn-12-6") | |
summary = summarizer(transcription, max_length=60, min_length=10, do_sample=False)[0]['summary_text'] | |
# Save for download | |
all_feedback.append({"Language": language, "Transcription": transcription, "Summary": summary}) | |
# Extract meaningful words from summary | |
words = [w for w in summary.lower().split() if w.isalpha() and w not in stop_words] | |
feedback_words.extend(words) | |
freq = {w: feedback_words.count(w) for w in set(feedback_words)} | |
wc = WordCloud(width=800, height=400, background_color="white").generate_from_frequencies(freq) | |
buf = io.BytesIO() | |
plt.imshow(wc, interpolation="bilinear") | |
plt.axis("off") | |
plt.savefig(buf, format="png") | |
buf.seek(0) | |
image = plt.imread(buf, format="png") | |
return summary, image | |
def export_to_csv(): | |
with open("feedback_export.csv", "w", newline="") as csvfile: | |
fieldnames = ["Language", "Transcription", "Summary"] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for row in all_feedback: | |
writer.writerow(row) | |
return "feedback_export.csv" | |
demo = gr.Blocks() | |
with demo: | |
gr.Markdown("# 🎙️ VoicePulse Multilingual Feedback Collector") | |
gr.Markdown(""" | |
🗣️ **VoicePulse** lets you speak feedback in your language — Telugu, Hindi, Tamil, Kannada, or English. | |
It transcribes, translates, and summarizes the feedback, building a live word cloud to show what people care about. | |
Try speaking or use sample audio buttons below! | |
""") | |
with gr.Row(): | |
audio_input = gr.Audio(type="numpy", label="🎤 Speak your feedback") | |
lang_dropdown = gr.Dropdown(label="🌐 Language", choices=["English", "Hindi", "Telugu", "Tamil", "Kannada"], value="English") | |
with gr.Row(): | |
submit_btn = gr.Button("Process Feedback") | |
with gr.Row(): | |
gr.Markdown("### 🎧 Sample Feedback (Telugu, Hindi, Tamil, English)") | |
sample_btn_te = gr.Button("🔉 Telugu Sample") | |
sample_btn_hi = gr.Button("🔉 Hindi Sample") | |
sample_btn_ta = gr.Button("🔉 Tamil Sample") | |
sample_btn_en = gr.Button("🔉 English Sample") | |
sample_audio = gr.Audio(label="🔊 Sample Audio Output (Preview)") | |
with gr.Row(): | |
summary_out = gr.Textbox(label="📝 Summarized Feedback") | |
wordcloud_out = gr.Image(type="pil", label="☁️ Word Cloud of All Feedback") | |
with gr.Row(): | |
export_btn = gr.Button("📁 Export Feedback to CSV") | |
csv_file_output = gr.File(label="📄 Download CSV") | |
submit_btn.click(process_feedback, inputs=[audio_input, lang_dropdown], outputs=[summary_out, wordcloud_out]) | |
export_btn.click(export_to_csv, inputs=[], outputs=csv_file_output) | |
sample_btn_te.click(lambda: generate_sample_audio(sample_texts["Telugu Sample"], 'te'), inputs=[], outputs=sample_audio) | |
sample_btn_hi.click(lambda: generate_sample_audio(sample_texts["Hindi Sample"], 'hi'), inputs=[], outputs=sample_audio) | |
sample_btn_ta.click(lambda: generate_sample_audio(sample_texts["Tamil Sample"], 'ta'), inputs=[], outputs=sample_audio) | |
sample_btn_en.click(lambda: generate_sample_audio(sample_texts["English Sample"], 'en'), inputs=[], outputs=sample_audio) | |
demo.launch() | |