mdasad3617 commited on
Commit
717ad69
·
verified ·
1 Parent(s): 0927160

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
  import logging
4
  from PyPDF2 import PdfReader
5
 
@@ -21,12 +21,19 @@ def extract_text_from_pdf(pdf_file):
21
  text += page.extract_text()
22
  return text
23
 
 
 
 
 
 
 
24
  def main():
25
  setup_logging()
26
  logging.info("Starting the Streamlit app.")
27
 
28
- # Initialize the summarization pipeline with a public model for long medical texts
29
- summarizer = pipeline("summarization", model="allenai/led-large-16384-arxiv")
 
30
 
31
  # Streamlit UI
32
  st.title("GenAI Lab Report Analyzer")
@@ -58,16 +65,16 @@ def main():
58
  summary = None
59
  if input_type == "Text" and text:
60
  logging.info("Processing text input.")
61
- summary = summarizer(text, max_length=2000, min_length=1500, do_sample=False)
62
  logging.info("Text input processed successfully.")
63
  elif input_type == "Text File" and file:
64
  logging.info(f"Processing text file: {file.name}")
65
  text = file.read().decode("utf-8") # Assuming UTF-8 encoding
66
- summary = summarizer(text, max_length=2000, min_length=1500, do_sample=False)
67
  elif input_type == "PDF" and file:
68
  logging.info(f"Processing PDF file: {file.name}")
69
  text = extract_text_from_pdf(file)
70
- summary = summarizer(text, max_length=2000, min_length=1500, do_sample=False)
71
  elif input_type == "Audio" and audio:
72
  logging.info("Processing audio input.")
73
  # Add audio processing logic here
@@ -76,8 +83,7 @@ def main():
76
  summary = "Invalid input. Please provide a valid file or text."
77
  logging.warning("Invalid input type provided.")
78
 
79
- # Display the summarized result
80
- st.text_area("Report Result:", summary[0]['summary_text'] if isinstance(summary, list) else summary, height=200)
81
  except Exception as e:
82
  logging.error(f"Error during summarization: {e}")
83
  st.error("An error occurred during summarization. Please check the logs for more details.")
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import logging
4
  from PyPDF2 import PdfReader
5
 
 
21
  text += page.extract_text()
22
  return text
23
 
24
+ # Function to summarize text using the specified model and tokenizer
25
+ def summarize_text(model, tokenizer, text):
26
+ inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=1024, truncation=True)
27
+ outputs = model.generate(inputs, max_length=130, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
28
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
  def main():
31
  setup_logging()
32
  logging.info("Starting the Streamlit app.")
33
 
34
+ # Load the model and tokenizer
35
+ tokenizer = AutoTokenizer.from_pretrained("Falconsai/medical_summarization")
36
+ model = AutoModelForSeq2SeqLM.from_pretrained("Falconsai/medical_summarization")
37
 
38
  # Streamlit UI
39
  st.title("GenAI Lab Report Analyzer")
 
65
  summary = None
66
  if input_type == "Text" and text:
67
  logging.info("Processing text input.")
68
+ summary = summarize_text(model, tokenizer, text)
69
  logging.info("Text input processed successfully.")
70
  elif input_type == "Text File" and file:
71
  logging.info(f"Processing text file: {file.name}")
72
  text = file.read().decode("utf-8") # Assuming UTF-8 encoding
73
+ summary = summarize_text(model, tokenizer, text)
74
  elif input_type == "PDF" and file:
75
  logging.info(f"Processing PDF file: {file.name}")
76
  text = extract_text_from_pdf(file)
77
+ summary = summarize_text(model, tokenizer, text)
78
  elif input_type == "Audio" and audio:
79
  logging.info("Processing audio input.")
80
  # Add audio processing logic here
 
83
  summary = "Invalid input. Please provide a valid file or text."
84
  logging.warning("Invalid input type provided.")
85
 
86
+ st.text_area("Report Result:", summary, height=200)
 
87
  except Exception as e:
88
  logging.error(f"Error during summarization: {e}")
89
  st.error("An error occurred during summarization. Please check the logs for more details.")