CaioXapelaum commited on
Commit
86573b5
1 Parent(s): 6dc4c3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -1,24 +1,33 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer
 
3
 
4
  # Function to load and tokenize the message using the selected tokenizer
5
- def tokenize(message, history, tokenizer_name):
 
6
  try:
7
  tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
8
  return str(tokenizer.encode(message))
9
  except Exception as e:
10
  return f"Error loading tokenizer: {str(e)}"
11
 
 
 
 
 
 
 
12
  # Gradio interface
13
- def create_chat_interface():
14
- with gr.Blocks() as demo:
15
- tokenizer_input = gr.Textbox(placeholder="Enter the tokenizer name (e.g., gpt2, bert-base-uncased)", label="Tokenizer Name")
16
- chatbot = gr.ChatInterface(fn=tokenize, inputs=["text", "state", tokenizer_input])
17
- demo.append(tokenizer_input)
18
- demo.append(chatbot)
19
- return demo
 
 
 
 
20
 
21
- # Launch the interface
22
- if __name__ == "__main__":
23
- demo = create_chat_interface()
24
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer
3
+ import spaces
4
 
5
  # Function to load and tokenize the message using the selected tokenizer
6
+ @spaces.GPU
7
+ def tokenize(message, tokenizer_name):
8
  try:
9
  tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
10
  return str(tokenizer.encode(message))
11
  except Exception as e:
12
  return f"Error loading tokenizer: {str(e)}"
13
 
14
+ # Wrapper function to handle chat interaction
15
+ def chat(message, history, tokenizer_name):
16
+ tokenized_message = tokenize(message, tokenizer_name)
17
+ history.append((message, tokenized_message))
18
+ return history, history
19
+
20
  # Gradio interface
21
+ with gr.Blocks() as demo:
22
+ tokenizer_input = gr.Textbox(placeholder="Enter the tokenizer name (e.g., gpt2, bert-base-uncased)", label="Tokenizer Name")
23
+ chatbot = gr.Chatbot()
24
+ msg = gr.Textbox()
25
+ clear = gr.Button("Clear")
26
+
27
+ def clear_history():
28
+ return [], []
29
+
30
+ msg.submit(chat, inputs=[msg, chatbot, tokenizer_input], outputs=[chatbot, chatbot])
31
+ clear.click(clear_history, outputs=[chatbot, chatbot])
32
 
33
+ demo.launch()