diff --git "a/app.py" "b/app.py" new file mode 100644--- /dev/null +++ "b/app.py" @@ -0,0 +1,695 @@ + + + + + + + + + + + + + + + + + + + + + + + + app.py Β· paragon-analytics/Persuade at main + + +
Hugging Face's logo + + +
+ +
+ + +
+
+ + +

+ Spaces: + +
Persuade +
+
+
+
+
+
+ + Running +
+ View logs +

+ +
+ +
+ + + + +
+
Persuade + / + app.py
+ + +
+
paragon-analytics's picture + +
Update app.py
+ ebb66e9 + +
+ + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
# Import packages:
+
import numpy as np
import matplotlib.pyplot as plt
import re
# tensorflow imports:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import losses
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.optimizers import RMSprop
# # keras imports:
from keras.models import Model
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, RepeatVector, TimeDistributed
from keras.preprocessing.text import Tokenizer
from keras_preprocessing import sequence
from tensorflow.keras.utils import to_categorical
from keras.callbacks import EarlyStopping
from keras.models import Sequential
from keras import layers
from keras.backend import clear_session
import pickle
import gradio as gr
import yake
import spacy
from spacy import displacy
import streamlit as st
import spacy_streamlit
nlp = spacy.load('en_core_web_sm')
+
kw_extractor = yake.KeywordExtractor()
custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None)
+
max_words = 2000
max_len = 111
+
# load the model from disk
filename = 'lstm_model.sav'
lmodel = pickle.load(open(filename, 'rb'))
+
# load the model from disk
filename = 'tokenizer.pickle'
tok = pickle.load(open(filename, 'rb'))
+
def process_final_text(text):
X_test = str(text).lower()
l = []
l.append(X_test)
test_sequences = tok.texts_to_sequences(l)
test_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)
lstm_prob = lmodel.predict(test_sequences_matrix.tolist()).flatten()
lstm_pred = np.where(lstm_prob>=0.5,1,0)
# Get Keywords:
keywords = custom_kw_extractor.extract_keywords(X_test)
letter = []
score = []
for i in keywords:
if i[1]>0.4:
a = "+++"
elif (i[1]<=0.4) and (i[1]>0.1):
a = "++"
elif (i[1]<=0.1) and (i[1]>0.01):
a = "+"
else:
a = "NA"
letter.append(i[0])
score.append(a)
keywords = [(letter[i], score[i]) for i in range(0, len(letter))]
# Get NER:
# NER:
doc = nlp(text)
sp_html = displacy.render(doc, style="ent", page=True, jupyter=False)
NER = (
""
+ sp_html
+ ""
)
return {"Persuasive": float(lstm_prob[0]), "Non-Persuasive": 1-float(lstm_prob[0])},keywords,NER
def main(prob1,prob2,sol1,sol2,inv,act):
text = str(prob1) + " " + str(prob2) + " " + str(sol1) + " " + str(sol2) + " " + str(inv) + " " + str(act)
obj = process_final_text(text)
return obj[0],obj[1],obj[2]
title = "Welcome to **PersuAID** πŸͺ"
description1 = """
It is difficult to write persuasive product descriptions. It could take time and money to create a good one. PersuAID uses a template to organize your thoughts when writing a persuasive product description. It's AI model is trained on tens of thousands of product descriptions. Why don't you give it a try? Just add your text and hit Create & Analyze ✨
"""
+
description2 = """
Although encouraged, you don't have to fill all the boxes. Just try the ones that matter to you. After getting your first score, modify your answers and hit Create & Analyze again 🀞
"""
+
with gr.Blocks(title=title) as demo:
gr.Markdown(f"## {title}")
gr.Markdown("""![marketing](file/marketing.jpg)""")
gr.Markdown(description1)
gr.Markdown("""---""")
gr.Markdown(description2)
gr.Markdown("""---""")
prob1 = gr.Textbox(label="A great product description is the one that solves an important problem. What is the single, most important problem that your product solves?",lines=2, placeholder="Type it here ...")
prob2 = gr.Textbox(label="Are there any other problems your product solves? If none, leave blank.",lines=2, placeholder="Type them here, ...")
sol1 = gr.Textbox(label="What solution does your product offer to the main problem you described first?",lines=2, placeholder="Type your answer here ...")
sol2 = gr.Textbox(label="How does your product solves the problem?",lines=2, placeholder="Type your answer here ...")
inv = gr.Textbox(label="Now it's time to invite your audience to take action. Encourage them to try your product πŸ’–",lines=2, placeholder="Type your invitation here ...")
act = gr.Textbox(label="And finally, describe (short sentence) what action you want them to take. This could also be your product description as a punchline πŸ’ͺ",lines=2, placeholder="Type the desired action/ outcome/ punchline here ...")
submit_btn = gr.Button("Create & Analyze")
#text = gr.Textbox(label="Text:",lines=2, placeholder="Please enter text here ...")
#submit_btn2 = gr.Button("Analyze")
+
with gr.Column(visible=True) as output_col:
label = gr.Label(label = "Predicted Label")
impplot = gr.HighlightedText(label="Important Words", combine_adjacent=False).style(
color_map={"+++": "royalblue","++": "cornflowerblue",
"+": "lightsteelblue", "NA":"white"})
NER = gr.HTML(label = 'NER:')
+
submit_btn.click(
main,
[prob1,prob2,sol1,sol2,inv,act],
[label,impplot,NER], api_name="PrsTalk"
)
+
gr.Markdown("### Click on any of the examples below to see how it works:")
gr.Examples([["It is difficult to write persuasive product descriptions.", "It could take time and money to create a good one.", "PersuAID uses a template to organize your thoughts when writing a persuasive product description.", "It's AI model is trained on tens of thousands of product descriptions.", "Why don't you give it a try?", "Just add your text and hit Create & Analyze ✨"], ["What is performance?", "Zero to Sixty or Sixty to Zero? How a car performs a quarter mile or a quarter century? Is performance about the joy of driving or the importance of surviving?",
"To us performance is not about doing one thing well ...", "it is about doing everything well .. because in the end everything matters.", "Performance without compromise.", "That is what drives you..... Mercedes Benz"], ["Talking to your friends about their problems with drugs and alcohol might not be easy.", "", "", "", "Courage.", "The anti-drug."], ["When experiencing depression, I couldn't get out of bed or focus.", "My goals and dreams drifted away.", "I got my life back through the help of a support group of people who had been where I was.", "They helped me see that my life wasn't limited that I could still achieve my dreams.","We've been there.", "We can help. Call 800-826-3632 to learn more."], ["Up to 6 million homeless animals enter shelters nationwide every year.", "Some abandoned, some sick or injured they're just looking for a place to call home.", "When you rescue a shelter animal you won't just be making a difference in their life, you could be making a difference in your own.", "", "Visit your local animal shelter today and save a life.", "HumaneSociety.org"], ["You always dreamed of living in a place like this,", "But the land can be challenging.", "That's where our expertise comes in.", "Your John Deere dealer will find the best solution to get the most for your money and the most out of your property...", "Because we know you love your land...every last bit of it.", "That's how we run... and nothin' runs like a Deere."]], [prob1,prob2,sol1,sol2,inv,act], [label,impplot,NER], main, cache_examples=True)
demo.launch()
+
+
+ + + + + +