Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Configura tu cliente de modelo de Hugging Face
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
| 8 |
+
# Tu clave de API de Google Custom Search
|
| 9 |
+
GOOGLE_API_KEY = "AIzaSyDI48Q_Ez8-KXQ6Dfe_r7JyOkk-dloER0I"
|
| 10 |
+
# Tu ID de motor de b煤squeda
|
| 11 |
+
SEARCH_ENGINE_ID = "030a88810b398467c"
|
| 12 |
|
| 13 |
+
def web_search(query):
|
| 14 |
+
# Realiza la b煤squeda en Google
|
| 15 |
+
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={GOOGLE_API_KEY}&cx={SEARCH_ENGINE_ID}"
|
| 16 |
+
response = requests.get(url)
|
| 17 |
+
results = response.json()
|
| 18 |
+
|
| 19 |
+
# Devuelve un resumen de los primeros resultados
|
| 20 |
+
search_results = []
|
| 21 |
+
for item in results.get("items", []):
|
| 22 |
+
title = item.get("title", "No title")
|
| 23 |
+
link = item.get("link", "")
|
| 24 |
+
snippet = item.get("snippet", "")
|
| 25 |
+
search_results.append(f"{title}: {snippet} ({link})")
|
| 26 |
+
return "\n".join(search_results) # Devuelve los resultados como texto
|
| 27 |
|
| 28 |
+
# Define la funci贸n del chatbot con navegaci贸n web
|
| 29 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 30 |
+
# Prepara el contexto de la conversaci贸n para el modelo
|
| 31 |
+
messages = [{"role": "system", "content": system_message}]
|
| 32 |
for val in history:
|
| 33 |
if val[0]:
|
| 34 |
messages.append({"role": "user", "content": val[0]})
|
| 35 |
if val[1]:
|
| 36 |
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
|
| 38 |
+
# Realiza la b煤squeda en la web
|
| 39 |
+
search_summary = web_search(message)
|
| 40 |
+
|
| 41 |
+
# Incluye los resultados de la b煤squeda en el contexto para el modelo
|
| 42 |
+
messages.append({"role": "system", "content": f"Search results:\n{search_summary}"})
|
| 43 |
+
|
| 44 |
+
# Genera la respuesta del modelo
|
| 45 |
+
response = client.text_completion(
|
| 46 |
+
prompt=message, max_tokens=max_tokens, temperature=temperature, top_p=top_p
|
| 47 |
+
)
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
return response
|
|
|
|
| 50 |
|
| 51 |
+
# Interfaz de Gradio
|
| 52 |
+
with gr.Blocks() as demo:
|
| 53 |
+
chatbot = gr.Chatbot()
|
| 54 |
+
msg = gr.Textbox()
|
| 55 |
+
clear = gr.Button("Clear")
|
| 56 |
|
| 57 |
+
def chat_interface(user_message, history=[]):
|
| 58 |
+
output = respond(
|
| 59 |
+
user_message, history, "You are a helpful assistant.", 200, 0.7, 0.9
|
| 60 |
+
)
|
| 61 |
+
history.append((user_message, output))
|
| 62 |
+
return history, chatbot.update(history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
msg.submit(chat_interface, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
| 65 |
+
clear.click(lambda: [], None, chatbot)
|
| 66 |
|
| 67 |
+
demo.launch()
|
|
|