Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
| 22 |
|
| 23 |
neo4j_url = "neo4j+s://" + str(os.getenv("NEO4J_URL"))
|
| 24 |
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
|
| 25 |
-
|
| 26 |
|
| 27 |
|
| 28 |
|
|
@@ -211,12 +211,13 @@ def get_embedding_inputstorypoints(storyline_output_storypoint_name_list, model=
|
|
| 211 |
|
| 212 |
|
| 213 |
# Function to fetch embeddings from Neo4j
|
| 214 |
-
def fetch_embeddings(
|
| 215 |
query = """
|
| 216 |
MATCH (sp:STORYPOINT)
|
| 217 |
RETURN sp.id AS id, sp.embedding AS embedding
|
| 218 |
"""
|
| 219 |
embeddings = {}
|
|
|
|
| 220 |
with driver.session() as session:
|
| 221 |
try:
|
| 222 |
result = session.run(query)
|
|
@@ -225,6 +226,7 @@ def fetch_embeddings(driver):
|
|
| 225 |
|
| 226 |
for record in result:
|
| 227 |
embeddings[record['id']] = np.array(record['embedding'])
|
|
|
|
| 228 |
return embeddings
|
| 229 |
|
| 230 |
# Function to calculate cosine similarity and find the highest similarities
|
|
@@ -250,7 +252,7 @@ def find_highest_similarities(existing_embeddings, new_embeddings):
|
|
| 250 |
def coordinate_simcalculation(storyline_output_storypoint_name_list):
|
| 251 |
|
| 252 |
# Fetch existing embeddings from Neo4j
|
| 253 |
-
existing_embeddings = fetch_embeddings(
|
| 254 |
input_storypoints = get_embedding_inputstorypoints(storyline_output_storypoint_name_list)
|
| 255 |
# Assume new_embeddings come from your Python processing earlier
|
| 256 |
new_embeddings = {row['description']: row['ada_embedding'] for index, row in input_storypoints.iterrows()}
|
|
@@ -336,7 +338,8 @@ def profile_user(request: gr.Request):
|
|
| 336 |
|
| 337 |
|
| 338 |
def get_neo4j_response(query):
|
| 339 |
-
|
|
|
|
| 340 |
#filter out the textual content and embeddings from the response as they waste space and are not needed for visualization
|
| 341 |
with driver.session() as session:
|
| 342 |
result = session.run(query)
|
|
@@ -351,7 +354,7 @@ def get_neo4j_response(query):
|
|
| 351 |
filtered_record[key] = value
|
| 352 |
response.append(filtered_record)
|
| 353 |
|
| 354 |
-
|
| 355 |
return response
|
| 356 |
|
| 357 |
def construct_hmtl(highest_similarities = None, nodes_to_show=["SLIDE_DECK", "SLIDE", "STORYPOINT"], query=None):
|
|
|
|
| 22 |
|
| 23 |
neo4j_url = "neo4j+s://" + str(os.getenv("NEO4J_URL"))
|
| 24 |
AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
|
| 25 |
+
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 211 |
|
| 212 |
|
| 213 |
# Function to fetch embeddings from Neo4j
|
| 214 |
+
def fetch_embeddings():
|
| 215 |
query = """
|
| 216 |
MATCH (sp:STORYPOINT)
|
| 217 |
RETURN sp.id AS id, sp.embedding AS embedding
|
| 218 |
"""
|
| 219 |
embeddings = {}
|
| 220 |
+
driver = GraphDatabase.driver(neo4j_url, auth=AUTH)
|
| 221 |
with driver.session() as session:
|
| 222 |
try:
|
| 223 |
result = session.run(query)
|
|
|
|
| 226 |
|
| 227 |
for record in result:
|
| 228 |
embeddings[record['id']] = np.array(record['embedding'])
|
| 229 |
+
driver.close()
|
| 230 |
return embeddings
|
| 231 |
|
| 232 |
# Function to calculate cosine similarity and find the highest similarities
|
|
|
|
| 252 |
def coordinate_simcalculation(storyline_output_storypoint_name_list):
|
| 253 |
|
| 254 |
# Fetch existing embeddings from Neo4j
|
| 255 |
+
existing_embeddings = fetch_embeddings()
|
| 256 |
input_storypoints = get_embedding_inputstorypoints(storyline_output_storypoint_name_list)
|
| 257 |
# Assume new_embeddings come from your Python processing earlier
|
| 258 |
new_embeddings = {row['description']: row['ada_embedding'] for index, row in input_storypoints.iterrows()}
|
|
|
|
| 338 |
|
| 339 |
|
| 340 |
def get_neo4j_response(query):
|
| 341 |
+
|
| 342 |
+
driver = GraphDatabase.driver(neo4j_url, auth=AUTH)
|
| 343 |
#filter out the textual content and embeddings from the response as they waste space and are not needed for visualization
|
| 344 |
with driver.session() as session:
|
| 345 |
result = session.run(query)
|
|
|
|
| 354 |
filtered_record[key] = value
|
| 355 |
response.append(filtered_record)
|
| 356 |
|
| 357 |
+
driver.close()
|
| 358 |
return response
|
| 359 |
|
| 360 |
def construct_hmtl(highest_similarities = None, nodes_to_show=["SLIDE_DECK", "SLIDE", "STORYPOINT"], query=None):
|