Spaces:
Runtime error
Runtime error
| from pydantic import NoneStr | |
| import os | |
| import mimetypes | |
| import validators | |
| import requests | |
| import tempfile | |
| import gradio as gr | |
| import openai | |
| import re | |
| import json | |
| import matplotlib.pyplot as plt | |
| import plotly.express as px | |
| import pandas as pd | |
| class SentimentAnalyzer: | |
| def __init__(self): | |
| # self.model="facebook/bart-large-mnli" | |
| openai.api_key=os.getenv("OPENAI_API_KEY") | |
| def emotion_analysis(self,text): | |
| prompt = f""" Your task is find the top 3 emotion for this converstion {text}: <Sadness, Happiness, Fear, Disgust, Anger> and it's emotion score for the Human Resource Assistant Chatbot and Job Seeker conversation text.\ | |
| you are analyze the text and provide the output in the following list format heigher to lower order: '''["emotion1","emotion2","emotion3"][score1,score2,score3]''' [with top 3 result having the highest score] | |
| The scores should be in the range of 0.0 to 1.0, where 1.0 represents the highest intensity of the emotion. | |
| """ | |
| response = openai.Completion.create( | |
| model="text-davinci-003", | |
| prompt=prompt, | |
| temperature=0, | |
| max_tokens=60, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0 | |
| ) | |
| message = response.choices[0].text.strip().replace("\n","") | |
| return message | |
| def analyze_sentiment_for_graph(self, text): | |
| prompt = f""" Your task is find the setiments for this converstion {text} : <labels = positive, negative, neutral> and it's sentiment score for the Human Resource Assistant Chatbot and Job Seeker conversation text.\ | |
| you are analyze the text and provide the output in the following json format heigher to lower order: '''["label1","label2","label3"][score1,score2,score3]''' | |
| """ | |
| response = openai.Completion.create( | |
| model="text-davinci-003", | |
| prompt=prompt, | |
| temperature=0, | |
| max_tokens=60, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0 | |
| ) | |
| # Extract the generated text | |
| sentiment_scores = response.choices[0].text.strip() | |
| start_index = sentiment_scores.find("[") | |
| end_index = sentiment_scores.find("]") | |
| list1_text = sentiment_scores[start_index + 1: end_index] | |
| list2_text = sentiment_scores[end_index + 2:-1] | |
| sentiment = list(map(str.strip, list1_text.split(","))) | |
| scores = list(map(float, list2_text.split(","))) | |
| score_dict={"Sentiment": sentiment, "Score": scores} | |
| print(score_dict) | |
| return score_dict | |
| def emotion_analysis_for_graph(self,text): | |
| start_index = text.find("[") | |
| end_index = text.find("]") | |
| list1_text = text[start_index + 1: end_index] | |
| list2_text = text[end_index + 2:-1] | |
| emotions = list(map(str.strip, list1_text.split(","))) | |
| scores = list(map(float, list2_text.split(","))) | |
| score_dict={"Emotion": emotions, "Score": scores} | |
| print(score_dict) | |
| return score_dict | |
| class Summarizer: | |
| def __init__(self): | |
| openai.api_key=os.getenv("OPENAI_API_KEY") | |
| def generate_summary(self, text): | |
| model_engine = "text-davinci-003" | |
| prompt = f"""summarize the following conversation delimited by triple backticks. write within 30 words.```{text}``` """ | |
| completions = openai.Completion.create( | |
| engine=model_engine, | |
| prompt=prompt, | |
| max_tokens=60, | |
| n=1, | |
| stop=None, | |
| temperature=0.5, | |
| ) | |
| message = completions.choices[0].text.strip() | |
| return message | |
| history_state = gr.State() | |
| summarizer = Summarizer() | |
| sentiment = SentimentAnalyzer() | |
| class LangChain_Document_QA: | |
| def __init__(self): | |
| openai.api_key=os.getenv("OPENAI_API_KEY") | |
| def _add_text(self,history, text): | |
| history = history + [(text, None)] | |
| history_state.value = history | |
| return history,gr.update(value="", interactive=False) | |
| def _agent_text(self,history, text): | |
| response = text | |
| history[-1][1] = response | |
| history_state.value = history | |
| return history | |
| def _chat_history(self): | |
| history = history_state.value | |
| formatted_history = " " | |
| for entry in history: | |
| customer_text, agent_text = entry | |
| formatted_history += f"Job Seeker: {customer_text}\n" | |
| if agent_text: | |
| formatted_history += f"Human Resource Assistant Chatbot: {agent_text}\n" | |
| return formatted_history | |
| def _display_history(self): | |
| formatted_history=self._chat_history() | |
| summary=summarizer.generate_summary(formatted_history) | |
| return summary | |
| def _display_graph(self,sentiment_scores): | |
| df = pd.DataFrame(sentiment_scores) | |
| fig = px.bar(df, x='Score', y='Sentiment', orientation='h', labels={'Score': 'Score', 'Labels': 'Sentiment'}) | |
| fig.update_layout(height=500, width=200) | |
| return fig | |
| def _display_graph_emotion(self,customer_emotion_score): | |
| fig = px.pie(customer_emotion_score, values='Score', names='Emotion', title='Emotion Distribution', hover_data=['Score']) | |
| #fig.update_traces(texttemplate='Emotion', textposition='outside') | |
| fig.update_layout(height=500, width=200) | |
| return fig | |
| def _history_of_chat(self): | |
| history = history_state.value | |
| formatted_history = "" | |
| client="" | |
| agent="" | |
| for entry in history: | |
| customer_text, agent_text = entry | |
| client+=customer_text | |
| formatted_history += f"Job Seeker: {customer_text}\n" | |
| if agent_text: | |
| agent+=agent_text | |
| formatted_history += f"Human Resource Assistant Chatbot: {agent_text}\n" | |
| return client,agent | |
| def _suggested_answer(self,text): | |
| try: | |
| history = self._chat_history() | |
| prompt = f"""Looking for a new job? I'm here to help. First, let's discuss your background and what you're aiming for career-wise. | |
| Here are some job-hunt tips: | |
| First Impressions: Be on time, dress well, and stay positive during interviews. | |
| Preparation: Learn about the company, remember your interviewer's name, carry extra resumes, and have good questions ready. | |
| Presentation: Be neat and professional in your appearance. | |
| Resume: Pick a clear format, include your contact details and summary. List your experiences, achievements, skills, and tailor each application. | |
| Attitude: Highlight your transferable skills and eagerness to learn. Don't apologize for lack of experience - focus on what you can offer. | |
| With patience and strategy, you'll find a job that suits you. What kind of job are you seeking? | |
| Please note, I'll be professional and respectful during our chat. Expect a polite closing even when you thank me. Remember, I won't repeat my replies. | |
| So, how can I help with your job search today? | |
| Chat History:['''{history}'''] | |
| Job Seeker: ['''{text}'''] | |
| Perform as Human Resource Assistant Chatbot | |
| """ | |
| response = openai.Completion.create( | |
| model="text-davinci-003", | |
| prompt=prompt, | |
| temperature=0, | |
| max_tokens=500, | |
| top_p=1, | |
| frequency_penalty=0, | |
| presence_penalty=0.6, | |
| ) | |
| message = response.choices[0].text.strip() | |
| if ":" in message: | |
| message = re.sub(r'^.*:', '', message) | |
| return message.strip() | |
| except: | |
| return "Hi, How can I help you?" | |
| def _text_box(self,customer_emotion,customer_sentiment_score): | |
| sentiment_str = ', '.join([f'{label}: {score}' for label, score in zip(customer_sentiment_score['Sentiment'], customer_sentiment_score['Score'])]) | |
| #emotion_str = ', '.join([f'{emotion}: {score}' for emotion, score in zip(customer_emotion['Emotion'], customer_emotion['Score'])]) | |
| return f"Sentiment: {sentiment_str},\nEmotion: {customer_emotion}" | |
| def _on_sentiment_btn_click(self): | |
| client=self._history_of_chat() | |
| customer_emotion=sentiment.emotion_analysis(client) | |
| customer_sentiment_score = sentiment.analyze_sentiment_for_graph(client) | |
| scores=self._text_box(customer_emotion,customer_sentiment_score) | |
| customer_fig=self._display_graph(customer_sentiment_score) | |
| customer_fig.update_layout(title="Sentiment Analysis",width=800) | |
| customer_emotion_score = sentiment.emotion_analysis_for_graph(customer_emotion) | |
| customer_emotion_fig=self._display_graph_emotion(customer_emotion_score) | |
| customer_emotion_fig.update_layout(title="Emotion Analysis",width=800) | |
| return scores,customer_fig,customer_emotion_fig | |
| def clear_func(self): | |
| history_state.clear() | |
| def gradio_interface(self): | |
| with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo: | |
| with gr.Row(): | |
| gr.HTML("""<img class="leftimage" align="left" src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Randstad_Logo.svg/2560px-Randstad_Logo.svg.png" alt="Image" width="210" height="210"> | |
| <img align="right" class="rightimage brightness" src="https://workllama.com/wp-content/uploads/2022/05/WL_Logo.svg" alt="Image" width="230" height="230" > | |
| <br>""") | |
| with gr.Row(): | |
| gr.HTML("""<center><h1>Job Seeker Assistant ChatBot</h1></center>""") | |
| chatbot = gr.Chatbot([], elem_id="chatbot").style(height=360) | |
| with gr.Row(): | |
| with gr.Column(scale=0.8): | |
| txt = gr.Textbox( | |
| show_label=False, | |
| placeholder="Job_Seeker").style(container=False) | |
| with gr.Column(scale=0.2): | |
| emptyBtn = gr.Button("🧹 Clear") | |
| with gr.Row(): | |
| with gr.Column(scale=0.80): | |
| txt3 =gr.Textbox( | |
| show_label=False, | |
| placeholder="HR Assistant Suggesstion").style(container=False) | |
| with gr.Column(scale=0.20, min_width=0): | |
| button=gr.Button(value="🚀send") | |
| with gr.Row(): | |
| with gr.Column(scale=0.50): | |
| txt4 =gr.Textbox( | |
| show_label=False, | |
| lines=4, | |
| placeholder="Summary").style(container=False) | |
| with gr.Column(scale=0.50): | |
| txt5 =gr.Textbox( | |
| show_label=False, | |
| lines=4, | |
| placeholder="Sentiment").style(container=False) | |
| with gr.Row(): | |
| with gr.Column(scale=0.50, min_width=0): | |
| end_btn=gr.Button(value="End") | |
| with gr.Column(scale=0.50, min_width=0): | |
| Sentiment_btn=gr.Button(value="📊",callback=self._on_sentiment_btn_click) | |
| with gr.Row(): | |
| gr.HTML("""<center><h1>Sentiment and Emotion Score Graph</h1></center>""") | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=0): | |
| plot =gr.Plot(label="Job_Seeker", size=(500, 600)) | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=0): | |
| plot_3 =gr.Plot(label="Job_Seeker_Emotion", size=(500, 600)) | |
| txt_msg = txt.submit(self._add_text, [chatbot, txt], [chatbot, txt]) | |
| txt_msg.then(lambda: gr.update(interactive=True), None, [txt]) | |
| txt.submit(self._suggested_answer,txt,txt3) | |
| button.click(self._agent_text, [chatbot,txt3], chatbot) | |
| end_btn.click(self._display_history, [], txt4) | |
| emptyBtn.click(self.clear_func,[],[]) | |
| emptyBtn.click(lambda: None, None, chatbot, queue=False) | |
| Sentiment_btn.click(self._on_sentiment_btn_click,[],[txt5,plot,plot_3]) | |
| demo.title = "AI HR ChatBot" | |
| demo.launch() | |
| document_qa =LangChain_Document_QA() | |
| document_qa.gradio_interface() |