Spaces:
Runtime error
Runtime error
simple modelo
Browse files
app.py
CHANGED
@@ -1,136 +1,23 @@
|
|
1 |
-
import
|
2 |
-
warnings.filterwarnings("ignore", message="Can't initialize NVML")
|
3 |
-
|
4 |
-
import datetime
|
5 |
-
import requests
|
6 |
import gradio as gr
|
7 |
-
import torch
|
8 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
9 |
-
|
10 |
-
# Detectar si hay GPU disponible, de lo contrario usar CPU (-1)
|
11 |
-
device = 0 if torch.cuda.is_available() else -1
|
12 |
-
|
13 |
-
# Cargar el modelo y el tokenizador (se usar谩 CPU si no hay GPU)
|
14 |
-
model_name = "microsoft/Phi-4-mini-instruct"
|
15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
17 |
-
if device == 0:
|
18 |
-
model.to("cuda")
|
19 |
-
|
20 |
-
# Crear un pipeline de generaci贸n de texto utilizando el dispositivo adecuado
|
21 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=device)
|
22 |
-
|
23 |
-
# Funci贸n para obtener las reservaciones de hotel filtradas por t铆tulo
|
24 |
-
def get_hotel_reservations(title_filter):
|
25 |
-
url = "http://127.0.0.1:4000/api/accommodations"
|
26 |
-
try:
|
27 |
-
response = requests.get(url)
|
28 |
-
if response.status_code == 200:
|
29 |
-
data = response.json() # Se espera que 'data' sea una lista de reservaciones
|
30 |
-
summary = "Reservaciones de Hotel:\n\n"
|
31 |
-
found = False
|
32 |
-
for reservation in data:
|
33 |
-
hotel_title = reservation.get("title", "N/A")
|
34 |
-
# Filtrar solo las reservaciones que contengan el filtro en el t铆tulo
|
35 |
-
if title_filter.lower() not in hotel_title.lower():
|
36 |
-
continue
|
37 |
-
|
38 |
-
found = True
|
39 |
-
hotel_id = reservation.get("id", "N/A")
|
40 |
-
|
41 |
-
address = reservation.get("address", {})
|
42 |
-
street = address.get("street", "N/A")
|
43 |
-
zip_code = address.get("zip_code", "N/A")
|
44 |
-
latitude = address.get("latitude", "N/A")
|
45 |
-
longitude = address.get("longitude", "N/A")
|
46 |
-
|
47 |
-
guests = reservation.get("guests", {})
|
48 |
-
adult = guests.get("adult", "N/A")
|
49 |
-
child = guests.get("child", "N/A")
|
50 |
-
|
51 |
-
price = reservation.get("price", "N/A")
|
52 |
-
|
53 |
-
summary += (
|
54 |
-
f"Reservaci贸n {hotel_id}:\n"
|
55 |
-
f" - Hotel: {hotel_title}\n"
|
56 |
-
f" - Direcci贸n: {street}, C贸digo Postal: {zip_code}\n"
|
57 |
-
f" (Latitud: {latitude}, Longitud: {longitude})\n"
|
58 |
-
f" - Hu茅spedes: {adult} adultos, {child} ni帽os\n"
|
59 |
-
f" - Precio: {price}\n\n"
|
60 |
-
)
|
61 |
-
if not found:
|
62 |
-
summary += f"No se encontraron reservaciones que coincidan con el filtro '{title_filter}'.\n"
|
63 |
-
return summary
|
64 |
-
else:
|
65 |
-
return "Lo sentimos, no se pudieron obtener las reservaciones de hotel."
|
66 |
-
except Exception as e:
|
67 |
-
return f"Error al conectar con la API: {e}"
|
68 |
-
|
69 |
-
# Diccionario que asocia nombres de funciones a sus implementaciones
|
70 |
-
function_map = {
|
71 |
-
"get_hotel_reservations": get_hotel_reservations,
|
72 |
-
}
|
73 |
-
|
74 |
-
# Definir la herramienta para function calling (煤til para documentar la funci贸n)
|
75 |
-
tools = [
|
76 |
-
{
|
77 |
-
"type": "function",
|
78 |
-
"function": {
|
79 |
-
"name": "get_hotel_reservations",
|
80 |
-
"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.",
|
81 |
-
"parameters": {
|
82 |
-
"type": "object",
|
83 |
-
"properties": {
|
84 |
-
"title": {
|
85 |
-
"type": "string",
|
86 |
-
"description": "Parte del nombre del hotel o regi贸n, e.g., Medell铆n, Bogot谩, Cartagena"
|
87 |
-
}
|
88 |
-
},
|
89 |
-
"required": ["title"]
|
90 |
-
}
|
91 |
-
}
|
92 |
-
}
|
93 |
-
]
|
94 |
-
|
95 |
-
def process_instruction(instruction: str):
|
96 |
-
"""
|
97 |
-
Env铆a la instrucci贸n al modelo y verifica si se debe llamar a una funci贸n.
|
98 |
-
Se espera que el modelo devuelva una cadena que contenga un llamado a funci贸n en el siguiente formato:
|
99 |
-
"Llamada a funci贸n: get_hotel_reservations(Bogot谩)"
|
100 |
-
"""
|
101 |
-
output = ""
|
102 |
-
result = generator(instruction, max_length=150)[0]['generated_text']
|
103 |
-
output += "Respuesta generada:\n" + result + "\n\n"
|
104 |
-
|
105 |
-
if "Llamada a funci贸n:" in result:
|
106 |
-
try:
|
107 |
-
# Extraer la parte de la cadena que contiene el llamado a funci贸n
|
108 |
-
start_index = result.find("Llamada a funci贸n:") + len("Llamada a funci贸n:")
|
109 |
-
# Se asume que el llamado est谩 en una 煤nica l铆nea, por ejemplo: get_hotel_reservations(Bogot谩)
|
110 |
-
call_str = result[start_index:].strip().split()[0]
|
111 |
-
func_name, params = call_str.split("(", 1)
|
112 |
-
params = params.rstrip(")")
|
113 |
-
func_name = func_name.strip()
|
114 |
-
params = params.strip()
|
115 |
-
|
116 |
-
if func_name in function_map:
|
117 |
-
function_result = function_map[func_name](params)
|
118 |
-
output += "Resultado de la funci贸n:\n" + str(function_result)
|
119 |
-
else:
|
120 |
-
output += "Funci贸n no encontrada: " + func_name
|
121 |
-
except Exception as e:
|
122 |
-
output += "Error al procesar la llamada a funci贸n: " + str(e)
|
123 |
-
else:
|
124 |
-
output += "No se encontr贸 ninguna llamada a funci贸n en la respuesta."
|
125 |
-
return output
|
126 |
|
127 |
-
#
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
)
|
134 |
|
|
|
135 |
if __name__ == "__main__":
|
136 |
-
|
|
|
1 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Cargar un modelo de texto de Hugging Face (puede ser de completado, traducci贸n, etc.)
|
5 |
+
modelo = pipeline("text-generation", model="microsoft/Phi-4-multimodal-instruct") # Cambia el modelo si lo deseas
|
6 |
+
|
7 |
+
# Funci贸n para generar texto
|
8 |
+
def generar_texto(prompt):
|
9 |
+
resultado = modelo(prompt, max_length=100, do_sample=True)
|
10 |
+
return resultado[0]["generated_text"]
|
11 |
+
|
12 |
+
# Interfaz con Gradio
|
13 |
+
interfaz = gr.Interface(
|
14 |
+
fn=generar_texto,
|
15 |
+
inputs=gr.Textbox(label="Introduce tu texto"),
|
16 |
+
outputs=gr.Textbox(label="Texto generado"),
|
17 |
+
title="Generador de Texto con Hugging Face",
|
18 |
+
description="Escribe un prompt y el modelo generar谩 un texto basado en 茅l."
|
19 |
)
|
20 |
|
21 |
+
# Ejecutar la app
|
22 |
if __name__ == "__main__":
|
23 |
+
interfaz.launch()
|