Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,13 @@ import requests
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
-
from transformers import pipeline
|
8 |
import gradio as gr
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
# 0. Tool to get the topic from the user (not used directly in the UI)
|
11 |
@tool
|
12 |
def get_topic() -> str:
|
13 |
"""
|
@@ -28,7 +31,8 @@ def search_web(query: str) -> str:
|
|
28 |
Returns:
|
29 |
str: A snippet from the top search result (excluding Hugging Face-related pages).
|
30 |
"""
|
31 |
-
|
|
|
32 |
print("DuckDuckGo Search Results:", results) # Debug logging
|
33 |
if results and len(results) > 0:
|
34 |
# Iterate over results to skip pages with 'huggingface' in the URL
|
@@ -52,8 +56,13 @@ def summarise_content(text: str) -> str:
|
|
52 |
Returns:
|
53 |
str: The summarized text.
|
54 |
"""
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
57 |
return summary[0]['summary_text']
|
58 |
|
59 |
# 3. Tool to generate a social media post based on the summary
|
@@ -97,19 +106,21 @@ agent = CodeAgent(
|
|
97 |
prompt_templates=prompt_templates
|
98 |
)
|
99 |
|
100 |
-
#
|
101 |
-
# 1. Accept the topic
|
102 |
-
# 2.
|
103 |
-
# 3. Summarize the search result
|
104 |
-
# 4. Generate a social media post
|
105 |
def agent_interface(user_input_topic: str) -> str:
|
106 |
topic = user_input_topic
|
107 |
search_result = search_web(topic)
|
|
|
|
|
108 |
summary = summarise_content(search_result)
|
109 |
post = write_social_media_post(summary)
|
110 |
return post
|
111 |
|
112 |
-
# Define a Gradio Interface with a customized landing page
|
113 |
interface = gr.Interface(
|
114 |
fn=agent_interface,
|
115 |
inputs=gr.Textbox(label="Enter a Topic", placeholder="e.g., AI developments, climate change..."),
|
@@ -126,4 +137,4 @@ interface = gr.Interface(
|
|
126 |
"""
|
127 |
)
|
128 |
|
129 |
-
interface.launch()
|
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
7 |
import gradio as gr
|
8 |
+
from transformers import pipeline
|
9 |
+
|
10 |
+
# Initialize the summarization pipeline globally to avoid reloading it on every call.
|
11 |
+
summarizer_pipeline = pipeline("summarization")
|
12 |
|
13 |
+
# 0. Tool to get the topic from the user (not used directly in the Gradio UI)
|
14 |
@tool
|
15 |
def get_topic() -> str:
|
16 |
"""
|
|
|
31 |
Returns:
|
32 |
str: A snippet from the top search result (excluding Hugging Face-related pages).
|
33 |
"""
|
34 |
+
duck_tool = DuckDuckGoSearchTool() # instantiate the search tool
|
35 |
+
results = duck_tool(query=query)
|
36 |
print("DuckDuckGo Search Results:", results) # Debug logging
|
37 |
if results and len(results) > 0:
|
38 |
# Iterate over results to skip pages with 'huggingface' in the URL
|
|
|
56 |
Returns:
|
57 |
str: The summarized text.
|
58 |
"""
|
59 |
+
# If text is too short, return it directly.
|
60 |
+
if len(text.split()) < 30:
|
61 |
+
return text
|
62 |
+
# If text is too long, trim to the first 1000 characters.
|
63 |
+
if len(text) > 1000:
|
64 |
+
text = text[:1000]
|
65 |
+
summary = summarizer_pipeline(text, max_length=130, min_length=30, do_sample=False)
|
66 |
return summary[0]['summary_text']
|
67 |
|
68 |
# 3. Tool to generate a social media post based on the summary
|
|
|
106 |
prompt_templates=prompt_templates
|
107 |
)
|
108 |
|
109 |
+
# Function to chain the agent actions:
|
110 |
+
# 1. Accept the topic.
|
111 |
+
# 2. Use the topic to perform a web search.
|
112 |
+
# 3. Summarize the search result.
|
113 |
+
# 4. Generate a social media post.
|
114 |
def agent_interface(user_input_topic: str) -> str:
|
115 |
topic = user_input_topic
|
116 |
search_result = search_web(topic)
|
117 |
+
if not search_result.strip():
|
118 |
+
return "No valid search result found."
|
119 |
summary = summarise_content(search_result)
|
120 |
post = write_social_media_post(summary)
|
121 |
return post
|
122 |
|
123 |
+
# Define a Gradio Interface with a customized landing page.
|
124 |
interface = gr.Interface(
|
125 |
fn=agent_interface,
|
126 |
inputs=gr.Textbox(label="Enter a Topic", placeholder="e.g., AI developments, climate change..."),
|
|
|
137 |
"""
|
138 |
)
|
139 |
|
140 |
+
interface.launch(share=True)
|