Kukedlc commited on
Commit
25aa566
verified
1 Parent(s): bf5203f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import os
3
+ from llama_cpp import Llama
4
+ from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
5
+ from llama_cpp_agent.providers import LlamaCppPythonProvider
6
+ from llama_cpp_agent.chat_history import BasicChatHistory
7
+ from llama_cpp_agent.chat_history.messages import Roles
8
+ import gradio as gr
9
+ from huggingface_hub import hf_hub_download
10
+
11
+ token_huggingface = os.getenv("HUGGINGFACE_TOKEN")
12
+
13
+ hf_hub_download(
14
+ repo_id="google/gemma-2-2b-it-GGUF",
15
+ filename="2b_it_v2.gguf",
16
+ local_dir="./modelos",
17
+ token=token_huggingface
18
+ )
19
+
20
+ llm = None
21
+
22
+ @spaces.GPU(duration=120)
23
+ def responder(
24
+ mensaje,
25
+ historial: list[tuple[str, str]],
26
+ mensaje_sistema,
27
+ max_tokens,
28
+ temperatura,
29
+ top_p,
30
+ top_k,
31
+ penalizacion_repeticion,
32
+ ):
33
+ plantilla_chat = MessagesFormatterType.GEMMA_2
34
+
35
+ global llm
36
+
37
+ if llm is None:
38
+ llm = Llama(
39
+ model_path="modelos/2b_it_v2.gguf",
40
+ flash_attn=True,
41
+ n_gpu_layers=81,
42
+ n_batch=1024,
43
+ n_ctx=8192,
44
+ )
45
+
46
+ proveedor = LlamaCppPythonProvider(llm)
47
+
48
+ agente = LlamaCppAgent(
49
+ proveedor,
50
+ system_prompt=f"{mensaje_sistema}",
51
+ predefined_messages_formatter_type=plantilla_chat,
52
+ debug_output=True
53
+ )
54
+
55
+ configuracion = proveedor.get_provider_default_settings()
56
+ configuracion.temperature = temperatura
57
+ configuracion.top_k = top_k
58
+ configuracion.top_p = top_p
59
+ configuracion.max_tokens = max_tokens
60
+ configuracion.repeat_penalty = penalizacion_repeticion
61
+ configuracion.stream = True
62
+
63
+ mensajes = BasicChatHistory()
64
+
65
+ for msj in historial:
66
+ usuario = {
67
+ 'role': Roles.user,
68
+ 'content': msj[0]
69
+ }
70
+ asistente = {
71
+ 'role': Roles.assistant,
72
+ 'content': msj[1]
73
+ }
74
+ mensajes.add_message(usuario)
75
+ mensajes.add_message(asistente)
76
+
77
+ flujo = agente.get_chat_response(
78
+ mensaje,
79
+ llm_sampling_settings=configuracion,
80
+ chat_history=mensajes,
81
+ returns_streaming_generator=True,
82
+ print_output=False
83
+ )
84
+
85
+ salida = ""
86
+ for fragmento in flujo:
87
+ salida += fragmento
88
+ yield salida
89
+
90
+ descripcion = """<p align="center">Chat con Gemma 2B usando llama.cpp</p>
91
+ <p><center>
92
+ <a href="https://huggingface.co/google/gemma-2-2b-it" target="_blank">[Modelo Gemma 2B it]</a>
93
+ <a href="https://huggingface.co/google/gemma-2-2b-it-GGUF" target="_blank">[Modelo Gemma 2B it GGUF]</a>
94
+ </center></p>
95
+ """
96
+
97
+ demo = gr.ChatInterface(
98
+ responder,
99
+ additional_inputs=[
100
+ gr.Textbox(value="Eres un asistente 煤til.", label="Mensaje del sistema"),
101
+ gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Tokens m谩ximos"),
102
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
103
+ gr.Slider(
104
+ minimum=0.1,
105
+ maximum=1.0,
106
+ value=0.95,
107
+ step=0.05,
108
+ label="Top-p",
109
+ ),
110
+ gr.Slider(
111
+ minimum=0,
112
+ maximum=100,
113
+ value=40,
114
+ step=1,
115
+ label="Top-k",
116
+ ),
117
+ gr.Slider(
118
+ minimum=0.0,
119
+ maximum=2.0,
120
+ value=1.1,
121
+ step=0.1,
122
+ label="Penalizaci贸n por repetici贸n",
123
+ ),
124
+ ],
125
+ retry_btn="Reintentar",
126
+ undo_btn="Deshacer",
127
+ clear_btn="Limpiar",
128
+ submit_btn="Enviar",
129
+ title="Chat con Gemma 2B usando llama.cpp",
130
+ description=descripcion,
131
+ chatbot=gr.Chatbot(
132
+ scale=1,
133
+ likeable=False,
134
+ show_copy_button=True
135
+ )
136
+ )
137
+
138
+ if __name__ == "__main__":
139
+ demo.launch()