Spaces:
Runtime error
Runtime error
Thanks Bing AI
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
2 |
+
import torch
|
3 |
+
|
4 |
+
# Load the model and tokenizer
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("jfernandez/cebfil-roberta")
|
6 |
+
model = AutoModelForMaskedLM.from_pretrained("jfernandez/cebfil-roberta")
|
7 |
+
|
8 |
+
# Define a function to generate responses
|
9 |
+
def generate_response(text):
|
10 |
+
# Add a mask token at the end of the text
|
11 |
+
text = text + " <mask>"
|
12 |
+
# Tokenize the text and get the input ids
|
13 |
+
inputs = tokenizer(text, return_tensors="pt")
|
14 |
+
input_ids = inputs["input_ids"]
|
15 |
+
# Get the logits from the model
|
16 |
+
outputs = model(**inputs)
|
17 |
+
logits = outputs.logits
|
18 |
+
# Get the most likely token id for the mask
|
19 |
+
mask_token_id = tokenizer.mask_token_id
|
20 |
+
mask_token_index = torch.where(input_ids == mask_token_id)[1]
|
21 |
+
token_logits = logits[0, mask_token_index, :]
|
22 |
+
top_5_tokens = torch.topk(token_logits, k=5).indices # get top 5 tokens
|
23 |
+
predicted_tokens = tokenizer.convert_ids_to_tokens(top_5_tokens) # convert ids to tokens
|
24 |
+
|
25 |
+
# Choose one of the predicted tokens randomly and replace the mask with it
|
26 |
+
import random
|
27 |
+
response_token = random.choice(predicted_tokens)
|
28 |
+
response_text = text.replace("<mask>", response_token)
|
29 |
+
|
30 |
+
return response_text
|
31 |
+
|
32 |
+
# Test the function with some examples
|
33 |
+
print(generate_response("Komosta ka"))
|
34 |
+
print(generate_response("Unsa imong pangalan"))
|
35 |
+
print(generate_response("Salamat sa"))
|