Kukedlc commited on
Commit
67a297b
·
verified ·
1 Parent(s): c98587e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -47
app.py CHANGED
@@ -3,66 +3,77 @@ import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import spaces
5
 
6
- titulo = """# 🤖 Bienvenido al Chatbot con Yi-9B"""
 
 
 
 
 
 
 
7
 
8
- descripcion = """Este chatbot utiliza el modelo Yi de 9B parámetros para generar respuestas.
9
- Puedes mantener una conversación fluida y realizar preguntas sobre diversos temas."""
 
10
 
11
- # Definir el dispositivo y la ruta del modelo
12
- dispositivo = "cuda" if torch.cuda.is_available() else "cpu"
13
- ruta_modelo = "01-ai/Yi-9B-Chat"
14
-
15
- # Cargar el tokenizador y el modelo
16
- tokenizador = AutoTokenizer.from_pretrained(ruta_modelo)
17
- modelo = AutoModelForCausalLM.from_pretrained(ruta_modelo, device_map="auto").eval()
18
 
19
  @spaces.GPU(duration=130)
20
- def generar_respuesta(prompt_sistema, prompt_usuario, max_longitud):
21
- mensajes = [
22
- {"role": "system", "content": prompt_sistema},
23
- {"role": "user", "content": prompt_usuario}
24
  ]
25
- texto = tokenizador.apply_chat_template(
26
- mensajes,
27
  tokenize=False,
28
  add_generation_prompt=True
29
  )
30
- entradas_modelo = tokenizador([texto], return_tensors="pt").to(dispositivo)
31
- ids_generados = modelo.generate(
32
- entradas_modelo.input_ids,
33
- max_new_tokens=max_longitud,
34
- eos_token_id=tokenizador.eos_token_id
 
35
  )
36
- ids_generados = [
37
- output_ids[len(input_ids):] for input_ids, output_ids in zip(entradas_modelo.input_ids, ids_generados)
38
  ]
39
- respuesta = tokenizador.batch_decode(ids_generados, skip_special_tokens=True)[0]
40
- return respuesta
41
 
42
- def interfaz_gradio():
43
- with gr.Blocks() as interfaz:
44
- gr.Markdown(titulo)
45
- gr.Markdown(descripcion)
46
-
47
- prompt_sistema = gr.Textbox(
48
- label="Instrucción del sistema:",
49
- value="Eres un asistente útil y amigable. Proporciona respuestas claras y concisas.",
 
 
 
50
  lines=2
51
  )
52
- prompt_usuario = gr.Textbox(label="Tu mensaje", lines=3)
53
- respuesta = gr.Textbox(label="Respuesta del asistente", lines=10)
54
- max_longitud_slider = gr.Slider(minimum=1, maximum=1000, value=500, label="Longitud máxima de la respuesta")
55
-
56
- boton_generar = gr.Button("Generar respuesta")
57
- boton_generar.click(
58
- generar_respuesta,
59
- inputs=[prompt_sistema, prompt_usuario, max_longitud_slider],
60
- outputs=respuesta
61
  )
62
-
63
- return interfaz
 
 
 
 
 
 
 
 
 
64
 
65
  if __name__ == "__main__":
66
- interfaz = interfaz_gradio()
67
- interfaz.queue()
68
- interfaz.launch()
 
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()