Spaces:
Running
Running
Update app.py
Browse files
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="
|
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 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def process_input(input_type, input_data):
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
if input_type == "Text":
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
return "
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
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
|
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=
|
80 |
-
inputs=[
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
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__":
|