alexanderHSG commited on
Commit
9220c08
·
1 Parent(s): 925f086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +226 -26
app.py CHANGED
@@ -3,6 +3,9 @@ import openai
3
  import json
4
  import gradio as gr
5
  from neo4j import GraphDatabase
 
 
 
6
 
7
 
8
 
@@ -12,7 +15,7 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
12
 
13
  neo4j_url = os.getenv("NEO4J_URL")
14
  AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
15
- #driver = GraphDatabase.driver(neo4j_url, auth=AUTH)
16
 
17
 
18
 
@@ -97,41 +100,238 @@ def iterator_for_gr(nested_list, i):
97
 
98
 
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
 
102
  ## GRADIO UI LAYOUT & FUNCTIONALITY
103
  ## ---------------------------------------------------------------------------------------------------------------------
104
 
105
  with gr.Blocks(title='Slide Inspo', theme='Soft') as demo:
106
- with gr.Row():
107
- with gr.Column(scale=1):
108
- gr.Markdown("# 1. Input: 🔍")
109
- storyline_prompt = gr.Textbox(placeholder = 'Give us a topic and we will provide a storyline for you! For example: "Risk Management in Venture Capital"',
110
- label = 'Topic to build:',
111
- lines=5,
112
- scale = 3)
113
- nr_storypoints_to_build = gr.Number(value=5,
114
- label="How many storypoints?",
115
- scale =1)
116
- storyline_output_JSON = gr.JSON(visible=False)
117
- storyline_output_storypoint_name_list = gr.List(visible=True, type="array")
118
- btn = gr.Button("Build Storyline 🦄")
119
-
120
- with gr.Column(scale=1):
121
- gr.Markdown("# 2. Storyline: 🦄")
122
 
123
- storyline_output_pretty = gr.Textbox(label="Your Storyline:", lines=13, scale=3)
124
- submit_button = gr.Button("⚡ Find Slides ⚡")
 
125
 
126
- btn.click(slide_deck_storyline,
127
- inputs = [storyline_prompt, nr_storypoints_to_build],
128
- outputs = [storyline_output_JSON, storyline_output_storypoint_name_list, storyline_output_pretty])
129
 
130
- storyline_prompt.submit(slide_deck_storyline,
131
- inputs = [storyline_prompt, nr_storypoints_to_build],
132
- outputs = [storyline_output_JSON, storyline_output_storypoint_name_list, storyline_output_pretty])
 
133
 
 
 
 
134
 
135
 
136
  gr.close_all()
137
- demo.launch(share=True)
 
3
  import json
4
  import gradio as gr
5
  from neo4j import GraphDatabase
6
+ import pandas as pd
7
+ import numpy as np
8
+ from sklearn.metrics.pairwise import cosine_similarity
9
 
10
 
11
 
 
15
 
16
  neo4j_url = os.getenv("NEO4J_URL")
17
  AUTH = (os.getenv("NEO4J_USERNAME"), os.getenv("NEO4J_PASSWORD"))
18
+ driver = GraphDatabase.driver(neo4j_url, auth=AUTH)
19
 
20
 
21
 
 
100
 
101
 
102
 
103
+ # ---------------------------------------------------------------------------------------------------------------------
104
+ ## Calculate Input Storypoints Similarity to Storypoints in Database
105
+
106
+ from openai import OpenAI
107
+ client = OpenAI(api_key = os.getenv("OPENAI_API_KEY"))
108
+
109
+
110
+
111
+
112
+ def get_embedding_inputstorypoints(storyline_output_storypoint_name_list, model="text-embedding-ada-002"):
113
+ # transform storyline_output_storypoint_name_list to pandas dataframe
114
+ input_storypoints = pd.DataFrame(storyline_output_storypoint_name_list, columns=['description'])
115
+ # get embeddings for input storypoints
116
+ input_storypoints['ada_embedding'] = input_storypoints.description.apply(lambda x: client.embeddings.create(input = [x], model=model).data[0].embedding)
117
+
118
+ return input_storypoints
119
+
120
+
121
+ # Function to fetch embeddings from Neo4j
122
+ def fetch_embeddings(driver):
123
+ query = """
124
+ MATCH (sp:STORYPOINT)
125
+ RETURN sp.id AS id, sp.embedding AS embedding
126
+ """
127
+ embeddings = {}
128
+ with driver.session() as session:
129
+ result = session.run(query)
130
+ for record in result:
131
+ embeddings[record['id']] = np.array(record['embedding'])
132
+ return embeddings
133
+
134
+ # Function to calculate cosine similarity and find the highest similarities
135
+ def find_highest_similarities(existing_embeddings, new_embeddings):
136
+ # Transform embeddings into arrays for the calculation
137
+ existing_ids, existing_vecs = zip(*existing_embeddings.items())
138
+ new_ids, new_vecs = zip(*new_embeddings.items())
139
+ existing_vecs = np.array(existing_vecs)
140
+ new_vecs = np.array(new_vecs)
141
+
142
+ # Calculate cosine similarity
143
+ similarity_matrix = cosine_similarity(new_vecs, existing_vecs)
144
+
145
+ # Find the index with the highest similarity for each new embedding
146
+ max_indices = np.argmax(similarity_matrix, axis=1)
147
+ similarities = np.max(similarity_matrix, axis=1)
148
+
149
+ # Pair each new storypoint with the existing one that has the highest similarity
150
+ highest_pairs = [(new_ids[i], existing_ids[max_indices[i]], similarities[i]) for i in range(len(new_ids))]
151
+ return highest_pairs
152
+
153
+ def coordinate_simcalculation(storyline_output_storypoint_name_list):
154
+
155
+ # Fetch existing embeddings from Neo4j
156
+ existing_embeddings = fetch_embeddings(driver)
157
+ input_storypoints = get_embedding_inputstorypoints(storyline_output_storypoint_name_list)
158
+ # Assume new_embeddings come from your Python processing earlier
159
+ new_embeddings = {row['description']: row['ada_embedding'] for index, row in input_storypoints.iterrows()}
160
+
161
+ # Find highest similarities
162
+ highest_similarities = find_highest_similarities(existing_embeddings, new_embeddings)
163
+
164
+ # Display results
165
+ for new_id, existing_id, similarity in highest_similarities:
166
+ print(f"Input STORYPOINT '{new_id}' is most similar to existing STORYPOINT '{existing_id}' with a similarity of {similarity:.2f}")
167
+
168
+ HTMLoutput = fetch_storypoints_and_slides(highest_similarities)
169
+
170
+ return HTMLoutput
171
+
172
+
173
+ def fetch_storypoints_and_slides(highest_similarities):
174
+ storypoint_ids = [existing_id for _, existing_id, _ in highest_similarities]
175
+ query = """
176
+ MATCH (sp:STORYPOINT) WHERE sp.id IN $storypoint_ids
177
+ MATCH (sp)<-[:ASSIGNED_TO]-(s:SLIDE)-[:CONTAINS]->(sd:SLIDE_DECK)
178
+ RETURN sd, s, sp
179
+ """
180
+ with driver.session() as session:
181
+ result = session.run(query, {"storypoint_ids": storypoint_ids})
182
+ for record in result:
183
+ print("Slide Deck:", record["sd"])
184
+ print("Slide:", record["s"])
185
+ print("Storypoint:", record["sp"])
186
+
187
+
188
+ graphVisualHTML = f"""
189
+ <html>
190
+
191
+ <head>
192
+ <title>DataViz</title>
193
+ <style type="text/css">
194
+ #viz {{
195
+ width: 1400px;
196
+ height: 700px;
197
+ }}
198
+ </style>
199
+ <script src="https://rawgit.com/neo4j-contrib/neovis.js/master/dist/neovis.js"></script>
200
+ </head>
201
+
202
+ <script type="text/javascript">
203
+ var viz;
204
+ function draw() {{
205
+ var config = {{
206
+ containerId: "viz",
207
+ neo4j: {{
208
+ serverUrl: "bolt://localhost:7687",
209
+ serverUser: "neo4j",
210
+ serverPassword: "123testtest"
211
+ }},
212
+ labels: {{
213
+ SLIDE: {{
214
+ [NeoVis.NEOVIS_ADVANCED_CONFIG]: {{
215
+ static: {{
216
+ shape: "image" // Sets the shape to use an image (use "circularImage" for circular nodes)
217
+ }},
218
+ function: {{
219
+ image: (node) => "https://slidestorage.s3.eu-north-1.amazonaws.com/" + node.properties.object_id + ".png"
220
+ }}
221
+ }}
222
+ }},
223
+ STORYPOINT:{{
224
+ label:"description",
225
+
226
+ [NeoVis.NEOVIS_ADVANCED_CONFIG]: {{
227
+ static: {{
228
+ caption: "description",
229
+ shape: 'box',
230
+ color: {{
231
+ background: 'white',
232
+ border: 'lightgray',
233
+ highlight: {{
234
+ background: 'lightblue',
235
+ border: 'blue'
236
+ }}
237
+ }},
238
+ font: {{
239
+ color: 'black',
240
+ size: 14, // Pixel size
241
+ face: 'Arial' // A modern and widely used font
242
+ }}
243
+ }}
244
+ }}
245
+ }}
246
+ }},
247
+
248
+ relationships: {{
249
+ CONTAINS: {{
250
+ color: 'gray',
251
+ arrows: {{
252
+ to: {{
253
+ enabled: true,
254
+ scaleFactor: 1.2 // Makes the arrow slightly larger
255
+ }}
256
+ }},
257
+ font: {{
258
+ color: 'black',
259
+ size: 12,
260
+ face: 'Arial'
261
+ }}
262
+ }}
263
+ }},
264
+ visConfig: {{
265
+ edges: {{
266
+ arrows: {{
267
+ to: {{ enabled: true }}
268
+ }}
269
+ }}
270
+ }},
271
+
272
+
273
+ initialCypher: "{query}"
274
+ }};
275
+ viz = new NeoVis.default(config);
276
+ viz.render();
277
+
278
+ }}
279
+ </script>
280
+
281
+ <body onload="draw()">
282
+ <div id="viz"></div>
283
+ </body>
284
+
285
+ </html>
286
+ """
287
+
288
+
289
+ return graphVisualHTML
290
+
291
+
292
+
293
+
294
+
295
 
296
 
297
  ## GRADIO UI LAYOUT & FUNCTIONALITY
298
  ## ---------------------------------------------------------------------------------------------------------------------
299
 
300
  with gr.Blocks(title='Slide Inspo', theme='Soft') as demo:
301
+ with gr.Row():
302
+ with gr.Column(scale=1):
303
+ gr.Markdown("# 1. Input: 🔍")
304
+ storyline_prompt = gr.Textbox(placeholder = 'Give us a topic and we will provide a storyline for you! For example: "Risk Management in Venture Capital"',
305
+ label = 'Topic to build:',
306
+ lines=5,
307
+ scale = 3)
308
+ nr_storypoints_to_build = gr.Number(value=5,
309
+ label="How many storypoints?",
310
+ scale =1)
311
+ storyline_output_JSON = gr.JSON(visible=False)
312
+ storyline_output_storypoint_name_list = gr.List(visible=False, type="array")
313
+ btn = gr.Button("Build Storyline 🦄")
314
+
315
+ with gr.Column(scale=1):
316
+ gr.Markdown("# 2. Storyline: 🦄")
317
 
318
+ storyline_output_pretty = gr.Textbox(label="Your Storyline:", lines=13, scale=3)
319
+ submit_button = gr.Button("⚡ Find Slides ⚡")
320
+ submit_button.click(coordinate_simcalculation, inputs=[storyline_output_storypoint_name_list], outputs=[graphVisual])
321
 
322
+ btn.click(slide_deck_storyline,
323
+ inputs = [storyline_prompt, nr_storypoints_to_build],
324
+ outputs = [storyline_output_JSON, storyline_output_storypoint_name_list, storyline_output_pretty])
325
 
326
+ storyline_prompt.submit(slide_deck_storyline,
327
+ inputs = [storyline_prompt, nr_storypoints_to_build],
328
+ outputs = [storyline_output_JSON, storyline_output_storypoint_name_list, storyline_output_pretty])
329
+
330
 
331
+
332
+ with gr.Row():
333
+ graphVisual = gr.HTML()
334
 
335
 
336
  gr.close_all()
337
+ demo.launch()