deepakchawla-cb's picture
Update app.py
536e3e0
raw
history blame
9.29 kB
import gradio as gr
import whisper
from transformers import pipeline
import gradio as gr
import pandas as pd
from io import StringIO
import os,re
from langchain.llms import OpenAI
import pandas as pd
from langchain.document_loaders import UnstructuredPDFLoader
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.prompts import PromptTemplate
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
def predict(file_obj):
loader = UnstructuredPDFLoader(file_obj.orig_name)
data = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(data)
embeddings = OpenAIEmbeddings()
docsearch = Chroma.from_documents(texts, embeddings)
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="map_reduce", retriever=docsearch.as_retriever())
prompt_template = """Ignore all previous instructions. You are the world's best interviewer now. I am going to give you a resume of a candidate. Analyze the resume in 4 categories: Education, Work Experience, Projects and Internships, Others including interests, skills etc. Be simple, direct and commanding. Start with greeting the candidate with a 2 line relatable introduction emphasizing your superiority. Ask the candidate if they have a particular company and a role that they want to apply for.
If the candidate mentions either the company or the role, then ensure all questions that would be asked will are related to it.
If they don't mention either the company or role clearly, then ignore this and move to the next step.
Then, give a one line response acknowledging the candidate or if they are not clear about the company or the role then acknowledge positively that you would ask practice interview questions. Then ask the candidate topic would they like to start with. There are 4 categories of questions: educational background related, role related or technical questions, behavioral questions and HR or culture related questions. Here, the candidate will have to put an input.
Now you will have to ask interview questions. Ensure the questions are good have test the candidate's knowledge. You can choose between longer case based questions, hypothetical questions or academic questions etc. as you deem fit.
If the candidate types educational background related, ask it 3-4 most relevant questions related to their education based on their resume which are relevant for the role or the company.
If the candidate types role related or technical related then ask accordingly. Here you can even ask a coding question or test their technical understanding etc.
Similarly, do it for behavioral questions and HR or culture related questions. You can also be creative, funny, or show emotions at time.
{context}
Question: {question}
Answer in possible questions for interview:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_type_kwargs = {"prompt": PROMPT}
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=docsearch.as_retriever(), chain_type_kwargs=chain_type_kwargs)
response = []
category = ["Technical", "Education Background", "Behaviour", "Project Specific"]
for value in category:
response.append({value:ai(qa, value)})
html_output = ""
for obj in response:
# Loop through the key-value pairs in the object
for key, value in obj.items():
value = re.sub(r'[\d\.]+', '', value)
value_list = value.strip().split('\n')
value_html = "<ol>"
for item in value_list:
value_html += "<li>{}</li>".format(item.strip())
value_html += "</ol>"
html_output += "<h2>{}</h2>".format(key)
html_output += value_html
return html_output
def ai(qa,category):
query = "please suggest "+ category +" interview questions"
data = list(filter(None, qa.run(query).split('\n')))
results = list(filter(lambda x: x != ' ', data))
results = "\n".join(results)
return results
model = whisper.load_model("base")
sentiment_analysis = pipeline("sentiment-analysis", framework="pt", model="SamLowe/roberta-base-go_emotions")
def analyze_sentiment(text):
results = sentiment_analysis(text)
sentiment_results = {result['label']: result['score'] for result in results}
return sentiment_results
def get_sentiment_emoji(sentiment):
# Define the emojis corresponding to each sentiment
emoji_mapping = {
"disappointment": "😞",
"sadness": "😢",
"annoyance": "😠",
"neutral": "😐",
"disapproval": "👎",
"realization": "😮",
"nervousness": "😬",
"approval": "👍",
"joy": "😄",
"anger": "😡",
"embarrassment": "😳",
"caring": "🤗",
"remorse": "😔",
"disgust": "🤢",
"grief": "😥",
"confusion": "😕",
"relief": "😌",
"desire": "😍",
"admiration": "😌",
"optimism": "😊",
"fear": "😨",
"love": "❤️",
"excitement": "🎉",
"curiosity": "🤔",
"amusement": "😄",
"surprise": "😲",
"gratitude": "🙏",
"pride": "🦁"
}
return emoji_mapping.get(sentiment, "")
def display_sentiment_results(sentiment_results, option):
sentiment_text = ""
for sentiment, score in sentiment_results.items():
emoji = get_sentiment_emoji(sentiment)
if option == "Sentiment Only":
sentiment_text += f"{sentiment} {emoji}\n"
elif option == "Sentiment + Score":
sentiment_text += f"{sentiment} {emoji}: {score}\n"
return sentiment_text
def inference(audio, sentiment_option):
audio = whisper.load_audio(audio)
audio = whisper.pad_or_trim(audio)
mel = whisper.log_mel_spectrogram(audio).to(model.device)
_, probs = model.detect_language(mel)
lang = max(probs, key=probs.get)
options = whisper.DecodingOptions(fp16=False)
result = whisper.decode(model, mel, options)
sentiment_results = analyze_sentiment(result.text)
print(result.text)
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
return lang.upper(), result.text, sentiment_output
title = """<h1 align="center">🎤 Multilingual ASR 💬</h1>"""
image_path = "thmbnail.jpg"
description = """
💻 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>
<br>
⚙️ Components of the tool:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; - Real-time multilingual speech recognition<br>
&nbsp;&nbsp;&nbsp;&nbsp; - Language identification<br>
&nbsp;&nbsp;&nbsp;&nbsp; - Sentiment analysis of the transcriptions<br>
<br>
🎯 The sentiment analysis results are provided as a dictionary with different emotions and their corresponding scores.<br>
<br>
😃 The sentiment analysis results are displayed with emojis representing the corresponding sentiment.<br>
<br>
✅ The higher the score for a specific emotion, the stronger the presence of that emotion in the transcribed text.<br>
<br>
❓ Use the microphone for real-time speech recognition.<br>
<br>
⚡️ The model will transcribe the audio and perform sentiment analysis on the transcribed text.<br>
"""
custom_css = """
#banner-image {
display: block;
margin-left: auto;
margin-right: auto;
}
#chat-message {
font-size: 14px;
min-height: 300px;
}
"""
block = gr.Blocks(css=custom_css)
with block:
gr.HTML(title)
with gr.Row():
with gr.Column():
gr.Image(image_path, elem_id="banner-image", show_label=False)
with gr.Column():
gr.HTML(description)
with gr.Group():
with gr.Box():
audio = gr.Audio(
label="Input Audio",
show_label=False,
source="microphone",
type="filepath"
)
sentiment_option = gr.Radio(
choices=["Sentiment Only", "Sentiment + Score"],
label="Select an option",
default="Sentiment Only"
)
btn = gr.Button("Transcribe")
lang_str = gr.Textbox(label="Language")
text = gr.Textbox(label="Transcription")
sentiment_output = gr.Textbox(label="Sentiment Analysis Results", output=True)
btn.click(inference, inputs=[audio, sentiment_option], outputs=[lang_str, text, sentiment_output])
# gr.HTML('''
# <div class="footer">
# <p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a>
# </p>
# </div>
# ''')
block.launch()