|
from transformers import pipeline |
|
import streamlit as st |
|
|
|
@st.cache_resource |
|
def context_text(text): return f"### Context\n{text}\n\n### Answer" |
|
|
|
@st.cache_resource |
|
def load_pipe(): |
|
model_name = "MSey/pbt_CaBERT_7_c10731" |
|
return pipeline("token-classification", model=model_name), AutoTokenizer.from_pretrained(model_name) |
|
|
|
pipe, tokenizer = load_pipe() |
|
|
|
st.header("Test Environment for pbt_CaBERT_7_c10731") |
|
user_input = st.text_input("Enter your Prompt here:", "") |
|
contexted_ipnut = context_text(user_input) |
|
context_len = len(contexted_ipnut) |
|
|
|
if user_input: |
|
with st.spinner('Generating response...'): |
|
response = pipe(contexted_ipnut) |
|
st.write("Response:") |
|
st.markdown(response) |
|
|
|
tokens = tokenizer(user_input, return_offsets_mapping=True, return_tensors='pt') |
|
offset_mapping = tokens['offset_mapping'][0].numpy().tolist() |
|
|
|
|
|
highlighted_text = "" |
|
last_position = 0 |
|
|
|
|
|
for entity in response: |
|
start, end = offset_mapping[entity['index']][0], offset_mapping[entity['index']][1] |
|
label = entity['entity'] |
|
|
|
|
|
highlighted_text += user_input[last_position:start] |
|
|
|
|
|
highlighted_text += f'<mark style="background-color: #FFFF00;">{user_input[start:end]}</mark><sup>{label}</sup>' |
|
|
|
|
|
last_position = end |
|
|
|
|
|
highlighted_text += user_input[last_position:] |
|
|
|
|
|
st.markdown(highlighted_text, unsafe_allow_html=True) |