Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,53 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import
|
4 |
import pandas as pd
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
@st.cache_resource
|
7 |
def load_model():
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
return
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
if st.button("Predict"):
|
41 |
-
label, confidence = predict_label(premise, hypothesis, tokenizer, model)
|
42 |
-
st.success(f"Prediction: {label} ({confidence:.2f} confidence)")
|
43 |
-
|
44 |
-
st.markdown("---")
|
45 |
-
st.subheader("Explore Sample Data")
|
46 |
-
|
47 |
-
df = load_sample_data()
|
48 |
-
if not df.empty:
|
49 |
-
st.dataframe(df.sample(10))
|
50 |
-
else:
|
51 |
-
st.warning("Upload a file named `xnli_sample.csv` for sample data display.")
|
|
|
1 |
import streamlit as st
|
2 |
+
import zipfile
|
3 |
+
import os
|
4 |
import pandas as pd
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
6 |
|
7 |
+
# Paths
|
8 |
+
ZIP_FILE = "xnli-multilingual-nli-dataset.zip"
|
9 |
+
EXTRACT_DIR = "extracted_data"
|
10 |
+
|
11 |
+
@st.cache_data
|
12 |
+
def extract_and_load():
|
13 |
+
if not os.path.exists(EXTRACT_DIR):
|
14 |
+
with zipfile.ZipFile(ZIP_FILE, "r") as zip_ref:
|
15 |
+
zip_ref.extractall(EXTRACT_DIR)
|
16 |
+
csv_files = [f for f in os.listdir(EXTRACT_DIR) if f.endswith('.csv')]
|
17 |
+
return csv_files
|
18 |
+
|
19 |
+
# Load model and tokenizer
|
20 |
@st.cache_resource
|
21 |
def load_model():
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained("MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
|
23 |
+
model = AutoModelForSequenceClassification.from_pretrained("MoritzLaurer/mDeBERTa-v3-base-mnli-xnli")
|
24 |
+
nli_pipeline = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
25 |
+
return nli_pipeline
|
26 |
|
27 |
+
st.set_page_config(page_title="Multilingual NLI App", layout="centered")
|
28 |
+
|
29 |
+
st.title("🌍 Multilingual NLI (Natural Language Inference) Explorer")
|
30 |
+
st.markdown("Upload premise & hypothesis pairs or use the dataset to explore entailment, contradiction, or neutrality.")
|
31 |
+
|
32 |
+
nli_pipeline = load_model()
|
33 |
+
|
34 |
+
csv_files = extract_and_load()
|
35 |
+
selected_csv = st.selectbox("Choose a language CSV file:", csv_files)
|
36 |
+
|
37 |
+
df = pd.read_csv(os.path.join(EXTRACT_DIR, selected_csv)).dropna()
|
38 |
+
sample_df = df.sample(5).reset_index(drop=True)
|
39 |
+
|
40 |
+
st.subheader("Sample from Dataset")
|
41 |
+
st.dataframe(sample_df[['premise', 'hypothesis', 'label']])
|
42 |
+
|
43 |
+
st.subheader("🔍 Run Inference")
|
44 |
+
index = st.number_input("Select Sample Index", min_value=0, max_value=len(sample_df)-1, value=0, step=1)
|
45 |
+
premise = sample_df.loc[index, 'premise']
|
46 |
+
hypothesis = sample_df.loc[index, 'hypothesis']
|
47 |
+
|
48 |
+
st.markdown(f"**Premise:** {premise}")
|
49 |
+
st.markdown(f"**Hypothesis:** {hypothesis}")
|
50 |
+
|
51 |
+
if st.button("Run NLI Prediction"):
|
52 |
+
result = nli_pipeline(f"{premise} </s> {hypothesis}")
|
53 |
+
st.success(f"**Prediction:** {result[0]['label']} (Score: {result[0]['score']:.2f})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|