Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,89 @@
|
|
1 |
-
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
|
5 |
-
# Set
|
6 |
-
|
7 |
-
client = AI71(AI71_API_KEY)
|
8 |
|
9 |
-
# Initialize summarization pipeline
|
10 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
11 |
|
12 |
-
#
|
13 |
-
messages = [{"role": "system", "content": "You are a helpful assistant."}]
|
14 |
-
|
15 |
def summarize_text(text):
|
16 |
-
"""Summarize the provided text."""
|
17 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
18 |
return summary[0]['summary_text']
|
19 |
|
|
|
|
|
|
|
|
|
20 |
def generate_flashcards(text):
|
21 |
-
|
22 |
-
|
23 |
-
return [card['summary_text'] for card in flashcards]
|
24 |
|
25 |
def process_file(file_path):
|
26 |
-
"""Process the uploaded text file."""
|
27 |
with open(file_path, 'r', encoding='utf-8') as file:
|
28 |
text = file.read()
|
29 |
summary = summarize_text(text)
|
|
|
30 |
flashcards = generate_flashcards(text)
|
31 |
-
return summary, flashcards
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
else:
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
|
5 |
+
# Set the page configuration to wide layout
|
6 |
+
st.set_page_config(layout="wide")
|
|
|
7 |
|
8 |
+
# Initialize the summarization pipeline
|
9 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
10 |
|
11 |
+
# Define functions for summarizing, extracting key points, and generating flashcards
|
|
|
|
|
12 |
def summarize_text(text):
|
|
|
13 |
summary = summarizer(text, max_length=150, min_length=50, do_sample=False)
|
14 |
return summary[0]['summary_text']
|
15 |
|
16 |
+
def extract_key_points(text):
|
17 |
+
summary = summarizer(text, max_length=60, min_length=20, do_sample=False)
|
18 |
+
return [point['summary_text'] for point in summary]
|
19 |
+
|
20 |
def generate_flashcards(text):
|
21 |
+
# Assume flashcards are similar to key points in this case
|
22 |
+
return extract_key_points(text)
|
|
|
23 |
|
24 |
def process_file(file_path):
|
|
|
25 |
with open(file_path, 'r', encoding='utf-8') as file:
|
26 |
text = file.read()
|
27 |
summary = summarize_text(text)
|
28 |
+
key_points = extract_key_points(text)
|
29 |
flashcards = generate_flashcards(text)
|
30 |
+
return summary, key_points, flashcards
|
31 |
+
|
32 |
+
# Define tabs for the application
|
33 |
+
tab1, tab2 = st.tabs(["Text Summarization", "File Upload & Process"])
|
34 |
+
|
35 |
+
# Text Summarization Tab
|
36 |
+
with tab1:
|
37 |
+
st.title("Text Summarization")
|
38 |
+
|
39 |
+
st.subheader("Enter Your Text")
|
40 |
+
user_text = st.text_area("Input Text", height=300)
|
41 |
+
|
42 |
+
if st.button("Generate Summary"):
|
43 |
+
if user_text:
|
44 |
+
summary = summarize_text(user_text)
|
45 |
+
key_points = extract_key_points(user_text)
|
46 |
+
flashcards = generate_flashcards(user_text)
|
47 |
+
|
48 |
+
st.write("### Summary")
|
49 |
+
st.write(summary)
|
50 |
+
|
51 |
+
st.write("### Key Points")
|
52 |
+
for idx, point in enumerate(key_points, start=1):
|
53 |
+
st.write(f"**Point {idx}:** {point}")
|
54 |
+
|
55 |
+
st.write("### Flashcards")
|
56 |
+
for idx, card in enumerate(flashcards, start=1):
|
57 |
+
st.write(f"**Flashcard {idx}:** {card}")
|
58 |
else:
|
59 |
+
st.warning("Please enter text to generate a summary.")
|
60 |
+
|
61 |
+
# File Upload & Process Tab
|
62 |
+
with tab2:
|
63 |
+
st.title("Upload and Process File")
|
64 |
+
|
65 |
+
uploaded_file = st.file_uploader("Choose a text file", type=["txt"])
|
66 |
+
|
67 |
+
if uploaded_file is not None:
|
68 |
+
# Save uploaded file temporarily
|
69 |
+
file_path = os.path.join("uploads", uploaded_file.name)
|
70 |
+
with open(file_path, "wb") as f:
|
71 |
+
f.write(uploaded_file.getbuffer())
|
72 |
+
|
73 |
+
# Process the file
|
74 |
+
summary, key_points, flashcards = process_file(file_path)
|
75 |
+
|
76 |
+
# Display the summary, key points, and flashcards
|
77 |
+
st.write("### Summary")
|
78 |
+
st.write(summary)
|
79 |
+
|
80 |
+
st.write("### Key Points")
|
81 |
+
for idx, point in enumerate(key_points, start=1):
|
82 |
+
st.write(f"**Point {idx}:** {point}")
|
83 |
+
|
84 |
+
st.write("### Flashcards")
|
85 |
+
for idx, card in enumerate(flashcards, start=1):
|
86 |
+
st.write(f"**Flashcard {idx}:** {card}")
|
87 |
+
|
88 |
+
# Clean up the temporary file
|
89 |
+
os.remove(file_path)
|