MHamdan commited on
Commit
1575e7a
·
verified ·
1 Parent(s): 50a21e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -58
app.py CHANGED
@@ -67,64 +67,61 @@ def analyze_text(input_text, input_type, tasks, progress=gr.Progress()):
67
  return original_text, summary, sentiment, ", ".join(topics)
68
 
69
  def create_interface():
70
- input_type = gr.Dropdown(["Text", "URL", "File"], label="Input Type")
71
- text_input = gr.Textbox(visible=False)
72
- url_input = gr.Textbox(visible=False)
73
- file_input = gr.File(visible=False)
74
-
75
- tasks_checkboxes = gr.CheckboxGroup(["Summarization", "Sentiment Analysis", "Topic Detection"], label="Analysis Tasks")
76
-
77
- submit_button = gr.Button("Analyze")
78
- progress_bar = gr.Progress()
79
-
80
- def update_input_visibility(input_type):
81
- return {
82
- text_input: gr.update(visible=input_type == "Text"),
83
- url_input: gr.update(visible=input_type == "URL"),
84
- file_input: gr.update(visible=input_type == "File"),
85
- }
86
-
87
- input_type.change(update_input_visibility, [input_type], [text_input, url_input, file_input])
88
-
89
- original_text_output = gr.Textbox(label="Original Text")
90
- summary_output = gr.Textbox(label="Summary")
91
- sentiment_output = gr.Textbox(label="Sentiment")
92
- topics_output = gr.Textbox(label="Topics")
93
-
94
- def process_input(input_type, text, url, file, tasks):
95
- if input_type == "Text":
96
- input_value = text
97
- elif input_type == "URL":
98
- input_value = url
99
- else:
100
- input_value = file
101
-
102
- try:
103
- original_text, summary, sentiment, topics = analyze_text(input_value, input_type, tasks, progress_bar)
104
- except Exception as e:
105
- original_text = f"Error: {str(e)}"
106
- summary, sentiment, topics = "", "", ""
107
-
108
- return original_text, summary, sentiment, topics
109
-
110
- submit_button.click(
111
- fn=process_input,
112
- inputs=[input_type, text_input, url_input, file_input, tasks_checkboxes],
113
- outputs=[original_text_output, summary_output, sentiment_output, topics_output]
114
- )
115
-
116
- interface = gr.TabbedInterface([
117
- gr.Tab(original_text_output, label="Original Text"),
118
- gr.Tab(summary_output, label="Summary"),
119
- gr.Tab(sentiment_output, label="Sentiment"),
120
- gr.Tab(topics_output, label="Topics")
121
- ])
122
-
123
- return gr.Blocks(
124
- title="Text Analysis App",
125
- inputs=[input_type, text_input, url_input, file_input, tasks_checkboxes, submit_button],
126
- outputs=[interface, progress_bar]
127
- )
128
 
129
  if __name__ == "__main__":
130
  create_interface().launch()
 
67
  return original_text, summary, sentiment, ", ".join(topics)
68
 
69
  def create_interface():
70
+ with gr.Blocks(title="Text Analysis App") as interface:
71
+ input_type = gr.Dropdown(["Text", "URL", "File"], label="Input Type")
72
+ text_input = gr.Textbox(visible=False)
73
+ url_input = gr.Textbox(visible=False)
74
+ file_input = gr.File(visible=False)
75
+
76
+ tasks_checkboxes = gr.CheckboxGroup(["Summarization", "Sentiment Analysis", "Topic Detection"], label="Analysis Tasks")
77
+
78
+ submit_button = gr.Button("Analyze")
79
+ progress_bar = gr.Progress()
80
+
81
+ original_text_output = gr.Textbox(label="Original Text")
82
+ summary_output = gr.Textbox(label="Summary")
83
+ sentiment_output = gr.Textbox(label="Sentiment")
84
+ topics_output = gr.Textbox(label="Topics")
85
+
86
+ def update_input_visibility(input_type):
87
+ text_input.visible = input_type == "Text"
88
+ url_input.visible = input_type == "URL"
89
+ file_input.visible = input_type == "File"
90
+
91
+ input_type.change(update_input_visibility, inputs=input_type)
92
+
93
+ def process_input(input_type, text, url, file, tasks):
94
+ if input_type == "Text":
95
+ input_value = text
96
+ elif input_type == "URL":
97
+ input_value = url
98
+ else:
99
+ input_value = file
100
+
101
+ try:
102
+ original_text, summary, sentiment, topics = analyze_text(input_value, input_type, tasks, progress_bar)
103
+ except Exception as e:
104
+ original_text = f"Error: {str(e)}"
105
+ summary, sentiment, topics = "", "", ""
106
+
107
+ return original_text, summary, sentiment, topics
108
+
109
+ submit_button.click(
110
+ fn=process_input,
111
+ inputs=[input_type, text_input, url_input, file_input, tasks_checkboxes],
112
+ outputs=[original_text_output, summary_output, sentiment_output, topics_output]
113
+ )
114
+
115
+ interface.set_layout(
116
+ gr.Tabs([
117
+ gr.Tab(original_text_output, label="Original Text"),
118
+ gr.Tab(summary_output, label="Summary"),
119
+ gr.Tab(sentiment_output, label="Sentiment"),
120
+ gr.Tab(topics_output, label="Topics")
121
+ ])
122
+ )
123
+
124
+ return interface
 
 
 
125
 
126
  if __name__ == "__main__":
127
  create_interface().launch()