ADKU commited on
Commit
afcbab5
Β·
verified Β·
1 Parent(s): 06dd718

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -166
app.py CHANGED
@@ -1,167 +1,168 @@
1
- import gradio as gr
2
- import google.generativeai as genai
3
- import faiss
4
- import numpy as np
5
- from sentence_transformers import SentenceTransformer
6
- from pymongo import MongoClient
7
-
8
- # βœ… Configure Gemini API
9
- genai.configure(api_key="AIzaSyBWMLGBoKDeA7_Z_AzHDWtFBKOJ91BJnaY")
10
-
11
- # βœ… Load Sentence Transformer Model
12
- model = SentenceTransformer('all-MiniLM-L6-v2')
13
-
14
- # βœ… Connect to MongoDB
15
- MONGO_URI = "mongodb+srv://aiworkspaceadku:FstomozFvaR6maVs@cluster0.5dtl1.mongodb.net/AiWork"
16
- client = MongoClient(MONGO_URI)
17
- db = client["AiWork"]
18
-
19
- # βœ… Fetch latest data dynamically
20
- def fetch_latest_data():
21
- return {
22
- "users": list(db.users.find()),
23
- "teams": list(db.teams.find()),
24
- "projects": list(db.projects.find()),
25
- "modules": list(db.modules.find()),
26
- "documents": list(db.documents.find()),
27
- "schedules": list(db.schedules.find())
28
- }
29
-
30
- def generate_sentences(db):
31
- users, teams, projects, modules, documents, schedules = (
32
- db["users"], db["teams"], db["projects"], db["modules"], db["documents"], db["schedules"]
33
- )
34
- user_sentences = {} # Store categorized sentences per user
35
-
36
- for user in users:
37
- username = user.get("username", "Unknown User")
38
- email = user.get("email", "Unknown Email")
39
-
40
- if email not in user_sentences:
41
- user_sentences[email] = {
42
- "Teams": [],
43
- "Projects": [],
44
- "Modules & Tasks": [],
45
- "Documents": [],
46
- "Schedules": []
47
- }
48
-
49
- # User team ownership and membership
50
- owned_teams = [team for team in teams if team.get("owner", {}).get("email") == email]
51
- if owned_teams:
52
- team_names = ", ".join(f'"{team["teamName"]}"' for team in owned_teams)
53
- user_sentences[email]["Teams"].append(f"User {username} owns the teams: {team_names}.")
54
-
55
- member_teams = [team for team in teams if any(m["email"] == email for m in team.get("members", []))]
56
- if member_teams:
57
- team_names = ", ".join(f'"{team["teamName"]}"' for team in member_teams)
58
- user_sentences[email]["Teams"].append(f"User {username} is a member of the teams: {team_names}.")
59
-
60
- # Find projects in teams they own or are part of
61
- relevant_teams = owned_teams + member_teams
62
- team_ids = [str(team["_id"]) for team in relevant_teams]
63
- user_projects = [p for p in projects if str(p.get("owner", {}).get("teamId")) in team_ids]
64
-
65
- if user_projects:
66
- for project in user_projects:
67
- proj_name = project["projName"]
68
- user_sentences[email]["Projects"].append(f"User {username} is involved in project {proj_name}.")
69
-
70
- # Find modules under this project
71
- proj_modules = [m for m in modules if str(m.get("projId")) == str(project["_id"])]
72
- if proj_modules:
73
- for module in proj_modules:
74
- module_name = module["moduleName"]
75
- user_sentences[email]["Modules & Tasks"].append(f"In project {proj_name}, module {module_name} exists.")
76
-
77
- # Find tasks in this module assigned to the user
78
- assigned_tasks = [
79
- task for task in module.get("tasks", [])
80
- if any(a["email"] == email for a in task.get("assignedTo", []))
81
- ]
82
- if assigned_tasks:
83
- task_names = ", ".join(f'"{t["taskName"]}"' for t in assigned_tasks)
84
- user_sentences[email]["Modules & Tasks"].append(f"Tasks assigned to {username} in {module_name}: {task_names}.")
85
-
86
- # Find documents in this project
87
- proj_docs = [d for d in documents if str(d.get("owner", {}).get("projId")) == str(project["_id"])]
88
- if proj_docs:
89
- doc_names = ", ".join(f'"{d["title"]}"' for d in proj_docs)
90
- user_sentences[email]["Documents"].append(f"Documents related to project {proj_name}: {doc_names}.")
91
-
92
- # Find meeting schedules related to their teams
93
- user_schedules = [s for s in schedules if str(s.get("teamId")) in team_ids]
94
- if user_schedules:
95
- for schedule in user_schedules:
96
- schedule_detail = f'{schedule["moto"]} scheduled on {schedule["date"]} at {schedule["time"]}.'
97
- user_sentences[email]["Schedules"].append(schedule_detail)
98
-
99
- return user_sentences
100
-
101
- # βœ… Update FAISS Index dynamically
102
- def update_faiss_index(user_sentences):
103
- faiss_indices = {}
104
- for email, categories in user_sentences.items():
105
- sentences = sum(categories.values(), [])
106
-
107
- if not sentences:
108
- continue
109
-
110
- embeddings = model.encode(sentences, convert_to_numpy=True)
111
- embedding_dim = embeddings.shape[1]
112
-
113
- index = faiss.IndexFlatL2(embedding_dim)
114
- index.add(embeddings)
115
-
116
- faiss_indices[email] = {"index": index, "sentences": sentences}
117
-
118
- return faiss_indices
119
-
120
- # βœ… Query FAISS Index
121
- def get_relevant_sentences(email, query, faiss_indices):
122
- if email not in faiss_indices:
123
- return ["User not found or no data available."]
124
-
125
- index_data = faiss_indices[email]
126
- index = index_data["index"]
127
- sentences = index_data["sentences"]
128
-
129
- # Compute query embedding
130
- query_embedding = model.encode([query], convert_to_numpy=True)
131
- k = 100
132
- distances, indices = index.search(query_embedding, k)
133
-
134
- # Filter sentences based on FAISS similarity threshold
135
- threshold = 1.5
136
- filtered_sentences = [sentences[idx] for dist, idx in zip(distances[0], indices[0]) if dist < threshold]
137
-
138
- return filtered_sentences if filtered_sentences else ["No relevant information found."]
139
-
140
- # βœ… Generate response using Gemini API
141
- def generate_response(email, query):
142
- filtered_sentences = get_relevant_sentences(email, query, faiss_indices)
143
-
144
- if filtered_sentences == ["No relevant information found."]:
145
- return "No relevant information found."
146
-
147
- prompt = f"Based on query '{query}', generate a short answer using:\n\n" + "\n".join(filtered_sentences)
148
- model = genai.GenerativeModel("gemini-1.5-flash")
149
- response = model.generate_content(prompt)
150
-
151
- return response.text
152
-
153
- # βœ… Real-time data update function
154
- def real_time_update():
155
- latest_data = fetch_latest_data()
156
- user_sentences = generate_sentences(latest_data)
157
- return update_faiss_index(user_sentences)
158
-
159
- # βœ… Run initial update
160
- faiss_indices = real_time_update()
161
-
162
- # βœ… Gradio Web UI
163
- def chat(email, query):
164
- return generate_response(email, query)
165
-
166
- iface = gr.Interface(fn=chat, inputs=["text", "text"], outputs="text", title="AI Workspace Assistant")
 
167
  iface.launch()
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+ import faiss
5
+ import numpy as np
6
+ from sentence_transformers import SentenceTransformer
7
+ from pymongo import MongoClient
8
+
9
+ # βœ… Configure Gemini API
10
+ genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
11
+
12
+ # βœ… Load Sentence Transformer Model
13
+ model = SentenceTransformer('all-MiniLM-L6-v2')
14
+
15
+ # βœ… Connect to MongoDB
16
+ MONGO_URI = os.getenv("MONGO_URI")
17
+ client = MongoClient(MONGO_URI)
18
+ db = client["AiWork"]
19
+
20
+ # βœ… Fetch latest data dynamically
21
+ def fetch_latest_data():
22
+ return {
23
+ "users": list(db.users.find()),
24
+ "teams": list(db.teams.find()),
25
+ "projects": list(db.projects.find()),
26
+ "modules": list(db.modules.find()),
27
+ "documents": list(db.documents.find()),
28
+ "schedules": list(db.schedules.find())
29
+ }
30
+
31
+ def generate_sentences(db):
32
+ users, teams, projects, modules, documents, schedules = (
33
+ db["users"], db["teams"], db["projects"], db["modules"], db["documents"], db["schedules"]
34
+ )
35
+ user_sentences = {} # Store categorized sentences per user
36
+
37
+ for user in users:
38
+ username = user.get("username", "Unknown User")
39
+ email = user.get("email", "Unknown Email")
40
+
41
+ if email not in user_sentences:
42
+ user_sentences[email] = {
43
+ "Teams": [],
44
+ "Projects": [],
45
+ "Modules & Tasks": [],
46
+ "Documents": [],
47
+ "Schedules": []
48
+ }
49
+
50
+ # User team ownership and membership
51
+ owned_teams = [team for team in teams if team.get("owner", {}).get("email") == email]
52
+ if owned_teams:
53
+ team_names = ", ".join(f'"{team["teamName"]}"' for team in owned_teams)
54
+ user_sentences[email]["Teams"].append(f"User {username} owns the teams: {team_names}.")
55
+
56
+ member_teams = [team for team in teams if any(m["email"] == email for m in team.get("members", []))]
57
+ if member_teams:
58
+ team_names = ", ".join(f'"{team["teamName"]}"' for team in member_teams)
59
+ user_sentences[email]["Teams"].append(f"User {username} is a member of the teams: {team_names}.")
60
+
61
+ # Find projects in teams they own or are part of
62
+ relevant_teams = owned_teams + member_teams
63
+ team_ids = [str(team["_id"]) for team in relevant_teams]
64
+ user_projects = [p for p in projects if str(p.get("owner", {}).get("teamId")) in team_ids]
65
+
66
+ if user_projects:
67
+ for project in user_projects:
68
+ proj_name = project["projName"]
69
+ user_sentences[email]["Projects"].append(f"User {username} is involved in project {proj_name}.")
70
+
71
+ # Find modules under this project
72
+ proj_modules = [m for m in modules if str(m.get("projId")) == str(project["_id"])]
73
+ if proj_modules:
74
+ for module in proj_modules:
75
+ module_name = module["moduleName"]
76
+ user_sentences[email]["Modules & Tasks"].append(f"In project {proj_name}, module {module_name} exists.")
77
+
78
+ # Find tasks in this module assigned to the user
79
+ assigned_tasks = [
80
+ task for task in module.get("tasks", [])
81
+ if any(a["email"] == email for a in task.get("assignedTo", []))
82
+ ]
83
+ if assigned_tasks:
84
+ task_names = ", ".join(f'"{t["taskName"]}"' for t in assigned_tasks)
85
+ user_sentences[email]["Modules & Tasks"].append(f"Tasks assigned to {username} in {module_name}: {task_names}.")
86
+
87
+ # Find documents in this project
88
+ proj_docs = [d for d in documents if str(d.get("owner", {}).get("projId")) == str(project["_id"])]
89
+ if proj_docs:
90
+ doc_names = ", ".join(f'"{d["title"]}"' for d in proj_docs)
91
+ user_sentences[email]["Documents"].append(f"Documents related to project {proj_name}: {doc_names}.")
92
+
93
+ # Find meeting schedules related to their teams
94
+ user_schedules = [s for s in schedules if str(s.get("teamId")) in team_ids]
95
+ if user_schedules:
96
+ for schedule in user_schedules:
97
+ schedule_detail = f'{schedule["moto"]} scheduled on {schedule["date"]} at {schedule["time"]}.'
98
+ user_sentences[email]["Schedules"].append(schedule_detail)
99
+
100
+ return user_sentences
101
+
102
+ # βœ… Update FAISS Index dynamically
103
+ def update_faiss_index(user_sentences):
104
+ faiss_indices = {}
105
+ for email, categories in user_sentences.items():
106
+ sentences = sum(categories.values(), [])
107
+
108
+ if not sentences:
109
+ continue
110
+
111
+ embeddings = model.encode(sentences, convert_to_numpy=True)
112
+ embedding_dim = embeddings.shape[1]
113
+
114
+ index = faiss.IndexFlatL2(embedding_dim)
115
+ index.add(embeddings)
116
+
117
+ faiss_indices[email] = {"index": index, "sentences": sentences}
118
+
119
+ return faiss_indices
120
+
121
+ # βœ… Query FAISS Index
122
+ def get_relevant_sentences(email, query, faiss_indices):
123
+ if email not in faiss_indices:
124
+ return ["User not found or no data available."]
125
+
126
+ index_data = faiss_indices[email]
127
+ index = index_data["index"]
128
+ sentences = index_data["sentences"]
129
+
130
+ # Compute query embedding
131
+ query_embedding = model.encode([query], convert_to_numpy=True)
132
+ k = 100
133
+ distances, indices = index.search(query_embedding, k)
134
+
135
+ # Filter sentences based on FAISS similarity threshold
136
+ threshold = 1.5
137
+ filtered_sentences = [sentences[idx] for dist, idx in zip(distances[0], indices[0]) if dist < threshold]
138
+
139
+ return filtered_sentences if filtered_sentences else ["No relevant information found."]
140
+
141
+ # βœ… Generate response using Gemini API
142
+ def generate_response(email, query):
143
+ filtered_sentences = get_relevant_sentences(email, query, faiss_indices)
144
+
145
+ if filtered_sentences == ["No relevant information found."]:
146
+ return "No relevant information found."
147
+
148
+ prompt = f"Based on query '{query}', generate a short answer using:\n\n" + "\n".join(filtered_sentences)
149
+ model = genai.GenerativeModel("gemini-1.5-flash")
150
+ response = model.generate_content(prompt)
151
+
152
+ return response.text
153
+
154
+ # βœ… Real-time data update function
155
+ def real_time_update():
156
+ latest_data = fetch_latest_data()
157
+ user_sentences = generate_sentences(latest_data)
158
+ return update_faiss_index(user_sentences)
159
+
160
+ # βœ… Run initial update
161
+ faiss_indices = real_time_update()
162
+
163
+ # βœ… Gradio Web UI
164
+ def chat(email, query):
165
+ return generate_response(email, query)
166
+
167
+ iface = gr.Interface(fn=chat, inputs=["text", "text"], outputs="text", title="AI Workspace Assistant")
168
  iface.launch()