Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
from keras.models import load_model
|
6 |
+
from keras.preprocessing.text import tokenizer_from_json, Tokenizer
|
7 |
+
from keras.preprocessing.sequence import pad_sequences
|
8 |
+
import spacy
|
9 |
+
from string import punctuation
|
10 |
+
import re
|
11 |
+
|
12 |
+
import en_core_web_sm
|
13 |
+
nlp = en_core_web_sm.load()
|
14 |
+
|
15 |
+
stopwords = nlp.Defaults.stop_words
|
16 |
+
|
17 |
+
def clean_text(text):
|
18 |
+
|
19 |
+
text = text.translate(punctuation)
|
20 |
+
|
21 |
+
text = re.sub(r"[^\w\s]", " ",text)
|
22 |
+
text = re.sub(r"[^A-Za-z0-9^,!.\/'+-=]", " ",text)
|
23 |
+
|
24 |
+
doc = nlp(text)
|
25 |
+
|
26 |
+
text = [token for token in doc if str(token) not in stopwords]
|
27 |
+
lemmatized = [token.lemma_ for token in text]
|
28 |
+
|
29 |
+
text = ' '.join(lemmatized)
|
30 |
+
|
31 |
+
return text
|
32 |
+
|
33 |
+
with open("tokenizer.json", "r") as read_file:
|
34 |
+
tokenizer = json.load(read_file)
|
35 |
+
|
36 |
+
tokenizer = tokenizer_from_json(tokenizer)
|
37 |
+
|
38 |
+
model = load_model('tweets_disaster_model.h5')
|
39 |
+
|
40 |
+
def tweets_predictions(text):
|
41 |
+
text = clean_text(text)
|
42 |
+
text = tokenizer.texts_to_sequences([text])
|
43 |
+
text = pad_sequences(text, padding='post', maxlen=50)
|
44 |
+
pred = model.predict(text.reshape(1,-1)).tolist()[0]
|
45 |
+
dic = {}
|
46 |
+
dic['No disaster'] = 1 - pred[0]
|
47 |
+
dic['Disaster'] = pred[0]
|
48 |
+
return dic
|
49 |
+
|
50 |
+
interface = gr.Interface(fn=tweets_predictions, inputs='textbox', outputs='label', theme='darkdefault').launch(share=True)
|