Kukedlc commited on
Commit
886c73a
1 Parent(s): 20c1563

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -63
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
- title = """# 🙋🏻‍♂️Welcome to 🌟Tonic's ☯️🧑‍💻Yi-Coder-9B-Chat Demo!"""
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
- # Define the device and model path
16
- device = "cuda" if torch.cuda.is_available() else "cpu"
17
- model_path = "01-ai/Yi-Coder-9B-Chat"
18
 
19
- # Load the tokenizer and model
20
- tokenizer = AutoTokenizer.from_pretrained(model_path)
21
- model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto").eval()
22
 
23
- @spaces.GPU(duration=130)
24
- def generate_code(system_prompt, user_prompt, max_length):
25
- messages = [
26
- {"role": "system", "content": system_prompt},
27
- {"role": "user", "content": user_prompt}
 
 
28
  ]
29
- text = tokenizer.apply_chat_template(
30
- messages,
 
 
 
 
 
 
 
31
  tokenize=False,
32
  add_generation_prompt=True
33
  )
34
- model_inputs = tokenizer([text], return_tensors="pt").to(device)
35
-
36
- generated_ids = model.generate(
37
- model_inputs.input_ids,
38
- max_new_tokens=max_length,
39
- eos_token_id=tokenizer.eos_token_id
40
  )
41
- generated_ids = [
42
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
 
43
  ]
 
 
 
 
44
 
45
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
46
- return response
47
-
48
- def gradio_interface():
49
- with gr.Blocks() as interface:
50
- gr.Markdown(title)
51
- gr.Markdown(description)
52
-
53
- system_prompt_input = gr.Textbox(
54
- label="☯️Yinstruction:",
55
- value="You are a helpful coding assistant. Provide clear and concise code examples.",
56
- lines=2
57
- )
58
- user_prompt_input = gr.Code(
59
- label="🤔Coding Question",
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
- interface = gradio_interface()
78
- interface.queue()
79
- interface.launch()
 
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()