thechaiexperiment commited on
Commit
9b2f654
·
1 Parent(s): f5a7c15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -36
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import os
2
- import pickle
3
  import numpy as np
4
  from fastapi import FastAPI, HTTPException
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from pydantic import BaseModel
7
  from transformers import (
8
- AutoTokenizer,
9
- AutoModelForSeq2SeqLM,
10
  AutoModelForTokenClassification,
11
  AutoModelForCausalLM,
12
  pipeline
@@ -17,12 +16,9 @@ from bs4 import BeautifulSoup
17
  import nltk
18
  import torch
19
  import pandas as pd
20
- import subprocess
21
- from typing import Dict, Optional
22
- import codecs
23
  from huggingface_hub import hf_hub_download
 
24
 
25
-
26
  # Initialize FastAPI app
27
  app = FastAPI()
28
 
@@ -56,31 +52,31 @@ def load_models():
56
  """Initialize all required models"""
57
  try:
58
  print("Loading models...")
59
-
60
  # Set device
61
  device = "cuda" if torch.cuda.is_available() else "cpu"
62
  print(f"Device set to use {device}")
63
-
64
  # Embedding models
65
  models['embedding'] = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
66
  models['cross_encoder'] = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', max_length=512)
67
-
68
  # Translation models
69
  models['ar_to_en_tokenizer'] = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
70
  models['ar_to_en_model'] = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
71
  models['en_to_ar_tokenizer'] = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
72
  models['en_to_ar_model'] = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
73
-
74
  # NER model
75
  models['bio_tokenizer'] = AutoTokenizer.from_pretrained("blaze999/Medical-NER")
76
  models['bio_model'] = AutoModelForTokenClassification.from_pretrained("blaze999/Medical-NER")
77
  models['ner_pipeline'] = pipeline("ner", model=models['bio_model'], tokenizer=models['bio_tokenizer'])
78
-
79
  # LLM model
80
  model_name = "M4-ai/Orca-2.0-Tau-1.8B"
81
  models['llm_tokenizer'] = AutoTokenizer.from_pretrained(model_name)
82
  models['llm_model'] = AutoModelForCausalLM.from_pretrained(model_name)
83
-
84
  print("Models loaded successfully")
85
  return True
86
  except Exception as e:
@@ -89,40 +85,26 @@ def load_models():
89
 
90
 
91
  def load_embeddings() -> Optional[Dict[str, np.ndarray]]:
92
- """Load embeddings from local file or HuggingFace Hub"""
93
  try:
94
- import pickle
95
- import numpy as np
96
- import os
97
- from typing import Dict, Optional
98
-
99
- embeddings_path = 'embeddings.pkl'
100
  if not os.path.exists(embeddings_path):
101
- from huggingface_hub import hf_hub_download
102
  embeddings_path = hf_hub_download(
103
  repo_id=os.environ.get('HF_SPACE_ID', ''),
104
- filename="embeddings.pkl",
105
  repo_type="space"
106
  )
107
 
108
- class ASCIIUnpickler(pickle.Unpickler):
109
- def find_class(self, module, name):
110
- if module == "__main__":
111
- module = "numpy"
112
- return super().find_class(module, name)
113
-
114
- with open(embeddings_path, 'rb') as f:
115
- unpickler = ASCIIUnpickler(f)
116
- embeddings = unpickler.load()
117
-
118
  if not isinstance(embeddings, dict):
119
- return None
120
-
121
- return {k: np.array(v, dtype=np.float32) for k, v in embeddings.items()}
122
-
123
  except Exception as e:
124
  print(f"Error loading embeddings: {e}")
125
  return None
 
126
 
127
  def load_documents_data():
128
  """Load document data with error handling"""
 
1
  import os
 
2
  import numpy as np
3
  from fastapi import FastAPI, HTTPException
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from pydantic import BaseModel
6
  from transformers import (
7
+ AutoTokenizer,
8
+ AutoModelForSeq2SeqLM,
9
  AutoModelForTokenClassification,
10
  AutoModelForCausalLM,
11
  pipeline
 
16
  import nltk
17
  import torch
18
  import pandas as pd
 
 
 
19
  from huggingface_hub import hf_hub_download
20
+ from safetensors.torch import load_file # Import Safetensors loader
21
 
 
22
  # Initialize FastAPI app
23
  app = FastAPI()
24
 
 
52
  """Initialize all required models"""
53
  try:
54
  print("Loading models...")
55
+
56
  # Set device
57
  device = "cuda" if torch.cuda.is_available() else "cpu"
58
  print(f"Device set to use {device}")
59
+
60
  # Embedding models
61
  models['embedding'] = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
62
  models['cross_encoder'] = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', max_length=512)
63
+
64
  # Translation models
65
  models['ar_to_en_tokenizer'] = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
66
  models['ar_to_en_model'] = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
67
  models['en_to_ar_tokenizer'] = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
68
  models['en_to_ar_model'] = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
69
+
70
  # NER model
71
  models['bio_tokenizer'] = AutoTokenizer.from_pretrained("blaze999/Medical-NER")
72
  models['bio_model'] = AutoModelForTokenClassification.from_pretrained("blaze999/Medical-NER")
73
  models['ner_pipeline'] = pipeline("ner", model=models['bio_model'], tokenizer=models['bio_tokenizer'])
74
+
75
  # LLM model
76
  model_name = "M4-ai/Orca-2.0-Tau-1.8B"
77
  models['llm_tokenizer'] = AutoTokenizer.from_pretrained(model_name)
78
  models['llm_model'] = AutoModelForCausalLM.from_pretrained(model_name)
79
+
80
  print("Models loaded successfully")
81
  return True
82
  except Exception as e:
 
85
 
86
 
87
  def load_embeddings() -> Optional[Dict[str, np.ndarray]]:
88
+ """Load embeddings from Safetensors file"""
89
  try:
90
+ embeddings_path = 'embeddings.safetensors'
 
 
 
 
 
91
  if not os.path.exists(embeddings_path):
 
92
  embeddings_path = hf_hub_download(
93
  repo_id=os.environ.get('HF_SPACE_ID', ''),
94
+ filename="embeddings.safetensors",
95
  repo_type="space"
96
  )
97
 
98
+ embeddings = load_file(embeddings_path)
 
 
 
 
 
 
 
 
 
99
  if not isinstance(embeddings, dict):
100
+ raise ValueError("Invalid format for embeddings in Safetensors file.")
101
+
102
+ # Convert to dictionary with numpy arrays
103
+ return {k: tensor.numpy() for k, tensor in embeddings.items()}
104
  except Exception as e:
105
  print(f"Error loading embeddings: {e}")
106
  return None
107
+
108
 
109
  def load_documents_data():
110
  """Load document data with error handling"""