Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: tokenization service initial implementation #210

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tokenizer_service/example-curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
curl -X POST http://127.0.0.1:5000/granite-7b-lab \
-H "Content-Type: application/json" \
-d '{"text": "This is an example sentence to check tokenization."}'
2 changes: 2 additions & 0 deletions tokenizer_service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask
transformers
37 changes: 37 additions & 0 deletions tokenizer_service/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from flask import Flask, request, jsonify
from transformers import AutoTokenizer

app = Flask(__name__)


granite_tokenizer = AutoTokenizer.from_pretrained("instructlab/granite-7b-lab")
merlinite_tokenizer = AutoTokenizer.from_pretrained("instructlab/merlinite-7b-lab")

def count_tokens(tokenizer, text):
tokens = tokenizer.tokenize(text)
return len(tokens)

# Endpoint for the 'granite-7b-lab' model
@app.route('/granite-7b-lab', methods=['POST'])
def granite_token_count():
data = request.json
if 'text' not in data:
return jsonify({"error": "Text is required"}), 400

text = data['text']
num_tokens = count_tokens(granite_tokenizer, text)
return jsonify({"model": "instructlab/granite-7b-lab", "tokens": num_tokens})

# Endpoint for the 'merlinite' model
@app.route('/merlinite-7b-lab', methods=['POST'])
def merlinite_token_count():
data = request.json
if 'text' not in data:
return jsonify({"error": "Text is required"}), 400

text = data['text']
num_tokens = count_tokens(merlinite_tokenizer, text)
return jsonify({"model": "instructlab/merlinite-7b-lab", "tokens": num_tokens})

if __name__ == '__main__':
app.run(debug=True)
Loading