akadhim commited on
Commit
25cae17
·
verified ·
1 Parent(s): dc4a259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -6,11 +6,24 @@ from numpy.linalg import norm
6
  from huggingface_hub import hf_hub_download
7
  from sentence_transformers import SentenceTransformer
8
  import os
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Get Hugging Face Token from Environment Variables
11
  HF_TOKEN = os.environ.get("HF_TOKEN")
12
  if not HF_TOKEN:
13
- raise ValueError("Missing Hugging Face API token. Please set HF_TOKEN as an environment variable in Hugging Face Secrets.")
 
 
14
 
15
  # Load the Nomic-Embed Model from Hugging Face
16
  EMBEDDING_MODEL = "nomic-ai/nomic-embed-text-v1.5"
@@ -22,8 +35,12 @@ db_repo = "UoS-HGIG/hpo_genes"
22
  db_path = os.path.join(os.getcwd(), db_filename)
23
 
24
  if not os.path.exists(db_path):
25
- db_path = hf_hub_download(repo_id=db_repo, filename=db_filename, repo_type="dataset", use_auth_token=HF_TOKEN)
26
-
 
 
 
 
27
 
28
  def find_best_hpo_match(finding, region, threshold):
29
  query_text = f"{finding} in {region}" if region else finding
@@ -44,8 +61,13 @@ def find_best_hpo_match(finding, region, threshold):
44
  best_match = {"hpo_id": hpo_id, "hpo_term": hpo_name}
45
 
46
  conn.close()
47
- return best_match if best_score >= threshold else None
48
 
 
 
 
 
 
 
49
 
50
  def get_genes_for_hpo(hpo_id):
51
  conn = sqlite3.connect(db_path)
@@ -53,11 +75,17 @@ def get_genes_for_hpo(hpo_id):
53
  cursor.execute("SELECT genes FROM hpo_gene WHERE hpo_id = ?", (hpo_id,))
54
  result = cursor.fetchone()
55
  conn.close()
56
- return result[0].split(", ") if result else []
57
 
 
 
 
58
 
59
  def hpo_mapper_ui(finding, region, threshold):
 
 
60
  if not finding:
 
 
61
  return "Please enter a pathological finding.", "", ""
62
 
63
  match = find_best_hpo_match(finding, region, threshold)
@@ -68,7 +96,6 @@ def hpo_mapper_ui(finding, region, threshold):
68
 
69
  return "No match found.", "", ""
70
 
71
-
72
  demo = gr.Interface(
73
  fn=hpo_mapper_ui,
74
  inputs=[
@@ -98,4 +125,5 @@ demo = gr.Interface(
98
  )
99
 
100
  if __name__ == "__main__":
101
- demo.launch()
 
 
6
  from huggingface_hub import hf_hub_download
7
  from sentence_transformers import SentenceTransformer
8
  import os
9
+ import logging
10
+
11
+ # Create a logs directory if it does not exist
12
+ log_dir = "logs"
13
+ if not os.path.exists(log_dir):
14
+ os.makedirs(log_dir)
15
+
16
+ # Configure logging
17
+ log_file = os.path.join(log_dir, "hpo_mapper.log")
18
+ logging.basicConfig(filename=log_file, level=logging.INFO,
19
+ format="%(asctime)s - %(levelname)s - %(message)s")
20
 
21
  # Get Hugging Face Token from Environment Variables
22
  HF_TOKEN = os.environ.get("HF_TOKEN")
23
  if not HF_TOKEN:
24
+ error_msg = "Missing Hugging Face API token. Please set HF_TOKEN as an environment variable in Hugging Face Secrets."
25
+ logging.error(error_msg)
26
+ raise ValueError(error_msg)
27
 
28
  # Load the Nomic-Embed Model from Hugging Face
29
  EMBEDDING_MODEL = "nomic-ai/nomic-embed-text-v1.5"
 
35
  db_path = os.path.join(os.getcwd(), db_filename)
36
 
37
  if not os.path.exists(db_path):
38
+ try:
39
+ db_path = hf_hub_download(repo_id=db_repo, filename=db_filename, repo_type="dataset", use_auth_token=HF_TOKEN)
40
+ logging.info("Database successfully downloaded from Hugging Face.")
41
+ except Exception as e:
42
+ logging.error(f"Failed to download database: {e}")
43
+ raise
44
 
45
  def find_best_hpo_match(finding, region, threshold):
46
  query_text = f"{finding} in {region}" if region else finding
 
61
  best_match = {"hpo_id": hpo_id, "hpo_term": hpo_name}
62
 
63
  conn.close()
 
64
 
65
+ if best_score >= threshold:
66
+ logging.info(f"Match found: {best_match['hpo_id']} - {best_match['hpo_term']} with score {best_score}")
67
+ return best_match
68
+ else:
69
+ logging.info(f"No suitable match found for query '{query_text}' with threshold {threshold}.")
70
+ return None
71
 
72
  def get_genes_for_hpo(hpo_id):
73
  conn = sqlite3.connect(db_path)
 
75
  cursor.execute("SELECT genes FROM hpo_gene WHERE hpo_id = ?", (hpo_id,))
76
  result = cursor.fetchone()
77
  conn.close()
 
78
 
79
+ genes = result[0].split(", ") if result else []
80
+ logging.info(f"Genes retrieved for HPO ID {hpo_id}: {genes}")
81
+ return genes
82
 
83
  def hpo_mapper_ui(finding, region, threshold):
84
+ logging.info(f"User input: Finding='{finding}', Region='{region}', Threshold={threshold}")
85
+
86
  if not finding:
87
+ error_msg = "No pathological finding entered."
88
+ logging.warning(error_msg)
89
  return "Please enter a pathological finding.", "", ""
90
 
91
  match = find_best_hpo_match(finding, region, threshold)
 
96
 
97
  return "No match found.", "", ""
98
 
 
99
  demo = gr.Interface(
100
  fn=hpo_mapper_ui,
101
  inputs=[
 
125
  )
126
 
127
  if __name__ == "__main__":
128
+ logging.info("Launching Gradio app.")
129
+ demo.launch()