Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,95 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
import
|
|
|
4 |
|
5 |
# Set the page configuration to wide layout
|
6 |
st.set_page_config(layout="wide")
|
7 |
|
8 |
-
#
|
9 |
-
|
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.
|
38 |
-
|
39 |
-
|
40 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from config import get_ai71_api_key
|
3 |
+
from file_utils import extract_text_from_pdf as _extract_text_from_pdf, extract_text_from_word as _extract_text_from_word
|
4 |
+
from ai_utils import generate_response as _generate_response
|
5 |
|
6 |
# Set the page configuration to wide layout
|
7 |
st.set_page_config(layout="wide")
|
8 |
|
9 |
+
# Define the tabs
|
10 |
+
tab1, tab2, tab3, tab4, tab5 = st.tabs(["Introduction", "Text Processing", "Summary & Main Points", "Flashcards", "Results"])
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
with tab1:
|
13 |
+
st.write("""
|
14 |
+
Welcome to the AI71 Text Processing Tool! This application helps you process text files by summarizing content,
|
15 |
+
extracting main points, and creating flashcards. Follow the instructions to upload your files and generate content.
|
16 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
with tab2:
|
19 |
+
AI71_API_KEY = get_ai71_api_key()
|
20 |
+
st.title("Text Processing")
|
21 |
+
|
22 |
+
# Initialize session state variables
|
23 |
+
session_vars = ['uploaded_text', 'summary', 'main_points', 'flashcards', 'results']
|
24 |
+
for var in session_vars:
|
25 |
+
if var not in st.session_state:
|
26 |
+
st.session_state[var] = ""
|
27 |
+
|
28 |
+
# Caching file extraction functions
|
29 |
+
@st.cache_data
|
30 |
+
def extract_text_from_pdf(file):
|
31 |
+
return _extract_text_from_pdf(file)
|
32 |
+
|
33 |
+
@st.cache_data
|
34 |
+
def extract_text_from_word(file):
|
35 |
+
return _extract_text_from_word(file)
|
36 |
+
|
37 |
+
# Caching API call function
|
38 |
+
@st.cache_data
|
39 |
+
def generate_response(system_message, user_message):
|
40 |
+
return _generate_response(system_message, user_message)
|
41 |
+
|
42 |
+
st.subheader("Upload Text File")
|
43 |
+
|
44 |
+
file = st.file_uploader("Upload a text file (PDF or Word)", type=["pdf", "docx", "doc"])
|
45 |
+
if file is not None:
|
46 |
+
if file.type == "application/pdf":
|
47 |
+
st.session_state.uploaded_text = extract_text_from_pdf(file)
|
48 |
+
elif file.type in ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"]:
|
49 |
+
st.session_state.uploaded_text = extract_text_from_word(file)
|
50 |
+
st.text_area("Uploaded Text", value=st.session_state.uploaded_text, height=200, key="uploaded_text")
|
51 |
+
|
52 |
+
with tab3:
|
53 |
+
st.title("Summary & Main Points")
|
54 |
+
|
55 |
+
if st.session_state.uploaded_text:
|
56 |
+
summary_prompt = "Summarize the following text into a brief summary:"
|
57 |
+
main_points_prompt = "Extract the main points from the following text:"
|
58 |
+
|
59 |
+
if st.button("Generate Summary"):
|
60 |
+
st.session_state.summary = generate_response(summary_prompt, st.session_state.uploaded_text)
|
61 |
+
st.text_area("Summary", value=st.session_state.summary, height=200, key="summary")
|
62 |
+
|
63 |
+
if st.button("Extract Main Points"):
|
64 |
+
st.session_state.main_points = generate_response(main_points_prompt, st.session_state.uploaded_text)
|
65 |
+
st.text_area("Main Points", value=st.session_state.main_points, height=200, key="main_points")
|
66 |
+
|
67 |
+
with tab4:
|
68 |
+
st.title("Flashcards")
|
69 |
+
|
70 |
+
if st.session_state.uploaded_text:
|
71 |
+
flashcards_prompt = "Create flashcards with questions and answers based on the following text:"
|
72 |
+
|
73 |
+
if st.button("Generate Flashcards"):
|
74 |
+
st.session_state.flashcards = generate_response(flashcards_prompt, st.session_state.uploaded_text)
|
75 |
+
st.text_area("Flashcards", value=st.session_state.flashcards, height=200, key="flashcards")
|
76 |
+
|
77 |
+
with tab5:
|
78 |
+
st.title("Results")
|
79 |
+
st.subheader("Summary")
|
80 |
+
st.text_area("Summary", value=st.session_state.summary, height=200, key="summary_results")
|
81 |
+
|
82 |
+
st.subheader("Main Points")
|
83 |
+
st.text_area("Main Points", value=st.session_state.main_points, height=200, key="main_points_results")
|
84 |
+
|
85 |
+
st.subheader("Flashcards")
|
86 |
+
st.text_area("Flashcards", value=st.session_state.flashcards, height=200, key="flashcards_results")
|
87 |
+
|
88 |
+
st.subheader("Download Results")
|
89 |
+
results = f"Summary:\n{st.session_state.summary}\n\nMain Points:\n{st.session_state.main_points}\n\nFlashcards:\n{st.session_state.flashcards}"
|
90 |
+
st.download_button(
|
91 |
+
label="Download Results",
|
92 |
+
data=results,
|
93 |
+
file_name="results.txt",
|
94 |
+
mime="text/plain"
|
95 |
+
)
|