Spaces:
Sleeping
Sleeping
Initial commit with full functionality
Browse filesFiles changed (4)
- app.pyAdded+61
- requirements.txtAdded+6
- smart_web_analyzer.pyAdded+54
- space.ymlAdded+5
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061# app.pyimport gradio as grfrom smart_web_analyzer import WebAnalyzer
analyzer = WebAnalyzer()
def format_results(results: dict) -> dict: """Format analysis results for Gradio tabs""" outputs = {} if 'error' in results: return {"π Error": f"β {results['error']}"} outputs["π Clean Text"] = results.get('clean_text', 'No text extracted') if 'summary' in results: outputs["π Summary"] = f"**AI Summary:**\n{results['summary']}" if 'sentiment' in results: outputs["π Sentiment"] = f"**Sentiment Score:**\n{results['sentiment']}" if 'topics' in results: topics = "\n".join([f"- **{k}**: {v:.2f}" for k,v in results['topics'].items()]) outputs["π Topics"] = f"**Detected Topics:**\n{topics}" return outputs
with gr.Blocks(title="Smart Web Analyzer Plus") as demo: gr.Markdown("# π Smart Web Analyzer Plus") with gr.Row(): url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com") modes = gr.CheckboxGroup(["summarize", "sentiment", "topics"], label="Analysis Types") submit_btn = gr.Button("Analyze", variant="primary") with gr.Tabs(): with gr.Tab("π Clean Text"): clean_text = gr.Markdown() with gr.Tab("π Summary"): summary = gr.Markdown() with gr.Tab("π Sentiment"): sentiment = gr.Markdown() with gr.Tab("π Topics"): topics = gr.Markdown() examples = gr.Examples( examples=[ ["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]], ["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]] ], inputs=[url_input, modes] ) submit_btn.click( fn=lambda url, m: format_results(analyzer.analyze(url, m)), inputs=[url_input, modes], outputs=[clean_text, summary, sentiment, topics] )
if __name__ == "__main__": demo.launch()123456# requirements.txtgradio>=4.0.0beautifulsoup4>=4.12.0requests>=2.31.0transformers>=4.40.0torch>=2.2.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# smart_web_analyzer.pyimport requestsfrom bs4 import BeautifulSoupfrom transformers import pipelineimport torch
class WebAnalyzer: def __init__(self): self.device = 0 if torch.cuda.is_available() else -1 self.models = { 'summarize': pipeline("summarization", model="facebook/bart-large-cnn"), 'sentiment': pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment"), 'topics': pipeline("zero-shot-classification", model="facebook/bart-large-mnli") } def fetch_content(self, url: str) -> str: """Fetch webpage content with custom headers""" headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'} response = requests.get(url, headers=headers, timeout=15) response.raise_for_status() return response.text
def clean_html(self, html: str) -> str: """Basic HTML cleaning preserving all tags""" soup = BeautifulSoup(html, 'html.parser') return soup.prettify()
def analyze(self, url: str, modes: list) -> dict: """Core analysis pipeline""" results = {} try: html = self.fetch_content(url) results['clean_text'] = self.clean_html(html) if 'summarize' in modes: results['summary'] = self.models['summarize'](html, max_length=150)[0]['summary_text'] if 'sentiment' in modes: sentiment = self.models['sentiment'](html[:512])[0] results['sentiment'] = f"{sentiment['label']} ({sentiment['score']:.2f})" if 'topics' in modes: topics = self.models['topics'](html[:512], candidate_labels=["Technology", "AI", "Business", "Science", "Politics"]) results['topics'] = {topic: score for topic, score in zip(topics['labels'], topics['scores'])} except Exception as e: results['error'] = str(e) return results12345# space.ymltitle: Content Web Analyzer Plussdk: gradiopython: version: "3.10"app.pyAdded+61
requirements.txtAdded+6
smart_web_analyzer.pyAdded+54
space.ymlAdded+5