Spaces:
Sleeping
Sleeping
naveenvenkatesh
commited on
Commit
•
10baefe
1
Parent(s):
bac98b5
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
|
|
1 |
import os
|
2 |
import mimetypes
|
3 |
import requests
|
4 |
import tempfile
|
5 |
import gradio as gr
|
|
|
6 |
import re
|
7 |
import json
|
8 |
-
from openai import OpenAI
|
9 |
from transformers import pipeline
|
10 |
import matplotlib.pyplot as plt
|
11 |
import plotly.express as px
|
@@ -13,8 +14,6 @@ import plotly.express as px
|
|
13 |
class SentimentAnalyzer:
|
14 |
def __init__(self):
|
15 |
self.model="facebook/bart-large-mnli"
|
16 |
-
self.client = OpenAI()
|
17 |
-
|
18 |
def analyze_sentiment(self, text):
|
19 |
pipe = pipeline("zero-shot-classification", model=self.model)
|
20 |
label=["positive","negative","neutral"]
|
@@ -25,11 +24,14 @@ class SentimentAnalyzer:
|
|
25 |
def emotion_analysis(self,text):
|
26 |
prompt = f""" Your task is to analyze {text} and predict the emotion using scores. Emotions are categorized into the following list: Sadness, Happiness, Joy, Fear, Disgust, and Anger. You need to provide the emotion with 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.
|
27 |
Please analyze the text and provide the output in the following format: emotion: score [with one result having the highest score]."""
|
28 |
-
response =
|
29 |
-
model="
|
30 |
prompt=prompt,
|
31 |
temperature=1,
|
32 |
-
max_tokens=60
|
|
|
|
|
|
|
33 |
)
|
34 |
|
35 |
message = response.choices[0].text.strip().replace("\n","")
|
@@ -61,14 +63,14 @@ Please analyze the text and provide the output in the following format: emotion:
|
|
61 |
|
62 |
class Summarizer:
|
63 |
def __init__(self):
|
64 |
-
|
65 |
|
66 |
def generate_summary(self, text):
|
67 |
-
model_engine = "
|
68 |
prompt = f"""summarize the following conversation delimited by triple backticks.
|
69 |
write within 30 words.
|
70 |
```{text}``` """
|
71 |
-
completions =
|
72 |
engine=model_engine,
|
73 |
prompt=prompt,
|
74 |
max_tokens=60,
|
@@ -86,7 +88,7 @@ sentiment = SentimentAnalyzer()
|
|
86 |
class LangChain_Document_QA:
|
87 |
|
88 |
def __init__(self):
|
89 |
-
|
90 |
|
91 |
def _add_text(self,history, text):
|
92 |
history = history + [(text, None)]
|
@@ -138,7 +140,7 @@ class LangChain_Document_QA:
|
|
138 |
|
139 |
|
140 |
def _suggested_answer(self,text):
|
141 |
-
|
142 |
history = self._chat_history()
|
143 |
start_sequence = "\nCustomer:"
|
144 |
restart_sequence = "\nVodafone Customer Relationship Manager:"
|
@@ -149,18 +151,22 @@ class LangChain_Document_QA:
|
|
149 |
prompt = f"""{history}{start_sequence}{text}{restart_sequence} if customer ask any information take it from {customer_details}.
|
150 |
if customer say thanks or thankyou tone related messages You should not ask anything to end the conversation with greetings tone.
|
151 |
"""
|
152 |
-
response =
|
153 |
-
model="
|
154 |
prompt=prompt,
|
155 |
temperature=0,
|
156 |
-
max_tokens=500
|
|
|
|
|
|
|
157 |
)
|
158 |
|
159 |
message = response.choices[0].text.strip()
|
160 |
if ":" in message:
|
161 |
message = re.sub(r'^.*:', '', message)
|
162 |
return message.strip()
|
163 |
-
|
|
|
164 |
|
165 |
|
166 |
|
@@ -201,14 +207,13 @@ class LangChain_Document_QA:
|
|
201 |
def clear_func(self):
|
202 |
history_state.clear()
|
203 |
def gradio_interface(self):
|
204 |
-
with gr.Blocks(css="style.css") as demo:
|
|
|
|
|
|
|
|
|
205 |
with gr.Row():
|
206 |
-
gr.HTML("""<center
|
207 |
-
<h1 style="color:#fff">
|
208 |
-
Virtual Assistant Chatbot
|
209 |
-
</h1>
|
210 |
-
</center>""")
|
211 |
-
|
212 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=300)
|
213 |
with gr.Row():
|
214 |
with gr.Column(scale=0.50):
|
@@ -285,7 +290,7 @@ class LangChain_Document_QA:
|
|
285 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
286 |
Sentiment_btn.click(self._on_sentiment_btn_click,[],[txt5,plot,plot_2,plot_3,plot_4])
|
287 |
|
|
|
288 |
demo.launch()
|
289 |
-
|
290 |
document_qa =LangChain_Document_QA()
|
291 |
document_qa.gradio_interface()
|
|
|
1 |
+
# from pydantic import NoneStr
|
2 |
import os
|
3 |
import mimetypes
|
4 |
import requests
|
5 |
import tempfile
|
6 |
import gradio as gr
|
7 |
+
import openai
|
8 |
import re
|
9 |
import json
|
|
|
10 |
from transformers import pipeline
|
11 |
import matplotlib.pyplot as plt
|
12 |
import plotly.express as px
|
|
|
14 |
class SentimentAnalyzer:
|
15 |
def __init__(self):
|
16 |
self.model="facebook/bart-large-mnli"
|
|
|
|
|
17 |
def analyze_sentiment(self, text):
|
18 |
pipe = pipeline("zero-shot-classification", model=self.model)
|
19 |
label=["positive","negative","neutral"]
|
|
|
24 |
def emotion_analysis(self,text):
|
25 |
prompt = f""" Your task is to analyze {text} and predict the emotion using scores. Emotions are categorized into the following list: Sadness, Happiness, Joy, Fear, Disgust, and Anger. You need to provide the emotion with 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.
|
26 |
Please analyze the text and provide the output in the following format: emotion: score [with one result having the highest score]."""
|
27 |
+
response = openai.Completion.create(
|
28 |
+
model="text-davinci-003",
|
29 |
prompt=prompt,
|
30 |
temperature=1,
|
31 |
+
max_tokens=60,
|
32 |
+
top_p=1,
|
33 |
+
frequency_penalty=0,
|
34 |
+
presence_penalty=0
|
35 |
)
|
36 |
|
37 |
message = response.choices[0].text.strip().replace("\n","")
|
|
|
63 |
|
64 |
class Summarizer:
|
65 |
def __init__(self):
|
66 |
+
pass
|
67 |
|
68 |
def generate_summary(self, text):
|
69 |
+
model_engine = "text-davinci-003"
|
70 |
prompt = f"""summarize the following conversation delimited by triple backticks.
|
71 |
write within 30 words.
|
72 |
```{text}``` """
|
73 |
+
completions = openai.Completion.create(
|
74 |
engine=model_engine,
|
75 |
prompt=prompt,
|
76 |
max_tokens=60,
|
|
|
88 |
class LangChain_Document_QA:
|
89 |
|
90 |
def __init__(self):
|
91 |
+
pass
|
92 |
|
93 |
def _add_text(self,history, text):
|
94 |
history = history + [(text, None)]
|
|
|
140 |
|
141 |
|
142 |
def _suggested_answer(self,text):
|
143 |
+
try:
|
144 |
history = self._chat_history()
|
145 |
start_sequence = "\nCustomer:"
|
146 |
restart_sequence = "\nVodafone Customer Relationship Manager:"
|
|
|
151 |
prompt = f"""{history}{start_sequence}{text}{restart_sequence} if customer ask any information take it from {customer_details}.
|
152 |
if customer say thanks or thankyou tone related messages You should not ask anything to end the conversation with greetings tone.
|
153 |
"""
|
154 |
+
response = openai.Completion.create(
|
155 |
+
model="text-davinci-003",
|
156 |
prompt=prompt,
|
157 |
temperature=0,
|
158 |
+
max_tokens=500,
|
159 |
+
top_p=1,
|
160 |
+
frequency_penalty=0,
|
161 |
+
presence_penalty=0.6,
|
162 |
)
|
163 |
|
164 |
message = response.choices[0].text.strip()
|
165 |
if ":" in message:
|
166 |
message = re.sub(r'^.*:', '', message)
|
167 |
return message.strip()
|
168 |
+
except:
|
169 |
+
return "I can't get the response"
|
170 |
|
171 |
|
172 |
|
|
|
207 |
def clear_func(self):
|
208 |
history_state.clear()
|
209 |
def gradio_interface(self):
|
210 |
+
with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo:
|
211 |
+
with gr.Row():
|
212 |
+
gr.HTML("""<img class="leftimage" align="left" src="https://templates.images.credential.net/1612472097627370951721412474196.png" alt="Image" width="210" height="210">
|
213 |
+
<img align="right" class="rightimage" src="https://download.logo.wine/logo/Vodafone/Vodafone-Logo.wine.png" alt="Image" width="230" height="230" >""")
|
214 |
+
|
215 |
with gr.Row():
|
216 |
+
gr.HTML("""<center><h1>Vodafone Generative AI CRM ChatBot</h1></center>""")
|
|
|
|
|
|
|
|
|
|
|
217 |
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=300)
|
218 |
with gr.Row():
|
219 |
with gr.Column(scale=0.50):
|
|
|
290 |
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
291 |
Sentiment_btn.click(self._on_sentiment_btn_click,[],[txt5,plot,plot_2,plot_3,plot_4])
|
292 |
|
293 |
+
demo.title = "Vodafone Generative AI CRM ChatBot"
|
294 |
demo.launch()
|
|
|
295 |
document_qa =LangChain_Document_QA()
|
296 |
document_qa.gradio_interface()
|