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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -49
app.py CHANGED
@@ -1,67 +1,89 @@
1
- from ai71 import AI71
2
  from transformers import pipeline
3
  import os
4
 
5
- # Set your API key here
6
- AI71_API_KEY = "your_api_key_here" # Replace with your actual AI71 API key
7
- client = AI71(AI71_API_KEY)
8
 
9
- # Initialize summarization pipeline using a pre-trained model
10
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
11
 
12
- # Initial system message
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
- """Generate key points or flashcards from the provided text."""
22
- flashcards = summarizer(text, max_length=60, min_length=20, do_sample=False)
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
- while True:
34
- user_input = input("User (type 'upload' to upload a file, or ask a question): ")
35
-
36
- if user_input.lower() in ["exit", "quit"]:
37
- print("Exiting the chat. Goodbye!")
38
- break
39
- elif user_input.lower() == "upload":
40
- file_path = input("Enter the file path to upload: ")
41
- if os.path.exists(file_path):
42
- summary, flashcards = process_file(file_path)
43
- print("\nSummary:\n", summary)
44
- print("\nFlashcards:\n")
45
- for i, card in enumerate(flashcards, 1):
46
- print(f"Flashcard {i}: {card}")
47
- print("\n")
 
 
 
 
 
 
 
 
 
 
 
48
  else:
49
- print("File not found. Please try again.")
50
- continue
51
-
52
- messages.append({"role": "user", "content": user_input})
53
- print("Falcon: ", end="", flush=True)
54
- response_content = ""
55
-
56
- for chunk in client.chat.completions.create(
57
- messages=messages,
58
- model="tiiuae/falcon-180B-chat",
59
- stream=True,
60
- ):
61
- delta_content = chunk.choices[0].delta.content
62
- if delta_content:
63
- print(delta_content, end="", flush=True)
64
- response_content += delta_content
65
-
66
- messages.append({"role": "assistant", "content": response_content})
67
- print("\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
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)