Spaces:
Sleeping
Sleeping
fix bugs
Browse files
app.py
CHANGED
|
@@ -36,7 +36,7 @@ def reliable_url(url:str)-> bool:
|
|
| 36 |
return False
|
| 37 |
|
| 38 |
@tool
|
| 39 |
-
def
|
| 40 |
"""A tool that receives a news url and returns the main claim of the webpage content and some aditional context
|
| 41 |
Args:
|
| 42 |
url: the address of the webpage we want to summarize into main claim and context
|
|
@@ -45,22 +45,27 @@ def url_to_claim(url:str)-> dict:
|
|
| 45 |
response = requests.get(url, timeout=10)
|
| 46 |
response.raise_for_status()
|
| 47 |
except requests.exceptions.RequestException as e:
|
| 48 |
-
return f"An error occurred: {e}"
|
| 49 |
|
| 50 |
soup = BeautifulSoup(response.content, "html.parser")
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
context = soup.get_text()[:5000]
|
| 53 |
|
| 54 |
-
return {"
|
| 55 |
|
| 56 |
@tool
|
| 57 |
-
def support_decision(claim:str, news
|
| 58 |
"""A tool that decides if a given news article supports, contradicts or have an unclear opinion about a given claim
|
| 59 |
Args:
|
| 60 |
claim: the claim we want to decide whether it is supported or contradicted
|
| 61 |
news: the news that may support or contradict the claim
|
| 62 |
"""
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
prompt = f"""
|
| 66 |
Analyse the sentiment of the provided news article in contrast with the given claim and respond with only one of the following words
|
|
@@ -68,12 +73,13 @@ def support_decision(claim:str, news="":dict)-> str:
|
|
| 68 |
|
| 69 |
Claim: "{claim}"
|
| 70 |
|
| 71 |
-
News: "{news}
|
|
|
|
| 72 |
|
| 73 |
Response:
|
| 74 |
"""
|
| 75 |
|
| 76 |
-
|
| 77 |
response = model.run(prompt)
|
| 78 |
response = response.strip().lower()
|
| 79 |
|
|
@@ -110,16 +116,18 @@ def check(claim:str)-> str:
|
|
| 110 |
Args:
|
| 111 |
claim: the claim we want to check for support
|
| 112 |
"""
|
|
|
|
|
|
|
| 113 |
support_reliable = []
|
| 114 |
contradict_reliable = []
|
| 115 |
support_others = []
|
| 116 |
contradict_others = []
|
| 117 |
|
| 118 |
-
news_articles =
|
| 119 |
for article in news_articles:
|
| 120 |
reliable_source = reliable_url(article)
|
| 121 |
-
news =
|
| 122 |
-
decision = support_decision(claim, news)
|
| 123 |
if reliable_source:
|
| 124 |
if decision == "supports":
|
| 125 |
support_reliable.append(article)
|
|
@@ -169,7 +177,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 169 |
agent = ToolCallingAgent(
|
| 170 |
model=model,
|
| 171 |
tools=[final_answer,
|
| 172 |
-
|
| 173 |
support_decision,
|
| 174 |
search,
|
| 175 |
check],
|
|
|
|
| 36 |
return False
|
| 37 |
|
| 38 |
@tool
|
| 39 |
+
def url_to_news(url:str)-> dict:
|
| 40 |
"""A tool that receives a news url and returns the main claim of the webpage content and some aditional context
|
| 41 |
Args:
|
| 42 |
url: the address of the webpage we want to summarize into main claim and context
|
|
|
|
| 45 |
response = requests.get(url, timeout=10)
|
| 46 |
response.raise_for_status()
|
| 47 |
except requests.exceptions.RequestException as e:
|
| 48 |
+
return {"title": "Error", "context": f"An error occurred: {e}"}
|
| 49 |
|
| 50 |
soup = BeautifulSoup(response.content, "html.parser")
|
| 51 |
+
if soup.title:
|
| 52 |
+
title = soup.title.text
|
| 53 |
+
else:
|
| 54 |
+
title = ""
|
| 55 |
+
|
| 56 |
context = soup.get_text()[:5000]
|
| 57 |
|
| 58 |
+
return {"title": title, "context": context}
|
| 59 |
|
| 60 |
@tool
|
| 61 |
+
def support_decision(model, claim:str, news:dict=None)-> str:
|
| 62 |
"""A tool that decides if a given news article supports, contradicts or have an unclear opinion about a given claim
|
| 63 |
Args:
|
| 64 |
claim: the claim we want to decide whether it is supported or contradicted
|
| 65 |
news: the news that may support or contradict the claim
|
| 66 |
"""
|
| 67 |
+
if news is None:
|
| 68 |
+
news = {"title": "", "context": ""}
|
| 69 |
|
| 70 |
prompt = f"""
|
| 71 |
Analyse the sentiment of the provided news article in contrast with the given claim and respond with only one of the following words
|
|
|
|
| 73 |
|
| 74 |
Claim: "{claim}"
|
| 75 |
|
| 76 |
+
News: "{news["title"]}
|
| 77 |
+
{news["context"]}"
|
| 78 |
|
| 79 |
Response:
|
| 80 |
"""
|
| 81 |
|
| 82 |
+
try:
|
| 83 |
response = model.run(prompt)
|
| 84 |
response = response.strip().lower()
|
| 85 |
|
|
|
|
| 116 |
Args:
|
| 117 |
claim: the claim we want to check for support
|
| 118 |
"""
|
| 119 |
+
analyzer = HfApiModel()
|
| 120 |
+
|
| 121 |
support_reliable = []
|
| 122 |
contradict_reliable = []
|
| 123 |
support_others = []
|
| 124 |
contradict_others = []
|
| 125 |
|
| 126 |
+
news_articles = search(claim)
|
| 127 |
for article in news_articles:
|
| 128 |
reliable_source = reliable_url(article)
|
| 129 |
+
news = url_to_news(article)
|
| 130 |
+
decision = support_decision(analyzer, claim, news)
|
| 131 |
if reliable_source:
|
| 132 |
if decision == "supports":
|
| 133 |
support_reliable.append(article)
|
|
|
|
| 177 |
agent = ToolCallingAgent(
|
| 178 |
model=model,
|
| 179 |
tools=[final_answer,
|
| 180 |
+
url_to_news,
|
| 181 |
support_decision,
|
| 182 |
search,
|
| 183 |
check],
|