File size: 6,474 Bytes
060e0ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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()