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