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

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +167 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ faiss-cpu
3
+ sentence-transformers
4
+ pymongo
5
+ google-generativeai