Spaces:
Sleeping
Sleeping
ashirhashmi
commited on
Commit
•
32a82a5
1
Parent(s):
76355e6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""app.py"""
|
3 |
+
|
4 |
+
import streamlit as st
|
5 |
+
from transformers import pipeline, BartForConditionalGeneration, BartTokenizer
|
6 |
+
|
7 |
+
# Load pre-trained GPT-2 model and tokenizer
|
8 |
+
model_name = "gpt2"
|
9 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
10 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
model_name = "facebook/bart-large-cnn" # BART large model for summarization
|
13 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
14 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
15 |
+
|
16 |
+
# Define function to generate blog post
|
17 |
+
def generate_summary(topic):
|
18 |
+
input_text = f"{topic}"
|
19 |
+
inputs = tokenizer([input_text], max_length=1024, return_tensors='pt')
|
20 |
+
|
21 |
+
# Generate summary
|
22 |
+
summary_ids = model.generate(inputs['input_ids'], max_length=150, num_beams=4, length_penalty=2.0, early_stopping=True)
|
23 |
+
|
24 |
+
# Decode and return summary
|
25 |
+
generated_summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
26 |
+
return generated_summary
|
27 |
+
|
28 |
+
|
29 |
+
# Streamlit app
|
30 |
+
def main():
|
31 |
+
st.title("Summarization App")
|
32 |
+
|
33 |
+
# Sidebar input for topic
|
34 |
+
topic = st.sidebar.text_area("Enter text to summarize", "Enter your text here...")
|
35 |
+
|
36 |
+
# Generate button
|
37 |
+
if st.sidebar.button("Generate Summary"):
|
38 |
+
summary = generate_summary(topic)
|
39 |
+
st.subheader("Generated Summary:")
|
40 |
+
st.write(summary)
|
41 |
+
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
main()
|
45 |
+
|