-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
216 lines (171 loc) · 6.76 KB
/
app.py
File metadata and controls
216 lines (171 loc) · 6.76 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify
from pymongo import MongoClient
from config import DB_URL
import bcrypt
import os
from openai import OpenAI
from dotenv import load_dotenv
from bson import ObjectId
from datetime import datetime
import traceback
import datetime
import json
import re
app = Flask(__name__)
app.secret_key = "your_secret_key_here"
load_dotenv()
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
client = MongoClient(DB_URL)
db = client['SentiScan']
# Explicitly create collections if they don't exist
if 'user' not in db.list_collection_names():
db.create_collection('user')
if 'analysis' not in db.list_collection_names():
db.create_collection('analysis')
# Reference the collections
user_collection = db['user']
analysis_collection = db['analysis']
@app.route('/')
def index():
return redirect(url_for('login'))
@app.route('/register', methods=['POST', 'GET'])
def register():
if request.method == "POST":
print("its working")
name = request.form.get('name')
phone = request.form.get('phone')
email = request.form.get('email')
password = request.form.get('password')
# Check if user already exists
if user_collection.find_one({"email": email}):
flash("User with this email already exists!", "danger")
return redirect(url_for('register'))
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
user_collection.insert_one({
"name": name,
"phone": phone,
"email": email,
"password": hashed,
"created_at": datetime.datetime.utcnow()
})
flash("Account Created Successfully!", "success")
return redirect(url_for('login'))
return render_template("register.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email') # either an email or phone_number
password = request.form.get('password')
if '@' in email:
user = user_collection.find_one({"email": email})
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
session['user_id'] = str(user['_id'])
session['username'] = user['name']
flash("Logged in successfully!", "success")
return redirect(url_for('home'))
else:
flash("Invalid email or password", "danger")
return redirect(url_for('login'))
else:
user = user_collection.find_one({"phone": email})
if user and bcrypt.checkpw(password.encode('utf-8'), user['password']):
session['user_id'] = str(user['_id'])
session['username'] = user['name']
flash("Logged in successfully!", "success")
return redirect(url_for('home'))
else:
flash("Invalid email or password", "danger")
return redirect(url_for('login'))
return render_template("login.html")
@app.route('/logout')
def logout():
session.clear()
flash("You have been logged out", "info")
return redirect(url_for('login'))
@app.route('/home')
def home():
if 'user_id' in session:
user = user_collection.find_one({'_id': ObjectId(session['user_id'])})
if user:
return render_template("home.html", user={
"name": user.get("name"),
"email": user.get("email")
})
return redirect(url_for('login'))
@app.route('/admin')
def admin():
total_users = user_collection.count_documents({})
pipeline = [
{"$group": {"_id": "$predicted_emotion", "count": {"$sum": 1}}}
]
emotions_cursor = analysis_collection.aggregate(pipeline)
emotions = {doc['_id']: doc['count'] for doc in emotions_cursor}
usage_summary = {
"avgSessions": 3.4,
"avgDuration": "5m 12s"
}
return render_template('admin.html', total_users=total_users, emotions=emotions, usage_summary=usage_summary)
@app.route('/analyze', methods=['POST'])
def analyze_text():
try:
if 'user_id' not in session:
return jsonify({'error': 'Unauthorized'}), 401
data = request.get_json()
user_input = data.get('text')
if not user_input:
return jsonify({'error': 'No text input provided'}), 400
user = user_collection.find_one({'_id': ObjectId(session['user_id'])})
if not user:
return jsonify({'error': 'User not found'}), 404
user_name = user.get("name", "anonymous")
prompt = f"""
Analyze the following text and return ONLY a JSON object with the following:
{{
"content": "...",
"sentiment_score": float (between -1 and 1),
"key_words": ["..."],
"sentiment_label": "Positive" | "Negative" | "Neutral",
"confidence_score": float (between 0 and 1),
"emotion_analysis": "Joy" | "Sadness" | "Anger" | etc.
}}
Text: "{user_input}"
"""
# 🔧 Use the OpenAI client properly
chat_response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a sentiment analysis expert."},
{"role": "user", "content": prompt}
],
temperature=0.3
)
raw_response = chat_response.choices[0].message.content
print("🔵 OpenAI Response:\n", raw_response)
# ✅ Extract only the JSON block
json_match = re.search(r'{.*}', raw_response, re.DOTALL)
if not json_match:
return jsonify({"error": "Invalid OpenAI response format"}), 500
result = json.loads(json_match.group())
# ✅ Build MongoDB record with user name as key
record = {
user_name: {
"text": result.get("content", user_input),
"sentiment_score": result.get("sentiment_score"),
"key_words": result.get("key_words"),
"sentiment_label": result.get("sentiment_label"),
"confidence_score": result.get("confidence_score"),
"emotion_analysis": result.get("emotion_analysis"),
"date": datetime.utcnow()
}
}
analysis_collection.insert_one(record)
return jsonify({
"predicted_emotion": result.get("emotion_analysis"),
"predicted_polarity": result.get("sentiment_label")
})
except Exception as e:
print("❌ Error in /analyze route:")
traceback.print_exc()
return jsonify({"error": "Something went wrong on the server."}), 500
if __name__ == '__main__':
app.run(debug=True)