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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from transformers import pipeline
|
4 |
+
import gradio as gr
|
5 |
+
import pandas as pd
|
6 |
+
from io import StringIO
|
7 |
+
import os,re
|
8 |
+
from langchain.llms import OpenAI
|
9 |
+
import pandas as pd
|
10 |
|
11 |
+
from langchain.document_loaders import UnstructuredPDFLoader
|
12 |
|
13 |
+
from langchain.prompts import PromptTemplate
|
14 |
+
from langchain.chains import LLMChain
|
15 |
|
16 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
17 |
+
from langchain.vectorstores import Chroma
|
18 |
+
from langchain.text_splitter import CharacterTextSplitter
|
19 |
+
from langchain.llms import OpenAI
|
20 |
+
from langchain.chains import RetrievalQA
|
21 |
+
from langchain.document_loaders import TextLoader
|
22 |
+
from langchain.prompts import PromptTemplate
|
23 |
+
from langchain.callbacks.stdout import StdOutCallbackHandler
|
24 |
+
from langchain.chat_models.openai import ChatOpenAI
|
25 |
+
from langchain.prompts.prompt import PromptTemplate
|
26 |
|
27 |
+
from langchain.llms import OpenAI
|
28 |
+
from langchain.prompts import PromptTemplate
|
29 |
+
from langchain.chains import LLMChain
|
30 |
+
model = whisper.load_model("base")
|
31 |
+
sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
|
32 |
|
33 |
+
def predict(text):
|
34 |
+
# loader = UnstructuredPDFLoader(file_obj.orig_name)
|
35 |
+
# data = loader.load()
|
36 |
+
# text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
37 |
+
# texts = text_splitter.split_documents(data)
|
38 |
+
|
39 |
+
# embeddings = OpenAIEmbeddings()
|
40 |
+
# docsearch = Chroma.from_documents(texts, embeddings)
|
41 |
+
# qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="map_reduce", retriever=docsearch.as_retriever())
|
42 |
+
|
43 |
+
prompt_template = """Ignore all previous instructions. You are the world's hearing aid company markerting agent.
|
44 |
+
I am going to give you a text of a customer. Analyze it and you have 4 products in list which you have to suggest to the customer:
|
45 |
+
ampli-mini it is mainly works for Maximum comfort and discretion, ampli-connect it is mainly works for Connected to the things you love,
|
46 |
+
ampli-energy it is mainly works for Full of energy, like you, ampli-easy it is mainly works for Allow yourself to hear well.
|
47 |
+
You can also be creative, funny, or show emotions at time.
|
48 |
+
also share the book a appointment link of your company https://www.amplifon.com/uk/book-an-appointment
|
49 |
+
Question: {question}
|
50 |
+
Product details:"""
|
51 |
+
|
52 |
+
prompt_template_lang = """
|
53 |
+
You are the world's best languages translator. Will give you some text or paragraph which you have to convert into Tamil, Hindi, Kannada
|
54 |
+
and French.
|
55 |
+
Input Text: {text}
|
56 |
+
Tamil:
|
57 |
+
Hindi:
|
58 |
+
Kannada:
|
59 |
+
French:
|
60 |
+
"""
|
61 |
+
PROMPT = PromptTemplate(
|
62 |
+
template=prompt_template, input_variables=["question"]
|
63 |
+
)
|
64 |
+
PROMPT_lang = PromptTemplate(
|
65 |
+
template=prompt_template_lang, input_variables=["text"]
|
66 |
+
)
|
67 |
+
# chain_type_kwargs = {"prompt": PROMPT}
|
68 |
+
# qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever(), chain_type_kwargs=chain_type_kwargs)
|
69 |
+
|
70 |
+
#Actually, Hi, how are you doing? Actually, I am looking for the hearing aid for my grandfather. He has like age around 62, 65 year old and one of the like major thing that I am looking for the hearing aid product which is like maximum comfort. So if you have anything in that category, so can you please tell me? Thank you.
|
71 |
+
|
72 |
+
llm = OpenAI()
|
73 |
+
# prompt = PromptTemplate(
|
74 |
+
# input_variables=["product"],
|
75 |
+
# template="What is a good name for a company that makes {product}?",
|
76 |
+
# )
|
77 |
+
chain = LLMChain(llm=llm, prompt=PROMPT)
|
78 |
+
chain_lang = LLMChain(llm=llm, prompt=PROMPT_lang)
|
79 |
+
|
80 |
+
resp = chain.run(question=text)
|
81 |
+
resp_lang = chain_lang.run(text=resp)
|
82 |
+
|
83 |
+
# print(resp)
|
84 |
+
|
85 |
+
# response = []
|
86 |
+
# category = ["ampli-mini", "ampli-connect", "ampli-energy", "ampli-easy"]
|
87 |
+
# for value in category:
|
88 |
+
|
89 |
+
# response.append({value:ai(qa, value)})
|
90 |
+
|
91 |
+
# html_output = ""
|
92 |
+
# for obj in response:
|
93 |
+
# # Loop through the key-value pairs in the object
|
94 |
+
# for key, value in obj.items():
|
95 |
+
# value = re.sub(r'[\d\.]+', '', value)
|
96 |
+
# value_list = value.strip().split('\n')
|
97 |
+
# value_html = "<ol>"
|
98 |
+
# for item in value_list:
|
99 |
+
# value_html += "<li>{}</li>".format(item.strip())
|
100 |
+
# value_html += "</ol>"
|
101 |
+
# html_output += "<h2>{}</h2>".format(key)
|
102 |
+
# html_output += value_html
|
103 |
+
|
104 |
+
|
105 |
+
return [resp, resp_lang]
|
106 |
+
|
107 |
+
# def ai(qa,category):
|
108 |
+
# query = "please suggest "+ category +" interview questions"
|
109 |
+
# data = list(filter(None, qa.run(query).split('\n')))
|
110 |
+
# results = list(filter(lambda x: x != ' ', data))
|
111 |
+
# results = "\n".join(results)
|
112 |
+
|
113 |
+
# return results
|
114 |
+
|
115 |
+
|
116 |
+
def analyze_sentiment(text):
|
117 |
+
results = sentiment_analysis(text)
|
118 |
+
sentiment_results = {result['label']: result['score'] for result in results}
|
119 |
+
return sentiment_results
|
120 |
+
|
121 |
+
def get_sentiment_emoji(sentiment):
|
122 |
+
# Define the emojis corresponding to each sentiment
|
123 |
+
emoji_mapping = {
|
124 |
+
"disappointment": "๐",
|
125 |
+
"sadness": "๐ข",
|
126 |
+
"annoyance": "๐ ",
|
127 |
+
"neutral": "๐",
|
128 |
+
"disapproval": "๐",
|
129 |
+
"realization": "๐ฎ",
|
130 |
+
"nervousness": "๐ฌ",
|
131 |
+
"approval": "๐",
|
132 |
+
"joy": "๐",
|
133 |
+
"anger": "๐ก",
|
134 |
+
"embarrassment": "๐ณ",
|
135 |
+
"caring": "๐ค",
|
136 |
+
"remorse": "๐",
|
137 |
+
"disgust": "๐คข",
|
138 |
+
"grief": "๐ฅ",
|
139 |
+
"confusion": "๐",
|
140 |
+
"relief": "๐",
|
141 |
+
"desire": "๐",
|
142 |
+
"admiration": "๐",
|
143 |
+
"optimism": "๐",
|
144 |
+
"fear": "๐จ",
|
145 |
+
"love": "โค๏ธ",
|
146 |
+
"excitement": "๐",
|
147 |
+
"curiosity": "๐ค",
|
148 |
+
"amusement": "๐",
|
149 |
+
"surprise": "๐ฒ",
|
150 |
+
"gratitude": "๐",
|
151 |
+
"pride": "๐ฆ"
|
152 |
+
}
|
153 |
+
return emoji_mapping.get(sentiment, "")
|
154 |
+
|
155 |
+
def display_sentiment_results(sentiment_results, option):
|
156 |
+
sentiment_text = ""
|
157 |
+
for sentiment, score in sentiment_results.items():
|
158 |
+
emoji = get_sentiment_emoji(sentiment)
|
159 |
+
if option == "Sentiment Only":
|
160 |
+
sentiment_text += f"{sentiment} {emoji}\n"
|
161 |
+
elif option == "Sentiment + Score":
|
162 |
+
sentiment_text += f"{sentiment} {emoji}: {score}\n"
|
163 |
+
return sentiment_text
|
164 |
+
|
165 |
+
def inference(audio, sentiment_option):
|
166 |
+
audio = whisper.load_audio(audio)
|
167 |
+
audio = whisper.pad_or_trim(audio)
|
168 |
+
|
169 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
170 |
+
|
171 |
+
_, probs = model.detect_language(mel)
|
172 |
+
lang = max(probs, key=probs.get)
|
173 |
+
|
174 |
+
options = whisper.DecodingOptions(fp16=False)
|
175 |
+
result = whisper.decode(model, mel, options)
|
176 |
+
|
177 |
+
sentiment_results = analyze_sentiment(result.text)
|
178 |
+
print(result.text)
|
179 |
+
prediction = predict(result.text)
|
180 |
+
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
|
181 |
+
|
182 |
+
return lang.upper(), result.text, sentiment_output, prediction[0], prediction[1]
|
183 |
+
|
184 |
+
|
185 |
+
def main():
|
186 |
+
title = """<h1 align="center">๐ค Multilingual ASR ๐ฌ</h1>"""
|
187 |
+
description = """
|
188 |
+
๐ป This demo showcases a general-purpose speech recognition model called Whisper. It is trained on a large dataset of diverse audio and supports multilingual speech recognition, speech translation, and language identification tasks.<br><br>
|
189 |
+
<br>
|
190 |
+
โ๏ธ Components of the tool:<br>
|
191 |
+
<br>
|
192 |
+
- Real-time multilingual speech recognition<br>
|
193 |
+
- Language identification<br>
|
194 |
+
- Sentiment analysis of the transcriptions<br>
|
195 |
+
<br>
|
196 |
+
๐ฏ The sentiment analysis results are provided as a dictionary with different emotions and their corresponding scores.<br>
|
197 |
+
<br>
|
198 |
+
๐ The sentiment analysis results are displayed with emojis representing the corresponding sentiment.<br>
|
199 |
+
<br>
|
200 |
+
โ
The higher the score for a specific emotion, the stronger the presence of that emotion in the transcribed text.<br>
|
201 |
+
<br>
|
202 |
+
โ Use the microphone for real-time speech recognition.<br>
|
203 |
+
<br>
|
204 |
+
โก๏ธ The model will transcribe the audio and perform sentiment analysis on the transcribed text.<br>
|
205 |
+
"""
|
206 |
+
|
207 |
+
custom_css = """
|
208 |
+
#banner-image {
|
209 |
+
display: block;
|
210 |
+
margin-left: auto;
|
211 |
+
margin-right: auto;
|
212 |
+
}
|
213 |
+
#chat-message {
|
214 |
+
font-size: 14px;
|
215 |
+
min-height: 300px;
|
216 |
+
}
|
217 |
+
"""
|
218 |
+
|
219 |
+
block = gr.Blocks(css=custom_css)
|
220 |
+
|
221 |
+
with block:
|
222 |
+
gr.HTML(title)
|
223 |
+
|
224 |
+
with gr.Row():
|
225 |
+
with gr.Column():
|
226 |
+
gr.HTML(description)
|
227 |
+
|
228 |
+
with gr.Group():
|
229 |
+
with gr.Box():
|
230 |
+
|
231 |
+
audio = gr.Audio(
|
232 |
+
label="Input Audio",
|
233 |
+
show_label=False,
|
234 |
+
source="microphone",
|
235 |
+
type="filepath"
|
236 |
+
)
|
237 |
+
|
238 |
+
sentiment_option = gr.Radio(
|
239 |
+
choices=["Sentiment Only", "Sentiment + Score"],
|
240 |
+
label="Select an option",
|
241 |
+
default="Sentiment Only"
|
242 |
+
)
|
243 |
+
|
244 |
+
btn = gr.Button("Transcribe")
|
245 |
+
|
246 |
+
lang_str = gr.Textbox(label="Language")
|
247 |
+
|
248 |
+
text = gr.Textbox(label="Transcription")
|
249 |
+
|
250 |
+
sentiment_output = gr.Textbox(label="Sentiment Analysis Results", output=True)
|
251 |
+
|
252 |
+
prediction = gr.Textbox(label="Prediction")
|
253 |
+
|
254 |
+
language_translation = gr.Textbox(label="Language Translation")
|
255 |
+
|
256 |
+
|
257 |
+
btn.click(inference, inputs=[audio, sentiment_option], outputs=[lang_str, text, sentiment_output, prediction,language_translation])
|
258 |
+
|
259 |
+
# gr.HTML('''
|
260 |
+
# <div class="footer">
|
261 |
+
# <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a>
|
262 |
+
# </p>
|
263 |
+
# </div>
|
264 |
+
# ''')
|
265 |
+
|
266 |
+
block.launch()
|