mdasad3617's picture
Create app.py
8e4890d verified
raw
history blame
3.65 kB
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 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")
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 interface_fn(input_type, input_data):
updated_input = update_input_type(input_type)
return process_input(input_type, input_data), updated_input
interface = gr.Interface(
fn=interface_fn,
inputs=[input_type, input_data],
outputs=[gr.outputs.Textbox(label="Report Result"), input_data],
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()