datafreak commited on
Commit
2964773
·
verified ·
1 Parent(s): 4df4e60

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +233 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ from datetime import datetime
5
+ from dotenv import load_dotenv
6
+ from supabase import create_client, Client
7
+ from pinecone import Pinecone
8
+ from sentence_transformers import SentenceTransformer
9
+ from typing import List, Dict
10
+
11
+ load_dotenv()
12
+ SUPABASE_URL = os.getenv("DB_URL")
13
+ SUPABASE_KEY = os.getenv("DB_KEY")
14
+ supabase_client = create_client(SUPABASE_URL, SUPABASE_KEY)
15
+
16
+ pinecone_api_key = os.getenv("PINECONE")
17
+ pc = Pinecone(api_key=pinecone_api_key)
18
+ index = pc.Index("focus-guru")
19
+ model = SentenceTransformer('all-MiniLM-L6-v2')
20
+
21
+ def ingest_user_progress(supabase_client: Client, user_id: int, video_id: str, rating: float, time_spent: int, play_count: int, completed: bool):
22
+ data = {
23
+ 'user_id': user_id,
24
+ 'video_id': video_id,
25
+ 'rating': rating,
26
+ 'time_spent': time_spent,
27
+ 'play_count': play_count,
28
+ 'completed': completed,
29
+ 'updated_at': datetime.now().isoformat()
30
+ }
31
+ response = supabase_client.table('user_progress').insert(data, upsert=True).execute()
32
+ return response.data
33
+
34
+ def gradio_ingest(user_input):
35
+ try:
36
+ data = json.loads(user_input)
37
+ user_id = int(data.get("user_id", 0))
38
+ video_id = data.get("video_id", "")
39
+ rating = float(data.get("rating", 0))
40
+ time_spent = int(data.get("time_spent", 0))
41
+ play_count = int(data.get("play_count", 0))
42
+ completed = bool(data.get("completed", False))
43
+ except Exception as e:
44
+ return f"<div style='color: red;'>Error parsing input: {e}</div>"
45
+ res = ingest_user_progress(supabase_client, user_id, video_id, rating, time_spent, play_count, completed)
46
+ return f"<div style='color: green;'>Ingested data: {res}</div>"
47
+
48
+ def recommend_playlists_by_package_and_module(assessment_output, index, model):
49
+ report_text = assessment_output.get("report", "")
50
+ packages = assessment_output.get("package", [])
51
+ modules = ["Nutrition", "Exercise", "Meditation"]
52
+ recommendations = {}
53
+ if not report_text:
54
+ for pkg in packages:
55
+ recommendations[pkg] = {mod: {"title": "No playlist found", "description": ""} for mod in modules}
56
+ return recommendations
57
+ query_embedding = model.encode(report_text, convert_to_numpy=True).tolist()
58
+ for pkg in packages:
59
+ recommendations[pkg] = {}
60
+ for mod in modules:
61
+ filter_dict = {"type": "playlist", "Package": pkg, "Module": mod}
62
+ results = index.query(vector=query_embedding, top_k=1, include_metadata=True, filter=filter_dict)
63
+ if results["matches"]:
64
+ match = results["matches"][0]
65
+ metadata = match["metadata"]
66
+ title = metadata.get("Playlist Name", "Unknown Playlist")
67
+ description = metadata.get("Description", "")
68
+ recommendations[pkg][mod] = {"title": title, "description": description}
69
+ else:
70
+ recommendations[pkg][mod] = {"title": "No playlist found", "description": ""}
71
+ return recommendations
72
+
73
+ def gradio_recommend_playlist(input_json):
74
+ try:
75
+ assessment_data = json.loads(input_json)
76
+ except json.JSONDecodeError:
77
+ return "<div style='color: red;'>Error: Invalid JSON format</div>"
78
+ if "package" not in assessment_data or "report" not in assessment_data:
79
+ return "<div style='color: red;'>Error: Missing 'package' or 'report' field</div>"
80
+ recs = recommend_playlists_by_package_and_module(assessment_data, index, model)
81
+ html_output = """
82
+ <div style="
83
+ display: flex;
84
+ flex-direction: column;
85
+ gap: 30px;
86
+ padding: 20px;
87
+ font-family: Arial, sans-serif;
88
+ ">
89
+ """
90
+ for pkg, mod_recs in recs.items():
91
+ html_output += f"<h2 style='color: #2d3436;'>{pkg} Package</h2>"
92
+ html_output += "<div style='display: flex; flex-wrap: wrap; gap: 20px;'>"
93
+ for mod, rec in mod_recs.items():
94
+ card_html = f"""
95
+ <div style="
96
+ border: 1px solid #e0e0e0;
97
+ border-radius: 10px;
98
+ padding: 20px;
99
+ background: white;
100
+ width: 300px;
101
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
102
+ ">
103
+ <h3 style="margin: 0 0 12px 0; color: #0984e3;">{mod} Module</h3>
104
+ <h4 style="margin: 0 0 8px 0; color: #2d3436;">{rec['title']}</h4>
105
+ <p style="margin: 0; color: #636e72; line-height: 1.5;">{rec['description']}</p>
106
+ </div>
107
+ """
108
+ html_output += card_html
109
+ html_output += "</div>"
110
+ html_output += "</div>"
111
+ return html_output
112
+
113
+ def recommend_videos(user_id: int, K: int = 5, M: int = 10, N: int = 5) -> List[Dict]:
114
+ response = supabase_client.table('user_progress').select('video_id, rating, completed, play_count, videos!inner(playlist_id)').eq('user_id', user_id).execute()
115
+ interactions = response.data
116
+ if not interactions:
117
+ return []
118
+ for inter in interactions:
119
+ rating = inter['rating'] if inter['rating'] is not None else 0
120
+ completed_val = 1 if inter['completed'] else 0
121
+ play_count = inter['play_count']
122
+ engagement = rating + 2 * completed_val + play_count
123
+ inter['engagement'] = engagement
124
+ top_videos = sorted(interactions, key=lambda x: x['engagement'], reverse=True)[:K]
125
+ watched_completed_videos = {i['video_id'] for i in interactions if i['completed']}
126
+ candidates = {}
127
+ for top_video in top_videos:
128
+ query_id = f"video_{top_video['video_id']}"
129
+ response = index.query(id=query_id, top_k=M + 1, include_metadata=True)
130
+ for match in response.get('matches', []):
131
+ if match['id'] == query_id:
132
+ continue
133
+ metadata = match.get('metadata', {})
134
+ vid = metadata.get('vid')
135
+ if not vid:
136
+ continue
137
+ if vid in watched_completed_videos:
138
+ continue
139
+ similarity = match['score']
140
+ pid = metadata.get('PID')
141
+ boost = 1.1 if pid == top_video['videos']['playlist_id'] else 1.0
142
+ partial_score = top_video['engagement'] * similarity * boost
143
+ if vid in candidates:
144
+ candidates[vid]['total_score'] += partial_score
145
+ else:
146
+ candidates[vid] = {'total_score': partial_score, 'metadata': metadata}
147
+ sorted_candidates = sorted(candidates.items(), key=lambda x: x[1]['total_score'], reverse=True)[:N]
148
+ recommendations = []
149
+ for vid, data in sorted_candidates:
150
+ metadata = data['metadata']
151
+ recommendations.append({
152
+ 'video_id': vid,
153
+ 'title': metadata.get('video_title'),
154
+ 'description': metadata.get('video_description'),
155
+ 'score': data['total_score']
156
+ })
157
+ return recommendations
158
+
159
+ def gradio_recommend_videos(user_id_input):
160
+ try:
161
+ user_id = int(user_id_input)
162
+ except Exception as e:
163
+ return f"<div style='color: red;'>Error: {e}</div>"
164
+ recs = recommend_videos(user_id)
165
+ if not recs:
166
+ return "<div style='color: #636e72;'>No video recommendations found for this user.</div>"
167
+ html_output = """
168
+ <div style="
169
+ display: flex;
170
+ flex-direction: column;
171
+ gap: 30px;
172
+ padding: 20px;
173
+ font-family: Arial, sans-serif;
174
+ ">
175
+ """
176
+ html_output += "<h2 style='color: #2d3436;'>Recommended Videos</h2>"
177
+ html_output += "<div style='display: flex; flex-wrap: wrap; gap: 20px;'>"
178
+ for rec in recs:
179
+ card_html = f"""
180
+ <div style="
181
+ border: 1px solid #e0e0e0;
182
+ border-radius: 10px;
183
+ padding: 20px;
184
+ background: white;
185
+ width: 300px;
186
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
187
+ ">
188
+ <h3 style="margin: 0 0 12px 0; color: #0984e3;">{rec['title']}</h3>
189
+ <p style="margin: 0 0 8px 0; color: #636e72;">{rec['description']}</p>
190
+ <p style="margin: 0; color: #2d3436;">Score: {rec['score']:.2f}</p>
191
+ </div>
192
+ """
193
+ html_output += card_html
194
+ html_output += "</div></div>"
195
+ return html_output
196
+
197
+ with gr.Blocks() as demo:
198
+ with gr.Tabs():
199
+ with gr.TabItem("Playlist Recommendation"):
200
+ playlist_input = gr.Textbox(
201
+ lines=10,
202
+ label="Assessment Data (JSON)",
203
+ placeholder='''{
204
+ "package": ["Focus", "Insomnia"],
205
+ "report": "Based on your responses, you may struggle with focus, anxiety, and burnout. The Focus and Insomnia packages can help improve your mental clarity and sleep quality."
206
+ }'''
207
+ )
208
+ playlist_output = gr.HTML(label="Recommended Playlists")
209
+ playlist_btn = gr.Button("Get Playlist Recommendations")
210
+ playlist_btn.click(gradio_recommend_playlist, playlist_input, playlist_output)
211
+ with gr.TabItem("Video Recommendation"):
212
+ user_id_input = gr.Textbox(lines=1, label="User ID", placeholder="1")
213
+ videos_output = gr.HTML(label="Recommended Videos")
214
+ videos_btn = gr.Button("Get Video Recommendations")
215
+ videos_btn.click(gradio_recommend_videos, user_id_input, videos_output)
216
+ with gr.TabItem("User Interaction Ingestion"):
217
+ ingest_input = gr.Textbox(
218
+ lines=10,
219
+ label="User Progress Data (JSON)",
220
+ placeholder='''{
221
+ "user_id": 1,
222
+ "video_id": "abc123",
223
+ "rating": 4.5,
224
+ "time_spent": 300,
225
+ "play_count": 1,
226
+ "completed": false
227
+ }'''
228
+ )
229
+ ingest_output = gr.HTML(label="Ingestion Result")
230
+ ingest_btn = gr.Button("Ingest Data")
231
+ ingest_btn.click(gradio_ingest, ingest_input, ingest_output)
232
+
233
+ demo.launch()
requirements.txt ADDED
Binary file (3.35 kB). View file