Spaces:
Sleeping
Sleeping
initial commit: main app files
Browse files- app.py +145 -4
- jinaai.py +46 -0
- requirements.txt +9 -0
app.py
CHANGED
@@ -1,7 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
demo.launch()
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append('.') # Add current directory to path
|
3 |
+
|
4 |
+
from smolagents import ToolCallingAgent, tool, HfApiModel, DuckDuckGoSearchTool, CodeAgent
|
5 |
+
from jinaai import scrape_page_with_jina_ai, search_facts_with_jina_ai
|
6 |
import gradio as gr
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
import os
|
9 |
+
import datetime
|
10 |
+
import time
|
11 |
+
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Initialize agent
|
15 |
+
model_id = "meta-llama/Llama-3.3-70B-Instruct" #"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
16 |
+
|
17 |
+
agent = CodeAgent(
|
18 |
+
tools=[scrape_page_with_jina_ai, search_facts_with_jina_ai, DuckDuckGoSearchTool()],
|
19 |
+
model=HfApiModel(model_id=model_id),
|
20 |
+
max_steps=7
|
21 |
+
)
|
22 |
+
|
23 |
+
def analyze_urls(urls: str, prompt: str, progress=gr.Progress()) -> str:
|
24 |
+
"""Analyze multiple URLs based on the given prompt"""
|
25 |
+
try:
|
26 |
+
progress(0, desc="Starting analysis...")
|
27 |
+
url_list = [url.strip() for url in urls.split('\n') if url.strip()]
|
28 |
+
|
29 |
+
progress(0.3, desc="Analyzing URLs...")
|
30 |
+
# Create bullet-pointed list of URLs with simple newline join
|
31 |
+
url_bullets = "\n".join(f"- {url}" for url in url_list)
|
32 |
+
|
33 |
+
result = agent.run(f"""Analyze these URLs:
|
34 |
+
{url_bullets}
|
35 |
+
|
36 |
+
Create a comprehensive report that answers: {prompt}
|
37 |
+
|
38 |
+
Format the report in markdown with these sections:
|
39 |
+
1. π Overall Summary
|
40 |
+
2. π Analysis by Source
|
41 |
+
- Include key findings from each URL
|
42 |
+
- Compare and contrast information across sources
|
43 |
+
3. π‘ Consolidated Analysis
|
44 |
+
4. π Sources
|
45 |
+
|
46 |
+
Make it visually appealing with clear headings and bullet points.""")
|
47 |
+
|
48 |
+
progress(0.9, desc="Formatting report...")
|
49 |
+
formatted_result = f"""# π Multi-URL Analysis Report
|
50 |
+
|
51 |
+
## π Analyzed URLs
|
52 |
+
{url_bullets}
|
53 |
+
|
54 |
+
## β Analysis Prompt
|
55 |
+
> {prompt}
|
56 |
+
|
57 |
+
---
|
58 |
+
|
59 |
+
{result}
|
60 |
+
|
61 |
+
### π Metadata
|
62 |
+
- **Generated**: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
63 |
+
- **URLs Analyzed**: {len(url_list)}
|
64 |
+
"""
|
65 |
+
progress(1.0, desc="Done!")
|
66 |
+
return formatted_result
|
67 |
+
except Exception as e:
|
68 |
+
return f"""### β Error
|
69 |
+
|
70 |
+
**Analysis Failed**
|
71 |
+
```
|
72 |
+
Error during analysis: {str(e)}
|
73 |
+
```
|
74 |
+
|
75 |
+
Please check the URLs and try again."""
|
76 |
+
|
77 |
+
def show_loading():
|
78 |
+
return """# β³ Analyzing...
|
79 |
+
|
80 |
+
## Current Status
|
81 |
+
- π Fetching webpage content
|
82 |
+
- π€ AI processing
|
83 |
+
- π Generating report
|
84 |
+
|
85 |
+
Please wait while we analyze your URLs..."""
|
86 |
|
87 |
+
# Create Gradio interface
|
88 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
89 |
+
gr.Markdown("# π URL Analyzer")
|
90 |
+
gr.Markdown("""
|
91 |
+
Enter multiple URLs separated by new lines and what you want to know about them.
|
92 |
+
The AI will analyze the content and create a detailed report.
|
93 |
+
|
94 |
+
---
|
95 |
+
""")
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
urls = gr.Textbox(
|
99 |
+
label="URLs",
|
100 |
+
placeholder="https://example.com\nhttps://example.org",
|
101 |
+
scale=2
|
102 |
+
)
|
103 |
+
prompt = gr.Textbox(
|
104 |
+
label="What do you want to know?",
|
105 |
+
placeholder="What are the main points discussed?",
|
106 |
+
scale=2
|
107 |
+
)
|
108 |
+
|
109 |
+
submit = gr.Button("π Analyze", variant="primary", size="lg")
|
110 |
+
status = gr.Markdown("", elem_id="status")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
output = gr.Markdown(label="Analysis Report", show_label=False, value="")
|
114 |
+
|
115 |
+
# Example inputs with better descriptions
|
116 |
+
gr.Examples(
|
117 |
+
label="Example Analyses",
|
118 |
+
examples=[
|
119 |
+
["https://www.dabangasudan.org/en/all-news/category/news",
|
120 |
+
"What are the latest developments in Sudan's conflict, focusing on humanitarian situation and military movements?"],
|
121 |
+
["https://www.dabangasudan.org/en/all-news/category/news",
|
122 |
+
"Analyze the economic and agricultural situation in Sudan based on recent news."],
|
123 |
+
["https://littlesis.org/research/reports/",
|
124 |
+
"What are the latest corporate influence investigations and their key findings?"],
|
125 |
+
["https://littlesis.org/research/reports/",
|
126 |
+
"Summarize the recent reports about energy companies and environmental impact."]
|
127 |
+
],
|
128 |
+
inputs=[urls, prompt]
|
129 |
+
)
|
130 |
+
|
131 |
+
def clear_output():
|
132 |
+
return "", ""
|
133 |
+
|
134 |
+
submit.click(
|
135 |
+
fn=show_loading,
|
136 |
+
outputs=output,
|
137 |
+
).then(
|
138 |
+
fn=analyze_urls,
|
139 |
+
inputs=[urls, prompt],
|
140 |
+
outputs=output,
|
141 |
+
show_progress="full"
|
142 |
+
).then(
|
143 |
+
fn=lambda: gr.update(visible=True),
|
144 |
+
outputs=[status]
|
145 |
+
)
|
146 |
|
147 |
+
if __name__ == "__main__":
|
148 |
+
demo.queue().launch(share=True)
|
jinaai.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from requests.exceptions import RequestException
|
4 |
+
import datetime
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from smolagents import tool
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
headers = {'Authorization': 'Bearer ' + os.getenv('JINA_API_KEY')}
|
11 |
+
|
12 |
+
@tool
|
13 |
+
def scrape_page_with_jina_ai(url: str) -> str:
|
14 |
+
"""Scrapes content from a webpage using Jina AI's web scraping service.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
url: The URL of the webpage to scrape. Must be a valid web address to extract content from.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
str: The scraped content in markdown format.
|
21 |
+
"""
|
22 |
+
try:
|
23 |
+
print(f"Scraping Jina AI..: {url}")
|
24 |
+
response = requests.get("https://r.jina.ai/" + url, headers=headers)
|
25 |
+
response.raise_for_status()
|
26 |
+
return response.text
|
27 |
+
except RequestException as e:
|
28 |
+
return f"Error scraping webpage: {str(e)}"
|
29 |
+
|
30 |
+
@tool
|
31 |
+
def search_facts_with_jina_ai(query: str) -> str:
|
32 |
+
"""Searches for facts and information using Jina AI's search service.
|
33 |
+
|
34 |
+
Args:
|
35 |
+
query: The search query string used to find relevant facts and information.
|
36 |
+
|
37 |
+
Returns:
|
38 |
+
str: The search results in markdown format containing relevant facts and information.
|
39 |
+
"""
|
40 |
+
try:
|
41 |
+
print(f"Searching Jina AI..: {query}")
|
42 |
+
response = requests.get("https://s.jina.ai/" + query, headers=headers)
|
43 |
+
response.raise_for_status()
|
44 |
+
return response.text
|
45 |
+
except RequestException as e:
|
46 |
+
return f"Error searching facts: {str(e)}"
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
groq
|
3 |
+
requests
|
4 |
+
python-dotenv
|
5 |
+
markdownify
|
6 |
+
duckduckgo-search
|
7 |
+
litellm
|
8 |
+
huggingface-hub
|
9 |
+
smolagents
|