Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
ML backend proper error processing. Replies to GUI added.
Browse files Browse the repository at this point in the history
  • Loading branch information
makseq committed Jul 23, 2019
1 parent 3c952e0 commit 59badc2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def smart_filter(tweets, smart_project_id, threshold_score):
"""
# apply smart filter
request_data = [{'text': t['text']} for t in tweets] # leave only text field to speed up data transfer
predictions = heartex.api.run_predict(token=token, project=smart_project_id, data=request_data).json()['results']
predictions = heartex.api.run_predict(token=token, project=smart_project_id, data=request_data).json()
if 'results' in predictions:
predictions = predictions['results']
else:
raise Exception('ML backend returns incorrect result: ' + str(predictions))

# take only relevant tweets
new_tweets = []
Expand All @@ -62,7 +66,8 @@ def smart_filter(tweets, smart_project_id, threshold_score):
new_tweets.append(tweets[i])

log.info('Smart filter by project id ' + str(smart_project_id) + ' processed '
'with input len = ' + str(len(tweets)) + ' and output len = ' + str(len(new_tweets)))
'with input len = ' + str(
len(tweets)) + ' and output len = ' + str(len(new_tweets)))
return new_tweets


Expand Down Expand Up @@ -137,7 +142,12 @@ def heartex_build_plot(data, threshold_score=0.5, period='1D'):
request_data.append({'text': reply})

# heartex predict
predictions = heartex.api.run_predict(token=token, project=sentiment_project_id, data=request_data).json()['results']
predictions = heartex.api.run_predict(token=token, project=sentiment_project_id, data=request_data).json()
if 'results' in predictions:
predictions = predictions['results']
else:
raise Exception('ML backend returns incorrect result: ' + str(predictions))

if not isinstance(predictions, list):
log.warning('No predictions by ML backend returned: ' + str(predictions))
raise Exception('No predictions by ML backend returned')
Expand All @@ -148,6 +158,7 @@ def heartex_build_plot(data, threshold_score=0.5, period='1D'):
tweet['predictions'] = []
for _ in tweet['replies']:
tweet['predictions'].append(predictions[count])
tweet['replies_visible'] = False
count += 1

# collect score values (positives & negatives)
Expand Down Expand Up @@ -231,7 +242,7 @@ def api_build_sentiment():

output = heartex_build_plot(tweets)
log.info('Heartex prediction completed')

return answer(200, 'ok', output)


Expand Down
36 changes: 34 additions & 2 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
color: #ccc !important;
background: #28206c !important;
margin-right: 0.2em;
cursor: pointer;
}

.news-replies {
display: block;
font-size: 70%;
}

.news-replies ul {
list-style-type: none;
}

.legend {
Expand Down Expand Up @@ -244,12 +254,24 @@
<div class="ui tiny label news-time">
{{ (new Date(item['created_at']*1000)).format('yyyy.mm.dd HH:MM') }}
</div>
<div class="ui tiny label news-sentiment">
<div class="ui tiny label news-sentiment" @click="show_replies(item)">
{{ item['positives'] }} <i class="ui icon thumbs up outline"></i>
{{ item['negatives'] }} <i class="ui icon thumbs down outline"
style="margin-right:0 !important;"></i>
</div>
{{ item['text'] }}

<!-- Replies -->
<div class="news-replies" v-if="item['replies_visible']">
<ul>
<li v-for="(p, i) in item['predictions']">
<i v-if="item.predictions[i].result[0].value.choices[0] === 'Positive'" class="ui icon thumbs up outline"></i>
<i v-if="item.predictions[i].result[0].value.choices[0] === 'Negative'" class="ui icon thumbs down outline"></i>
<i v-if="item.predictions[i].result[0].value.choices[0] === 'Neutral'" class="ui icon"></i>
{{ item['replies'][i] }}
</li>
</ul>
</div>
</div>
</ul>
</div>
Expand Down Expand Up @@ -319,7 +341,7 @@
chart_instance: null,
news: [],
timer: null,
query: "",
query: "Bitcoin",
start_date: "",
current_request_id: 0,
smart_filters: []
Expand All @@ -331,6 +353,16 @@
Object.assign(this.$data, this.$options.data.apply(this));
},

show_replies(item) {
if (item.hasOwnProperty('replies_visible')) {
item['replies_visible'] = !item['replies_visible'];
}
else {
item['replies_visible'] = true;
}
console.log('show')
},

use_ml_query(model_name) {
this.query = 'model:"' + model_name + '"'
},
Expand Down

0 comments on commit 59badc2

Please sign in to comment.