mdasad3617 commited on
Commit
7426b1c
·
verified ·
1 Parent(s): 8e4890d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -69
app.py CHANGED
@@ -1,92 +1,62 @@
1
  import gradio as gr
2
  import logging
3
  from transformers import pipeline
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
  class TextSummarizer:
10
- def __init__(self, model_name="t5-small"):
11
  self.summarizer = pipeline("summarization", model=model_name)
12
-
13
  def summarize(self, text):
14
  if not text:
15
  return "No text to summarize."
16
- summary = self.summarizer(text, max_length=150, min_length=30, do_sample=False)
17
- return summary[0]['summary_text']
 
 
 
 
 
 
 
 
18
 
19
  def process_input(input_type, input_data):
20
  try:
21
- logging.info(f"Processing input type: {input_type}")
22
-
23
  if input_type == "Text":
24
- processed_text = handle_text_input(input_data)
25
- elif input_type == "Text File":
26
- processed_text = read_text_file(input_data)
27
- elif input_type == "PDF":
28
- processed_text = read_pdf_file(input_data)
29
- elif input_type == "DOCX":
30
- processed_text = read_docx_file(input_data)
31
- elif input_type == "Audio":
32
- processed_text = audio_to_text(input_data)
33
- else:
34
- return "Invalid input type."
35
-
36
- if processed_text:
37
- summary = summarizer.summarize(processed_text)
38
- logging.info(f"{input_type} processed successfully.")
39
- return summary
40
- else:
41
- logging.error(f"Failed to process {input_type}.")
42
- return "Failed to process the input. Check logs for more details."
43
  except Exception as e:
44
- logging.error(f"Error during summarization: {e}")
45
- return "An error occurred during summarization. Please check the logs for more details."
46
-
47
- def update_input_type(input_type):
48
- if input_type == "Text":
49
- return gr.update(value="", placeholder="Type your text here...")
50
- elif input_type == "Text File":
51
- return gr.update(value=None, type="file", label="Upload your text file")
52
- elif input_type == "PDF":
53
- return gr.update(value=None, type="file", label="Upload your PDF file")
54
- elif input_type == "DOCX":
55
- return gr.update(value=None, type="file", label="Upload your DOCX file")
56
- elif input_type == "Audio":
57
- return gr.update(value=None, type="file", label="Upload your audio file")
58
- else:
59
- return gr.update(value="", placeholder="Invalid input type")
60
 
61
  def main():
62
- # Setup logging
63
- setup_logging()
64
- logging.info("Starting GenAI Lab Report Analyzer with Gradio.")
65
-
66
- # Initialize summarizer
67
  global summarizer
68
  summarizer = TextSummarizer()
69
-
70
- # Gradio interface
71
- input_type = gr.inputs.Radio(choices=["Text", "Text File", "PDF", "DOCX", "Audio"], label="Select Input Type")
72
- input_data = gr.inputs.Textbox(lines=5, label="Enter your text here") # Default for text input
73
-
74
- def interface_fn(input_type, input_data):
75
- updated_input = update_input_type(input_type)
76
- return process_input(input_type, input_data), updated_input
77
-
78
  interface = gr.Interface(
79
- fn=interface_fn,
80
- inputs=[input_type, input_data],
81
- outputs=[gr.outputs.Textbox(label="Report Result"), input_data],
82
- title="GenAI Lab Report Analyzer",
83
- description="Upload a file, record audio, or type text to generate a summary. Select the appropriate input type and provide the input.",
84
- live=True,
85
- theme="default",
86
- layout="vertical",
87
- allow_flagging="never"
88
  )
89
-
90
  interface.launch()
91
 
92
  if __name__ == "__main__":
 
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__":