-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
156 lines (103 loc) · 4.75 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import spacy
from aitextgen import aitextgen #for ai text gen
import streamlit as st
from textblob import TextBlob
from gingerit.gingerit import GingerIt
import nltk;
nltk.download('punkt')
nlp = spacy.load('en_core_web_sm')
from transformers import pipeline
#Sumy packages
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lex_rank import LexRankSummarizer
#Web Scrapping Packages
from bs4 import BeautifulSoup
from urllib.request import urlopen
#Function for Web Scraping
@st.cache
def get_text(raw_url):
page = urlopen(raw_url)
soup = BeautifulSoup(page, features="lxml")
fetched_text = ' '.join(map(lambda p:p.text,soup.find_all('p')))
return fetched_text
#Fuctionforsumy
def sumy_summarizer(docx):
parser = PlaintextParser.from_string(docx, Tokenizer("english"))
lex_summarizer = LexRankSummarizer()
summary = lex_summarizer(parser.document, 3)
summary_list = [str(sentence) for sentence in summary]
result = ' '.join(summary_list)
return result
@st.cache(allow_output_mutation=True)
def load_qa_model():
model = pipeline("question-answering")
return model
def main():
st.title("Text Processing Web App")
activities = ["Summarize Via Text", "Summarize via URL", "Translate text","Automatic text generation" ,"Grammar Check", "Spell correction", "Question Answer from text"]
choice = st.sidebar.selectbox("Select Activity", activities)
if choice == 'Summarize Via Text':
st.subheader("Summarize your Text")
raw_text = st.text_area("Enter Text Here","Type here")
if st.button("Summarize Via Text"):
summary_result = sumy_summarizer(raw_text)
st.write(summary_result)
if choice == 'Summarize via URL':
st.subheader("Summarize Your URL")
raw_url = st.text_input("Enter URL","Type Here")
if st.button("Summarize"):
result = get_text(raw_url)
#st.write(result)
st.subheader("Summarized Text")
docx = sumy_summarizer(result)
html = docx.replace("\n\n" , "\n")
st.markdown(html,unsafe_allow_html=True)
if choice == 'Spell correction':
st.subheader(" Check your text")
raw_text = st.text_area("Enter Text Here","Type here")
if st.button("Check Spelling"):
a = TextBlob(raw_text)
#st.write(result)
st.subheader("Summarized Text")
st.write(a.correct())
if choice == 'Translate text':
st.subheader("Translate your text")
raw_text = st.text_area("Enter Text Here","Type here")
translation_text = TextBlob(raw_text)
list1 = ["en","ta","pa","gu","hi","ur","kn","bn","te"]
a= st.selectbox("select",list1)
if st.button("Translate"):
st.subheader("Translated text")
st.write(translation_text.translate(to=a))
if choice == 'Grammar Check':
st.subheader("Check your text")
raw_text = st.text_area("Enter Text Here","Type here")
parser = GingerIt()
if st.button("Check"):
st.subheader("Corrected text")
result_dict = parser.parse(raw_text)
st.markdown( str(result_dict["result"]))
if choice == 'Automatic text generation':
st.subheader("Automatic Text Generation")
ai = aitextgen()
prompt_text = st.text_input(label = "Enter your Prompt text...",
value = "Computer is beautiful")
with st.spinner("AI is at Work........"):
# text generation
gpt_text = ai.generate_one(prompt=prompt_text,
max_length = 100 )
st.success("AI Successfully generated the below text ")
st.write(gpt_text)
if choice == 'Question Answer from text':
qa = load_qa_model()
st.title("Ask Questions about your Text")
sentence = st.text_area('Please paste your article :', height=30)
question = st.text_input("Questions from this article?")
button = st.button("Get me Answers")
with st.spinner("Discovering Answers.."):
if button and sentence:
answers = qa(question=question, context=sentence)
st.write(answers['answer'])
if __name__ == '__main__':
main()