tut / app.py
iqra785's picture
Update app.py
08a0246 verified
raw
history blame
2.96 kB
import streamlit as st
from transformers import pipeline
import os
# Set the page configuration to wide layout
st.set_page_config(layout="wide")
# Initialize the summarization pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Define functions for summarizing, extracting key points, and generating flashcards
def summarize_text(text):
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
return summary[0]['summary_text']
def extract_key_points(text):
summary = summarizer(text, max_length=60, min_length=20, do_sample=False)
return [point['summary_text'] for point in summary]
def generate_flashcards(text):
# Assume flashcards are similar to key points in this case
return extract_key_points(text)
def process_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
summary = summarize_text(text)
key_points = extract_key_points(text)
flashcards = generate_flashcards(text)
return summary, key_points, flashcards
# Define tabs for the application
tab1, tab2 = st.tabs(["Text Summarization", "File Upload & Process"])
# Text Summarization Tab
with tab1:
st.title("Text Summarization")
st.subheader("Enter Your Text")
user_text = st.text_area("Input Text", height=300)
if st.button("Generate Summary"):
if user_text:
summary = summarize_text(user_text)
key_points = extract_key_points(user_text)
flashcards = generate_flashcards(user_text)
st.write("### Summary")
st.write(summary)
st.write("### Key Points")
for idx, point in enumerate(key_points, start=1):
st.write(f"**Point {idx}:** {point}")
st.write("### Flashcards")
for idx, card in enumerate(flashcards, start=1):
st.write(f"**Flashcard {idx}:** {card}")
else:
st.warning("Please enter text to generate a summary.")
# File Upload & Process Tab
with tab2:
st.title("Upload and Process File")
uploaded_file = st.file_uploader("Choose a text file", type=["txt"])
if uploaded_file is not None:
# Save uploaded file temporarily
file_path = os.path.join("uploads", uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
# Process the file
summary, key_points, flashcards = process_file(file_path)
# Display the summary, key points, and flashcards
st.write("### Summary")
st.write(summary)
st.write("### Key Points")
for idx, point in enumerate(key_points, start=1):
st.write(f"**Point {idx}:** {point}")
st.write("### Flashcards")
for idx, card in enumerate(flashcards, start=1):
st.write(f"**Flashcard {idx}:** {card}")
# Clean up the temporary file
os.remove(file_path)