Spaces:
Sleeping
Sleeping
CaioXapelaum
commited on
Commit
•
86573b5
1
Parent(s):
6dc4c3e
Update app.py
Browse files
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 |
-
|
|
|
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 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
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()
|
|
|
|
|
|