Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,108 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
def respond(
|
| 6 |
-
message,
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=
|
| 56 |
-
value=0.
|
| 57 |
-
step=0.
|
| 58 |
-
label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
),
|
| 60 |
],
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
| 4 |
def respond(
|
| 5 |
+
message,
|
| 6 |
+
history: list[dict[str, str]],
|
| 7 |
+
system_message,
|
| 8 |
+
max_tokens,
|
| 9 |
+
temperature,
|
| 10 |
+
top_p,
|
| 11 |
hf_token: gr.OAuthToken,
|
| 12 |
):
|
| 13 |
+
"""ZeroGPU Optimized Chat - microsoft/DialoGPT-medium"""
|
| 14 |
+
client = InferenceClient(
|
| 15 |
+
token=hf_token.token,
|
| 16 |
+
model="microsoft/DialoGPT-medium"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 20 |
messages.extend(history)
|
|
|
|
| 21 |
messages.append({"role": "user", "content": message})
|
| 22 |
+
|
| 23 |
response = ""
|
| 24 |
+
try:
|
| 25 |
+
for chunk in client.chat_completion(
|
| 26 |
+
messages,
|
| 27 |
+
max_tokens=max_tokens,
|
| 28 |
+
stream=True,
|
| 29 |
+
temperature=temperature,
|
| 30 |
+
top_p=top_p,
|
| 31 |
+
):
|
| 32 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 33 |
+
token = chunk.choices[0].delta.content
|
| 34 |
+
response += token
|
| 35 |
+
yield response
|
| 36 |
+
except Exception:
|
| 37 |
+
yield "Error: Modelo no disponible. Verifica ZeroGPU."
|
| 38 |
|
| 39 |
+
# Configuración optimizada para ZeroGPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
chatbot = gr.ChatInterface(
|
| 41 |
respond,
|
| 42 |
type="messages",
|
| 43 |
additional_inputs=[
|
| 44 |
+
gr.Textbox(
|
| 45 |
+
value="Eres un asistente útil y amigable.",
|
| 46 |
+
label="Mensaje del sistema",
|
| 47 |
+
max_lines=3
|
| 48 |
+
),
|
| 49 |
+
gr.Slider(
|
| 50 |
+
minimum=1,
|
| 51 |
+
maximum=256, # Reducido para ZeroGPU
|
| 52 |
+
value=128,
|
| 53 |
+
step=1,
|
| 54 |
+
label="Tokens máximos"
|
| 55 |
+
),
|
| 56 |
gr.Slider(
|
| 57 |
+
minimum=0.1,
|
| 58 |
+
maximum=2.0,
|
| 59 |
+
value=0.7,
|
| 60 |
+
step=0.1,
|
| 61 |
+
label="Temperatura"
|
| 62 |
+
),
|
| 63 |
+
gr.Slider(
|
| 64 |
+
minimum=0.1,
|
| 65 |
+
maximum=1.0,
|
| 66 |
+
value=0.9,
|
| 67 |
+
step=0.05,
|
| 68 |
+
label="Top-p"
|
| 69 |
),
|
| 70 |
],
|
| 71 |
+
title="💬 Chat ZeroGPU",
|
| 72 |
+
description="Chat rápido con ZeroGPU • microsoft/DialoGPT-medium",
|
| 73 |
+
theme="soft"
|
| 74 |
)
|
| 75 |
|
| 76 |
+
# Layout optimizado para Spaces
|
| 77 |
+
with gr.Blocks(
|
| 78 |
+
title="ZeroGPU Chat",
|
| 79 |
+
theme=gr.themes.Soft()
|
| 80 |
+
) as demo:
|
| 81 |
+
gr.Markdown(
|
| 82 |
+
"""
|
| 83 |
+
# 🚀 Chat ZeroGPU
|
| 84 |
+
**Rápido y gratuito** con GPU acelerada
|
| 85 |
+
|
| 86 |
+
*Modelo:* microsoft/DialoGPT-medium
|
| 87 |
+
*Hardware:* ZeroGPU (A10G)
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
chatbot.render()
|
| 94 |
+
with gr.Column(scale=0.3):
|
| 95 |
+
gr.Markdown("### ⚙️ Configuración")
|
| 96 |
+
gr.Markdown("**ZeroGPU activo**")
|
| 97 |
+
gr.Markdown("**Tiempo respuesta:** <1s")
|
| 98 |
+
|
| 99 |
+
gr.Markdown("***Creado para Hugging Face Spaces***")
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch(
|
| 103 |
+
ssr_mode=False,
|
| 104 |
+
show_api=False,
|
| 105 |
+
share=True,
|
| 106 |
+
server_name="0.0.0.0",
|
| 107 |
+
server_port=7860
|
| 108 |
+
)
|