khurrameycon commited on
Commit
72da32b
·
verified ·
1 Parent(s): d8346cb

Update app_logic.py

Browse files
Files changed (1) hide show
  1. app_logic.py +76 -69
app_logic.py CHANGED
@@ -1,78 +1,85 @@
1
  # app_logic.py
2
- import requests
3
- import json
4
- from py2neo import Graph, Node, Relationship
5
 
6
- def run_my_application():
7
- """
8
- Fetch and process a remote JSON file.
9
- Replace this with your real application logic.
10
- """
11
- # json_file_path = ["https://arinsight.co/skills/Customer_Service_skills.json", "https://arinsight.co/skills/Computer_Programming_skills.json",
12
- # "https://arinsight.co/skills/Data_Science_skills.json", "https://arinsight.co/skills/Communications_skills.json",
13
- # "https://arinsight.co/skills/Cleaning_and_Facilities_skills.json",
14
- # "https://arinsight.co/skills/Caregiving_skills.json","https://arinsight.co/skills/Business_Development_and_Sales_skills.json",
15
- # "https://arinsight.co/skills/Arts_and_Design_skills.json", "https://arinsight.co/skills/Architecture_and_Construction_skills.json",
16
- # "https://arinsight.co/skills/Civil_Engineering_skills.json"]
17
 
18
- json_file_path = ["https://arinsight.co/skills/Accounting_and_Finance_skills.json"]
 
 
 
19
 
20
- # Connection to Neo4j
21
- graph = Graph("bolt://20.64.235.115:7687", auth=("neo4j", "123456789"))
22
-
23
- # Read the JSON file
24
- # json_file_path = ["Engineering_skills.json", "Energy and Utilities_skills.json", "Electrical Engineering_skills.json"]
25
- # graph.delete_all()
26
- for file in json_file_path:
27
- # with open(file, "r") as file:
28
- # data = json.load(file)
29
- response = requests.get(file)
30
-
31
- # # Raise an error if the request failed
32
- response.raise_for_status()
33
-
34
- # # Parse the JSON content
35
- data = response.json()
36
-
37
- # Clear the graph database before starting
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
-
40
- # Create graph nodes and relationships
41
- for entry in data:
42
- primary_skill = entry["Primary Skill"]
43
- secondary_skill = entry["Secondary Skill"]
44
- primary_description = entry.get("Description", "")
45
- educational_alignment = entry.get("Educational Alignment", "")
46
- curriculum_areas = "; ".join(entry.get("Curriculum Areas", []))
47
- tertiary_skills = entry["Tertiary Skills"]
48
-
49
- # Create or get the Primary Skill node
50
- primary_node = Node("PrimarySkill", name=primary_skill, description=primary_description)
51
- graph.merge(primary_node, "PrimarySkill", "name")
52
-
53
- # Create the Secondary Skill node
54
- secondary_node = Node(
55
- "SecondarySkill",
56
- name=secondary_skill,
57
- description=primary_description,
58
- educational_alignment=educational_alignment,
59
- curriculum_areas=curriculum_areas,
60
  )
61
- graph.merge(secondary_node, "SecondarySkill", "name")
62
-
63
- # Relationship: PrimarySkill -> has_secondary -> SecondarySkill
64
- relationship = Relationship(primary_node, "HAS_SECONDARY", secondary_node)
65
- graph.merge(relationship)
66
-
67
- # Create Tertiary Skills nodes and relationships
68
- for tertiary_skill in tertiary_skills:
69
- tertiary_node = Node("TertiarySkill", name=tertiary_skill, description="")
70
- graph.merge(tertiary_node, "TertiarySkill", "name")
71
-
72
- # Relationship: SecondarySkill -> has_tertiary -> TertiarySkill
73
- tertiary_relationship = Relationship(secondary_node, "HAS_TERTIARY", tertiary_node)
74
- graph.merge(tertiary_relationship)
75
-
 
 
 
 
 
 
 
 
 
 
 
 
76
  return "Graph database has been populated."
77
 
78
 
 
1
  # app_logic.py
2
+ from neo4j import GraphDatabase
3
+ from huggingface_hub import InferenceClient
4
+ import time
5
 
6
+ # Setup Hugging Face inference client
7
+ client = InferenceClient(api_key=HF_API_KEY)
 
 
 
 
 
 
 
 
 
8
 
9
+ # Setup Neo4j database connection
10
+ uri = "bolt://20.64.235.115:7687"
11
+ auth = ("neo4j", "123456789")
12
+ graph = GraphDatabase.driver(uri, auth=auth)
13
 
14
+ # Function to generate description using Hugging Face
15
+ def generate_description(tertiary_skill, secondary_skill, primary_skill):
16
+ messages = [
17
+ {
18
+ "role": "user",
19
+ "content": f"Give me 3 line description of {tertiary_skill} as tertiary skill, with background of Secondary Skill \"{secondary_skill}\" and degree or primary skill \"{primary_skill}\". Just 3 line description of {tertiary_skill} nothing else within scope of primary and secondary skill"
20
+ }
21
+ ]
22
+ try:
23
+ completion = client.chat.completions.create(
24
+ model="meta-llama/Llama-3.3-70B-Instruct",
25
+ messages=messages,
26
+ max_tokens=500
27
+ )
28
+ return completion.choices[0].message['content']
29
+ except Exception as e:
30
+ print(f"Error generating description for {tertiary_skill}: {e}")
31
+ return ""
32
+
33
+ # Function to update tertiary skill description in the database
34
+ def update_description(tx, tertiary_skill_name, secondary_skill_name, primary_skill_name, description):
35
+ query = (
36
+ "MATCH (p:PrimarySkill {name: $primary_skill_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_skill_name})-[:HAS_TERTIARY]->(t:TertiarySkill {name: $tertiary_skill_name}) "
37
+ "SET t.description = $description"
38
+ )
39
+ tx.run(query, tertiary_skill_name=tertiary_skill_name, secondary_skill_name=secondary_skill_name, primary_skill_name=primary_skill_name, description=description)
40
+
41
+ # Main processing function
42
+ def run_my_application():
43
+ with graph.session() as session:
44
+ # Fetch all primary skills
45
+ primary_skills = session.run("MATCH (p:PrimarySkill) RETURN p.name AS primary_name")
46
 
47
+ for primary in primary_skills:
48
+ primary_name = primary["primary_name"]
49
+ print(f"Processing Primary Skill: {primary_name}")
50
+
51
+ # Fetch secondary skills for each primary skill
52
+ secondary_skills = session.run(
53
+ "MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill) RETURN s.name AS secondary_name, p.name AS primary_name",
54
+ primary_name=primary_name
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  )
56
+
57
+ for secondary in secondary_skills:
58
+ secondary_name = secondary["secondary_name"]
59
+ print(f" Processing Secondary Skill: {secondary_name}")
60
+
61
+ # Fetch tertiary skills for each secondary skill
62
+ tertiary_skills = session.run(
63
+ "MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_name})-[:HAS_TERTIARY]->(t:TertiarySkill) RETURN t.name AS tertiary_name, s.name AS secondary_name, p.name AS primary_name",
64
+ primary_name=primary_name, secondary_name=secondary_name
65
+ )
66
+
67
+ for tertiary in tertiary_skills:
68
+ tertiary_name = tertiary["tertiary_name"]
69
+ # print(f" Processing Tertiary Skill: {tertiary_name}")
70
+
71
+ # Generate description for the tertiary skill
72
+ description = generate_description(tertiary_name, secondary_name, primary_name)
73
+
74
+ if description:
75
+ # Update the tertiary skill's description in the database
76
+ session.write_transaction(update_description, tertiary_name, secondary_name, primary_name, description)
77
+ # print(f" Updated description for {tertiary_name}")
78
+
79
+ # To prevent rate-limiting or overload, introduce a small delay
80
+ time.sleep(0.5)
81
+
82
+
83
  return "Graph database has been populated."
84
 
85