import gradio as gr from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline import pandas as pd import util import folium # Model names (replace with your actual Hugging Face repo names) MODEL_NAMES = { "BioBERT": "nattkorat/biobert-base-uncased-ner", "SciBERT": "nattkorat/scibert-base-uncased-ner", "BERT": "nattkorat/bert-base-uncased-ner" } # Cache loaded models to avoid reloading every time loaded_models = {} def load_model(model_key): model_name = MODEL_NAMES[model_key] if model_name not in loaded_models: tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForTokenClassification.from_pretrained(model_name) ner = pipeline("ner", model=model, tokenizer=tokenizer, aggregation_strategy="average") loaded_models[model_name] = ner return loaded_models[model_name] def extract_entities(model_choice, text): ner = load_model(model_choice) entities = ner(text) if not entities: return pd.DataFrame(columns=["outbreak", "cases", "deaths", "date", "location", "latitude", "longtitude"]) data = {} for ent in entities: if ent['entity_group'] == 'VIRUS': data['outbreak'] = ent['word'] elif ent['entity_group'] == 'CASES': data['cases'] = ent['word'] elif ent['entity_group'] == 'DEATHS': data['deaths'] = ent['word'] elif ent['entity_group'] == 'DATE': data['date'] = util.parse_date(ent['word']) elif ent['entity_group'] == 'COUNTRY': location_info = util.get_location(ent['word']) data['location'] = location_info['name'] data['latitude'] = location_info['latitude'] data['longtitude'] = location_info['longitude'] m = None if 'latitude' in data: m = folium.Map(location=[data['latitude'], data['longtitude']]) info = f"""