Spaces:
Sleeping
Sleeping
ModificacionesV1
Browse files- app.py +81 -0
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
@@ -62,3 +63,83 @@ demo = gr.ChatInterface(
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|
| 66 |
+
"""
|
| 67 |
+
import os
|
| 68 |
+
import gradio as gr
|
| 69 |
+
from huggingface_hub import InferenceClient
|
| 70 |
+
|
| 71 |
+
"""
|
| 72 |
+
For more information on `huggingface_hub` Inference API support, please check the docs:
|
| 73 |
+
https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
# Actualizamos la referencia al modelo para usar meta-llama/Llama-3.1-8B-Instruct
|
| 77 |
+
# Si el modelo requiere acceso mediante token, define HF_API_TOKEN como variable de entorno
|
| 78 |
+
# en tu Space o en tu entorno local (huggingface-cli login).
|
| 79 |
+
hf_api_token = os.getenv("HF_API_TOKEN") # opcional si lo necesitas
|
| 80 |
+
client = InferenceClient(
|
| 81 |
+
model="meta-llama/Llama-3.1-8B-Instruct",
|
| 82 |
+
token=hf_api_token
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def respond(
|
| 86 |
+
message,
|
| 87 |
+
history: list[tuple[str, str]],
|
| 88 |
+
system_message,
|
| 89 |
+
max_tokens,
|
| 90 |
+
temperature,
|
| 91 |
+
top_p,
|
| 92 |
+
):
|
| 93 |
+
"""
|
| 94 |
+
Reconstruimos la conversación como una lista de mensajes:
|
| 95 |
+
- El primer mensaje es de tipo "system" con el contenido de 'system_message'
|
| 96 |
+
- 'history' contiene pares (usuario, asistente)
|
| 97 |
+
- 'message' es lo que el usuario introduce en este turno
|
| 98 |
+
"""
|
| 99 |
+
messages = [{"role": "system", "content": system_message}]
|
| 100 |
+
|
| 101 |
+
for val in history:
|
| 102 |
+
if val[0]:
|
| 103 |
+
messages.append({"role": "user", "content": val[0]})
|
| 104 |
+
if val[1]:
|
| 105 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 106 |
+
|
| 107 |
+
messages.append({"role": "user", "content": message})
|
| 108 |
+
|
| 109 |
+
response = ""
|
| 110 |
+
|
| 111 |
+
# Llamamos a chat_completion con streaming: vamos recibiendo tokens y construyendo la respuesta
|
| 112 |
+
for msg_chunk in client.chat_completion(
|
| 113 |
+
messages,
|
| 114 |
+
max_tokens=max_tokens,
|
| 115 |
+
stream=True,
|
| 116 |
+
temperature=temperature,
|
| 117 |
+
top_p=top_p,
|
| 118 |
+
):
|
| 119 |
+
token = msg_chunk.choices[0].delta.get("content", "")
|
| 120 |
+
response += token
|
| 121 |
+
yield response
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
"""
|
| 125 |
+
For information on how to customize the ChatInterface, peruse the gradio docs:
|
| 126 |
+
https://www.gradio.app/docs/chatinterface
|
| 127 |
+
"""
|
| 128 |
+
demo = gr.ChatInterface(
|
| 129 |
+
respond,
|
| 130 |
+
additional_inputs=[
|
| 131 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 132 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 133 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 134 |
+
gr.Slider(
|
| 135 |
+
minimum=0.1,
|
| 136 |
+
maximum=1.0,
|
| 137 |
+
value=0.95,
|
| 138 |
+
step=0.05,
|
| 139 |
+
label="Top-p (nucleus sampling)",
|
| 140 |
+
),
|
| 141 |
+
],
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
-
|
|
|
|
|
|
| 1 |
+
gradio==5.0.1
|
| 2 |
+
huggingface_hub==0.22.2
|