Upload 2 files
Browse files- app.py +84 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Turkish Zero-Shot Text Classification with XLM-RoBERTa
|
| 2 |
+
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 4 |
+
import sentencepiece
|
| 5 |
+
import torch
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
text_1 = """Bilim insanları Botsvana’da Covid-19’un şu ana kadar en çok mutasyona uğramış varyantını tespit etti. \
|
| 10 |
+
Resmi olarak B.1.1.529 koduyla bilinen bu varyantı ise “Nu varyantı” adı verildi. Uzmanlar bu varyant içerisinde \
|
| 11 |
+
tam 32 farklı mutasyon tespit edildiğini açıklarken, bu virüsün corona virüsü aşılarına karşı daha dirençli olabileceğini duyurdu."""
|
| 12 |
+
|
| 13 |
+
text_2 = """Şampiyonlar Ligi’nde 5. hafta oynanan karşılaşmaların ardından sona erdi. Real Madrid, Inter ve Sporting \
|
| 14 |
+
oynadıkları mücadeleler sonrasında Son 16 turuna yükselmeyi başardı. Gecenin dev mücadelesinde ise Manchester City, \
|
| 15 |
+
PSG’yi yenerek liderliği garantiledi."""
|
| 16 |
+
|
| 17 |
+
@st.cache(allow_output_mutation=True)
|
| 18 |
+
def list2text(label_list):
|
| 19 |
+
labels = ""
|
| 20 |
+
for label in label_list:
|
| 21 |
+
labels = labels + label + ","
|
| 22 |
+
labels = labels[:-1]
|
| 23 |
+
return labels
|
| 24 |
+
|
| 25 |
+
label_list_1 = ["dünya", "ekonomi", "kültür", "sağlık", "siyaset", "spor", "teknoloji"]
|
| 26 |
+
label_list_2 = ["positive", "negative", "neutral"]
|
| 27 |
+
|
| 28 |
+
st.title("Turkish Zero-Shot Text Classification \
|
| 29 |
+
with Multilingual XLM-RoBERTa and mDeBERTa Models")
|
| 30 |
+
|
| 31 |
+
model_list = ['vicgalle/xlm-roberta-large-xnli-anli',
|
| 32 |
+
'joeddav/xlm-roberta-large-xnli',
|
| 33 |
+
'MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7']
|
| 34 |
+
|
| 35 |
+
st.sidebar.header("Select Model")
|
| 36 |
+
model_checkpoint = st.sidebar.radio("", model_list)
|
| 37 |
+
|
| 38 |
+
st.sidebar.write("For details of models:")
|
| 39 |
+
st.sidebar.write("https://huggingface.co/vicgalle")
|
| 40 |
+
st.sidebar.write("https://huggingface.co/joeddav")
|
| 41 |
+
st.sidebar.write("https://huggingface.co/MoritzLaurer")
|
| 42 |
+
|
| 43 |
+
st.sidebar.write("For XNLI Dataset:")
|
| 44 |
+
st.sidebar.write("https://huggingface.co/datasets/xnli")
|
| 45 |
+
|
| 46 |
+
st.subheader("Select Text and Label List")
|
| 47 |
+
st.text_area("Text #1", text_1, height=128)
|
| 48 |
+
st.text_area("Text #2", text_2, height=128)
|
| 49 |
+
st.write(f"Label List #1: {list2text(label_list_1)}")
|
| 50 |
+
st.write(f"Label List #2: {list2text(label_list_2)}")
|
| 51 |
+
|
| 52 |
+
text = st.radio("Select Text", ("Text #1", "Text #2", "New Text"))
|
| 53 |
+
labels = st.radio("Select Label List", ("Label List #1", "Label List #2", "New Label List"))
|
| 54 |
+
|
| 55 |
+
if text == "Text #1": selected_text = text_1
|
| 56 |
+
elif text == "Text #2": selected_text = text_2
|
| 57 |
+
elif text == "New Text":
|
| 58 |
+
selected_text = st.text_area("New Text", value="", height=128)
|
| 59 |
+
|
| 60 |
+
if labels == "Label List #1": selected_labels = label_list_1
|
| 61 |
+
elif labels == "Label List #2": selected_labels = label_list_2
|
| 62 |
+
elif labels == "New Label List":
|
| 63 |
+
selected_labels = st.text_area("New Label List (Pls Input as comma-separated)", value="", height=16).split(",")
|
| 64 |
+
|
| 65 |
+
@st.cache(allow_output_mutation=True)
|
| 66 |
+
def setModel(model_checkpoint):
|
| 67 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
|
| 68 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
| 69 |
+
return pipeline("zero-shot-classification", model=model, tokenizer=tokenizer)
|
| 70 |
+
|
| 71 |
+
Run_Button = st.button("Run", key=None)
|
| 72 |
+
if Run_Button == True:
|
| 73 |
+
|
| 74 |
+
zstc_pipeline = setModel(model_checkpoint)
|
| 75 |
+
output = zstc_pipeline(sequences=selected_text, candidate_labels=selected_labels)
|
| 76 |
+
output_labels = output["labels"]
|
| 77 |
+
output_scores = output["scores"]
|
| 78 |
+
|
| 79 |
+
st.header("Result")
|
| 80 |
+
import plotly.graph_objects as go
|
| 81 |
+
fig = go.Figure([go.Bar(x=output_labels, y=output_scores)])
|
| 82 |
+
st.plotly_chart(fig, use_container_width=False, sharing="streamlit")
|
| 83 |
+
|
| 84 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
sentencepiece
|
| 5 |
+
plotly
|