Update README.md
#1
by
Phase-RCS
- opened
README.md
CHANGED
@@ -1,3 +1,47 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
from flask import Flask, render_template, request, jsonify
|
5 |
+
|
6 |
+
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
import torch
|
9 |
+
|
10 |
+
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained("Davlan/xlm-roberta-base-finetuned-shona")
|
12 |
+
model = AutoModelForCausalLM.from_pretrained("Davlan/xlm-roberta-base-finetuned-shona")
|
13 |
+
|
14 |
+
|
15 |
+
app = Flask(__name__)
|
16 |
+
|
17 |
+
@app.route("/")
|
18 |
+
def index():
|
19 |
+
return render_template('chat.html')
|
20 |
+
|
21 |
+
|
22 |
+
@app.route("/get", methods=["GET", "POST"])
|
23 |
+
def chat():
|
24 |
+
msg = request.form["msg"]
|
25 |
+
input = msg
|
26 |
+
return get_Chat_response(input)
|
27 |
+
|
28 |
+
|
29 |
+
def get_Chat_response(text):
|
30 |
+
|
31 |
+
# Let's chat for 5 lines
|
32 |
+
for step in range(5):
|
33 |
+
# encode the new user input, add the eos_token and return a tensor in Pytorch
|
34 |
+
new_user_input_ids = tokenizer.encode(str(text) + tokenizer.eos_token, return_tensors='pt')
|
35 |
+
|
36 |
+
# append the new user input tokens to the chat history
|
37 |
+
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
|
38 |
+
|
39 |
+
# generated a response while limiting the total chat history to 1000 tokens,
|
40 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
41 |
+
|
42 |
+
# pretty print last ouput tokens from bot
|
43 |
+
return tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
44 |
+
|
45 |
+
|
46 |
+
if __name__ == '__main__':
|
47 |
+
app.run()
|