MHamdan commited on
Commit
588a44d
Β·
verified Β·
1 Parent(s): 5d80e4f

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +4 -66
  2. requirements.txt +3 -10
  3. tool.py +24 -19
app.py CHANGED
@@ -1,68 +1,6 @@
 
 
1
 
2
- import gradio as gr
3
- from smolagents import load_tool
4
- import torch # Add explicit torch import
5
 
6
- # Ensure CUDA is available
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
8
-
9
- # Load the tool
10
- web_analyzer = load_tool("MHamdan/web-analyzer", trust_remote_code=True)
11
-
12
- def create_interface():
13
- with gr.Blocks(title="AI Web Analyzer") as iface:
14
- gr.Markdown("# πŸ€– AI-Powered Web Content Analyzer")
15
- gr.Markdown("""
16
- ## Features:
17
- - πŸ“Š **Analyze**: Complete content analysis with AI summary
18
- - πŸ“ **Summarize**: AI-generated multi-section summary
19
- - 😊 **Sentiment**: Section-by-section sentiment analysis
20
- - 🎯 **Topics**: AI topic classification
21
- """)
22
-
23
- with gr.Row():
24
- with gr.Column():
25
- url_input = gr.Textbox(
26
- label="Webpage URL",
27
- placeholder="Enter URL to analyze..."
28
- )
29
- mode = gr.Dropdown(
30
- choices=["analyze", "summarize", "sentiment", "topics"],
31
- label="Analysis Mode",
32
- value="analyze"
33
- )
34
- submit_btn = gr.Button("Analyze Content", variant="primary")
35
-
36
- with gr.Column():
37
- output = gr.Textbox(
38
- label="AI Analysis Results",
39
- lines=15
40
- )
41
-
42
- # Example data using artificial-intelligence-news.com
43
- examples = [
44
- ["https://www.artificialintelligence-news.com/2024/02/14/openai-anthropic-google-white-house-red-teaming/", "analyze"],
45
- ["https://www.artificialintelligence-news.com/2024/02/13/ai-21-labs-wordtune-chatgpt-plugin/", "summarize"],
46
- ["https://www.artificialintelligence-news.com/2024/02/12/google-responds-gemini-ai-historical-images/", "sentiment"],
47
- ["https://www.artificialintelligence-news.com/2024/02/09/anthropic-claude-3-models-preview/", "topics"]
48
- ]
49
-
50
- gr.Examples(
51
- examples=examples,
52
- inputs=[url_input, mode],
53
- outputs=output,
54
- label="Try these examples:",
55
- cache_examples=True
56
- )
57
-
58
- submit_btn.click(
59
- fn=web_analyzer,
60
- inputs=[url_input, mode],
61
- outputs=output
62
- )
63
-
64
- return iface
65
-
66
- # Create and launch the interface
67
- demo = create_interface()
68
- demo.launch()
 
1
+ from smolagents import launch_gradio_demo
2
+ from tool import SimpleTool
3
 
4
+ tool = SimpleTool()
 
 
5
 
6
+ launch_gradio_demo(tool)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,12 +1,5 @@
1
-
2
- gradio>=4.0.0
3
- beautifulsoup4>=4.9.3
4
- requests>=2.25.1
5
- smolagents
6
  transformers
7
  torch
8
- accelerate
9
- sacremoses
10
- sentencepiece
11
- protobuf
12
- scipy
 
1
+ bs4
2
+ requests
 
 
 
3
  transformers
4
  torch
5
+ smolagents
 
 
 
 
tool.py CHANGED
@@ -21,48 +21,53 @@ class SimpleTool(Tool):
21
  from bs4 import BeautifulSoup
22
  import re
23
  from transformers import pipeline
 
 
 
 
24
 
25
  try:
26
- # Setup headers
27
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
28
-
29
- # Fetch content
30
  response = requests.get(url, headers=headers, timeout=10)
31
  response.raise_for_status()
32
 
33
- # Parse content
34
  soup = BeautifulSoup(response.text, 'html.parser')
 
 
35
  for tag in soup(['script', 'style', 'meta']):
36
  tag.decompose()
37
 
38
- # Get cleaned text
39
  title = soup.title.string if soup.title else "No title found"
40
  title = re.sub(r'\s+', ' ', title).strip()
41
  text_content = re.sub(r'\s+', ' ', soup.get_text()).strip()
42
 
43
- # Initialize ML models based on mode
 
 
44
  if mode == "analyze":
45
- # Basic analysis with summary
46
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
47
- classifier = pipeline("text-classification",
48
- model="nlptown/bert-base-multilingual-uncased-sentiment")
49
 
50
- # Get summary and sentiment
51
- summary = summarizer(text_content[:1024], max_length=100, min_length=30)[0]['summary_text']
52
- sentiment = classifier(text_content[:512])[0]
53
- sent_score = int(sentiment['label'][0])
54
- sent_text = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"][sent_score-1]
55
 
56
- # Format output
57
- return f"""πŸ“Š Content Analysis
58
 
59
  Title: {title}
60
- Length: {len(text_content)} characters
61
 
62
  πŸ“ AI Summary:
63
  {summary}
64
 
65
- 😊 Overall Sentiment: {sent_text} ({sent_score}/5)"""
 
 
 
 
 
 
66
 
67
  elif mode == "summarize":
68
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
21
  from bs4 import BeautifulSoup
22
  import re
23
  from transformers import pipeline
24
+ import torch
25
+
26
+ # Check if GPU is available
27
+ device = 0 if torch.cuda.is_available() else -1
28
 
29
  try:
 
30
  headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
 
 
31
  response = requests.get(url, headers=headers, timeout=10)
32
  response.raise_for_status()
33
 
 
34
  soup = BeautifulSoup(response.text, 'html.parser')
35
+
36
+ # Remove scripts and styles
37
  for tag in soup(['script', 'style', 'meta']):
38
  tag.decompose()
39
 
 
40
  title = soup.title.string if soup.title else "No title found"
41
  title = re.sub(r'\s+', ' ', title).strip()
42
  text_content = re.sub(r'\s+', ' ', soup.get_text()).strip()
43
 
44
+ if len(text_content) < 100:
45
+ return "Error: Not enough content to analyze"
46
+
47
  if mode == "analyze":
48
+ try:
49
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
50
+ classifier = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment", device=device)
 
51
 
52
+ summary = summarizer(text_content[:1024], max_length=100, min_length=30)[0]['summary_text']
53
+ sentiment = classifier(text_content[:512])[0]
54
+ sent_score = int(sentiment['label'][0])
55
+ sent_text = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"][sent_score-1]
 
56
 
57
+ return f"""πŸ“Š Content Analysis
 
58
 
59
  Title: {title}
 
60
 
61
  πŸ“ AI Summary:
62
  {summary}
63
 
64
+ 😊 Overall Sentiment: {sent_text} ({sent_score}/5)
65
+
66
+ Length: {len(text_content)} characters"""
67
+
68
+ except Exception as e:
69
+ return f"Error with AI analysis: {str(e)}. Please check if PyTorch and transformers are properly installed."
70
+
71
 
72
  elif mode == "summarize":
73
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")