File size: 9,645 Bytes
f79ca48
 
 
 
 
 
 
 
 
 
 
0461ac3
f79ca48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
047777e
f79ca48
 
0461ac3
f79ca48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
047777e
f79ca48
 
 
 
 
 
 
 
047777e
f79ca48
 
 
047777e
f79ca48
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import gradio as gr
import os
import whisper
import torch
from gtts import gTTS
from sentence_transformers import SentenceTransformer
import faiss
import pandas as pd
from datasets import load_dataset
from deep_translator import GoogleTranslator
from langdetect import detect
#from groq import Groq  # Correct import for Groq API

# Set up Whisper with a smaller model or on CPU
model_name = "small"  # Use "small", "base", or "medium" for smaller models
whisper_model = whisper.load_model(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
whisper_model.to(device)

# Initialize the GoogleTranslator from deep-translator
translator = GoogleTranslator(source='auto', target='en')

# Load and prepare the dataset for retrieval
dataset = load_dataset("qgyd2021/e_commerce_customer_service", "faq")
train_dataset = dataset['train']

# Initialize the SentenceTransformer model
embedder = SentenceTransformer('paraphrase-MiniLM-L6-v2')

# Encode the questions from the dataset and set up FAISS
dataset_embeddings = embedder.encode(train_dataset['question'], convert_to_tensor=True)
index = faiss.IndexFlatL2(dataset_embeddings.shape[1])  # Create an index based on L2 distance
index.add(dataset_embeddings.cpu().numpy())  # Add the embeddings to the index

# Set up Groq API with direct API key

api_key = os.getenv("api_key") 
import torch
from transformers import pipeline
from langdetect import detect
from deep_translator import GoogleTranslator
from gtts import gTTS

# Initialize the sentiment analysis pipeline
device = 0 if torch.cuda.is_available() else -1
try:
    sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english", device=device)
except Exception as e:
    print(f"Error loading sentiment analysis model: {e}")

# Function to detect the language
def detect_language(text):
    try:
        return detect(text)
    except Exception as e:
        print(f"Error during language detection: {e}")
        return "en"  # Default to English if detection fails

# Translate text using deep-translator
def translate_text(text, dest_lang):
    try:
        return GoogleTranslator(source='auto', target=dest_lang).translate(text)
    except Exception as e:
        print(f"Error during translation: {e}")
        return text  # Return original text if translation fails

# Function to generate a greeting based on sentiment
def generate_greeting(sentiment, lang):
    try:
        if sentiment == 'NEGATIVE':
            if lang in ['ur', 'hi']:
                return "پریشان نہ ہوں، میں آپ کی مدد کے لئے یہاں ہوں."
            else:
                return "Please don't be sad, I'm here to solve your problem."
        elif sentiment == 'NEUTRAL':
            if lang in ['ur', 'hi']:
                return "آپ کا مسئلہ حل کرتے ہیں، آپ فکر نہ کریں."
            else:
                return "I understand your concern, let's get that sorted out."
        elif sentiment == 'POSITIVE':
            if lang in ['ur', 'hi']:
                return "یہ خوشی کی بات ہے کہ آپ خوش ہیں! آئیں، ہم اسے بہتر بناتے ہیں."
            else:
                return "I'm glad you're feeling positive! Let's make things even better."
        else:
            if lang in ['ur', 'hi']:
                return "ہیلو! میں آج تمہاری مدد کیسے کر سکتا ہوں؟"
            else:
                return "Hello! How can I assist you today?"
    except Exception as e:
        print(f"Error generating greeting: {e}")
        return "Hello!"

# Function to transcribe audio using Whisper
def transcribe_audio(audio_path):
    try:
        result = whisper_model.transcribe(audio_path)
        transcription = result['text']
        print(f"Transcription result: {transcription}")
        return transcription
    except Exception as e:
        print(f"Error during transcription: {e}")
        return "Error during transcription"

# Function to generate a chatbot response based on transcription
def generate_chatbot_response(transcription):
    try:
        # Detect language of the transcription
        detected_language = detect_language(transcription)

        # Translate to English if necessary
        if detected_language in ['ur', 'hi']:
            transcription = translate_text(transcription, 'en')

        # Perform sentiment analysis
        sentiment_result = sentiment_analyzer(transcription)[0]
        sentiment = sentiment_result['label'].upper()

        # Generate a greeting based on sentiment
        greeting = generate_greeting(sentiment, detected_language)

        # Retrieve relevant context using FAISS
        transcription_embedding = embedder.encode([transcription], convert_to_tensor=True)
        _, indices = index.search(transcription_embedding.cpu().numpy(), k=1)
        best_match_index = indices[0][0]
        context = train_dataset['answer'][best_match_index]
        url = train_dataset['url'][best_match_index]

        # Generate the full response
        response = f"{greeting}\n\n{context}\n\nPlease visit this link for your query: {url}"

        # Translate the response back to Urdu if necessary
        if detected_language in ['ur', 'hi']:
            response = translate_text(response, 'ur')

        return response
    except Exception as e:
        print(f"Error during chatbot response generation: {e}")
        return "Error during response generation"

# Function to convert text to speech using gTTS
def text_to_speech(text, lang='en'):
    try:
        tts = gTTS(text=text, lang=lang)
        tts.save("response.mp3")
        return "response.mp3"
    except Exception as e:
        print(f"Error during text-to-speech conversion: {e}")
        return "Error during text-to-speech conversion"

# Main function for Gradio interface
def chatbot(text_input=None, audio_input=None):
    if audio_input:
        # Step 1: Transcribe audio to text if audio input is provided
        transcription = transcribe_audio(audio_input)
        input_text = transcription
    else:
        # Use the text input directly if provided
        input_text = text_input

    # Step 2: Generate a chatbot response based on the input text
    response = generate_chatbot_response(input_text)

    # Step 3: Convert the response text to speech if the original input was audio
    if audio_input:
        lang = 'ur' if detect_language(input_text) in ['ur', 'hi'] else 'en'
        audio_path = text_to_speech(response, lang=lang)
        return input_text, response, audio_path
    else:
        return input_text, response, None

# Custom CSS for styling the interface and buttons
custom_css = """
body {
    font-family: 'Arial', sans-serif;
    background-color: #1e1e1e;  /* Black background */
    color: white;  /* White text */
}

h1 {
    font-size: 36px;
    color: white;
    text-align: center;
    margin-bottom: 20px;
}

h2 {
    font-size: 24px;
    color: white;
    text-align: center;
    margin-bottom: 10px;
}

.instructions {
    font-size: 16px;  /* Smaller font size for instructions */
    color: #cccccc;  /* Light gray color for instructions */
    text-align: center;
    margin-bottom: 20px;
}

.gradio-container {
    background-color: #1e1e1e;
    padding: 20px;
    border-radius: 10px;
}

.gr-box {
    border-radius: 5px;
    border: 1px solid #333;
    padding: 10px;
    margin-bottom: 10px;
}

.gr-button {
    border-radius: 5px;
    padding: 10px;
    font-weight: bold;
    font-size: 16px;
    transition: background-color 0.3s;
}

.gr-button-submit {
    background-color: #28a745;  /* Green submit button */
    color: white;
}

.gr-button-submit:hover {
    background-color: #218838;
}

.gr-button-clear {
    background-color: #dc3545;  /* Red clear button */
    color: white;
}

.gr-button-clear:hover {
    background-color: #c82333;
}

.gr-textbox, .gr-audio {
    border-radius: 5px;
    border: 1px solid #0056b3;  /* Blue border */
    padding: 8px;
    background-color: #2e2e2e;
    color: white;
}

.gr-textbox {
    background-color: #0056b3;  /* Blue background for textboxes */
    color: white;
}

.gr-container {
    max-width: 900px;
    margin: auto;
}
"""

import gradio as gr

# Gradio interface setup with updated CSS
with gr.Blocks(css=custom_css) as iface:
    gr.Markdown("<h1>Multilingual Customer Service Chatbot</h1>")
    gr.Markdown("<h2>Ask your questions</h2>")
    gr.Markdown("<p class='instructions'>If you type in Urdu, it will respond in Urdu. If in English, it will respond in English. Same with voice.</p>")

    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(lines=2, placeholder="Type your query here...", label="Text Input (Optional)")
            audio_input = gr.Audio(type="filepath", label="Audio Input (Optional)")
        with gr.Column():
            transcription_output = gr.Textbox(label="Transcription")  # Add transcription output
            response_text = gr.Textbox(label="Chatbot Response")
            response_audio = gr.Audio(label="Response Audio (if applicable)")

    with gr.Row():
        submit_btn = gr.Button("Submit", elem_id="submit-btn", variant="primary")
        clear_btn = gr.Button("Clear", elem_id="clear-btn", variant="secondary")

    submit_btn.click(chatbot, inputs=[text_input, audio_input], outputs=[transcription_output, response_text, response_audio])
    clear_btn.click(lambda: (None, None, None, None, None), inputs=[], outputs=[text_input, audio_input, transcription_output, response_text, response_audio])

# Launch the Gradio interface
iface.launch()