-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
59 lines (48 loc) · 1.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import flask
from flask import Flask, render_template, request, jsonify
import numpy as np
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from tokenizer import BytePairEncodingTokenizer, get_gpt2_tokenization_pattern
app = Flask(__name__)
tokenizer = BytePairEncodingTokenizer(vocab_size=450)
try:
tokenizer.train_from_file("read.txt")
except FileNotFoundError:
print("Warning: Sample training file 'read.txt' not found. Tokenizer will have minimal training.")
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/tokenize', methods=['POST'])
def tokenize():
data = request.json
text = data.get('text', '')
if not text:
return jsonify({"error": "No text provided"}), 400
try:
encoded_tokens = tokenizer.encode(text)
decoded_text = tokenizer.decode(encoded_tokens)
return jsonify({
"original_text": text,
"encoded_tokens": encoded_tokens,
"decoded_text": decoded_text
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/decode', methods=['POST'])
def decode():
data = request.json
tokens = data.get('tokens', [])
if not tokens:
return jsonify({"error": "No tokens provided"}), 400
try:
decoded_text = tokenizer.decode(tokens)
return jsonify({
"tokens": tokens,
"decoded_text": decoded_text
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)