Spaces:
Sleeping
Sleeping
File size: 14,525 Bytes
35e1586 629c5b2 35e1586 1a71739 b2cd2f5 2c7697c 1a71739 35e1586 1a71739 b2cd2f5 1a71739 35e1586 56b03dc 35e1586 56b03dc 35e1586 56b03dc 35e1586 dc82676 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional, Dict
import pickle
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer, CrossEncoder, util
from bs4 import BeautifulSoup
import os
import nltk
import torch
from transformers import (
AutoTokenizer,
BartForConditionalGeneration,
AutoModelForCausalLM,
AutoModelForSeq2SeqLM,
AutoModelForTokenClassification
)
import pandas as pd
import time
# Define persistent_load to handle persistent IDs
def persistent_load(pers_id):
"""
Handle persistent IDs during unpickling.
"""
if pers_id == "sentence_transformer_model":
try:
# Load a pre-defined SentenceTransformer model
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
return model
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error loading SentenceTransformer model: {e}")
else:
raise HTTPException(status_code=500, detail=f"Unknown persistent ID: {pers_id}")
# Function to load models
def load_models():
try:
# Load embeddings data with custom persistent_load function
with open("models/embeddings.pkl", "rb") as file:
unpickler = pickle.Unpickler(file)
unpickler.persistent_load = persistent_load
global_models.embeddings_data = unpickler.load()
print("Embeddings data loaded successfully.")
except pickle.UnpicklingError as e:
raise HTTPException(status_code=500, detail=f"Unpickling error: {e}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to load models: {e}")
app = FastAPI()
@app.on_event("startup")
async def load_models():
"""Initialize all models and data on startup"""
try:
# Load embedding models
global_models.embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
global_models.cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', max_length=512)
global_models.semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
# Load BART models
global_models.tokenizer = AutoTokenizer.from_pretrained("facebook/bart-base")
global_models.model = BartForConditionalGeneration.from_pretrained("facebook/bart-base")
# Load Orca model
model_name = "M4-ai/Orca-2.0-Tau-1.8B"
global_models.tokenizer_f = AutoTokenizer.from_pretrained(model_name)
global_models.model_f = AutoModelForCausalLM.from_pretrained(model_name)
# Load translation models
global_models.ar_to_en_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
global_models.ar_to_en_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
global_models.en_to_ar_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
global_models.en_to_ar_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
# Load Medical NER models
global_models.bio_tokenizer = AutoTokenizer.from_pretrained("blaze999/Medical-NER")
global_models.bio_model = AutoModelForTokenClassification.from_pretrained("blaze999/Medical-NER")
# Load embeddings data with proper persistent_load handling
try:
with open('embeddings.pkl', 'rb') as file:
unpickler = pickle.Unpickler(file)
unpickler.persistent_load = persistent_load
global_models.embeddings_data = unpickler.load()
except (FileNotFoundError, pickle.UnpicklingError) as e:
print(f"Error loading embeddings data: {e}")
raise HTTPException(status_code=500, detail="Failed to load embeddings data.")
# Load URL mapping data
try:
df = pd.read_excel('finalcleaned_excel_file.xlsx')
global_models.file_name_to_url = {f"article_{index}.html": url for index, url in enumerate(df['Unnamed: 0'])}
except Exception as e:
print(f"Error loading URL mapping data: {e}")
raise HTTPException(status_code=500, detail="Failed to load URL mapping data.")
except Exception as e:
print(f"Error loading models: {e}")
raise HTTPException(status_code=500, detail="Failed to load models.")
@app.get("/")
async def root():
return {"message": "Server is running"}
# Models and data structures to store loaded models
class GlobalModels:
embedding_model = None
cross_encoder = None
semantic_model = None
tokenizer = None
model = None
tokenizer_f = None
model_f = None
ar_to_en_tokenizer = None
ar_to_en_model = None
en_to_ar_tokenizer = None
en_to_ar_model = None
embeddings_data = None
file_name_to_url = None
bio_tokenizer = None
bio_model = None
global_models = GlobalModels()
# Download NLTK data
nltk.download('punkt')
# Pydantic models for request validation
class QueryInput(BaseModel):
query_text: str
language_code: int # 0 for Arabic, 1 for English
query_type: str # "profile" or "question"
previous_qa: Optional[List[Dict[str, str]]] = None
class DocumentResponse(BaseModel):
title: str
url: str
text: str
score: float
@app.on_event("startup")
async def load_models():
"""Initialize all models and data on startup"""
try:
# Load embedding models
global_models.embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
global_models.cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2', max_length=512)
global_models.semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
# Load BART models
global_models.tokenizer = AutoTokenizer.from_pretrained("facebook/bart-base")
global_models.model = BartForConditionalGeneration.from_pretrained("facebook/bart-base")
# Load Orca model
model_name = "M4-ai/Orca-2.0-Tau-1.8B"
global_models.tokenizer_f = AutoTokenizer.from_pretrained(model_name)
global_models.model_f = AutoModelForCausalLM.from_pretrained(model_name)
# Load translation models
global_models.ar_to_en_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
global_models.ar_to_en_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-ar-en")
global_models.en_to_ar_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
global_models.en_to_ar_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-ar")
# Load Medical NER models
global_models.bio_tokenizer = AutoTokenizer.from_pretrained("blaze999/Medical-NER")
global_models.bio_model = AutoModelForTokenClassification.from_pretrained("blaze999/Medical-NER")
# Load embeddings data with better error handling
try:
with open('embeddings.pkl', 'rb') as file:
global_models.embeddings_data = pickle.load(file)
except (FileNotFoundError, pickle.UnpicklingError) as e:
print(f"Error loading embeddings data: {e}")
raise HTTPException(status_code=500, detail="Failed to load embeddings data.")
# Load URL mapping data
try:
df = pd.read_excel('finalcleaned_excel_file.xlsx')
global_models.file_name_to_url = {f"article_{index}.html": url for index, url in enumerate(df['Unnamed: 0'])}
except Exception as e:
print(f"Error loading URL mapping data: {e}")
raise HTTPException(status_code=500, detail="Failed to load URL mapping data.")
except Exception as e:
print(f"Error loading models: {e}")
raise HTTPException(status_code=500, detail="Failed to load models.")
def translate_ar_to_en(text):
try:
inputs = global_models.ar_to_en_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
translated_ids = global_models.ar_to_en_model.generate(
inputs.input_ids,
max_length=512,
num_beams=4,
early_stopping=True
)
translated_text = global_models.ar_to_en_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
return translated_text
except Exception as e:
print(f"Error during Arabic to English translation: {e}")
return None
def translate_en_to_ar(text):
try:
inputs = global_models.en_to_ar_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
translated_ids = global_models.en_to_ar_model.generate(
inputs.input_ids,
max_length=512,
num_beams=4,
early_stopping=True
)
translated_text = global_models.en_to_ar_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
return translated_text
except Exception as e:
print(f"Error during English to Arabic translation: {e}")
return None
def process_query(query_text, language_code):
if language_code == 0:
return translate_ar_to_en(query_text)
return query_text
def embed_query_text(query_text):
return global_models.embedding_model.encode([query_text])
def query_embeddings(query_embedding, n_results=5):
doc_ids = list(global_models.embeddings_data.keys())
doc_embeddings = np.array(list(global_models.embeddings_data.values()))
similarities = cosine_similarity(query_embedding, doc_embeddings).flatten()
top_indices = similarities.argsort()[-n_results:][::-1]
return [(doc_ids[i], similarities[i]) for i in top_indices]
def retrieve_document_texts(doc_ids, folder_path='downloaded_articles'):
texts = []
for doc_id in doc_ids:
file_path = os.path.join(folder_path, doc_id)
try:
with open(file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
text = soup.get_text(separator=' ', strip=True)
texts.append(text)
except FileNotFoundError:
texts.append("")
return texts
def extract_entities(text):
inputs = global_models.bio_tokenizer(text, return_tensors="pt")
outputs = global_models.bio_model(**inputs)
predictions = torch.argmax(outputs.logits, dim=2)
tokens = global_models.bio_tokenizer.convert_ids_to_tokens(inputs.input_ids[0])
return [tokens[i] for i in range(len(tokens)) if predictions[0][i].item() != 0]
def create_prompt(question, passage):
return f"""
As a medical expert, you are required to answer the following question based only on the provided passage.
Do not include any information not present in the passage. Your response should directly reflect the content
of the passage. Maintain accuracy and relevance to the provided information.
Passage: {passage}
Question: {question}
Answer:
"""
def generate_answer(prompt, max_length=860, temperature=0.2):
inputs = global_models.tokenizer_f(prompt, return_tensors="pt", truncation=True)
start_time = time.time()
output_ids = global_models.model_f.generate(
inputs.input_ids,
max_length=max_length,
num_return_sequences=1,
temperature=temperature,
pad_token_id=global_models.tokenizer_f.eos_token_id
)
duration = time.time() - start_time
answer = global_models.tokenizer_f.decode(output_ids[0], skip_special_tokens=True)
return answer, duration
def clean_answer(answer):
answer_part = answer.split("Answer:")[-1].strip()
if not answer_part.endswith('.'):
last_period_index = answer_part.rfind('.')
if last_period_index != -1:
answer_part = answer_part[:last_period_index + 1].strip()
return answer_part
@app.post("/retrieve_documents")
async def retrieve_documents(input_data: QueryInput):
try:
# Process query
processed_query = process_query(input_data.query_text, input_data.language_code)
query_embedding = embed_query_text(processed_query)
results = query_embeddings(query_embedding)
# Get document texts and rerank
document_ids = [doc_id for doc_id, _ in results]
document_texts = retrieve_document_texts(document_ids)
scores = global_models.cross_encoder.predict([(processed_query, doc) for doc in document_texts])
# Prepare response
documents = []
for score, doc_id, text in zip(scores, document_ids, document_texts):
url = global_models.file_name_to_url.get(doc_id, "")
documents.append({
"title": doc_id,
"url": url,
"text": text if input_data.language_code == 1 else translate_en_to_ar(text),
"score": float(score)
})
return {"status": "success", "documents": documents}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/get_answer")
async def get_answer(input_data: QueryInput):
try:
# Process query
processed_query = process_query(input_data.query_text, input_data.language_code)
# Get relevant documents
query_embedding = embed_query_text(processed_query)
results = query_embeddings(query_embedding)
document_ids = [doc_id for doc_id, _ in results]
document_texts = retrieve_document_texts(document_ids)
# Extract entities and create context
entities = extract_entities(processed_query)
context = " ".join(document_texts)
enhanced_context = f"{context}\n\nEntities: {', '.join(entities)}"
# Generate answer
prompt = create_prompt(processed_query, enhanced_context)
answer, duration = generate_answer(prompt)
final_answer = clean_answer(answer)
# Translate if needed
if input_data.language_code == 0:
final_answer = translate_en_to_ar(final_answer)
return {
"status": "success",
"answer": final_answer,
"processing_time": duration
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|