-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
143 lines (116 loc) · 4.92 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask, request
app = Flask(__name__)
from ml_utils import compare_user_match
from audio_utils import get_audio_transcription
from sentiment_utils import get_avg_list_sentiment, TOO_NEG_THRESHOLD
from username_utils import random_name
import datetime
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate("exun-finals-c3c44-63ff55bd007b.json")
firebase_app = firebase_admin.initialize_app(credential=cred)
db = firestore.client()
rooms = db.collection(u"rooms")
users = db.collection(u"users")
import time
@app.route('/username-generator', methods=['GET'])
def unique_username_generator():
name = random_name()
return name
@app.route('/transcribe-audio', methods=['POST'])
def transcribe_audio():
'''Downloads the audio file at the given url and returns the string transcribed by openAI whisper'''
print('Transcribing audio')
audio_url = request.form['audio_url']
room_id = request.form['room_id']
message_id = request.form['message_id']
transcribed = get_audio_transcription(audio_url)
room = rooms.document(room_id)
messages_collection = list(room.collections())[0]
message = messages_collection.document(message_id)
message.update({u'contents': transcribed})
message.update({u'isAudio': True})
return transcribed
def get_user_active_chats(user_data, user_id) -> int:
'''Returns the number of active chats the user is in'''
user = users.document(user_id)
user_data = user.get().to_dict()
user_rooms = rooms.where(u'members', u'array_contains', user_id).get()
return len(user_rooms)
@app.route('/match-user', methods=['POST'])
def match_user():
'''Finds the closest match for a user and opens a chatroom with them.'''
"""
1. Get the user's data from the database
2. Find all users who have less than 3 active chats OR are premium users
3. Rank them based on their match score
4. Open a chatroom model in the DB
"""
user_id = request.form['user_id']
print('Got user ID', user_id)
target_user = users.document(user_id)
if not target_user:
return 'User not found'
target_user_data = target_user.get().to_dict()
# Get all users who have less than 3 active chats OR are premium users
available_users = []
for other_user in users.stream():
other_user_data = other_user.to_dict()
if other_user_data['isPremium'] or get_user_active_chats(other_user_data, other_user.id) < 3:
# Check if the target user already has a chat with the other user
rooms_with_target_user = rooms.where(u'members', u'array_contains', user_id).get()
already_has_room = False
for room in rooms_with_target_user:
if other_user.id in room.to_dict()['members']:
already_has_room = True
break
if not already_has_room:
available_users.append(other_user)
user_scores = [compare_user_match(available_user.to_dict(), target_user_data) for available_user in available_users]
sorted_matches = sorted(zip(user_scores, available_users), key=lambda x: x[0], reverse=True)
sorted_match_ids = [match[1].id for match in sorted_matches]
selected_users = []
if len (user_scores) == 0:
return 'No users available'
elif len(user_scores) < 3:
selected_users = sorted_matches
else:
selected_users = sorted_matches[:3]
rooms.add({
u'members': [target_user.id, selected_users[0][1].id],
u'timeOpened': datetime.datetime.now(),
u'lastMessage': 'Chat was opened'
})
return 'Success'
@app.route('/check-chatroom-sentiment', methods=['GET'])
def check_chatroom_sentiment():
'''Checks the sentiment of the chatroom and sends notification about sentiment if it is too negative'''
chatroom_id = request.args['chatroom_id']
try:
room = rooms.document(chatroom_id)
except:
return "Error"
messages = list(room.collections())[0]
message_lists = {}
for message in messages.stream():
data = message.to_dict()
contents = data['contents']
sender_id = data['sender']
if sender_id not in message_lists.keys():
message_lists[sender_id] = [contents]
else:
message_lists[sender_id].append(contents)
flagged_users = []
for sender_id in message_lists.keys():
messages = message_lists[sender_id]
average_sender_sentiment = get_avg_list_sentiment(messages)
print(f'Got sentiment {average_sender_sentiment} for {sender_id}')
# if average_sender_sentiment <= TOO_NEG_THRESHOLD and len(messages) >= 3:
if average_sender_sentiment <= TOO_NEG_THRESHOLD:
flagged_users.append(sender_id)
room.update({"flaggedUsers": flagged_users})
return flagged_users
if __name__ == '__main__':
app.debug = True
app.run(port=4996)