24by7-description / app_logic.py
khurrameycon's picture
Update app_logic.py
226cbfb verified
raw
history blame
4.56 kB
# app_logic.py
from neo4j import GraphDatabase
from huggingface_hub import InferenceClient
import time
import os
HF_API_KEY = os.getenv("HF_API_KEY")
# Setup Hugging Face inference client
client = InferenceClient(api_key=HF_API_KEY)
# Setup Neo4j database connection
uri = "bolt://20.64.235.115:7687"
auth = ("neo4j", "123456789")
graph = GraphDatabase.driver(uri, auth=auth)
# Function to generate description using Hugging Face
def generate_description(tertiary_skill, secondary_skill, primary_skill):
messages = [
{
"role": "user",
"content": f"Provide a 2-line description of {tertiary_skill} as tertiary skill, with background of Secondary Skill \"{secondary_skill}\" with primary skill \"{primary_skill}\". Just 2 line description of {tertiary_skill} nothing else out of scope of the primary and secondary skill"
}
]
try:
completion = client.chat.completions.create(
model="meta-llama/Llama-3.3-70B-Instruct",
messages=messages,
max_tokens=500
)
return completion.choices[0].message['content']
except Exception as e:
print(f"Error generating description for {tertiary_skill}: {e}")
return ""
# Function to update tertiary skill description in the database
def update_description(tx, tertiary_skill_name, secondary_skill_name, primary_skill_name, description):
query = (
"MATCH (p:PrimarySkill {name: $primary_skill_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_skill_name})-[:HAS_TERTIARY]->(t:TertiarySkill {name: $tertiary_skill_name}) "
"SET t.description = $description"
)
tx.run(query, tertiary_skill_name=tertiary_skill_name, secondary_skill_name=secondary_skill_name, primary_skill_name=primary_skill_name, description=description)
# Main processing function
def run_my_application():
with graph.session() as session:
# Fetch all primary skills
primary_skills = session.run("MATCH (p:PrimarySkill) RETURN p.name AS primary_name")
for primary in primary_skills:
primary_name = primary["primary_name"]
print(f"Processing Primary Skill: {primary_name}")
# Fetch secondary skills for each primary skill
secondary_skills = session.run(
"MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill) RETURN s.name AS secondary_name, p.name AS primary_name",
primary_name=primary_name
)
for secondary in secondary_skills:
secondary_name = secondary["secondary_name"]
print(f" Processing Secondary Skill: {secondary_name}")
# Fetch tertiary skills for each secondary skill
tertiary_skills = session.run(
"MATCH (p:PrimarySkill {name: $primary_name})-[:HAS_SECONDARY]->(s:SecondarySkill {name: $secondary_name})-[:HAS_TERTIARY]->(t:TertiarySkill) WHERE t.description STARTS WITH 'Description for ' OR t.description = '' RETURN t.name AS tertiary_name, s.name AS secondary_name, p.name AS primary_name",
primary_name=primary_name, secondary_name=secondary_name
)
for tertiary in tertiary_skills:
tertiary_name = tertiary["tertiary_name"]
# print(f" Processing Tertiary Skill: {tertiary_name}")
# Generate description for the tertiary skill
description = generate_description(tertiary_name, secondary_name, primary_name)
if description:
# Update the tertiary skill's description in the database
session.write_transaction(update_description, tertiary_name, secondary_name, primary_name, description)
# print(f" Updated description for {tertiary_name}")
# To prevent rate-limiting or overload, introduce a small delay
time.sleep(5)
return "Graph database has been populated."
# for file_url in json_file_paths:
# # Fetch the content from the URL
# response = requests.get(file_url)
# # Raise an error if the request failed
# response.raise_for_status()
# # Parse the JSON content
# data = response.json()
# # Assuming you want to return the 'Primary Skill' of the first item
# return data[0]['Primary Skill']