Christopher Román Jaimes commited on
Commit ·
7f254c6
1
Parent(s): 2c6872f
feat: add bert model to predict house levels.
Browse files9 unmodified lines1011121314151617181920168 unmodified lines18919019119219319421 unmodified lines2162172182192202212229 unmodified linesimport gradio as gr# GLiNER Modelfrom gliner import GLiNER# Load Modelmodel = GLiNER.from_pretrained("chris32/gliner_multi_pii_real_state-v2")model.eval()# Global Variables: For Post Cleaning InferencesYEAR_OF_REMODELING_LIMIT = 100CURRENT_YEAR = int(datetime.date.today().year)168 unmodified lines'NOMBRE_DESARROLLO': 0.9,}def generate_answer(text):labels = ['SUPERFICIE_JARDIN',21 unmodified linesentity_prediction_cleaned = clean_prediction(entities_formatted, feature_name, threshols_dict, clean_functions_dict)if isinstance(entity_prediction_cleaned, str) or isinstance(entity_prediction_cleaned, int):entities_cleaned[feature_name] = entity_prediction_cleanedresult_json = json.dumps(entities_cleaned, indent = 4, ensure_ascii = False)return result_json + "\n \n" + json.dumps(entities_formatted, indent = 4, ensure_ascii = False)9 unmodified lines1011121314151617181920212223242526168 unmodified lines19519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323421 unmodified lines2562572582592602612622632642652662672682692702719 unmodified linesimport gradio as gr# GLiNER Modelfrom gliner import GLiNER# Transformersfrom transformers import pipeline# Load GLiNER Modelmodel = GLiNER.from_pretrained("chris32/gliner_multi_pii_real_state-v2")model.eval()# BERT Modelmodel_name = "chris32/distilbert-base-spanish-uncased-finetuned-text-intelligence"pipe = pipeline(model = model_name, device = "cpu")# Global Variables: For Post Cleaning InferencesYEAR_OF_REMODELING_LIMIT = 100CURRENT_YEAR = int(datetime.date.today().year)168 unmodified lines'NOMBRE_DESARROLLO': 0.9,}label_names_dict = {'LABEL_0': None,'LABEL_1': 1,'LABEL_2': 2,'LABEL_3': 3,}BERT_SCORE_LIMIT = 0.98def extract_max_label_score(probabilities):# Find the dictionary with the maximum scoremax_item = max(probabilities, key=lambda x: x['score'])# Extract the label and the scorelabel = max_item['label']score = max_item['score']return label, scoredef clean_prediction_bert(label, score):if score > BERT_SCORE_LIMIT:label_formatted = label_names_dict.get(label, None)return label_formattedelse:return None# BERT Inference Configpipe_config = {"batch_size": 8,"truncation": True,"max_length": 250,"add_special_tokens": True,"return_all_scores": True,"padding": True,}def generate_answer(text):labels = ['SUPERFICIE_JARDIN',21 unmodified linesentity_prediction_cleaned = clean_prediction(entities_formatted, feature_name, threshols_dict, clean_functions_dict)if isinstance(entity_prediction_cleaned, str) or isinstance(entity_prediction_cleaned, int):entities_cleaned[feature_name] = entity_prediction_cleaned# BERT Inferencepredictions = pipe([text], **pipe_config)# Format Predictionlabel, score = extract_max_label_score(predictions[0])prediction_cleaned = clean_prediction_bert(label, score)if isinstance(prediction_cleaned, int):entities_cleaned["NIVELES_CASA"] = prediction_cleanedresult_json = json.dumps(entities_cleaned, indent = 4, ensure_ascii = False)return result_json + "\n \n" + json.dumps(entities_formatted, indent = 4, ensure_ascii = False)
app.py+51-2