tani4057 commited on
Commit
efef8e0
·
verified ·
1 Parent(s): 8210f57

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +124 -0
  2. main.py +36 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from transformers.utils.logging import set_verbosity_error
4
+
5
+ # Silence warnings
6
+ set_verbosity_error()
7
+
8
+ # Page Config
9
+ st.set_page_config(page_title="Text Summarizer & QnA", layout="centered")
10
+
11
+ # Custom Style
12
+ st.markdown(
13
+ """
14
+ <style>
15
+ html, body, [data-testid="stAppViewContainer"], [data-testid="stApp"] {
16
+ background-color: #E8F0FE !important;
17
+ }
18
+ [data-testid="stHeader"] {
19
+ background: rgba(0,0,0,0);
20
+ }
21
+ [data-testid="stSidebar"] {
22
+ background-color: #E8F0FE !important;
23
+ }
24
+ .title {
25
+ text-align: center;
26
+ font-size: 2.2rem;
27
+ color: #1E3A8A;
28
+ font-weight: 700;
29
+ margin-top: 1rem;
30
+ margin-bottom: 0.5rem;
31
+ }
32
+ .subheader {
33
+ text-align: center;
34
+ color: #475569;
35
+ font-size: 1rem;
36
+ margin-bottom: 2rem;
37
+ }
38
+ .stTextArea textarea {
39
+ border-radius: 10px !important;
40
+ }
41
+ .stButton>button {
42
+ background-color: #1E40AF;
43
+ color: white;
44
+ border: none;
45
+ border-radius: 8px;
46
+ padding: 0.6rem 1.2rem;
47
+ font-weight: 600;
48
+ transition: 0.3s;
49
+ }
50
+ .stButton>button:hover {
51
+ background-color: #2563EB;
52
+ }
53
+ .output-box {
54
+ background-color: #C7D2FE;
55
+ padding: 1rem;
56
+ border-radius: 10px;
57
+ color: #1E3A8A;
58
+ font-weight: 500;
59
+ }
60
+ </style>
61
+ """,
62
+ unsafe_allow_html=True
63
+ )
64
+
65
+ # Title Section
66
+ st.markdown("<div class='title'>🧠 Smart Text Summarizer & QnA Assistant</div>", unsafe_allow_html=True)
67
+ st.markdown("<div class='subheader'>Summarize long text and ask questions about it easily!</div>", unsafe_allow_html=True)
68
+
69
+ # Load Models
70
+ @st.cache_resource
71
+ def load_pipelines():
72
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
73
+ refiner = pipeline("summarization", model="facebook/bart-large")
74
+ qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
75
+ return summarizer, refiner, qa_pipeline
76
+
77
+ summarizer, refiner, qa_pipeline = load_pipelines()
78
+
79
+ # Summarization
80
+ text_to_summarize = st.text_area("Enter text to summarize:", height=200)
81
+ length = st.radio("Select summary length:", ["short", "medium", "long"], horizontal=True)
82
+
83
+ # Define summary length dynamically
84
+ length_settings = {
85
+ "short": {"min_length": 30, "max_length": 80},
86
+ "medium": {"min_length": 80, "max_length": 160},
87
+ "long": {"min_length": 160, "max_length": 300},
88
+ }
89
+
90
+ if st.button("Summarize"):
91
+ if text_to_summarize.strip():
92
+ with st.spinner("Generating summary..."):
93
+ try:
94
+ # Apply variable summary lengths
95
+ params = length_settings[length]
96
+ raw_summary = summarizer(text_to_summarize, **params)
97
+ summary = raw_summary[0]["summary_text"]
98
+
99
+ # Optionally refine
100
+ refined_summary = refiner(summary, min_length=20, max_length=150)[0]["summary_text"]
101
+
102
+ st.session_state["summary"] = refined_summary
103
+
104
+ st.markdown("### 🔹 Generated Summary:")
105
+ st.markdown(f"<div class='output-box'>{refined_summary}</div>", unsafe_allow_html=True)
106
+ except Exception as e:
107
+ st.error(f"Error: {e}")
108
+ else:
109
+ st.warning("Please enter some text to summarize.")
110
+
111
+ # Q&A Section
112
+ if "summary" in st.session_state:
113
+ st.markdown("---")
114
+ st.subheader("❓ Ask a question about the summary:")
115
+ question = st.text_input("Type your question here:")
116
+
117
+ if st.button("Get Answer"):
118
+ if question.strip():
119
+ with st.spinner("Finding answer..."):
120
+ qa_result = qa_pipeline(question=question, context=st.session_state["summary"])
121
+ st.markdown("### 🔹 Answer:")
122
+ st.markdown(f"<div class='output-box'>{qa_result['answer']}</div>", unsafe_allow_html=True)
123
+ else:
124
+ st.warning("Please enter a question.")
main.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ from langchain_huggingface import HuggingFacePipeline
3
+ from langchain.prompts import PromptTemplate
4
+ from transformers.utils.logging import set_verbosity_error
5
+
6
+ set_verbosity_error()
7
+
8
+ summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn", device=0)
9
+ summarizer = HuggingFacePipeline(pipeline=summarization_pipeline)
10
+
11
+ refinement_pipeline = pipeline("summarization", model="facebook/bart-large", device=0)
12
+ refiner = HuggingFacePipeline(pipeline=refinement_pipeline)
13
+
14
+ qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2", device=0)
15
+
16
+ summary_template = PromptTemplate.from_template("Summarize the following text in a {length} way:\n\n{text}")
17
+
18
+ summarization_chain = summary_template | summarizer | refiner
19
+
20
+ text_to_summarize = input("\nEnter text to summarize:\n")
21
+ length = input("\nEnter the length (short/medium/long): ")
22
+
23
+ summary = summarization_chain.invoke({"text": text_to_summarize, "length": length})
24
+
25
+ print("\n🔹 **Generated Summary:**")
26
+ print(summary)
27
+
28
+ while True:
29
+ question = input("\nAsk a question about the summary (or type 'exit' to stop):\n")
30
+ if question.lower() == "exit":
31
+ break
32
+
33
+ qa_result = qa_pipeline(question=question, context=summary)
34
+
35
+ print("\n🔹 **Answer:**")
36
+ print(qa_result["answer"])
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ langchain
3
+ langchain-huggingface