-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (82 loc) · 3.04 KB
/
app.py
File metadata and controls
96 lines (82 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
from dotenv import load_dotenv
import asyncio
import streamlit as st
from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
# --- Page Configuration ---
st.set_page_config(
page_title="URL Summarizer",
page_icon="📄",
layout="centered"
)
# --- Configuration ---
@st.cache_resource
def setup_agent():
"""Initialize the agent with MCP server."""
load_dotenv()
if "GROQ_API_KEY" not in os.environ:
st.error("GROQ_API_KEY not found in environment. Please set it in your .env file.")
return None
try:
mcp_server = MCPServerStdio(command="python", args=["-m", "mcp_server_fetch"])
agent = Agent(model="groq:llama-3.3-70b-versatile", mcp_servers=[mcp_server])
return agent
except Exception as e:
st.error(f"Error setting up agent: {str(e)}")
return None
# --- Functions ---
async def summarize_url(agent, url: str) -> str:
"""Fetch content from URL and summarize it."""
try:
async with agent.run_mcp_servers():
result = await agent.run(
f"Please use the fetch tool to get the content from {url} and then provide a comprehensive summary of what you find. Make sure to actually fetch the content and summarize it, don't just show the function call."
)
return result.output
except Exception as e:
return f"Error: {str(e)}"
def run_async_summarization(agent, url):
"""Wrapper to run async function in Streamlit."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(summarize_url(agent, url))
finally:
loop.close()
# --- Streamlit App ---
def main():
st.title("URL Content Summarizer")
st.write("Enter a URL to generate an AI-powered summary of its content.")
# Initialize agent
agent = setup_agent()
if agent is None:
st.stop()
# URL input
url_input = st.text_input(
"URL:",
placeholder="https://example.com/article"
)
# Summarize button
if st.button("Summarize", type="primary"):
if not url_input:
st.warning("Please enter a URL.")
return
if not url_input.startswith(('http://', 'https://')):
st.error("Please enter a valid URL starting with http:// or https://")
return
# Show loading and process
with st.spinner("Processing..."):
try:
summary = run_async_summarization(agent, url_input)
if "<function=" in summary and "</function>" in summary:
st.error("Failed to process URL. Please try again.")
else:
st.success("Summary completed")
st.write("**Summary:**")
st.write(summary)
st.write(f"**Source:** {url_input}")
except Exception as e:
st.error("An error occurred while processing the URL.")
if __name__ == "__main__":
main()