mdasad3617's picture
Update app.py
2e7c2af verified
raw
history blame
3.39 kB
import streamlit as st
from transformers import pipeline
import logging
from PyPDF2 import PdfReader
# Setup logging
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler()
]
)
# Function to extract text from a PDF file
def extract_text_from_pdf(pdf_file):
pdf_reader = PdfReader(pdf_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text
def main():
setup_logging()
logging.info("Starting the Streamlit app.")
# Initialize the summarization pipeline with the specified model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Streamlit UI
st.title("GenAI Lab Report Analyzer")
st.write("Upload a file, record audio, or type text to generate a summary. Select the appropriate input type and provide the input.")
input_type = st.radio(
"Select Input Type:",
options=["Text", "Text File", "PDF", "DOCX", "Audio"],
index=0
)
file = None
text = None
audio = None
if input_type == "Text":
text = st.text_area("Enter your text here:", placeholder="Type your text here...")
elif input_type == "Text File":
file = st.file_uploader("Upload your text file:", type=["txt"])
elif input_type == "PDF":
file = st.file_uploader("Upload your PDF file:", type=["pdf"])
elif input_type == "DOCX":
file = st.file_uploader("Upload your DOCX file:", type=["docx"])
elif input_type == "Audio":
audio = st.file_uploader("Upload your audio file:", type=["wav", "mp3", "m4a"])
if st.button("Report Result"):
try:
summary = None
if input_type == "Text" and text:
logging.info("Processing text input.")
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
logging.info("Text input processed successfully.")
elif input_type == "Text File" and file:
logging.info(f"Processing text file: {file.name}")
text = file.read().decode("utf-8") # Assuming UTF-8 encoding
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
elif input_type == "PDF" and file:
logging.info(f"Processing PDF file: {file.name}")
text = extract_text_from_pdf(file)
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
elif input_type == "Audio" and audio:
logging.info("Processing audio input.")
# Add audio processing logic here
summary = "Audio processing not implemented yet."
else:
summary = "Invalid input. Please provide a valid file or text."
logging.warning("Invalid input type provided.")
st.text_area("Report Result:", summary[0]['summary_text'] if isinstance(summary, list) else summary, height=200)
except Exception as e:
logging.error(f"Error during summarization: {e}")
st.error("An error occurred during summarization. Please check the logs for more details.")
logging.info("Closing the Streamlit app.")
if __name__ == "__main__":
main()