Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,33 @@
|
|
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 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
st.warning("Please enter some text for classification.")
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
st.title("Kyrgyz Language Classifier")
|
5 |
st.write("This tool identifies whether the given text is Kyrgyz or not.")
|
6 |
|
|
|
7 |
st.markdown("""
|
8 |
**Instructions:**
|
9 |
|
10 |
+
* Please enter a **sentence** or a few sentences for better accuracy.
|
11 |
* **Note:** The word "**Салам**" might be classified as Non-Kyrgyz. This is a known exception.
|
12 |
""")
|
13 |
user_input = st.text_area("Enter text to classify:", placeholder="Type your sentence here...")
|
14 |
|
15 |
+
|
16 |
+
api_url = "https://4190-212-112-100-194.ngrok-free.app//classify"
|
17 |
+
|
18 |
if st.button("Classify"):
|
19 |
if user_input.strip():
|
20 |
+
try:
|
21 |
+
response = requests.post(api_url, json={'text': user_input})
|
22 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
23 |
+
|
24 |
+
result = response.json()
|
25 |
+
if 'error' in result:
|
26 |
+
st.error(f"Error: {result['error']}")
|
27 |
+
else:
|
28 |
+
st.write(f"Prediction: **{result['label']}**")
|
29 |
+
st.write(f"Confidence: **{result['confidence']:.2%}**")
|
30 |
+
except requests.exceptions.RequestException as e:
|
31 |
+
st.error(f"Connection error: {e}")
|
32 |
else:
|
33 |
st.warning("Please enter some text for classification.")
|