Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,78 +16,16 @@ tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
16 |
def tokenize_text(input_text):
|
17 |
return tokenizer.encode(input_text, truncation=False)
|
18 |
|
19 |
-
# Function to summarize text
|
20 |
-
def
|
21 |
-
tokens = tokenize_text(input_text)
|
22 |
-
text_tokens = len(tokens)
|
23 |
-
if text_tokens < 0.05 * TOKEN_LIMIT:
|
24 |
-
return "Error: Text too small to summarize."
|
25 |
-
|
26 |
-
batches = math.ceil(text_tokens / TOKEN_LIMIT)
|
27 |
-
summaries = []
|
28 |
-
|
29 |
-
while batches > 1:
|
30 |
-
sentences = sent_tokenize(input_text)
|
31 |
-
avg_batch_size = math.ceil(len(sentences) / batches)
|
32 |
-
text_batches = [
|
33 |
-
" ".join(sentences[i:i+avg_batch_size])
|
34 |
-
for i in range(0, len(sentences), avg_batch_size)
|
35 |
-
]
|
36 |
-
|
37 |
-
batch_summaries = []
|
38 |
-
for batch in text_batches:
|
39 |
-
max_length = int((TOKEN_LIMIT / batches) * 0.9)
|
40 |
-
min_length = int((TOKEN_LIMIT / batches) * 0.5)
|
41 |
-
summary_output = text_summary(
|
42 |
-
batch,
|
43 |
-
min_length=min_length,
|
44 |
-
max_length=max_length
|
45 |
-
)
|
46 |
-
batch_summaries.append(summary_output[0]['summary_text'])
|
47 |
-
|
48 |
-
input_text = " ".join(batch_summaries)
|
49 |
-
tokens = tokenize_text(input_text)
|
50 |
-
text_tokens = len(tokens)
|
51 |
-
batches = math.ceil(text_tokens / TOKEN_LIMIT)
|
52 |
-
|
53 |
-
if text_tokens < 0.05 * TOKEN_LIMIT:
|
54 |
-
return "Error: Text too small to summarize."
|
55 |
-
|
56 |
-
return input_text
|
57 |
-
|
58 |
-
# Gradio button to set max/min lengths
|
59 |
-
user_lengths = {"min_length": None, "max_length": None}
|
60 |
-
|
61 |
-
def set_max_min_lengths(latest_text):
|
62 |
-
tokens = tokenize_text(latest_text)
|
63 |
-
text_tokens = len(tokens)
|
64 |
-
max_range = int(0.8 * text_tokens)
|
65 |
-
min_range = int(0.2 * text_tokens)
|
66 |
-
return f"Set max_length between {min_range} and {max_range} tokens."
|
67 |
-
|
68 |
-
def validate_lengths(min_length, max_length, text):
|
69 |
-
if not (isinstance(min_length, int) and isinstance(max_length, int)):
|
70 |
-
return "Error: Length values must be integers."
|
71 |
-
if min_length <= 0 or max_length <= 0 or min_length >= max_length:
|
72 |
-
return "Error: Invalid length values. Ensure min_length < max_length and both are positive."
|
73 |
-
user_lengths["min_length"] = min_length
|
74 |
-
user_lengths["max_length"] = max_length
|
75 |
-
return "Done"
|
76 |
-
|
77 |
-
def summarize_text(input_text):
|
78 |
-
if user_lengths["min_length"] is None or user_lengths["max_length"] is None:
|
79 |
-
return "Error: Please set valid max and min lengths first."
|
80 |
summary_output = text_summary(
|
81 |
input_text,
|
82 |
-
min_length=
|
83 |
-
max_length=
|
84 |
)
|
85 |
return summary_output[0]['summary_text']
|
86 |
|
87 |
-
#
|
88 |
-
gr.close_all()
|
89 |
-
|
90 |
-
# Article Example
|
91 |
article_example = """Niteesh Nigam: A Visionary in Robotics, Machine Vision, and AI
|
92 |
|
93 |
Niteesh Nigam, a forward-thinking robotics engineer and AI developer, has consistently demonstrated his passion for innovation and his ability to transform complex technological concepts into impactful real-world solutions. With a Master’s degree in Robotics and Autonomous Systems from Arizona State University (ASU) and a Bachelor’s degree in Mechanical Engineering from the Birla Institute of Technology and Science, Pilani Dubai, Niteesh has cultivated a robust foundation in engineering, computer vision, and artificial intelligence. His interdisciplinary expertise and hands-on experience have made him a standout professional in fields such as robotics, machine vision, deep learning, and control systems.
|
@@ -135,33 +73,39 @@ Niteesh Nigam’s work embodies a perfect blend of technical mastery, innovation
|
|
135 |
As he looks to the future, Niteesh remains committed to making meaningful contributions in robotics, AI, and automation, with a focus on scalable solutions that benefit industries and communities alike. His journey is a testament to the transformative potential of technology when guided by a visionary like him.
|
136 |
"""
|
137 |
|
|
|
|
|
|
|
138 |
# Create Gradio interface
|
139 |
with gr.Blocks() as demo:
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
|
|
|
|
149 |
summarize_button = gr.Button("Summarize")
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
analyze_button.click(analyze_and_set_text, inputs=text_input, outputs=[analysis_output, length_result])
|
159 |
-
set_lengths_button.click(
|
160 |
-
validate_lengths,
|
161 |
-
inputs=[min_length_input, max_length_input, analysis_output],
|
162 |
-
outputs=length_result
|
163 |
)
|
164 |
-
summarize_button.click(summarize_text, inputs=analysis_output, outputs=summary_output)
|
165 |
|
166 |
# Launch the Gradio app
|
167 |
-
demo.launch(share=False)
|
|
|
16 |
def tokenize_text(input_text):
|
17 |
return tokenizer.encode(input_text, truncation=False)
|
18 |
|
19 |
+
# Function to summarize text
|
20 |
+
def summarize_text(input_text, min_length=50, max_length=200):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
summary_output = text_summary(
|
22 |
input_text,
|
23 |
+
min_length=min_length,
|
24 |
+
max_length=max_length
|
25 |
)
|
26 |
return summary_output[0]['summary_text']
|
27 |
|
28 |
+
# Preload article and summary
|
|
|
|
|
|
|
29 |
article_example = """Niteesh Nigam: A Visionary in Robotics, Machine Vision, and AI
|
30 |
|
31 |
Niteesh Nigam, a forward-thinking robotics engineer and AI developer, has consistently demonstrated his passion for innovation and his ability to transform complex technological concepts into impactful real-world solutions. With a Master’s degree in Robotics and Autonomous Systems from Arizona State University (ASU) and a Bachelor’s degree in Mechanical Engineering from the Birla Institute of Technology and Science, Pilani Dubai, Niteesh has cultivated a robust foundation in engineering, computer vision, and artificial intelligence. His interdisciplinary expertise and hands-on experience have made him a standout professional in fields such as robotics, machine vision, deep learning, and control systems.
|
|
|
73 |
As he looks to the future, Niteesh remains committed to making meaningful contributions in robotics, AI, and automation, with a focus on scalable solutions that benefit industries and communities alike. His journey is a testament to the transformative potential of technology when guided by a visionary like him.
|
74 |
"""
|
75 |
|
76 |
+
# Precomputed summary
|
77 |
+
summary_example = summarize_text(article_example, min_length=50, max_length=150)
|
78 |
+
|
79 |
# Create Gradio interface
|
80 |
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown("## **Niteesh Nigam Portfolio: Summarization Showcase**")
|
82 |
+
gr.Markdown(
|
83 |
+
"This app showcases the ability to summarize complex content into concise information. Below is an example:"
|
84 |
+
)
|
85 |
+
text_input = gr.Textbox(
|
86 |
+
label="Input Text",
|
87 |
+
lines=10,
|
88 |
+
value=article_example,
|
89 |
+
interactive=False
|
90 |
+
)
|
91 |
+
summary_output = gr.Textbox(
|
92 |
+
label="Summarized Output",
|
93 |
+
lines=10,
|
94 |
+
value=summary_example,
|
95 |
+
interactive=False
|
96 |
+
)
|
97 |
|
98 |
+
gr.Markdown("## Try It Yourself!")
|
99 |
+
custom_input = gr.Textbox(label="Input Your Text", lines=10, placeholder="Paste your text here...")
|
100 |
summarize_button = gr.Button("Summarize")
|
101 |
+
custom_summary_output = gr.Textbox(label="Summary Output", lines=10)
|
102 |
+
|
103 |
+
# Button action for custom input
|
104 |
+
summarize_button.click(
|
105 |
+
summarize_text,
|
106 |
+
inputs=[custom_input],
|
107 |
+
outputs=[custom_summary_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
|
|
109 |
|
110 |
# Launch the Gradio app
|
111 |
+
demo.launch(share=False)
|