Spaces:
Runtime error
Runtime error
| import warnings | |
| warnings.filterwarnings("ignore", message="Can't initialize NVML") | |
| import datetime | |
| import requests | |
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
| # Detectar si hay GPU disponible, de lo contrario usar CPU (-1) | |
| device = 0 if torch.cuda.is_available() else -1 | |
| # Cargar el modelo y el tokenizador (se usar谩 CPU si no hay GPU) | |
| model_name = "microsoft/Phi-4-mini-instruct" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| if device == 0: | |
| model.to("cuda") | |
| # Crear un pipeline de generaci贸n de texto utilizando el dispositivo adecuado | |
| generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device) | |
| # Funci贸n para obtener las reservaciones de hotel filtradas por t铆tulo | |
| def get_hotel_reservations(title_filter): | |
| url = "http://127.0.0.1:4000/api/accommodations" | |
| try: | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| data = response.json() # Se espera que 'data' sea una lista de reservaciones | |
| summary = "Reservaciones de Hotel:\n\n" | |
| found = False | |
| for reservation in data: | |
| hotel_title = reservation.get("title", "N/A") | |
| # Filtrar solo las reservaciones que contengan el filtro en el t铆tulo | |
| if title_filter.lower() not in hotel_title.lower(): | |
| continue | |
| found = True | |
| hotel_id = reservation.get("id", "N/A") | |
| address = reservation.get("address", {}) | |
| street = address.get("street", "N/A") | |
| zip_code = address.get("zip_code", "N/A") | |
| latitude = address.get("latitude", "N/A") | |
| longitude = address.get("longitude", "N/A") | |
| guests = reservation.get("guests", {}) | |
| adult = guests.get("adult", "N/A") | |
| child = guests.get("child", "N/A") | |
| price = reservation.get("price", "N/A") | |
| summary += ( | |
| f"Reservaci贸n {hotel_id}:\n" | |
| f" - Hotel: {hotel_title}\n" | |
| f" - Direcci贸n: {street}, C贸digo Postal: {zip_code}\n" | |
| f" (Latitud: {latitude}, Longitud: {longitude})\n" | |
| f" - Hu茅spedes: {adult} adultos, {child} ni帽os\n" | |
| f" - Precio: {price}\n\n" | |
| ) | |
| if not found: | |
| summary += f"No se encontraron reservaciones que coincidan con el filtro '{title_filter}'.\n" | |
| return summary | |
| else: | |
| return "Lo sentimos, no se pudieron obtener las reservaciones de hotel." | |
| except Exception as e: | |
| return f"Error al conectar con la API: {e}" | |
| # Diccionario que asocia nombres de funciones a sus implementaciones | |
| function_map = { | |
| "get_hotel_reservations": get_hotel_reservations, | |
| } | |
| # Definir la herramienta para function calling (煤til para documentar la funci贸n) | |
| tools = [ | |
| { | |
| "type": "function", | |
| "function": { | |
| "name": "get_hotel_reservations", | |
| "description": "Obtiene una lista de reservaciones de hotel filtradas por un t铆tulo. El par谩metro 'title' permite especificar parte del nombre del hotel o regi贸n.", | |
| "parameters": { | |
| "type": "object", | |
| "properties": { | |
| "title": { | |
| "type": "string", | |
| "description": "Parte del nombre del hotel o regi贸n, e.g., Medell铆n, Bogot谩, Cartagena" | |
| } | |
| }, | |
| "required": ["title"] | |
| } | |
| } | |
| } | |
| ] | |
| def process_instruction(instruction: str): | |
| """ | |
| Env铆a la instrucci贸n al modelo y verifica si se debe llamar a una funci贸n. | |
| Se espera que el modelo devuelva una cadena que contenga un llamado a funci贸n en el siguiente formato: | |
| "Llamada a funci贸n: get_hotel_reservations(Bogot谩)" | |
| """ | |
| output = "" | |
| result = generator(instruction, max_length=150)[0]['generated_text'] | |
| output += "Respuesta generada:\n" + result + "\n\n" | |
| if "Llamada a funci贸n:" in result: | |
| try: | |
| # Extraer la parte de la cadena que contiene el llamado a funci贸n | |
| start_index = result.find("Llamada a funci贸n:") + len("Llamada a funci贸n:") | |
| # Se asume que el llamado est谩 en una 煤nica l铆nea, por ejemplo: get_hotel_reservations(Bogot谩) | |
| call_str = result[start_index:].strip().split()[0] | |
| func_name, params = call_str.split("(", 1) | |
| params = params.rstrip(")") | |
| func_name = func_name.strip() | |
| params = params.strip() | |
| if func_name in function_map: | |
| function_result = function_map[func_name](params) | |
| output += "Resultado de la funci贸n:\n" + str(function_result) | |
| else: | |
| output += "Funci贸n no encontrada: " + func_name | |
| except Exception as e: | |
| output += "Error al procesar la llamada a funci贸n: " + str(e) | |
| else: | |
| output += "No se encontr贸 ninguna llamada a funci贸n en la respuesta." | |
| return output | |
| # Crear una interfaz Gradio para interactuar con el sistema | |
| iface = gr.Interface( | |
| fn=process_instruction, | |
| inputs=gr.Textbox(lines=5, placeholder="Escribe tu instrucci贸n aqu铆...", label="Instrucci贸n"), | |
| outputs="text", | |
| title="Demo de Function Calling con Phi-4-mini-instruct (CPU)" | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |