Spaces:
Sleeping
Sleeping
VFinal-v1
Browse files
app.py
CHANGED
@@ -15,18 +15,12 @@ https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
15 |
SERPER_API_KEY = os.getenv("SERPER_API_KEY")
|
16 |
|
17 |
def do_websearch(query: str) -> str:
|
18 |
-
"""
|
19 |
-
Llama a serper.dev para hacer la búsqueda en Google y devolver
|
20 |
-
un texto resumido de los resultados.
|
21 |
-
"""
|
22 |
if not SERPER_API_KEY:
|
23 |
return "(SERPER_API_KEY no está configurado)"
|
24 |
|
25 |
url = "https://google.serper.dev/search"
|
26 |
-
headers = {
|
27 |
-
"X-API-KEY": SERPER_API_KEY,
|
28 |
-
"Content-Type": "application/json",
|
29 |
-
}
|
30 |
payload = {"q": query}
|
31 |
|
32 |
try:
|
@@ -35,7 +29,6 @@ def do_websearch(query: str) -> str:
|
|
35 |
except Exception as e:
|
36 |
return f"(Error al llamar a serper.dev: {e})"
|
37 |
|
38 |
-
# Se espera un campo 'organic' con resultados
|
39 |
if "organic" not in data:
|
40 |
return "No se encontraron resultados en serper.dev."
|
41 |
|
@@ -51,17 +44,13 @@ def do_websearch(query: str) -> str:
|
|
51 |
|
52 |
return "\n".join(text)
|
53 |
|
54 |
-
|
55 |
# ----------------------------------------------------------------
|
56 |
# CONFIGURACIÓN DEL MODELO (AHORA CON TOKEN)
|
57 |
# ----------------------------------------------------------------
|
58 |
-
# Cargamos el token desde la variable de entorno 'HF_API_TOKEN'
|
59 |
hf_api_token = os.getenv("HF_API_TOKEN")
|
60 |
-
|
61 |
-
# Usamos el modelo meta-llama/Llama-3.1-8B-Instruct con acceso a tu cuenta
|
62 |
client = InferenceClient(
|
63 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
64 |
-
token=hf_api_token
|
65 |
)
|
66 |
|
67 |
def respond(
|
@@ -71,7 +60,7 @@ def respond(
|
|
71 |
max_tokens,
|
72 |
temperature,
|
73 |
top_p,
|
74 |
-
use_search # <--
|
75 |
):
|
76 |
"""
|
77 |
- system_message: Texto del rol "system"
|
@@ -80,14 +69,12 @@ def respond(
|
|
80 |
- use_search: booleano que indica si se habilita la búsqueda en serper
|
81 |
"""
|
82 |
|
83 |
-
#
|
84 |
-
# (2) Se mezclan los resultados en primer lugar + el mensaje del usuario en segundo lugar
|
85 |
if use_search:
|
86 |
web_info = do_websearch(message)
|
87 |
-
# "En primer lugar el resultado del scraper y luego el input del usuario"
|
88 |
message = f"Información de la web:\n{web_info}\n\nPregunta del usuario:\n{message}"
|
89 |
|
90 |
-
# Construimos la
|
91 |
messages = [{"role": "system", "content": system_message}]
|
92 |
for user_txt, assistant_txt in history:
|
93 |
if user_txt:
|
@@ -95,7 +82,7 @@ def respond(
|
|
95 |
if assistant_txt:
|
96 |
messages.append({"role": "assistant", "content": assistant_txt})
|
97 |
|
98 |
-
# Añadimos
|
99 |
messages.append({"role": "user", "content": message})
|
100 |
|
101 |
# Llamamos a la API con streaming
|
@@ -103,36 +90,27 @@ def respond(
|
|
103 |
for chunk in client.chat_completion(
|
104 |
messages=messages,
|
105 |
max_tokens=max_tokens,
|
106 |
-
stream=True,
|
107 |
temperature=temperature,
|
108 |
top_p=top_p,
|
|
|
109 |
):
|
110 |
token = chunk.choices[0].delta.get("content", "")
|
111 |
response_text += token
|
112 |
yield response_text
|
113 |
|
114 |
-
|
115 |
# ----------------------------------------------------------------
|
116 |
# CONFIGURACIÓN DE LA INTERFAZ
|
117 |
# ----------------------------------------------------------------
|
118 |
-
#
|
119 |
-
tailwind_toggle_classes = [
|
120 |
-
"inline-flex",
|
121 |
-
"items-center",
|
122 |
-
"bg-blue-500",
|
123 |
-
"hover:bg-blue-700",
|
124 |
-
"text-white",
|
125 |
-
"font-bold",
|
126 |
-
"py-1",
|
127 |
-
"px-2",
|
128 |
-
"rounded",
|
129 |
-
"cursor-pointer"
|
130 |
-
]
|
131 |
-
|
132 |
-
# ChatInterface, con un input Checkbox para "🌐 Búsqueda"
|
133 |
demo = gr.ChatInterface(
|
134 |
fn=respond,
|
135 |
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
gr.Textbox(
|
137 |
value=(
|
138 |
"Eres Juan, un asistente virtual en español. "
|
@@ -143,6 +121,7 @@ demo = gr.ChatInterface(
|
|
143 |
),
|
144 |
label="Mensaje del sistema",
|
145 |
),
|
|
|
146 |
gr.Slider(
|
147 |
minimum=1,
|
148 |
maximum=2048,
|
@@ -164,12 +143,6 @@ demo = gr.ChatInterface(
|
|
164 |
step=0.05,
|
165 |
label="Top-p (muestreo por núcleo)",
|
166 |
),
|
167 |
-
# Un checkbox para habilitar "búsqueda web" (use_search)
|
168 |
-
gr.Checkbox(
|
169 |
-
value=False,
|
170 |
-
label="🌐 Búsqueda",
|
171 |
-
elem_classes=tailwind_toggle_classes
|
172 |
-
),
|
173 |
],
|
174 |
)
|
175 |
|
|
|
15 |
SERPER_API_KEY = os.getenv("SERPER_API_KEY")
|
16 |
|
17 |
def do_websearch(query: str) -> str:
|
18 |
+
""" Llama a serper.dev para hacer la búsqueda en Google y devuelve texto. """
|
|
|
|
|
|
|
19 |
if not SERPER_API_KEY:
|
20 |
return "(SERPER_API_KEY no está configurado)"
|
21 |
|
22 |
url = "https://google.serper.dev/search"
|
23 |
+
headers = {"X-API-KEY": SERPER_API_KEY, "Content-Type": "application/json"}
|
|
|
|
|
|
|
24 |
payload = {"q": query}
|
25 |
|
26 |
try:
|
|
|
29 |
except Exception as e:
|
30 |
return f"(Error al llamar a serper.dev: {e})"
|
31 |
|
|
|
32 |
if "organic" not in data:
|
33 |
return "No se encontraron resultados en serper.dev."
|
34 |
|
|
|
44 |
|
45 |
return "\n".join(text)
|
46 |
|
|
|
47 |
# ----------------------------------------------------------------
|
48 |
# CONFIGURACIÓN DEL MODELO (AHORA CON TOKEN)
|
49 |
# ----------------------------------------------------------------
|
|
|
50 |
hf_api_token = os.getenv("HF_API_TOKEN")
|
|
|
|
|
51 |
client = InferenceClient(
|
52 |
model="meta-llama/Llama-3.1-8B-Instruct",
|
53 |
+
token=hf_api_token
|
54 |
)
|
55 |
|
56 |
def respond(
|
|
|
60 |
max_tokens,
|
61 |
temperature,
|
62 |
top_p,
|
63 |
+
use_search # <-- Checkbox en 1er lugar en additional_inputs
|
64 |
):
|
65 |
"""
|
66 |
- system_message: Texto del rol "system"
|
|
|
69 |
- use_search: booleano que indica si se habilita la búsqueda en serper
|
70 |
"""
|
71 |
|
72 |
+
# Si use_search es True, primero el contenido web y luego el input
|
|
|
73 |
if use_search:
|
74 |
web_info = do_websearch(message)
|
|
|
75 |
message = f"Información de la web:\n{web_info}\n\nPregunta del usuario:\n{message}"
|
76 |
|
77 |
+
# Construimos la conversación
|
78 |
messages = [{"role": "system", "content": system_message}]
|
79 |
for user_txt, assistant_txt in history:
|
80 |
if user_txt:
|
|
|
82 |
if assistant_txt:
|
83 |
messages.append({"role": "assistant", "content": assistant_txt})
|
84 |
|
85 |
+
# Añadimos turno nuevo
|
86 |
messages.append({"role": "user", "content": message})
|
87 |
|
88 |
# Llamamos a la API con streaming
|
|
|
90 |
for chunk in client.chat_completion(
|
91 |
messages=messages,
|
92 |
max_tokens=max_tokens,
|
|
|
93 |
temperature=temperature,
|
94 |
top_p=top_p,
|
95 |
+
stream=True
|
96 |
):
|
97 |
token = chunk.choices[0].delta.get("content", "")
|
98 |
response_text += token
|
99 |
yield response_text
|
100 |
|
|
|
101 |
# ----------------------------------------------------------------
|
102 |
# CONFIGURACIÓN DE LA INTERFAZ
|
103 |
# ----------------------------------------------------------------
|
104 |
+
# Subimos la casilla de verificación (checkbox) de websearch a la parte superior
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
demo = gr.ChatInterface(
|
106 |
fn=respond,
|
107 |
additional_inputs=[
|
108 |
+
# 1) Checkbox de websearch
|
109 |
+
gr.Checkbox(
|
110 |
+
value=False,
|
111 |
+
label="🌐 Búsqueda",
|
112 |
+
),
|
113 |
+
# 2) Texto system
|
114 |
gr.Textbox(
|
115 |
value=(
|
116 |
"Eres Juan, un asistente virtual en español. "
|
|
|
121 |
),
|
122 |
label="Mensaje del sistema",
|
123 |
),
|
124 |
+
# 3) Sliders
|
125 |
gr.Slider(
|
126 |
minimum=1,
|
127 |
maximum=2048,
|
|
|
143 |
step=0.05,
|
144 |
label="Top-p (muestreo por núcleo)",
|
145 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
],
|
147 |
)
|
148 |
|