Guilianno commited on
Commit
f371d6d
·
verified ·
1 Parent(s): c694dcd

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -122
app.py DELETED
@@ -1,122 +0,0 @@
1
- import streamlit as st
2
- from langchain_community.document_loaders import PDFPlumberLoader
3
- from transformers import PegasusForConditionalGeneration, PegasusTokenizer
4
- import sqlite3
5
- import textwrap
6
-
7
- # Set up the Streamlit page configuration
8
- st.set_page_config(page_title="Cognitive PDF Paraphraser")
9
-
10
- # Title of the application
11
- st.title("Fossong's Cognitive PDF Paraphraser")
12
- st.write("This application utilizes cognitive computing to analyze unstructured text in PDF documents and perform paraphrasing to make the text human written.")
13
-
14
- # Connect to SQLite database (or create it if it doesn't exist)
15
- conn = sqlite3.connect('user_data.db')
16
- c = conn.cursor()
17
-
18
- # Create tables for storing queries and feedback
19
- c.execute('''
20
- CREATE TABLE IF NOT EXISTS queries (
21
- id INTEGER PRIMARY KEY AUTOINCREMENT,
22
- original_text TEXT,
23
- paraphrased_text TEXT
24
- )
25
- ''')
26
- conn.commit()
27
-
28
- # Load the Pegasus paraphrasing model
29
- @st.cache_resource
30
- def load_model():
31
- model_name = "tuner007/pegasus_paraphrase"
32
- tokenizer = PegasusTokenizer.from_pretrained(model_name)
33
- model = PegasusForConditionalGeneration.from_pretrained(model_name)
34
- return model, tokenizer
35
-
36
- model, tokenizer = load_model()
37
-
38
- # Function to paraphrase text
39
- def paraphrase_text(text, max_chunk_words=250, max_length=60):
40
- if not text.strip():
41
- return "Input text is empty. Please provide valid text for paraphrasing."
42
-
43
- # Split the text into chunks
44
- chunks = textwrap.wrap(text, max_chunk_words * 5, break_long_words=False, break_on_hyphens=False)
45
- paraphrased_chunks = []
46
-
47
- for chunk in chunks:
48
- try:
49
- # Tokenize and ensure it doesn't exceed model limits
50
- inputs = tokenizer(
51
- chunk,
52
- truncation=True,
53
- padding="longest",
54
- return_tensors="pt",
55
- max_length=1024 # Pegasus maximum token limit
56
- )
57
-
58
- # Ensure input size is valid
59
- if inputs["input_ids"].size(1) > 1024:
60
- st.warning(f"Skipping chunk because it exceeds the token limit: {chunk}")
61
- continue
62
-
63
- # Generate paraphrased output
64
- outputs = model.generate(
65
- **inputs,
66
- max_length=max_length,
67
- num_return_sequences=1,
68
- do_sample=True,
69
- top_k=50,
70
- top_p=0.95
71
- )
72
-
73
- paraphrased_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
74
- paraphrased_chunks.append(paraphrased_text)
75
- except Exception as e:
76
- st.error(f"Error occurred during paraphrasing: {e}")
77
- paraphrased_chunks.append(f"Error: Unable to process this chunk: {chunk}")
78
-
79
- return " ".join(paraphrased_chunks) if paraphrased_chunks else "No valid text to paraphrase."
80
-
81
- # Streamlit file uploader for PDF files and text input
82
- uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
83
- text_input = st.text_area("Or enter text directly for paraphrasing:")
84
-
85
- if uploaded_file:
86
- with open("temp.pdf", "wb") as f:
87
- f.write(uploaded_file.getvalue())
88
-
89
- loader = PDFPlumberLoader("temp.pdf")
90
- docs = loader.load()
91
-
92
- for doc in docs:
93
- original_text = doc.page_content
94
-
95
- if not original_text.strip():
96
- st.warning("This page contains no text. Skipping...")
97
- continue
98
-
99
- paraphrased_text = paraphrase_text(original_text)
100
-
101
- st.subheader("Original Text from PDF")
102
- st.write(original_text)
103
-
104
- st.subheader("Paraphrased Text")
105
- st.write(paraphrased_text)
106
-
107
- c.execute("INSERT INTO queries (original_text, paraphrased_text) VALUES (?, ?)", (original_text, paraphrased_text))
108
- conn.commit()
109
-
110
- if text_input:
111
- paraphrased_text = paraphrase_text(text_input)
112
-
113
- st.subheader("Original Text Input")
114
- st.write(text_input)
115
-
116
- st.subheader("Paraphrased Text")
117
- st.write(paraphrased_text)
118
-
119
- c.execute("INSERT INTO queries (original_text, paraphrased_text) VALUES (?, ?)", (text_input, paraphrased_text))
120
- conn.commit()
121
-
122
- conn.close()