Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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__":
|
93 |
+
main()
|