Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
-
import spaces
|
5 |
|
6 |
-
|
7 |
-
description = """Yi-Coder-9B-Chat is a 9B parameter model fine-tuned for coding tasks. This demo showcases its ability to generate code based on your prompts. Yi-Coder is a series of open-source code language models that delivers state-of-the-art coding performance with fewer than 10 billion parameters. Excelling in long-context understanding with a maximum context length of 128K tokens. - Supporting 52 major programming languages:
|
8 |
-
```bash
|
9 |
-
'java', 'markdown', 'python', 'php', 'javascript', 'c++', 'c#', 'c', 'typescript', 'html', 'go', 'java_server_pages', 'dart', 'objective-c', 'kotlin', 'tex', 'swift', 'ruby', 'sql', 'rust', 'css', 'yaml', 'matlab', 'lua', 'json', 'shell', 'visual_basic', 'scala', 'rmarkdown', 'pascal', 'fortran', 'haskell', 'assembly', 'perl', 'julia', 'cmake', 'groovy', 'ocaml', 'powershell', 'elixir', 'clojure', 'makefile', 'coffeescript', 'erlang', 'lisp', 'toml', 'batchfile', 'cobol', 'dockerfile', 'r', 'prolog', 'verilog'
|
10 |
-
```
|
11 |
-
### Join us :
|
12 |
-
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/qdfnvSPcqP) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
13 |
-
"""
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
model_path = "01-ai/Yi-Coder-9B-Chat"
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
28 |
]
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
tokenize=False,
|
32 |
add_generation_prompt=True
|
33 |
)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
max_new_tokens=
|
39 |
-
eos_token_id=
|
40 |
)
|
41 |
-
|
42 |
-
|
|
|
43 |
]
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
gr.
|
51 |
-
gr.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
-
|
59 |
-
|
60 |
-
value="Write a quick sort algorithm in Python.",
|
61 |
-
language="python",
|
62 |
-
lines=15
|
63 |
-
)
|
64 |
-
code_output = gr.Code(label="☯️Yi-Coder-7B", language='python', lines=20, interactive=True)
|
65 |
-
max_length_slider = gr.Slider(minimum=1, maximum=1800, value=650, label="Max Token Length")
|
66 |
-
|
67 |
-
generate_button = gr.Button("Generate Code")
|
68 |
-
generate_button.click(
|
69 |
-
generate_code,
|
70 |
-
inputs=[system_prompt_input, user_prompt_input, max_length_slider],
|
71 |
-
outputs=code_output
|
72 |
-
)
|
73 |
-
|
74 |
-
return interface
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
4 |
|
5 |
+
titulo = """# 🤖 Bienvenido al Chatbot con Yi-9B"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
descripcion = """Este chatbot utiliza el modelo Yi de 9B parámetros para generar respuestas.
|
8 |
+
Puedes mantener una conversación fluida y realizar preguntas sobre diversos temas."""
|
|
|
9 |
|
10 |
+
# Definir el dispositivo y la ruta del modelo
|
11 |
+
dispositivo = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
ruta_modelo = "01-ai/Yi-9B-Chat"
|
13 |
|
14 |
+
# Cargar el tokenizador y el modelo
|
15 |
+
tokenizador = AutoTokenizer.from_pretrained(ruta_modelo)
|
16 |
+
modelo = AutoModelForCausalLM.from_pretrained(ruta_modelo, device_map="auto").eval()
|
17 |
+
|
18 |
+
def generar_respuesta(historial, usuario_input, max_longitud):
|
19 |
+
mensajes = [
|
20 |
+
{"role": "system", "content": "Eres un asistente útil y amigable. Proporciona respuestas claras y concisas."}
|
21 |
]
|
22 |
+
|
23 |
+
for entrada in historial:
|
24 |
+
mensajes.append({"role": "user", "content": entrada[0]})
|
25 |
+
mensajes.append({"role": "assistant", "content": entrada[1]})
|
26 |
+
|
27 |
+
mensajes.append({"role": "user", "content": usuario_input})
|
28 |
+
|
29 |
+
texto = tokenizador.apply_chat_template(
|
30 |
+
mensajes,
|
31 |
tokenize=False,
|
32 |
add_generation_prompt=True
|
33 |
)
|
34 |
+
|
35 |
+
entradas_modelo = tokenizador([texto], return_tensors="pt").to(dispositivo)
|
36 |
+
ids_generados = modelo.generate(
|
37 |
+
entradas_modelo.input_ids,
|
38 |
+
max_new_tokens=max_longitud,
|
39 |
+
eos_token_id=tokenizador.eos_token_id
|
40 |
)
|
41 |
+
|
42 |
+
ids_generados = [
|
43 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(entradas_modelo.input_ids, ids_generados)
|
44 |
]
|
45 |
+
|
46 |
+
respuesta = tokenizador.batch_decode(ids_generados, skip_special_tokens=True)[0]
|
47 |
+
historial.append((usuario_input, respuesta))
|
48 |
+
return historial, ""
|
49 |
|
50 |
+
def interfaz_gradio():
|
51 |
+
with gr.Blocks() as interfaz:
|
52 |
+
gr.Markdown(titulo)
|
53 |
+
gr.Markdown(descripcion)
|
54 |
+
|
55 |
+
chatbot = gr.Chatbot(label="Historial de chat")
|
56 |
+
msg = gr.Textbox(label="Tu mensaje")
|
57 |
+
clear = gr.Button("Limpiar")
|
58 |
+
|
59 |
+
max_longitud_slider = gr.Slider(minimum=1, maximum=1000, value=500, label="Longitud máxima de la respuesta")
|
60 |
+
|
61 |
+
msg.submit(generar_respuesta, [chatbot, msg, max_longitud_slider], [chatbot, msg])
|
62 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
63 |
+
|
64 |
+
return interfaz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
+
interfaz = interfaz_gradio()
|
68 |
+
interfaz.queue()
|
69 |
+
interfaz.launch()
|