Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import XLMRobertaTokenizer, XLMRobertaForSequenceClassification
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
|
6 |
+
model_path = "fine_tuned_xlm_roberta"
|
7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
|
9 |
+
|
10 |
+
tokenizer = XLMRobertaTokenizer.from_pretrained(model_path)
|
11 |
+
model = XLMRobertaForSequenceClassification.from_pretrained(model_path)
|
12 |
+
model.to(device)
|
13 |
+
model.eval()
|
14 |
+
|
15 |
+
|
16 |
+
def classify_text(text, max_length=128):
|
17 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding="max_length", max_length=max_length)
|
18 |
+
inputs = {key: val.to(device) for key, val in inputs.items()}
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model(**inputs)
|
22 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
23 |
+
pred_label = torch.argmax(probabilities, dim=-1).item()
|
24 |
+
confidence = probabilities[0, pred_label].item()
|
25 |
+
|
26 |
+
return "Kyrgyz" if pred_label == 1 else "Non-Kyrgyz", confidence
|
27 |
+
|
28 |
+
|
29 |
+
st.title("Kyrgyz Language Classifier")
|
30 |
+
st.write("This tool identifies whether the given text is Kyrgyz or not.")
|
31 |
+
|
32 |
+
|
33 |
+
st.markdown("""
|
34 |
+
**Instructions:**
|
35 |
+
|
36 |
+
* Please enter a **sentence** for better accuracy.
|
37 |
+
* **Note:** The word "**小邪谢邪屑**" might be classified as Non-Kyrgyz. This is a known exception.
|
38 |
+
""")
|
39 |
+
user_input = st.text_area("Enter text to classify:", placeholder="Type your sentence here...")
|
40 |
+
|
41 |
+
if st.button("Classify"):
|
42 |
+
if user_input.strip():
|
43 |
+
label, confidence = classify_text(user_input)
|
44 |
+
st.write(f"Prediction: **{label}**")
|
45 |
+
st.write(f"Confidence: **{confidence:.2%}**")
|
46 |
+
else:
|
47 |
+
st.warning("Please enter some text for classification.")
|