-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_creaters_edited.py
203 lines (164 loc) · 6.04 KB
/
project_creaters_edited.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
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
import os
import pandas as pd
import numpy as np
import google.generativeai as palm
import json
import re
import seaborn as sns
from urllib.parse import urlparse
import openpyxl
import spacy
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import firbase
nlp = spacy.load("en_core_web_sm")
palm.configure(api_key='AIzaSyDnUxKaSdabi0rNjxdcKtSvSuKWE-zDkNo')
path = "C:/Users/limah/OneDrive/Desktop/nasaSpaceApp/projects.json"
def get_embedding(skills):
model = "models/embedding-gecko-001"
x = skills
embedding = palm.generate_embeddings(model=model, text=x)
embedding = embedding['embedding']
return embedding
def plot_word_embeddings(x):
# x = x.split()
embeddings = []
for i in x:
emp = get_embedding(i)
embeddings.append(emp)
print(embeddings)
print(x)
embeddings = np.array(embeddings, dtype=np.float32)
tsne = TSNE(random_state=0, perplexity = 1, n_iter=1000)
tsne_results = tsne.fit_transform(embeddings)
df_tsne = pd.DataFrame(tsne_results, columns=['TSNE1', 'TSNE2'])
df_tsne['Class Name'] = x
fig, ax = plt.subplots(figsize=(8,6)) # Set figsize
sns.set_style('darkgrid', {"grid.color": ".6", "grid.linestyle": ":"})
sns.scatterplot(data=df_tsne, x='TSNE1', y='TSNE2', hue='Class Name', palette='hls')
sns.move_legend(ax, "upper left", bbox_to_anchor=(1, 1))
plt.title('Scatter plot of words embeddings');
plt.xlabel('TSNE1');
plt.ylabel('TSNE2');
plt.axis('equal')
for i, txt in enumerate(x):
ax.annotate(txt, (df_tsne['TSNE1'][i], df_tsne['TSNE2'][i]), fontsize=5)
plt.show()
def check_title(title):
while len(title) < 4:
print("title must be at least of 8 letters, retry")
title = input()
return title
class project:
def __init__(self, title, description, URL):
self.title = title
self.description = description
self.URL = URL
self.calc_embedings()
def calc_embedings(self):
model = "models/embedding-gecko-001"
x = self.description
self.embedding = palm.generate_embeddings(model=model, text=x)
self.embedding = self.embedding['embedding']
def get_URL(self):
return self.URL
def get_description(self):
return self.description
def get_title(self):
return self.title
def set_URL(self, val):
self.URL = val
def set_description(self, val):
self.description = val
def set_title(self, val):
self.title = val
def get_embedding(self):
return self.embedding
def save_proj(self):
existing_data = pd.read_json(path)
new_row_data = {'title': self.title, 'descriptions': self.description,'URL' : self.URL, 'embeddings' : f"{self.embedding}"}
new_row_df = pd.DataFrame(new_row_data, index=[0])
existing_data = pd.concat([existing_data, new_row_df], ignore_index=True)
# existing_data = [existing_data, new_row_df]
existing_data.to_json(path)
# new_row_df.to_json(path, orient='records')
print("saved")
def save_proj_first_time(self):
# existing_data = pd.read_json(path)
new_row_data = {'title': self.title, 'descriptions': self.description,'URL' : self.URL, 'embeddings' : f"{self.embedding}"}
new_row_df = pd.DataFrame(new_row_data, index=[0])
# existing_data = pd.concat([existing_data, new_row_df], ignore_index=True)
# existing_data = [existing_data, new_row_df]
# existing_data.to_json(path)
new_row_df.to_json(path, orient='records')
print("saved")
def check_desc(desc):
while(len(desc)) < 20:
print("Enter more details about the project")
inp = input()
desc+= " "
desc+= inp
return desc
def check_link(link):
pattern = r'^(https?|ftp)://[^\s/$.?#].[^\s]*$'
if re.match(pattern, link):
return link
else:
print("not valid website, retry")
inp = input()
check_link(inp)
def add_project(title):
# print("Hello!")
description = input("Please Write a simple description about the project")
description = check_desc(description)
description = nlp(description)
keywords = [token.text for token in description if not token.is_stop]
# x = keywords[0]
x = ""
for i in keywords:
x += i
# x = keywords[0]
plot_word_embeddings(keywords)
link = input("Provide the URL of your project")
link = check_link(link)
project1 = project(title, description, link)
print("project created!!")
project1.save_proj()
return project1
def get_contributers(title):
# loading data
df_users = pd.read_json("C:/Users/limah/OneDrive/Desktop/nasaSpaceApp/users.json")
existing_data = pd.read_json(path)
project_details = existing_data[existing_data["title"] == title]
if project_details.empty:
print("project not found")
add_project(title)
existing_data = pd.read_json(path)
indx = existing_data[existing_data["title"] == title].index[0]
measures = []
for i in range(0, len(df_users)):
user_name = df_users["name"].iloc[i]
user_skills = df_users["skills"].iloc[i]
user_embeddings = df_users["embeddings"].iloc[i]
user_embeddings = json.loads(user_embeddings)
project_embeddings = existing_data["embeddings"].iloc[indx]
# embedding_user = name_details["embeddings"]
project_embeddings = json.loads(project_embeddings)
similar_measure = np.dot(project_embeddings, user_embeddings)
measures.append((similar_measure, user_name, user_skills))
print(similar_measure)
measures.sort(reverse=True)
# print(measures)
for i in measures[0 : 6]:
print(i[1])
print(i[2])
title = input("What is the title of the project you want?")
# check_title(title)
# project1 = add_project(title)
# print(project.get_embedding())
# project.save_proj()
# project1.save_proj_first_time()
get_contributers(title)
df = pd.read_json(path)
excel_file_name = "output.xlsx"
df.to_excel(excel_file_name)