Spaces:
Runtime error
Runtime error
bugfix
Browse files
app.py
CHANGED
|
@@ -112,30 +112,30 @@ def main(button, choose_context):
|
|
| 112 |
df_topic_keywords["Topics"] = topics
|
| 113 |
df_topic_keywords
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
def apply_predict_topic(text):
|
| 141 |
text = [text]
|
|
@@ -193,8 +193,8 @@ with gr.Blocks() as demo:
|
|
| 193 |
choices=['comment', 'sup comment', 'sup comment + comment'], value='sup comment'
|
| 194 |
)
|
| 195 |
plot = gr.Plot(label="Plot")
|
| 196 |
-
button.change(main, inputs=[
|
| 197 |
-
demo.load(main, inputs=[
|
| 198 |
|
| 199 |
|
| 200 |
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
|
|
|
| 112 |
df_topic_keywords["Topics"] = topics
|
| 113 |
df_topic_keywords
|
| 114 |
|
| 115 |
+
# Define function to predict topic for a given text document.
|
| 116 |
+
nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner'])
|
| 117 |
+
def predict_topic(text, nlp=nlp):
|
| 118 |
+
global sent_to_words
|
| 119 |
+
global lemmatization
|
| 120 |
+
# Step 1: Clean with simple_preprocess
|
| 121 |
+
mytext_2 = list(sent_to_words(text))
|
| 122 |
+
# Step 2: Lemmatize
|
| 123 |
+
mytext_3 = lemmatization(mytext_2, allowed_postags=['NOUN', 'ADJ', 'VERB', 'ADV'])
|
| 124 |
+
# Step 3: Vectorize transform
|
| 125 |
+
mytext_4 = vectorizer.transform(mytext_3)
|
| 126 |
+
# Step 4: LDA Transform
|
| 127 |
+
topic_probability_scores = best_lda_model.transform(mytext_4)
|
| 128 |
+
topic = df_topic_keywords.iloc[np.argmax(topic_probability_scores), 1:14].values.tolist()
|
| 129 |
|
| 130 |
+
# Step 5: Infer Topic
|
| 131 |
+
infer_topic = df_topic_keywords.iloc[np.argmax(topic_probability_scores), -1]
|
| 132 |
|
| 133 |
+
#topic_guess = df_topic_keywords.iloc[np.argmax(topic_probability_scores), Topics]
|
| 134 |
+
return infer_topic, topic, topic_probability_scores
|
| 135 |
|
| 136 |
+
# Predict the topic
|
| 137 |
+
mytext = ["This is a test of a random topic where I talk about politics"]
|
| 138 |
+
infer_topic, topic, prob_scores = predict_topic(text = mytext)
|
| 139 |
|
| 140 |
def apply_predict_topic(text):
|
| 141 |
text = [text]
|
|
|
|
| 193 |
choices=['comment', 'sup comment', 'sup comment + comment'], value='sup comment'
|
| 194 |
)
|
| 195 |
plot = gr.Plot(label="Plot")
|
| 196 |
+
button.change(main, inputs=[choose_context], outputs=[plot])
|
| 197 |
+
demo.load(main, inputs=[choose_context], outputs=[plot])
|
| 198 |
|
| 199 |
|
| 200 |
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|