Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,65 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
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 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
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 |
demo = gr.ChatInterface(
|
47 |
-
|
|
|
|
|
|
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(
|
50 |
-
|
51 |
-
|
52 |
-
|
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 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
3 |
+
from threading import Thread
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
+
# Configuraci贸n del modelo
|
7 |
+
MODEL_NAME = "microsoft/phi-2" # Modelo ligero y eficiente
|
|
|
|
|
8 |
|
9 |
+
# Cargar modelo y tokenizer
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
12 |
+
MODEL_NAME,
|
13 |
+
torch_dtype=torch.float16,
|
14 |
+
device_map="auto"
|
15 |
+
)
|
16 |
|
17 |
+
def generate_response(message, history, system_prompt="Eres un asistente 煤til llamado Gerardo."):
|
18 |
+
# Formatear el historial de chat
|
19 |
+
messages = [{"role": "system", "content": system_prompt}]
|
20 |
+
for user_msg, bot_msg in history:
|
21 |
+
messages.extend([
|
22 |
+
{"role": "user", "content": user_msg},
|
23 |
+
{"role": "assistant", "content": bot_msg}
|
24 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
messages.append({"role": "user", "content": message})
|
26 |
|
27 |
+
# Generar respuesta con streaming
|
28 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
29 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
30 |
+
|
31 |
+
Thread(
|
32 |
+
target=model.generate,
|
33 |
+
kwargs=dict(
|
34 |
+
inputs,
|
35 |
+
streamer=streamer,
|
36 |
+
max_new_tokens=512,
|
37 |
+
temperature=0.7,
|
38 |
+
do_sample=True
|
39 |
+
)
|
40 |
+
).start()
|
41 |
|
42 |
+
# Retornar la respuesta progresivamente
|
43 |
+
response = ""
|
44 |
+
for token in streamer:
|
45 |
response += token
|
46 |
yield response
|
47 |
|
48 |
+
# Interfaz con Gradio
|
|
|
|
|
|
|
49 |
demo = gr.ChatInterface(
|
50 |
+
fn=generate_response,
|
51 |
+
title="馃 Chatbot Gerardo",
|
52 |
+
description="Un asistente IA creado por Gerardo. 隆Preg煤ntame lo que quieras!",
|
53 |
+
examples=["Hola", "驴C贸mo funciona la inteligencia artificial?"],
|
54 |
additional_inputs=[
|
55 |
+
gr.Textbox(
|
56 |
+
"Eres un asistente 煤til llamado Gerardo.",
|
57 |
+
label="Personalidad del bot"
|
58 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
60 |
+
theme="soft"
|
61 |
)
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
65 |
+
|