Spaces:
Sleeping
Sleeping
Initial commit with full functionality extend app req
Browse files- app.py +68 -143
- requirements.txt +5 -7
app.py
CHANGED
|
@@ -1,154 +1,79 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Format clean text
|
| 22 |
-
text = results.get('clean_text', 'No text extracted')
|
| 23 |
-
formatted["clean_text"] = text[:2000] + "..." if len(text) > 2000 else text
|
| 24 |
-
|
| 25 |
-
# Format summary
|
| 26 |
-
formatted["summary"] = (
|
| 27 |
-
f"**AI Summary:**\n{results['summary']}"
|
| 28 |
-
if 'summary' in results else "No summary requested"
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# Format sentiment
|
| 32 |
-
formatted["sentiment"] = (
|
| 33 |
-
f"**Sentiment Analysis:**\n{results['sentiment']}"
|
| 34 |
-
if 'sentiment' in results else "No sentiment analysis requested"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
# Format topics
|
| 38 |
-
if 'topics' in results:
|
| 39 |
-
topics_list = sorted(
|
| 40 |
-
results['topics'].items(),
|
| 41 |
-
key=lambda x: x[1],
|
| 42 |
-
reverse=True
|
| 43 |
-
)
|
| 44 |
-
topics_text = "\n".join(
|
| 45 |
-
f"- **{topic}**: {score:.1%}"
|
| 46 |
-
for topic, score in topics_list
|
| 47 |
-
)
|
| 48 |
-
formatted["topics"] = f"**Detected Topics:**\n{topics_text}"
|
| 49 |
-
else:
|
| 50 |
-
formatted["topics"] = "No topic analysis requested"
|
| 51 |
-
|
| 52 |
-
return formatted
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.Markdown("# 🌐 Smart Web Analyzer Plus")
|
| 65 |
-
gr.Markdown("Analyze web content using AI to extract summaries, determine sentiment, and identify topics.")
|
| 66 |
-
|
| 67 |
-
# Input Section
|
| 68 |
-
with gr.Row():
|
| 69 |
-
with gr.Column(scale=3):
|
| 70 |
-
url_input = gr.Textbox(
|
| 71 |
-
label="Enter URL",
|
| 72 |
-
placeholder="https://example.com",
|
| 73 |
-
show_label=True
|
| 74 |
-
)
|
| 75 |
-
with gr.Column(scale=2):
|
| 76 |
-
analysis_types = gr.CheckboxGroup(
|
| 77 |
-
choices=["summarize", "sentiment", "topics"],
|
| 78 |
-
label="Analysis Types",
|
| 79 |
-
value=["summarize"],
|
| 80 |
-
show_label=True
|
| 81 |
-
)
|
| 82 |
-
with gr.Column(scale=1):
|
| 83 |
-
analyze_btn = gr.Button(
|
| 84 |
-
"Analyze",
|
| 85 |
-
variant="primary",
|
| 86 |
-
interactive=False
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# Content display
|
| 90 |
-
clean_text_out = gr.Markdown(visible=True, label="Clean Text")
|
| 91 |
-
summary_out = gr.Markdown(visible=True, label="Summary")
|
| 92 |
-
sentiment_out = gr.Markdown(visible=True, label="Sentiment")
|
| 93 |
-
topics_out = gr.Markdown(visible=True, label="Topics")
|
| 94 |
-
|
| 95 |
-
with gr.Tabs() as tabs:
|
| 96 |
-
with gr.Tab("📄 Clean Text"):
|
| 97 |
-
clean_text_out
|
| 98 |
-
with gr.Tab("📝 Summary"):
|
| 99 |
-
summary_out
|
| 100 |
-
with gr.Tab("🎭 Sentiment"):
|
| 101 |
-
sentiment_out
|
| 102 |
-
with gr.Tab("📊 Topics"):
|
| 103 |
-
topics_out
|
| 104 |
-
|
| 105 |
-
# Loading indicator
|
| 106 |
-
status = gr.Markdown(visible=False)
|
| 107 |
-
|
| 108 |
-
# Example Section
|
| 109 |
-
gr.Examples(
|
| 110 |
-
label="Try these examples",
|
| 111 |
-
examples=[
|
| 112 |
-
["https://www.bbc.com/news/technology-67881954", ["summarize", "sentiment"]],
|
| 113 |
-
["https://arxiv.org/html/2312.17296v1", ["topics", "summarize"]]
|
| 114 |
-
],
|
| 115 |
-
inputs=[url_input, analysis_types]
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
# Event Handlers
|
| 119 |
-
url_input.change(
|
| 120 |
-
fn=update_button_state,
|
| 121 |
-
inputs=[url_input],
|
| 122 |
-
outputs=[analyze_btn],
|
| 123 |
-
queue=False
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
def on_analyze_start():
|
| 127 |
-
return gr.update(value="⏳ Analysis in progress...", visible=True)
|
| 128 |
-
|
| 129 |
-
def on_analyze_end():
|
| 130 |
-
return gr.update(value="", visible=False)
|
| 131 |
-
|
| 132 |
-
analyze_btn.click(
|
| 133 |
-
fn=on_analyze_start,
|
| 134 |
-
outputs=[status],
|
| 135 |
-
queue=False
|
| 136 |
-
).then(
|
| 137 |
-
fn=lambda url, m: format_results(analyzer.analyze(url, m)),
|
| 138 |
-
inputs=[url_input, analysis_types],
|
| 139 |
-
outputs=[
|
| 140 |
-
clean_text_out,
|
| 141 |
-
summary_out,
|
| 142 |
-
sentiment_out,
|
| 143 |
-
topics_out
|
| 144 |
-
]
|
| 145 |
-
).then(
|
| 146 |
-
fn=on_analyze_end,
|
| 147 |
-
outputs=[status]
|
| 148 |
-
)
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if __name__ == "__main__":
|
| 151 |
-
|
| 152 |
server_name="0.0.0.0",
|
| 153 |
server_port=7860
|
| 154 |
)
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 3 |
+
import datetime
|
| 4 |
+
import requests
|
| 5 |
+
import pytz
|
| 6 |
+
import yaml
|
| 7 |
+
from tools.final_answer import FinalAnswerTool
|
| 8 |
+
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# Helper function for text extraction
|
| 11 |
+
def extract_text_tool(url: str) -> str:
|
| 12 |
+
"""A tool that extracts clean text content from a webpage
|
| 13 |
+
Args:
|
| 14 |
+
url: The URL of the webpage to analyze
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
response = requests.get(url, timeout=10)
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
return response.text
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error extracting text: {str(e)}"
|
| 22 |
|
| 23 |
+
# Time zone tool from the example
|
| 24 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 26 |
+
Args:
|
| 27 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
+
"""
|
| 29 |
+
try:
|
| 30 |
+
tz = pytz.timezone(timezone)
|
| 31 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Initialize components
|
| 37 |
+
final_answer = FinalAnswerTool()
|
| 38 |
+
search_tool = DuckDuckGoSearchTool()
|
| 39 |
|
| 40 |
+
# Setup the model
|
| 41 |
+
model = HfApiModel(
|
| 42 |
+
max_tokens=2096,
|
| 43 |
+
temperature=0.5,
|
| 44 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 45 |
+
custom_role_conversions=None,
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
# Import image generation tool
|
| 49 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Load prompt templates
|
| 52 |
+
with open("prompts.yaml", 'r') as stream:
|
| 53 |
+
prompt_templates = yaml.safe_load(stream)
|
| 54 |
+
|
| 55 |
+
# Create the agent with all tools
|
| 56 |
+
agent = CodeAgent(
|
| 57 |
+
model=model,
|
| 58 |
+
tools=[
|
| 59 |
+
final_answer,
|
| 60 |
+
search_tool, # For web search
|
| 61 |
+
tool(extract_text_tool), # For text extraction
|
| 62 |
+
tool(get_current_time_in_timezone), # For timezone checks
|
| 63 |
+
image_generation_tool # For image generation
|
| 64 |
+
],
|
| 65 |
+
max_steps=6,
|
| 66 |
+
verbosity_level=1,
|
| 67 |
+
grammar=None,
|
| 68 |
+
planning_interval=None,
|
| 69 |
+
name="Web Analyzer Agent",
|
| 70 |
+
description="An agent that analyzes web content using various tools",
|
| 71 |
+
prompt_templates=prompt_templates
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Launch the Gradio interface
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
GradioUI(agent).launch(
|
| 77 |
server_name="0.0.0.0",
|
| 78 |
server_port=7860
|
| 79 |
)
|
requirements.txt
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
gradio
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
torch>=2.2.0
|
| 7 |
-
requests
|
|
|
|
| 1 |
+
smolagents
|
| 2 |
+
gradio
|
| 3 |
+
requests
|
| 4 |
+
pytz
|
| 5 |
+
pyyaml
|
|
|
|
|
|