Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sentiment working in branch #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def setup_args(parser=None):
help='Format to save logs in. conversations is a jsonl format, parlai is a text format.',
)
parser.set_defaults(interactive_mode=True, task='interactive')
LocalHumanAgent.add_cmdline_args(parser, partial_opt=None)
WorldLogger.add_cmdline_args(parser, partial_opt=None)
LocalHumanAgent.add_cmdline_args(parser)
WorldLogger.add_cmdline_args(parser)
return parser


Expand Down
50 changes: 38 additions & 12 deletions interactive_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@
import re
import nltk
import spacy

from transformers import pipeline, GPT2Tokenizer

# playsound("out.mp3", True)
import pyttsx3
engine = pyttsx3.init()
# import pyttsx3
# engine = pyttsx3.init()
# engine.setProperty('rate',125)
# voices = engine.getProperty('voices')
# engine.setProperty('voice', voices[1].id)

import json
import time
classifier = pipeline('sentiment-analysis')

def decontracted(phrase):
# specific
Expand All @@ -59,6 +60,7 @@ def decontracted(phrase):
phrase = re.sub(r"\' m", " am", phrase)
return phrase


HOST_NAME = 'localhost'
PORT = 8080
username = 'Sahil'
Expand Down Expand Up @@ -116,7 +118,7 @@ def decontracted(phrase):
</div>

<script>
function createChatRow(agent, text) {{
function createChatRow(agent, text, senti = 1) {{
var article = document.createElement("article");
article.className = "media"

Expand All @@ -126,9 +128,12 @@ def decontracted(phrase):
var span = document.createElement("span");
span.className = "icon is-large";


var icon = document.createElement("i");
icon.className = "fas fas fa-2x" + (agent === "You" ? " fa-user " : agent === "Taiga" ? " fa-robot" : "");

icon.className = "fas fas fa-2x" + (agent === "You" ? " fa-user " : agent === "Taiga" ? ( senti == 0 ? " fa-frown" : (senti == 1 ? " fa-meh" : " fa-smile" ) ) : "");
// icon.className = "fas fas fa-2x" + (agent === "You" ? " fa-user " : agent === "Taiga" ? " fa-robot" : "");

var media = document.createElement("div");
media.className = "media-content";

Expand Down Expand Up @@ -176,7 +181,8 @@ def decontracted(phrase):
parDiv.append(createChatRow("You", text));

// Change info for Model response
parDiv.append(createChatRow("Taiga", data.text));
parDiv.append(createChatRow("Taiga", data.text,data.sentiment));
// parDiv.append(createChatRow("Taiga", data.text));
parDiv.scrollTo(0, parDiv.scrollHeight);
}})
}});
Expand All @@ -194,7 +200,8 @@ def decontracted(phrase):
var parDiv = document.getElementById("parent");
parDiv.append(createChatRow("You", data.inputtext));
// Change info for Model response
parDiv.append(createChatRow("Taiga", data.text));
parDiv.append(createChatRow("Taiga", data.text,data.sentiment));
// parDiv.append(createChatRow("Taiga", data.text));
parDiv.scrollTo(0, parDiv.scrollHeight);
}})
}});
Expand Down Expand Up @@ -235,14 +242,23 @@ def _interactive_running(self, opt, reply_text):
rand = random.randint(0,2)
orig_text = model_res['text']
orig_text = decontracted(orig_text)
sent_result = classifier([orig_text])[0]
print ("ola la la la le oo ", sent_result)
if (sent_result['score']<0.6):
sentFinal = 1
elif sent_result['label'] == 'NEGATIVE':
sentFinal = 0
else :
sentFinal = 2
model_res.force_set('text',orig_text)
if rand == 1:
model_res.force_set('text',username + ' ' + orig_text)
if rand == 2:
model_res.force_set('text',orig_text + ' ' + username)
if "my name is" in model_res['text']:
model_res.force_set('text','My name is Taiga, the friend who loves talking to you.')
return model_res
return model_res,sentFinal
#return model_res

def _generate_family_tree(self, sentence):
tagger = spacy.load('en_core_web_sm')
Expand Down Expand Up @@ -286,13 +302,21 @@ def do_POST(self):
print(body)
print(body.decode('utf-8'))

model_response = self._interactive_running(
SHARED.get('opt'), body
model_response, sentF = self._interactive_running(
SHARED.get('opt'), body.decode("utf-8")
)
print(model_response['text'])
model_response['sentiment'] = sentF

assistant = gTTS(text=model_response['text'], lang='en', slow=False)
assistant.save("out.mp3")
Flag77 = True
while Flag77:
try:
print ("trying again")
assistant.save("out.mp3")
Flag77 = False
except:
Flag77 = True
playsound('out.mp3',True)

self.send_response(200)
Expand Down Expand Up @@ -333,16 +357,18 @@ def do_POST(self):
except Exception as ex:
print(ex)
body = text
model_response = self._interactive_running(
model_response,sentF = self._interactive_running(
SHARED.get('opt'), body
)
model_response['sentiment'] = sentF
assistant = gTTS(text=model_response['text'], lang='en', slow=False)
assistant.save("out.mp3")
playsound('out.mp3',True)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
model_response['inputtext'] = text
#model_response['sentiment'] = sentF
json_str = json.dumps(model_response)
self.wfile.write(bytes(json_str, 'utf-8'))

Expand Down
Loading