Spaces:
Sleeping
Sleeping
Initial commit with full functionality extend app
Browse files
app.py
CHANGED
|
@@ -1,61 +1,154 @@
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from smart_web_analyzer import WebAnalyzer
|
|
|
|
| 4 |
|
| 5 |
analyzer = WebAnalyzer()
|
| 6 |
|
| 7 |
-
def format_results(results:
|
| 8 |
-
"""Format analysis results for Gradio
|
| 9 |
-
outputs = {}
|
| 10 |
if 'error' in results:
|
| 11 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if 'topics' in results:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
return
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
with gr.Row():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
with gr.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
examples=[
|
| 48 |
["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
|
| 49 |
["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
|
| 50 |
],
|
| 51 |
-
inputs=[url_input, modes]
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
fn=lambda url, m: format_results(analyzer.analyze(url, m)),
|
| 56 |
inputs=[url_input, modes],
|
| 57 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from smart_web_analyzer import WebAnalyzer
|
| 4 |
+
from typing import Dict, List, Any
|
| 5 |
|
| 6 |
analyzer = WebAnalyzer()
|
| 7 |
|
| 8 |
+
def format_results(results: Dict[str, Any]) -> Dict[str, str]:
|
| 9 |
+
"""Format analysis results for Gradio components"""
|
|
|
|
| 10 |
if 'error' in results:
|
| 11 |
+
return {
|
| 12 |
+
"clean_text": f"β Error: {results['error']}",
|
| 13 |
+
"summary": "",
|
| 14 |
+
"sentiment": "",
|
| 15 |
+
"topics": ""
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
formatted = {}
|
| 19 |
|
| 20 |
+
# Format clean text
|
| 21 |
+
text = results.get('clean_text', 'No text extracted')
|
| 22 |
+
formatted["clean_text"] = text[:2000] + "..." if len(text) > 2000 else text
|
| 23 |
+
|
| 24 |
+
# Format summary
|
| 25 |
+
formatted["summary"] = (
|
| 26 |
+
f"**AI Summary:**\n{results['summary']}"
|
| 27 |
+
if 'summary' in results else ""
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Format sentiment
|
| 31 |
+
formatted["sentiment"] = (
|
| 32 |
+
f"**Sentiment Analysis:**\n{results['sentiment']}"
|
| 33 |
+
if 'sentiment' in results else ""
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Format topics
|
| 37 |
if 'topics' in results:
|
| 38 |
+
topics_list = sorted(
|
| 39 |
+
results['topics'].items(),
|
| 40 |
+
key=lambda x: x[1],
|
| 41 |
+
reverse=True
|
| 42 |
+
)
|
| 43 |
+
topics_text = "\n".join(
|
| 44 |
+
f"- **{topic}**: {score:.1%}"
|
| 45 |
+
for topic, score in topics_list
|
| 46 |
+
)
|
| 47 |
+
formatted["topics"] = f"**Detected Topics:**\n{topics_text}"
|
| 48 |
+
else:
|
| 49 |
+
formatted["topics"] = ""
|
| 50 |
|
| 51 |
+
return formatted
|
| 52 |
|
| 53 |
+
def validate_url(url: str) -> bool:
|
| 54 |
+
"""Basic URL validation"""
|
| 55 |
+
return bool(url and url.strip().startswith(('http://', 'https://')))
|
| 56 |
+
|
| 57 |
+
def update_button_state(url: str) -> Dict:
|
| 58 |
+
"""Update button state based on URL validity"""
|
| 59 |
+
return gr.update(interactive=validate_url(url))
|
| 60 |
+
|
| 61 |
+
with gr.Blocks(title="Smart Web Analyzer Plus", theme=gr.themes.Soft()) as demo:
|
| 62 |
+
# Header
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
"""
|
| 65 |
+
# π Smart Web Analyzer Plus
|
| 66 |
+
Analyze web content using AI to extract summaries, determine sentiment, and identify topics.
|
| 67 |
+
"""
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# Input Section
|
| 71 |
with gr.Row():
|
| 72 |
+
with gr.Column(scale=4):
|
| 73 |
+
url_input = gr.Textbox(
|
| 74 |
+
label="Enter URL",
|
| 75 |
+
placeholder="https://example.com",
|
| 76 |
+
show_label=True
|
| 77 |
+
)
|
| 78 |
+
with gr.Column(scale=2):
|
| 79 |
+
modes = gr.CheckboxGroup(
|
| 80 |
+
choices=["summarize", "sentiment", "topics"],
|
| 81 |
+
label="Analysis Types",
|
| 82 |
+
value=["summarize"], # Default selection
|
| 83 |
+
show_label=True
|
| 84 |
+
)
|
| 85 |
+
with gr.Column(scale=1):
|
| 86 |
+
analyze_btn = gr.Button(
|
| 87 |
+
"Analyze",
|
| 88 |
+
variant="primary",
|
| 89 |
+
interactive=False
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Results Section
|
| 93 |
+
with gr.Tabs() as tabs:
|
| 94 |
+
with gr.TabItem("π Clean Text"):
|
| 95 |
+
clean_text_output = gr.Markdown()
|
| 96 |
+
with gr.TabItem("π Summary"):
|
| 97 |
+
summary_output = gr.Markdown()
|
| 98 |
+
with gr.TabItem("π Sentiment"):
|
| 99 |
+
sentiment_output = gr.Markdown()
|
| 100 |
+
with gr.TabItem("π Topics"):
|
| 101 |
+
topics_output = gr.Markdown()
|
| 102 |
+
|
| 103 |
+
# Loading indicator
|
| 104 |
+
with gr.Row():
|
| 105 |
+
status = gr.Markdown("")
|
| 106 |
+
|
| 107 |
+
# Example Section
|
| 108 |
+
gr.Examples(
|
| 109 |
examples=[
|
| 110 |
["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
|
| 111 |
["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
|
| 112 |
],
|
| 113 |
+
inputs=[url_input, modes],
|
| 114 |
+
label="Try these examples"
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Event Handlers
|
| 118 |
+
url_input.change(
|
| 119 |
+
fn=update_button_state,
|
| 120 |
+
inputs=[url_input],
|
| 121 |
+
outputs=[analyze_btn],
|
| 122 |
+
queue=False
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
def on_analyze_start():
|
| 126 |
+
return gr.update(value="β³ Analysis in progress...")
|
| 127 |
+
|
| 128 |
+
def on_analyze_end():
|
| 129 |
+
return gr.update(value="")
|
| 130 |
+
|
| 131 |
+
analyze_btn.click(
|
| 132 |
+
fn=on_analyze_start,
|
| 133 |
+
outputs=[status],
|
| 134 |
+
queue=False
|
| 135 |
+
).then(
|
| 136 |
fn=lambda url, m: format_results(analyzer.analyze(url, m)),
|
| 137 |
inputs=[url_input, modes],
|
| 138 |
+
outputs=[
|
| 139 |
+
clean_text_output,
|
| 140 |
+
summary_output,
|
| 141 |
+
sentiment_output,
|
| 142 |
+
topics_output
|
| 143 |
+
]
|
| 144 |
+
).then(
|
| 145 |
+
fn=on_analyze_end,
|
| 146 |
+
outputs=[status]
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
| 150 |
+
demo.launch(
|
| 151 |
+
share=False, # Set to True to create a public link
|
| 152 |
+
server_name="0.0.0.0", # Allow external connections
|
| 153 |
+
server_port=7860 # Default Gradio port
|
| 154 |
+
)
|