Spaces:
Sleeping
Sleeping
Update app_logic.py
Browse files- app_logic.py +76 -69
app_logic.py
CHANGED
@@ -1,78 +1,85 @@
|
|
1 |
# app_logic.py
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
|
6 |
-
|
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 |
-
|
|
|
|
|
|
|
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 |
-
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 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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 |
|