File size: 3,641 Bytes
ad77dee
4751082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad77dee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4751082
 
 
ad77dee
4751082
 
ad77dee
4751082
 
ad77dee
 
 
4751082
ad77dee
 
 
 
 
 
 
 
 
 
 
 
 
4751082
ad77dee
 
4751082
ad77dee
 
 
 
 
 
 
 
 
 
 
4751082
ad77dee
4751082
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import gradio as gr
import logging
from transformers import pipeline
from services.text_input_handler import handle_text_input
from services.file_input_handler import read_text_file, read_pdf_file, read_docx_file
from services.audio_input_handler import audio_to_text
from utils.logging_utils import setup_logging

class TextSummarizer:
    def __init__(self, model_name="t5-small"):
        self.summarizer = pipeline("summarization", model=model_name)

    def summarize(self, text):
        if not text:
            return "No text to summarize."
        summary = self.summarizer(text, max_length=150, min_length=30, do_sample=False)
        return summary[0]['summary_text']

def process_input(input_type, input_data):
    try:
        logging.info(f"Processing input type: {input_type}")
        
        if input_type == "Text":
            processed_text = handle_text_input(input_data)
        elif input_type == "Text File":
            processed_text = read_text_file(input_data)
        elif input_type == "PDF":
            processed_text = read_pdf_file(input_data)
        elif input_type == "DOCX":
            processed_text = read_docx_file(input_data)
        elif input_type == "Audio":
            processed_text = audio_to_text(input_data)
        else:
            return "Invalid input type."

        if processed_text:
            summary = summarizer.summarize(processed_text)
            logging.info(f"{input_type} processed successfully.")
            return summary
        else:
            logging.error(f"Failed to process {input_type}.")
            return "Failed to process the input. Check logs for more details."
    except Exception as e:
        logging.error(f"Error during summarization: {e}")
        return "An error occurred during summarization. Please check the logs for more details."

def main():
    # Setup logging
    setup_logging()
    logging.info("Starting GenAI Lab Report Analyzer with Gradio.")
    
    # Initialize summarizer
    global summarizer
    summarizer = TextSummarizer()

    # Gradio interface
    input_type = gr.inputs.Radio(choices=["Text", "Text File", "PDF", "DOCX", "Audio"], label="Select Input Type")
    input_data = gr.inputs.Textbox(lines=5, label="Enter your text here")  # Default for text input

    def update_input_type(input_type):
        if input_type == "Text":
            return gr.update(value="", placeholder="Type your text here...")
        elif input_type == "Text File":
            return gr.update(value=None, type="file", label="Upload your text file")
        elif input_type == "PDF":
            return gr.update(value=None, type="file", label="Upload your PDF file")
        elif input_type == "DOCX":
            return gr.update(value=None, type="file", label="Upload your DOCX file")
        elif input_type == "Audio":
            return gr.update(value=None, type="file", label="Upload your audio file")
        else:
            return gr.update(value="", placeholder="Invalid input type")

    input_data = gr.inputs.Textbox(lines=5, label="Enter your text here")  # Default for text input
    output = gr.outputs.Textbox(label="Report Result")

    interface = gr.Interface(
        fn=process_input,
        inputs=[input_type, input_data],
        outputs=output,
        title="GenAI Lab Report Analyzer",
        description="Upload a file, record audio, or type text to generate a summary. Select the appropriate input type and provide the input.",
        live=True,
        theme="default",
        layout="vertical",
        allow_flagging="never"
    )

    interface.launch()

if __name__ == "__main__":
    main()