iqra785 commited on
Commit
9f8a212
·
verified ·
1 Parent(s): 08a0246

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -80
app.py CHANGED
@@ -1,89 +1,95 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )