Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
import re
|
5 |
|
6 |
-
#
|
7 |
-
model_name = "
|
8 |
-
tokenizer =
|
9 |
-
model =
|
10 |
|
11 |
def clean_text(text):
|
12 |
-
#
|
13 |
text = re.sub(r'http\S+|[^\w\s]', '', text)
|
14 |
return text.strip()
|
15 |
|
@@ -18,10 +18,10 @@ def predict(text):
|
|
18 |
return "Please enter some text"
|
19 |
|
20 |
try:
|
21 |
-
#
|
22 |
text = clean_text(text)
|
23 |
|
24 |
-
#
|
25 |
inputs = tokenizer(
|
26 |
text,
|
27 |
return_tensors="pt",
|
@@ -30,21 +30,21 @@ def predict(text):
|
|
30 |
max_length=128
|
31 |
)
|
32 |
|
33 |
-
#
|
34 |
with torch.no_grad():
|
35 |
outputs = model(**inputs)
|
36 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
37 |
prediction = torch.argmax(outputs.logits, dim=-1)
|
38 |
confidence = probabilities[0][prediction.item()].item()
|
39 |
|
40 |
-
#
|
41 |
result = "Disaster" if prediction.item() == 1 else "Not Disaster"
|
42 |
return f"{result} (Confidence: {confidence:.2%})"
|
43 |
|
44 |
except Exception as e:
|
45 |
return f"Error in prediction: {str(e)}"
|
46 |
|
47 |
-
# Gradio
|
48 |
iface = gr.Interface(
|
49 |
fn=predict,
|
50 |
inputs=gr.Textbox(
|
@@ -67,5 +67,5 @@ iface = gr.Interface(
|
|
67 |
)
|
68 |
)
|
69 |
|
70 |
-
#
|
71 |
-
iface.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
import torch
|
4 |
import re
|
5 |
|
6 |
+
# Load model and tokenizer
|
7 |
+
model_name = "alperugurcan/nlp-disaster"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
|
11 |
def clean_text(text):
|
12 |
+
# Remove URLs and special characters
|
13 |
text = re.sub(r'http\S+|[^\w\s]', '', text)
|
14 |
return text.strip()
|
15 |
|
|
|
18 |
return "Please enter some text"
|
19 |
|
20 |
try:
|
21 |
+
# Preprocess text
|
22 |
text = clean_text(text)
|
23 |
|
24 |
+
# Tokenize the input text
|
25 |
inputs = tokenizer(
|
26 |
text,
|
27 |
return_tensors="pt",
|
|
|
30 |
max_length=128
|
31 |
)
|
32 |
|
33 |
+
# Get model predictions
|
34 |
with torch.no_grad():
|
35 |
outputs = model(**inputs)
|
36 |
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
37 |
prediction = torch.argmax(outputs.logits, dim=-1)
|
38 |
confidence = probabilities[0][prediction.item()].item()
|
39 |
|
40 |
+
# Return result with confidence
|
41 |
result = "Disaster" if prediction.item() == 1 else "Not Disaster"
|
42 |
return f"{result} (Confidence: {confidence:.2%})"
|
43 |
|
44 |
except Exception as e:
|
45 |
return f"Error in prediction: {str(e)}"
|
46 |
|
47 |
+
# Create a Gradio interface with improved styling
|
48 |
iface = gr.Interface(
|
49 |
fn=predict,
|
50 |
inputs=gr.Textbox(
|
|
|
67 |
)
|
68 |
)
|
69 |
|
70 |
+
# Launch with share=True for public URL
|
71 |
+
iface.launch(share=True)
|