mdasad3617 commited on
Commit
304e1bc
·
verified ·
1 Parent(s): e608232

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -55
app.py CHANGED
@@ -1,63 +1,85 @@
1
- import gradio as gr
2
  import logging
3
- from transformers import pipeline
 
 
 
 
4
 
5
- class TextSummarizer:
6
- def __init__(self, model_name="facebook/bart-large-cnn"):
7
- self.summarizer = pipeline("summarization", model=model_name)
8
-
9
- def summarize(self, text):
10
- if not text:
11
- return "No text to summarize."
12
- try:
13
- summary = self.summarizer(
14
- text,
15
- max_length=150,
16
- min_length=50,
17
- do_sample=False
18
- )[0]['summary_text']
19
- return summary
20
- except Exception as e:
21
- return f"Summarization error: {str(e)}"
22
-
23
- def process_input(input_type, input_data):
24
- try:
25
- # Direct text handling
26
- if input_type == "Text":
27
- return summarizer.summarize(input_data)
28
-
29
- # File input handling (simplified)
30
- if input_type in ["Text File", "PDF", "DOCX"]:
31
- with open(input_data.name, 'r', encoding='utf-8') as file:
32
- text = file.read()
33
- return summarizer.summarize(text)
34
-
35
- # Audio input (placeholder - would require speech-to-text)
36
- if input_type == "Audio":
37
- return "Audio summarization not implemented"
38
-
39
- return "Invalid input type"
40
-
41
- except Exception as e:
42
- return f"Processing error: {str(e)}"
43
 
44
  def main():
45
- global summarizer
46
- summarizer = TextSummarizer()
 
47
 
48
- # Gradio Interface
49
- interface = gr.Interface(
50
- fn=process_input,
51
- inputs=[
52
- gr.Radio(["Text", "Text File", "PDF", "DOCX", "Audio"], label="Input Type"),
53
- gr.File(type="file", label="Input")
54
- ],
55
- outputs=gr.Textbox(label="Summary"),
56
- title="Text Summarization App",
57
- description="Upload text or select input type for summarization"
 
58
  )
59
-
60
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- main()
 
1
+ import streamlit as st
2
  import logging
3
+ from models.summarizer import TextSummarizer
4
+ from services.text_input_handler import handle_text_input
5
+ from services.file_input_handler import read_text_file, read_pdf_file, read_docx_file
6
+ from services.audio_input_handler import audio_to_text
7
+ from utils.logging_utils import setup_logging
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def main():
11
+ # Setup logging
12
+ setup_logging()
13
+ logging.info("Starting GenAI Lab Report Analyzer with Streamlit.")
14
 
15
+ # Initialize summarizer
16
+ summarizer = TextSummarizer()
17
+
18
+ # Streamlit UI
19
+ st.title("GenAI Lab Report Analyzer")
20
+ st.write("Upload a file, record audio, or type text to generate a summary. Select the appropriate input type and provide the input.")
21
+
22
+ input_type = st.radio(
23
+ "Select Input Type:",
24
+ options=["Text", "Text File", "PDF", "DOCX", "Audio"],
25
+ index=0
26
  )
27
+
28
+ file = None
29
+ text = None
30
+ audio = None
31
+
32
+ if input_type == "Text":
33
+ text = st.text_area("Enter your text here:", placeholder="Type your text here...")
34
+ elif input_type in ["Text File", "PDF", "DOCX"]:
35
+ file = st.file_uploader(f"Upload your {input_type}:", type=["txt", "pdf", "docx"])
36
+ elif input_type == "Audio":
37
+ audio = st.file_uploader("Upload your audio file:", type=["wav", "mp3", "m4a"])
38
+
39
+ if st.button("Report Result"):
40
+ try:
41
+ if input_type == "Text" and text:
42
+ logging.info("Processing text input.")
43
+ processed_text = handle_text_input(text)
44
+ summary = summarizer.summarize(processed_text)
45
+ logging.info("Text input processed successfully.")
46
+ elif input_type in ["Text File", "PDF", "DOCX"] and file:
47
+ if input_type == "Text File":
48
+ logging.info(f"Processing text file: {file.name}")
49
+ processed_text = read_text_file(file)
50
+ elif input_type == "PDF":
51
+ logging.info(f"Processing PDF file: {file.name}")
52
+ processed_text = read_pdf_file(file)
53
+ elif input_type == "DOCX":
54
+ logging.info(f"Processing DOCX file: {file.name}")
55
+ processed_text = read_docx_file(file)
56
+
57
+ if processed_text:
58
+ summary = summarizer.summarize(processed_text)
59
+ logging.info(f"{input_type} processed successfully.")
60
+ else:
61
+ summary = "Failed to process the file. Check logs for more details."
62
+ logging.error(f"Failed to process {input_type}: {file.name}")
63
+ elif input_type == "Audio" and audio:
64
+ logging.info("Processing audio input.")
65
+ processed_text = audio_to_text(audio)
66
+ if processed_text:
67
+ summary = summarizer.summarize(processed_text)
68
+ logging.info("Audio input processed successfully.")
69
+ else:
70
+ summary = "Failed to convert audio to text. Check logs for more details."
71
+ logging.error("Failed to convert audio to text.")
72
+ else:
73
+ summary = "Invalid input. Please provide a valid file or text."
74
+ logging.warning("Invalid input type provided.")
75
+
76
+ st.text_area("Report Result:", summary, height=200)
77
+ except Exception as e:
78
+ logging.error(f"Error during summarization: {e}")
79
+ st.error("An error occurred during summarization. Please check the logs for more details.")
80
+
81
+ logging.info("Closing GenAI Lab Report Analyzer with Streamlit.")
82
+
83
 
84
  if __name__ == "__main__":
85
+ main()