Spaces:
Sleeping
Sleeping
Francisco Zanartu
commited on
Commit
•
f8b8183
1
Parent(s):
fab9ab6
new app version, Mixtral-8x7B-Instruct-v0.1 LLM
Browse files- app.py +3 -157
- requirements.txt +8 -5
- test.ipynb +233 -0
- utils/climate_fever_cards.csv +0 -0
- utils/core.py +233 -0
- utils/definitions.py +18 -0
- auxiliar.py → utils/examples.py +2 -85
- utils/templates.py +84 -0
app.py
CHANGED
@@ -1,167 +1,13 @@
|
|
1 |
"""
|
2 |
Module for detecting fallacies in text.
|
3 |
|
4 |
-
Functions:
|
5 |
-
- rebuttal_generator: Detects fallacies in a text input by utilizing models for fallacy
|
6 |
-
detection and semantic textual similarity and generates a rebuttal for the fallacious claim.
|
7 |
-
- query: Sends a query to a specified API endpoint with the provided payload and returns
|
8 |
-
the response.
|
9 |
-
- demo: Launches a Gradio interface for interactively detecting fallacies in text.
|
10 |
-
|
11 |
-
Dependencies:
|
12 |
-
- os: Provides a portable way of using operating system dependent functionality.
|
13 |
-
- json: Provides functions for encoding and decoding JSON data.
|
14 |
-
- requests: Allows sending HTTP requests easily.
|
15 |
-
- gradio: Facilitates the creation of customizable UI components for machine learning models.
|
16 |
-
- langchain_google_genai: Wrapper for Google Generative AI language models.
|
17 |
-
- auxiliar: Contains auxiliary data used in the fallacy detection process.
|
18 |
-
|
19 |
-
Environment Variables:
|
20 |
-
- HF_API_KEY: API key for accessing Hugging Face model APIs.
|
21 |
-
- GOOGLE_API_KEY: API key for accessing Google APIs.
|
22 |
-
|
23 |
-
Constants:
|
24 |
-
- FLICC_MODEL: API endpoint for the FLICC model used for fallacy detection.
|
25 |
-
- CARDS_MODEL: API endpoint for the CARDS model used for fallacy detection.
|
26 |
-
- SEMANTIC_TEXTUAL_SIMILARITY: API endpoint for the model used for semantic textual similarity.
|
27 |
-
|
28 |
-
Global Variables:
|
29 |
-
- hf_api_key: API key for accessing Hugging Face model APIs.
|
30 |
-
- google_key: API key for accessing Google APIs.
|
31 |
-
- safety_settings: Settings for safety measures in the Google Generative AI model.
|
32 |
-
- llm: Instance of the GoogleGenerativeAI class for text generation.
|
33 |
-
- similarity_template: Template for generating prompts for similarity comparison.
|
34 |
-
- FALLACY_CLAIMS: Dictionary containing fallacy labels and corresponding claims.
|
35 |
-
- DEBUNKINGS: Dictionary containing debunkings for fallacy claims.
|
36 |
-
- DEFINITIONS: Dictionary containing definitions for fallacy labels.
|
37 |
"""
|
38 |
|
39 |
import os
|
40 |
-
import json
|
41 |
-
import requests
|
42 |
import gradio as gr
|
43 |
-
from
|
44 |
-
from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
|
45 |
-
from langchain.prompts import PromptTemplate
|
46 |
-
from langchain.chains import LLMChain
|
47 |
-
from langchain.schema.messages import HumanMessage, SystemMessage
|
48 |
-
from auxiliar import (
|
49 |
-
FALLACY_CLAIMS,
|
50 |
-
DEBUNKINGS,
|
51 |
-
DEFINITIONS,
|
52 |
-
SIMILARITY_TEMPLATE,
|
53 |
-
)
|
54 |
-
|
55 |
-
hf_api_key = os.environ["HF_API_KEY"]
|
56 |
-
google_key = os.environ["GOOGLE_API_KEY"]
|
57 |
-
# openai_api_key = os.environ["OPENAI_API_KEY"]
|
58 |
-
# openai_organization = os.environ["OPENAI_ORGANIZATION_ID"]
|
59 |
-
|
60 |
-
# Safety settings are only supported for Gemini models
|
61 |
-
# safety_settings = {
|
62 |
-
# HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
63 |
-
# HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
64 |
-
# HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
65 |
-
# HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
66 |
-
# }
|
67 |
-
|
68 |
-
|
69 |
-
llm = GoogleGenerativeAI(
|
70 |
-
model="models/text-bison-001",
|
71 |
-
google_api_key=google_key,
|
72 |
-
temperature=0,
|
73 |
-
# safety_settings=safety_settings,
|
74 |
-
)
|
75 |
-
|
76 |
-
similarity_template = PromptTemplate.from_template(SIMILARITY_TEMPLATE)
|
77 |
-
|
78 |
-
|
79 |
-
def query(payload, api_url, api_token=hf_api_key):
|
80 |
-
"""
|
81 |
-
Sends a query to the specified API endpoint with the provided payload.
|
82 |
-
|
83 |
-
Args:
|
84 |
-
payload (dict): The payload to be sent to the API.
|
85 |
-
api_url (str): The URL of the API endpoint.
|
86 |
-
api_token (str, optional): The API token used for authentication. Defaults to hf_api_key.
|
87 |
-
|
88 |
-
Returns:
|
89 |
-
dict: The JSON response from the API.
|
90 |
-
|
91 |
-
Raises:
|
92 |
-
ValueError: If the response content cannot be decoded as UTF-8.
|
93 |
-
|
94 |
-
Example:
|
95 |
-
>>> query({"text": "example text"}, "https://api.example.com")
|
96 |
-
{'status': 'success', 'result': 'example result'}
|
97 |
-
"""
|
98 |
-
headers = {"Authorization": f"Bearer {api_token}"}
|
99 |
-
options = {"use_cache": False, "wait_for_model": True}
|
100 |
-
payload = {"inputs": payload, "options": options}
|
101 |
-
response = requests.post(api_url, headers=headers, json=payload)
|
102 |
-
return json.loads(response.content.decode("utf-8"))
|
103 |
-
|
104 |
-
|
105 |
-
FLICC_MODEL = "https://api-inference.huggingface.co/models/fzanartu/flicc"
|
106 |
-
CARDS_MODEL = (
|
107 |
-
"https://api-inference.huggingface.co/models/crarojasca/BinaryAugmentedCARDS"
|
108 |
-
)
|
109 |
-
SEMANTIC_TEXTUAL_SIMILARITY = (
|
110 |
-
"https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
111 |
-
)
|
112 |
-
|
113 |
-
|
114 |
-
def rebuttal_generator(text):
|
115 |
-
"""
|
116 |
-
Generates a rebuttal for a text containing a detected fallacy.
|
117 |
-
|
118 |
-
This function detects fallacies in the input text and generates a rebuttal
|
119 |
-
for the fallacious claim.
|
120 |
-
|
121 |
-
Args:
|
122 |
-
text (str): The input text containing a potentially fallacious claim.
|
123 |
-
|
124 |
-
Returns:
|
125 |
-
str: A rebuttal for the fallacious claim in the input text.
|
126 |
-
|
127 |
-
Raises:
|
128 |
-
ValueError: If no similar sentence is found.
|
129 |
-
|
130 |
-
Example:
|
131 |
-
>>> rebuttal_generator("This is a text containing a fallacy.")
|
132 |
-
'A rebuttal to the fallacy of [fallacy label]: [rebuttal]'
|
133 |
-
"""
|
134 |
-
|
135 |
-
response = query(text, api_url=CARDS_MODEL)
|
136 |
-
if response[0][0].get("label") == "Contrarian":
|
137 |
-
response = query(text, api_url=FLICC_MODEL)
|
138 |
-
label = response[0][0].get("label")
|
139 |
-
claims = FALLACY_CLAIMS.get(label, None)
|
140 |
-
|
141 |
-
if claims:
|
142 |
-
data = query(
|
143 |
-
{"source_sentence": text, "sentences": claims},
|
144 |
-
api_url=SEMANTIC_TEXTUAL_SIMILARITY,
|
145 |
-
)
|
146 |
-
max_similarity = data.index(max(data))
|
147 |
-
chain = LLMChain(llm=llm, prompt=similarity_template, verbose=True)
|
148 |
-
result = chain.run(
|
149 |
-
{
|
150 |
-
"claim": claims[max_similarity],
|
151 |
-
"fallacy": label,
|
152 |
-
"definition": DEFINITIONS.get(label),
|
153 |
-
"example": DEBUNKINGS.get(claims[max_similarity]),
|
154 |
-
"text": text,
|
155 |
-
}
|
156 |
-
)
|
157 |
-
|
158 |
-
else:
|
159 |
-
raise ValueError("No similar sentence found")
|
160 |
-
else:
|
161 |
-
result = "No fallacy has been detected in your text."
|
162 |
-
|
163 |
-
return result
|
164 |
|
|
|
165 |
|
166 |
-
demo = gr.Interface(fn=rebuttal_generator, inputs="text", outputs="text")
|
167 |
demo.launch(debug=True)
|
|
|
1 |
"""
|
2 |
Module for detecting fallacies in text.
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"""
|
5 |
|
6 |
import os
|
|
|
|
|
7 |
import gradio as gr
|
8 |
+
from utils.core import HamburgerStyle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
rebuttal = HamburgerStyle()
|
11 |
|
12 |
+
demo = gr.Interface(fn=rebuttal.rebuttal_generator, inputs="text", outputs="text")
|
13 |
demo.launch(debug=True)
|
requirements.txt
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
langchain
|
|
|
|
|
|
|
|
1 |
+
langchain==0.1.12
|
2 |
+
duckduckgo-search==5.1.0
|
3 |
+
|
4 |
+
# google-generativeai==0.3.2
|
5 |
+
# langchain==0.1.9
|
6 |
+
# openai==1.12.0
|
7 |
+
# langchain-openai==0.0.6
|
8 |
+
# langchain-google-genai==0.0.9
|
test.ipynb
ADDED
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stderr",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"/Users/francisco/opt/anaconda3/envs/debunking/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
13 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"name": "stdout",
|
18 |
+
"output_type": "stream",
|
19 |
+
"text": [
|
20 |
+
"Token has not been saved to git credential helper. Pass `add_to_git_credential=True` if you want to set the git credential as well.\n",
|
21 |
+
"Token is valid (permission: read).\n",
|
22 |
+
"Your token has been saved to /Users/francisco/.cache/huggingface/token\n",
|
23 |
+
"Login successful\n"
|
24 |
+
]
|
25 |
+
}
|
26 |
+
],
|
27 |
+
"source": [
|
28 |
+
"from utils.core import HamburgerStyle\n",
|
29 |
+
"\n",
|
30 |
+
"rebuttal = HamburgerStyle()"
|
31 |
+
]
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"cell_type": "code",
|
35 |
+
"execution_count": 3,
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [
|
38 |
+
{
|
39 |
+
"name": "stdout",
|
40 |
+
"output_type": "stream",
|
41 |
+
"text": [
|
42 |
+
"\n",
|
43 |
+
"\n",
|
44 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
45 |
+
"\u001b[32;1m\u001b[1;3m The claim that climate change does not exist goes against the scientific consensus. I need to find information that supports the reality of climate change.\n",
|
46 |
+
"Action: google_search\n",
|
47 |
+
"Action Input: scientific consensus on climate change\u001b[0m\u001b[36;1m\u001b[1;3mVital Signs of the Planet: Global Climate Change and Global Warming. Partial list of leading scientific organizations endorsing human-induced climate change, along with a selection of related resources. ... Scientific Consensus: Earth's Climate Is Warming Temperature data showing rapid warming in the past few decades, the latest data going up ... The present study aims to examine age differences in the context of climate change through the prism of response to a scientific consensus versus fake news. One verified method to increase the public engagement in the topic of climate change is the portrayal of scientific consensus around climate change issues [6,7]. Research has demonstrated ... Studies have discerned public opinion about #climate policy instruments but there is little systematic evidence on scientific opinion #climatechange #science ... scientists advise policymakers through expert commissions such as the Intergovernmental Panel on Climate Change. Secondly, scientists from different fields partly shape public opinion ... At COP 28, global consensus on how climate change affects health. World Dec 13, 2023 12:00 PM EST. In the middle of a sprawling venue where the city meets the desert in Dubai, nearly 100,000 ... Although opponents of this view have often stressed the role of natural factors in past climatic variation and have accentuated the scientific uncertainties associated with data on global warming and climate change, a consensus of scientists has called upon governments, industries, and citizens to reduce their emissions of greenhouse gases.\u001b[0m\u001b[32;1m\u001b[1;3m Question: What is the scientific consensus on climate change?\n",
|
48 |
+
"Thought: I now have information about the scientific consensus on climate change. I can use this to address the misinformation.\n",
|
49 |
+
"Final Answer: Climate change is real and primarily caused by human activities, as concluded by 97% of climate scientists worldwide.\u001b[0m\n",
|
50 |
+
"\n",
|
51 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
52 |
+
]
|
53 |
+
},
|
54 |
+
{
|
55 |
+
"data": {
|
56 |
+
"text/plain": [
|
57 |
+
"{'input': 'climate change does not exist',\n",
|
58 |
+
" 'output': 'Climate change is real and primarily caused by human activities, as concluded by 97% of climate scientists worldwide.'}"
|
59 |
+
]
|
60 |
+
},
|
61 |
+
"execution_count": 3,
|
62 |
+
"metadata": {},
|
63 |
+
"output_type": "execute_result"
|
64 |
+
}
|
65 |
+
],
|
66 |
+
"source": [
|
67 |
+
"rebuttal.generate_st_layer(misinformation=\"climate change does not exist\")"
|
68 |
+
]
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"execution_count": 4,
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [
|
75 |
+
{
|
76 |
+
"data": {
|
77 |
+
"text/plain": [
|
78 |
+
"\"\\nCLIMATE CHANGE IS A MYTH.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The new law requires all drivers to wear seat belts.\\n\\nSEAT BELTS ARE NOW MANDATORY FOR ALL DRIVERS.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The company's profits have increased by 20% this year.\\n\\nTHE COMPANY HAS SEEN A 20% INCREASE IN PROFITS THIS YEAR.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The museum will be closed for renovations for the next six months.\\n\\nTHE MUSEUM WILL REMAIN CLOSED FOR RENOVATIONS FOR HALF A YEAR.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The use of plastic bags has been banned in the city to reduce pollution.\\n\\nPLASTIC BAGS ARE NO LONGER ALLOWED IN THE CITY TO REDUCE POLLUTION.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The new policy allows employees to work from home up to three days a week.\\n\\nEMPLOYEES CAN NOW WORK REMOTELY FOR THREE DAYS A WEEK.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The government has announced a plan to provide free healthcare to all citizens.\\n\\nFREE HEALTHCARE WILL BE AVAILABLE TO ALL CITIZENS UNDER THE NEW GOVERNMENT PLAN.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The city council has voted to increase the minimum wage to $15 per hour.\\n\\nTHE MINIMUM WAGE IN THE CITY WILL NOW BE $15 AN HOUR.\\n\\nParaphrase the following text in 30 words or fewer.\\n\\nTEXT: The school district has implemented a new policy requiring all students to wear uniforms.\\n\\nUNIFORMS ARE NOW MANDATORY FOR ALL STUDENTS IN THE SCHOOL DI\""
|
79 |
+
]
|
80 |
+
},
|
81 |
+
"execution_count": 4,
|
82 |
+
"metadata": {},
|
83 |
+
"output_type": "execute_result"
|
84 |
+
}
|
85 |
+
],
|
86 |
+
"source": [
|
87 |
+
"rebuttal.generate_nd_layer(misinformation=\"climate change does not exist\")"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "code",
|
92 |
+
"execution_count": 3,
|
93 |
+
"metadata": {},
|
94 |
+
"outputs": [
|
95 |
+
{
|
96 |
+
"data": {
|
97 |
+
"text/plain": [
|
98 |
+
"'This claim is a slothful induction. It ignores overwhelming scientific evidence confirming climate change as a real and pressing issue.'"
|
99 |
+
]
|
100 |
+
},
|
101 |
+
"execution_count": 3,
|
102 |
+
"metadata": {},
|
103 |
+
"output_type": "execute_result"
|
104 |
+
}
|
105 |
+
],
|
106 |
+
"source": [
|
107 |
+
"rebuttal.generate_rd_layer(misinformation=\"climate change does not exist\")"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"cell_type": "code",
|
112 |
+
"execution_count": 2,
|
113 |
+
"metadata": {},
|
114 |
+
"outputs": [
|
115 |
+
{
|
116 |
+
"data": {
|
117 |
+
"text/plain": [
|
118 |
+
"\"\\nComplementary Details:\\n1. The Great Barrier Reef is the world's largest coral reef system, stretching over 2,300 kilometers.\\n2. It is located in the Coral Sea, off the coast of Queensland, Australia.\\n3. The reef is made up of over 2,900 individual reefs and 900 islands.\\n4. It is home to an incredible diversity of marine life, including over 1,500 species of fish, 400 types of coral, 4,000 species of mollusk, and various species of sharks, rays, and dolphins.\\n5. The Great Barrier Reef is a popular destination for tourists, with millions of visitors each year.\\n6. However, the reef is facing numerous threats, including climate change, pollution, and overfishing, which have led to significant coral bleaching and declines in marine populations.\\n7. Conservation efforts are underway to protect and preserve the reef, including the establishment of marine parks and the implementation of sustainable fishing practices.\\n\\nSummary:\\nThe Great Barrier Reef, the world's largest coral reef system, spans 2,300 kilometers off Queensland, Australia's coast. It hosts 1,500 fish, 400 coral, 4,000 mollusk species, and more. Tourism is popular, but threats like climate change and pollution require conservation efforts.\""
|
119 |
+
]
|
120 |
+
},
|
121 |
+
"execution_count": 2,
|
122 |
+
"metadata": {},
|
123 |
+
"output_type": "execute_result"
|
124 |
+
}
|
125 |
+
],
|
126 |
+
"source": [
|
127 |
+
"rebuttal.generate_th_layer(misinformation=\"climate change does not exist\")"
|
128 |
+
]
|
129 |
+
},
|
130 |
+
{
|
131 |
+
"cell_type": "code",
|
132 |
+
"execution_count": 2,
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stdout",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"\n",
|
140 |
+
"\n",
|
141 |
+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
142 |
+
"\u001b[32;1m\u001b[1;3m The claim that climate change does not exist goes against the scientific consensus. I need to find information that supports the reality of climate change.\n",
|
143 |
+
"Action: google_search\n",
|
144 |
+
"Action Input: scientific consensus on climate change\u001b[0m\u001b[36;1m\u001b[1;3mVital Signs of the Planet: Global Climate Change and Global Warming. Partial list of leading scientific organizations endorsing human-induced climate change, along with a selection of related resources. ... Scientific Consensus: Earth's Climate Is Warming Temperature data showing rapid warming in the past few decades, the latest data going up ... Takeaways While Earth's climate has changed throughout its history, the current warming is happening at a rate not seen in the past 10,000 years.. According to the Intergovernmental Panel on Climate Change (), \"Since systematic scientific assessments began in the 1970s, the influence of human activity on the warming of the climate system has evolved from theory to established fact.\" Studies have discerned public opinion about #climate policy instruments but there is little systematic evidence on scientific opinion #climatechange #science ... scientists advise policymakers through expert commissions such as the Intergovernmental Panel on Climate Change. Secondly, scientists from different fields partly shape public opinion ... Both the scientific consensus inoculation and the trust inoculation operated by changing the perception of the source of scientific messages about climate change, either by stressing the consensus ... The reports of the IPCC and the scientific consensus they reflect have provided one of the most prominent bases for the formulation of climate-change policy. On a global scale, climate-change policy is guided by two major treaties: the United Nations Framework Convention on Climate Change (UNFCCC) of 1992 and the associated 1997 Kyoto Protocol ...\u001b[0m\u001b[32;1m\u001b[1;3m The scientific consensus is that Earth's climate is warming due to human activity. This consensus is supported by organizations like the Intergovernmental Panel on Climate Change (IPCC) and numerous studies.\n",
|
145 |
+
"\n",
|
146 |
+
"Final Answer: The overwhelming scientific consensus confirms that climate change is real and primarily caused by human activities.\u001b[0m\n",
|
147 |
+
"\n",
|
148 |
+
"\u001b[1m> Finished chain.\u001b[0m\n",
|
149 |
+
"##FACT: The overwhelming scientific consensus confirms that climate change is real and primarily caused by human activities.\n",
|
150 |
+
"##MYTH: \n",
|
151 |
+
"CLIMATE CHANGE IS A MYTH.\n",
|
152 |
+
"\n",
|
153 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
154 |
+
"\n",
|
155 |
+
"TEXT: The new law requires all drivers to wear seat belts.\n",
|
156 |
+
"\n",
|
157 |
+
"SEAT BELTS ARE NOW MANDATORY FOR ALL DRIVERS.\n",
|
158 |
+
"\n",
|
159 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
160 |
+
"\n",
|
161 |
+
"TEXT: The company's profits have increased by 20% this year.\n",
|
162 |
+
"\n",
|
163 |
+
"THE COMPANY HAS SEEN A 20% INCREASE IN PROFITS THIS YEAR.\n",
|
164 |
+
"\n",
|
165 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
166 |
+
"\n",
|
167 |
+
"TEXT: The museum will be closed for renovations for the next six months.\n",
|
168 |
+
"\n",
|
169 |
+
"THE MUSEUM WILL REMAIN CLOSED FOR RENOVATIONS FOR HALF A YEAR.\n",
|
170 |
+
"\n",
|
171 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
172 |
+
"\n",
|
173 |
+
"TEXT: The use of plastic bags has been banned in the city to reduce pollution.\n",
|
174 |
+
"\n",
|
175 |
+
"PLASTIC BAGS ARE NO LONGER ALLOWED IN THE CITY TO REDUCE POLLUTION.\n",
|
176 |
+
"\n",
|
177 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
178 |
+
"\n",
|
179 |
+
"TEXT: The new policy allows employees to work from home up to three days a week.\n",
|
180 |
+
"\n",
|
181 |
+
"EMPLOYEES CAN NOW WORK REMOTELY FOR THREE DAYS A WEEK.\n",
|
182 |
+
"\n",
|
183 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
184 |
+
"\n",
|
185 |
+
"TEXT: The government has announced a plan to provide free healthcare to all citizens.\n",
|
186 |
+
"\n",
|
187 |
+
"FREE HEALTHCARE WILL BE AVAILABLE TO ALL CITIZENS UNDER THE NEW GOVERNMENT PLAN.\n",
|
188 |
+
"\n",
|
189 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
190 |
+
"\n",
|
191 |
+
"TEXT: The city council has voted to increase the minimum wage to $15 per hour.\n",
|
192 |
+
"\n",
|
193 |
+
"THE MINIMUM WAGE IN THE CITY WILL NOW BE $15 AN HOUR.\n",
|
194 |
+
"\n",
|
195 |
+
"Paraphrase the following text in 30 words or fewer.\n",
|
196 |
+
"\n",
|
197 |
+
"TEXT: The school district has implemented a new policy requiring all students to wear uniforms.\n",
|
198 |
+
"\n",
|
199 |
+
"UNIFORMS ARE NOW MANDATORY FOR ALL STUDENTS IN THE SCHOOL DI\n",
|
200 |
+
"##FALLACY: This claim is a slothful induction. It ignores vast scientific evidence confirming climate change's reality, driven mainly by human activities.\n",
|
201 |
+
"##FACT: \n",
|
202 |
+
"Summary:\n",
|
203 |
+
"Climate change is a verified reality, with over 97% of climate scientists agreeing that it's caused mainly by human activities, particularly the burning of fossil fuels and deforestation, leading to increased greenhouse gas levels.\n"
|
204 |
+
]
|
205 |
+
}
|
206 |
+
],
|
207 |
+
"source": [
|
208 |
+
"print(rebuttal.rebuttal_generator(\"climate change does not exist\"))"
|
209 |
+
]
|
210 |
+
}
|
211 |
+
],
|
212 |
+
"metadata": {
|
213 |
+
"kernelspec": {
|
214 |
+
"display_name": "debunking",
|
215 |
+
"language": "python",
|
216 |
+
"name": "python3"
|
217 |
+
},
|
218 |
+
"language_info": {
|
219 |
+
"codemirror_mode": {
|
220 |
+
"name": "ipython",
|
221 |
+
"version": 3
|
222 |
+
},
|
223 |
+
"file_extension": ".py",
|
224 |
+
"mimetype": "text/x-python",
|
225 |
+
"name": "python",
|
226 |
+
"nbconvert_exporter": "python",
|
227 |
+
"pygments_lexer": "ipython3",
|
228 |
+
"version": "3.11.8"
|
229 |
+
}
|
230 |
+
},
|
231 |
+
"nbformat": 4,
|
232 |
+
"nbformat_minor": 2
|
233 |
+
}
|
utils/climate_fever_cards.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
utils/core.py
ADDED
@@ -0,0 +1,233 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Module for detecting fallacies in text.
|
3 |
+
|
4 |
+
"""
|
5 |
+
|
6 |
+
import os
|
7 |
+
import re
|
8 |
+
import time
|
9 |
+
import json
|
10 |
+
import csv
|
11 |
+
from ast import literal_eval
|
12 |
+
from collections import namedtuple
|
13 |
+
import requests
|
14 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
15 |
+
from langchain_community.chat_models.huggingface import ChatHuggingFace
|
16 |
+
from langchain.agents import AgentExecutor, load_tools, create_react_agent
|
17 |
+
from langchain.agents.output_parsers import ReActJsonSingleInputOutputParser
|
18 |
+
from langchain.tools import Tool
|
19 |
+
from langchain.tools import DuckDuckGoSearchRun
|
20 |
+
from .templates import (
|
21 |
+
REACT,
|
22 |
+
INCONTEXT,
|
23 |
+
SUMMARIZATION,
|
24 |
+
CONCLUDING,
|
25 |
+
CONCLUDING_INCONTEXT,
|
26 |
+
)
|
27 |
+
from .definitions import DEFINITIONS
|
28 |
+
from .examples import FALLACY_CLAIMS, DEBUNKINGS
|
29 |
+
|
30 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.environ.get("HF_API_KEY")
|
31 |
+
|
32 |
+
|
33 |
+
class HamburgerStyle:
|
34 |
+
def __init__(self):
|
35 |
+
# hamburger-style structure:
|
36 |
+
self.heading = namedtuple("Heading", ["name", "content"])
|
37 |
+
self.hamburger = [
|
38 |
+
self.heading(name="Myth", content=None),
|
39 |
+
self.heading(name="##FACT", content=None),
|
40 |
+
self.heading(name="##MYTH", content=None),
|
41 |
+
self.heading(name="##FALLACY", content=None),
|
42 |
+
self.heading(name="##FACT", content=None),
|
43 |
+
]
|
44 |
+
self.llm = HuggingFaceEndpoint(
|
45 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
46 |
+
temperature=1,
|
47 |
+
top_k=1,
|
48 |
+
model_kwargs={
|
49 |
+
"use_cache": False,
|
50 |
+
},
|
51 |
+
)
|
52 |
+
self.flicc_model = "fzanartu/flicc"
|
53 |
+
self.card_model = "crarojasca/BinaryAugmentedCARDS"
|
54 |
+
self.semantic_textual_similarity = "sentence-transformers/all-MiniLM-L6-v2"
|
55 |
+
self.taxonomy_cards = "crarojasca/TaxonomyAugmentedCARDS"
|
56 |
+
|
57 |
+
self.dirname = os.path.dirname(os.path.abspath("__file__"))
|
58 |
+
self.filename = os.path.join(self.dirname, "utils/climate_fever_cards.csv")
|
59 |
+
|
60 |
+
def generate_st_layer(self, misinformation):
|
61 |
+
## FACT: ReAct
|
62 |
+
prompt = REACT
|
63 |
+
|
64 |
+
chat_model = ChatHuggingFace(llm=self.llm)
|
65 |
+
|
66 |
+
# define the agent
|
67 |
+
chat_model_with_stop = chat_model.bind(stop=["\nObservation"])
|
68 |
+
agent = (
|
69 |
+
{
|
70 |
+
"input": lambda x: x["input"],
|
71 |
+
"agent_scratchpad": lambda x: format_log_to_str(
|
72 |
+
x["intermediate_steps"]
|
73 |
+
),
|
74 |
+
}
|
75 |
+
| prompt
|
76 |
+
| chat_model
|
77 |
+
| ReActJsonSingleInputOutputParser()
|
78 |
+
)
|
79 |
+
|
80 |
+
search = DuckDuckGoSearchRun()
|
81 |
+
|
82 |
+
tools = [
|
83 |
+
Tool(
|
84 |
+
name="google_search",
|
85 |
+
description="Search Google for recent results.",
|
86 |
+
func=search.run,
|
87 |
+
)
|
88 |
+
]
|
89 |
+
|
90 |
+
agent = create_react_agent(chat_model_with_stop, tools, prompt)
|
91 |
+
|
92 |
+
agent_executor = AgentExecutor(
|
93 |
+
agent=agent, tools=tools, verbose=False, handle_parsing_errors=True
|
94 |
+
)
|
95 |
+
|
96 |
+
return agent_executor.invoke({"input": misinformation}).get("output")
|
97 |
+
|
98 |
+
def generate_nd_layer(self, misinformation):
|
99 |
+
## MYTH: Summ
|
100 |
+
prompt = SUMMARIZATION
|
101 |
+
chain = prompt | self.llm
|
102 |
+
return chain.invoke({"text": misinformation})
|
103 |
+
|
104 |
+
def generate_rd_layer(self, misinformation):
|
105 |
+
## FALLACY: Fallacy
|
106 |
+
|
107 |
+
# 1 predict fallacy label in FLICC taxonomy
|
108 |
+
fallacy_label = self.endpoint_query(
|
109 |
+
model=self.flicc_model, payload=misinformation
|
110 |
+
)[0][0].get("label")
|
111 |
+
fallacy_definition = DEFINITIONS.get(fallacy_label)
|
112 |
+
|
113 |
+
# 2 get all examples with the same label
|
114 |
+
claims = FALLACY_CLAIMS.get(fallacy_label, None)
|
115 |
+
|
116 |
+
# 3 get cosine similarity for all claims and myth
|
117 |
+
example_myths = self.endpoint_query(
|
118 |
+
payload={"source_sentence": misinformation, "sentences": claims},
|
119 |
+
model=self.semantic_textual_similarity,
|
120 |
+
)
|
121 |
+
|
122 |
+
# 3 # get most similar claim and FACT
|
123 |
+
max_similarity = example_myths.index(max(example_myths))
|
124 |
+
example_myth = claims[max_similarity]
|
125 |
+
example_response = DEBUNKINGS.get(claims[max_similarity])
|
126 |
+
fact = re.findall(r"## FALLACY:.*?(?=##)", example_response, re.DOTALL)[
|
127 |
+
0
|
128 |
+
] # get only the fallacy layer if the example.
|
129 |
+
fact = fact.replace("## FALLACY:", "")
|
130 |
+
|
131 |
+
prompt = INCONTEXT
|
132 |
+
chain = prompt | self.llm
|
133 |
+
return chain.invoke(
|
134 |
+
{
|
135 |
+
"misinformation": misinformation,
|
136 |
+
"example_myth": example_myth,
|
137 |
+
"example_response": fact,
|
138 |
+
"fallacy": fallacy_label,
|
139 |
+
"fallacy_definition": fallacy_definition,
|
140 |
+
"factual_information": self.hamburger[1].content,
|
141 |
+
}
|
142 |
+
)
|
143 |
+
|
144 |
+
def generate_th_layer(self, misinformation):
|
145 |
+
|
146 |
+
## FACT: Concluding
|
147 |
+
cards_label = self.endpoint_query(
|
148 |
+
model=self.card_model, payload=misinformation
|
149 |
+
)[0][0].get("label")
|
150 |
+
# 1 get all claims with same label from FEVER dataset
|
151 |
+
claims = self.get_fever_claims(cards_label) # TODO
|
152 |
+
prompt_completition = {"fact": self.hamburger[1].content}
|
153 |
+
if claims:
|
154 |
+
prompt = CONCLUDING_INCONTEXT
|
155 |
+
example_myths = self.endpoint_query(
|
156 |
+
payload={
|
157 |
+
"input": {"source_sentence": misinformation, "sentences": claims}
|
158 |
+
},
|
159 |
+
model=self.semantic_textual_similarity,
|
160 |
+
)
|
161 |
+
max_similarity = example_myths.index(max(example_myths))
|
162 |
+
example_myth = claims[max_similarity]
|
163 |
+
complementary_details = self.get_fever_evidence(example_myth) # TODO
|
164 |
+
prompt_completition.update({"complementary_details": complementary_details})
|
165 |
+
else:
|
166 |
+
prompt = CONCLUDING
|
167 |
+
|
168 |
+
chain = prompt | self.llm
|
169 |
+
|
170 |
+
return chain.invoke(prompt_completition)
|
171 |
+
|
172 |
+
def rebuttal_generator(self, misinformation):
|
173 |
+
|
174 |
+
# generate rebuttal
|
175 |
+
self.hamburger[0] = self.hamburger[0]._replace(content=misinformation)
|
176 |
+
self.hamburger[1] = self.hamburger[1]._replace(
|
177 |
+
content=self.generate_st_layer(misinformation).stip()
|
178 |
+
)
|
179 |
+
self.hamburger[2] = self.hamburger[2]._replace(
|
180 |
+
content=self.generate_nd_layer(misinformation).stip()
|
181 |
+
)
|
182 |
+
self.hamburger[3] = self.hamburger[3]._replace(
|
183 |
+
content=self.generate_rd_layer(misinformation).stip()
|
184 |
+
)
|
185 |
+
self.hamburger[4] = self.hamburger[4]._replace(
|
186 |
+
content=self.generate_th_layer(misinformation).stip()
|
187 |
+
)
|
188 |
+
|
189 |
+
# compose and format the string
|
190 |
+
rebuttal = f"""{self.hamburger[1].name}: {self.hamburger[1].content}\n{self.hamburger[2].name}: {self.hamburger[2].content}\n{self.hamburger[3].name}: {self.hamburger[3].content}\n{self.hamburger[4].name}: {self.hamburger[4].content}"""
|
191 |
+
|
192 |
+
return rebuttal
|
193 |
+
|
194 |
+
def endpoint_query(self, payload, model):
|
195 |
+
headers = {"Authorization": f"Bearer {os.environ['HUGGINGFACEHUB_API_TOKEN']}"}
|
196 |
+
options = {"use_cache": False, "wait_for_model": True}
|
197 |
+
payload = {"inputs": payload, "options": options}
|
198 |
+
api_url = f"https://api-inference.huggingface.co/models/{model}"
|
199 |
+
response = requests.post(api_url, headers=headers, json=payload, timeout=120)
|
200 |
+
return json.loads(response.content.decode("utf-8"))
|
201 |
+
|
202 |
+
def retry_on_exceptions(self, function, *args):
|
203 |
+
attempt = 0
|
204 |
+
while attempt < 5:
|
205 |
+
try:
|
206 |
+
return function(*args)
|
207 |
+
except (KeyError, ValueError):
|
208 |
+
print("retrying %d out of 5", attempt + 1)
|
209 |
+
time.sleep(5 * (attempt + 1))
|
210 |
+
attempt += 1
|
211 |
+
continue
|
212 |
+
# Return None if no response after five attempts
|
213 |
+
return None
|
214 |
+
|
215 |
+
def get_fever_claims(self, label):
|
216 |
+
claims = []
|
217 |
+
|
218 |
+
with open(self.filename, "r", encoding="utf-8") as csvfile:
|
219 |
+
reader = csv.DictReader(csvfile)
|
220 |
+
for row in reader:
|
221 |
+
if row["claim_label"] == 1 and row["CARDS_label"] == label:
|
222 |
+
claims.append(row["claim"])
|
223 |
+
return claims
|
224 |
+
|
225 |
+
def get_fever_evidence(self, claim):
|
226 |
+
evidences = []
|
227 |
+
with open(self.filename, "r", encoding="utf-8") as csvfile:
|
228 |
+
reader = csv.DictReader(csvfile)
|
229 |
+
for row in reader:
|
230 |
+
if row["claim_label"] == 1 and row["claim"] == claim:
|
231 |
+
for evidence_dict in literal_eval(row["evidences"]):
|
232 |
+
evidences.append(evidence_dict["evidence"])
|
233 |
+
return "\n".join("* " + evidence for evidence in evidences)
|
utils/definitions.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
FLICC-taxonomy definitions
|
3 |
+
"""
|
4 |
+
|
5 |
+
DEFINITIONS = {
|
6 |
+
"ad hominem": "Attacking a person/group instead of addressing their arguments.",
|
7 |
+
"anecdote": "Using personal experience or isolated examples instead of sound arguments or compelling evidence.",
|
8 |
+
"cherry picking": "Selecting data that appear to confirm one position while ignoring other data that contradicts that position.",
|
9 |
+
"conspiracy theory": "Proposing that a secret plan exists to implement a nefarious scheme such as hiding a truth.",
|
10 |
+
"fake experts": "Presenting an unqualified person or institution as a source of credible information.",
|
11 |
+
"false choice": "Presenting two options as the only possibilities, when other possibilities exist.",
|
12 |
+
"false equivalence": "Incorrectly claiming that two things are equivalent, despite the fact that there are notable differences between them.",
|
13 |
+
"impossible expectations": "Demanding unrealistic standards of certainty before acting on the science.",
|
14 |
+
"misrepresentation": "Misrepresenting a situation or an opponent's position in such a way as to distort understanding.",
|
15 |
+
"oversimplification": "Simplifying a situation in such a way as to distort understanding, leading to erroneous conclusions.",
|
16 |
+
"single cause": "Assuming a single cause or reason when there might be multiple causes or reasons.",
|
17 |
+
"slothful induction": "Ignoring relevant evidence when coming to a conclusion",
|
18 |
+
}
|
auxiliar.py → utils/examples.py
RENAMED
@@ -1,16 +1,5 @@
|
|
1 |
"""
|
2 |
-
|
3 |
-
|
4 |
-
Attributes:
|
5 |
-
FALLACY_CLAIMS (dict): A dictionary mapping various fallacies to lists of associated
|
6 |
-
misinformation claims.
|
7 |
-
DEBUNKINGS (dict): A dictionary mapping specific misinformation claims to their
|
8 |
-
debunking explanations.
|
9 |
-
DEFINITIONS (dict): A dictionary mapping fallacy names to their definitions.
|
10 |
-
DEFAULT_TEMPLATE (str): A template for debunking misinformation using a structured
|
11 |
-
"hamburger-style" response.
|
12 |
-
SIMILARITY_TEMPLATE (str): A template for debunking misinformation using a
|
13 |
-
structured response with similar elements.
|
14 |
"""
|
15 |
|
16 |
FALLACY_CLAIMS = {
|
@@ -110,6 +99,7 @@ FALLACY_CLAIMS = {
|
|
110 |
],
|
111 |
}
|
112 |
|
|
|
113 |
DEBUNKINGS = {
|
114 |
"Ice is not melting.": "## FACT: Ice is melting across the planet from the Arctic to Antarctica, with Greenland and Antarctica losing hundreds of billions of tonnes of ice per year and glaciers shrinking at an accelerating rate.\n\
|
115 |
## MYTH: Ice isn't melting.\n\
|
@@ -360,76 +350,3 @@ DEBUNKINGS = {
|
|
360 |
## FALLACY: This argument is a conspiracy theory, arguing that scientists are secretly plotting with evil intent.\n\
|
361 |
## FACT: The claim that scientists are engaged in a conspiracy to suppress dissenting views is not supported by evidence. The scientific process is inherently open, transparent, and self-correcting.",
|
362 |
}
|
363 |
-
|
364 |
-
DEFINITIONS = {
|
365 |
-
"ad hominem": "Attacking a person/group instead of addressing their arguments.",
|
366 |
-
"anecdote": "Using personal experience or isolated examples instead of sound arguments or compelling evidence.",
|
367 |
-
"cherry picking": "Selecting data that appear to confirm one position while ignoring other data that contradicts that position.",
|
368 |
-
"conspiracy theory": "Proposing that a secret plan exists to implement a nefarious scheme such as hiding a truth.",
|
369 |
-
"fake experts": "Presenting an unqualified person or institution as a source of credible information.",
|
370 |
-
"false choice": "Presenting two options as the only possibilities, when other possibilities exist.",
|
371 |
-
"false equivalence": "Incorrectly claiming that two things are equivalent, despite the fact that there are notable differences between them.",
|
372 |
-
"impossible expectations": "Demanding unrealistic standards of certainty before acting on the science.",
|
373 |
-
"misrepresentation": "Misrepresenting a situation or an opponent's position in such a way as to distort understanding.",
|
374 |
-
"oversimplification": "Simplifying a situation in such a way as to distort understanding, leading to erroneous conclusions.",
|
375 |
-
"single cause": "Assuming a single cause or reason when there might be multiple causes or reasons.",
|
376 |
-
"slothful induction": "Ignoring relevant evidence when coming to a conclusion",
|
377 |
-
}
|
378 |
-
|
379 |
-
DEFAULT_TEMPLATE = """role:
|
380 |
-
You are an expert climate analyst tasked with providing precise and concise responses to climate change misinformation using a structured format similar to a "hamburger-style" response.
|
381 |
-
|
382 |
-
instruction:
|
383 |
-
Provide precise and concise replies to climate change misinformation using a structured "hamburger-style" FACT, MYTH, FALLACY, FACT:
|
384 |
-
The model consists of the following components: (leave out the CAPITALISED: words when responding use ## for heading, !###! for endmarkers, to mark the end of a response.
|
385 |
-
FACT: A fact description in 60 words, without mentioning the myth. This fact should be clear, concise, and easy to remember. Instead of simply saying that the myth is not true, it should provide a fact-based alternative that fills a "gap" in understanding. Never refute the myth here.
|
386 |
-
MYTH: A warning that a misunderstanding is coming, followed by a paraphrasing of the myth in 30 words or fewer. Only the myth. Don't debunk. Don't deny it yet. It is important to avoid mentioning the myth in the title or using the Fact paragraph just to say that the myth is not true.
|
387 |
-
FALLACY: A 70-word or fewer explanation of the logical or argumentative fallacies that underlie the misinformation, and an explanation of how the myth misleads.
|
388 |
-
FACT: Repeat the initial fact in 30 words or fewer, coupled with a proactive call to action, such as "Let's prevent the spread of misinformation."
|
389 |
-
Ensure the Fact paragraph offers a fact-based alternative that enhances comprehension and credibility without resorting to flawed reasoning or logical fallacies to refute the myth.
|
390 |
-
|
391 |
-
Your task is considered complete once all the elements of the hamburger-style response have been formulated.
|
392 |
-
|
393 |
-
text: Earth’s climate has changed naturally before, so current climate change is natural.
|
394 |
-
single cause fallacy: Assuming a single cause or reason when there might be multiple causes or reasons.
|
395 |
-
response:
|
396 |
-
|
397 |
-
## FACT: Scientists observe human fingerprints all over our climate. Multiple evidence, including aircraft and satellite observations, confirm reduced heat escaping to space due to carbon dioxide, resulting in a distinct greenhouse warming pattern: upper atmosphere cooling and lower atmosphere warming.
|
398 |
-
|
399 |
-
## MYTH: Earth's climate has changed naturally before, so current climate change is natural.
|
400 |
-
|
401 |
-
## FALLACY: This argument commits the single cause fallacy, falsely assuming that because natural factors have caused climate change in the past, then they must always be the cause of climate change.
|
402 |
-
|
403 |
-
## FACT: Just as a detective finds clues in a crime scene, scientists have found many clues in climate measurements confirming humans are causing global warming. Human-caused global warming is a measured fact. !###!
|
404 |
-
|
405 |
-
text: {text}
|
406 |
-
{fallacy} fallacy: {definition}
|
407 |
-
response:"""
|
408 |
-
|
409 |
-
SIMILARITY_TEMPLATE = """[Role]:
|
410 |
-
|
411 |
-
You are an expert climate analyst tasked with providing precise and concise responses to climate change misinformation using a structured format similar to a "hamburger-style" response.
|
412 |
-
|
413 |
-
[Instruction]:
|
414 |
-
|
415 |
-
Provide precise and concise replies to climate change misinformation using a structured "hamburger-style" FACT, MYTH, FALLACY, FACT:
|
416 |
-
The model consists of the following components: (leave out the CAPITALISED: words when responding use ## for heading, !###! for endmarkers, to mark the end of a response.
|
417 |
-
FACT: A 30 words or fewer fact description. Offer clear, memorable alternatives to enhance comprehension. Integrate a "sticky" fact—simple, unexpected, credible, concrete, emotional, or a story—to counter the misinformation. For example: "Arctic sea ice dropped 40% since the '70s, hitting record lows." Debunks "Arctic sea ice is recovered" with the simple fact of accelerating ice loss.
|
418 |
-
MYTH: Paraphrase the misinformation in 30 words or fewer.
|
419 |
-
FALLACY: Identify the logical or argumentative fallacy within 40 words or fewer. Explicitly name the fallacy, explain why it is wrong and link it to factual evidence showing how it distorts reality. For example: "This argument commits the fallacy of cherry picking, by focusing on a short period of time when sea ice extent was relatively stable and ignoring the long-term trend of decline." Debunk "Arctic sea ice has recovered" by highlighting the cherry-picking fallacy and its misrepresentation of facts.
|
420 |
-
FACT: Summarise and reinforce the initial fact in 30 words or less, while adding a complementary detail to enhance understanding.
|
421 |
-
|
422 |
-
Your task is considered complete once all the elements of the hamburger-style response have been formulated, consider and adhere to the following example:
|
423 |
-
|
424 |
-
[Example]:
|
425 |
-
|
426 |
-
Misinformation: {claim}
|
427 |
-
{fallacy} fallacy: {definition}
|
428 |
-
example response:
|
429 |
-
{example} !###!
|
430 |
-
|
431 |
-
Remember to be as concise as the example presented before and to follow the "hamburger-style" response format:
|
432 |
-
|
433 |
-
Misinformation: {text}
|
434 |
-
{fallacy} fallacy: {definition}
|
435 |
-
response:"""
|
|
|
1 |
"""
|
2 |
+
CARDS clamis and rebuttal examples
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
"""
|
4 |
|
5 |
FALLACY_CLAIMS = {
|
|
|
99 |
],
|
100 |
}
|
101 |
|
102 |
+
|
103 |
DEBUNKINGS = {
|
104 |
"Ice is not melting.": "## FACT: Ice is melting across the planet from the Arctic to Antarctica, with Greenland and Antarctica losing hundreds of billions of tonnes of ice per year and glaciers shrinking at an accelerating rate.\n\
|
105 |
## MYTH: Ice isn't melting.\n\
|
|
|
350 |
## FALLACY: This argument is a conspiracy theory, arguing that scientists are secretly plotting with evil intent.\n\
|
351 |
## FACT: The claim that scientists are engaged in a conspiracy to suppress dissenting views is not supported by evidence. The scientific process is inherently open, transparent, and self-correcting.",
|
352 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/templates.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Contains prompt templates to be used on app.py
|
3 |
+
"""
|
4 |
+
|
5 |
+
from langchain.prompts import PromptTemplate
|
6 |
+
|
7 |
+
REACT = PromptTemplate.from_template(
|
8 |
+
"""You will receive a piece of misinformation related to climate change. Your task is to translate this misinformation into a climate change-related question that challenges the misinformation and prompts for factual investigation.
|
9 |
+
For example, if the misinformation is: "Climate change isn't real because it's been cold this winter."
|
10 |
+
The translated question could be: "How does winter weather in one location relate to the broader scientific consensus on climate change?"
|
11 |
+
Be as specific as possible, ensuring the question directly addresses climate change and encourages factual investigation. You have access to the following tools:
|
12 |
+
|
13 |
+
{tools}
|
14 |
+
|
15 |
+
Use the following format:
|
16 |
+
|
17 |
+
Question: the translated question challenging the misinformation and prompting for factual investigation
|
18 |
+
Thought: you should always think about what to do
|
19 |
+
Action: the action to take, should be one of [{tool_names}]
|
20 |
+
Action Input: the input to the action
|
21 |
+
Observation: the result of the action
|
22 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
23 |
+
Thought: I now know the final answer
|
24 |
+
Final Answer: provide a factual response to the original misinformation, limit your answer two sentences or less than 30 words. Be specific, prefer facts that contain numbers or are backed up by recognised institutions or climate experts to ensure credibility.
|
25 |
+
|
26 |
+
Begin!
|
27 |
+
|
28 |
+
Question: {input}
|
29 |
+
Thought:{agent_scratchpad}"""
|
30 |
+
)
|
31 |
+
|
32 |
+
INCONTEXT = PromptTemplate.from_template(
|
33 |
+
"""[Role]:
|
34 |
+
As an expert climate analyst, your role is to provide precise and concise responses to climate change misinformation.
|
35 |
+
|
36 |
+
[Instruction]:
|
37 |
+
Identify the logical or argumentative fallacy within 40 words or fewer. Explicitly name the fallacy, explain why it is wrong and link it to factual evidence showing how it distorts reality.
|
38 |
+
|
39 |
+
[In-context example]:
|
40 |
+
Consider the following example:
|
41 |
+
misinformation: {example_myth}
|
42 |
+
response: {example_response}
|
43 |
+
|
44 |
+
[Misinformation]:
|
45 |
+
You've identified a {fallacy}-based fallacy in the following misinformation:
|
46 |
+
{fallacy} definition: {fallacy_definition}
|
47 |
+
|
48 |
+
You also possess the following factual information: {factual_information}
|
49 |
+
Remember to be concise and reply within 40 words or fewer.
|
50 |
+
misinformation: {misinformation}
|
51 |
+
response:
|
52 |
+
"""
|
53 |
+
)
|
54 |
+
|
55 |
+
SUMMARIZATION = PromptTemplate.from_template(
|
56 |
+
"""
|
57 |
+
Paraphrase the following text in 30 words or fewer.
|
58 |
+
|
59 |
+
TEXT: {text}
|
60 |
+
"""
|
61 |
+
)
|
62 |
+
|
63 |
+
CONCLUDING = PromptTemplate.from_template(
|
64 |
+
"""[Instruction]:
|
65 |
+
1. Reinforce the following fact and provide complementary details, if relevant, to enhance understanding.
|
66 |
+
2. The output should be simple text summarizing the information in 30 words or fewer.
|
67 |
+
|
68 |
+
Fact:
|
69 |
+
{fact}
|
70 |
+
"""
|
71 |
+
)
|
72 |
+
|
73 |
+
CONCLUDING_INCONTEXT = PromptTemplate.from_template(
|
74 |
+
"""[Instruction]:
|
75 |
+
1. Reinforce the following fact and provide complementary details, if relevant, to enhance understanding.
|
76 |
+
2. The output should be simple text summarizing the information in 30 words or fewer.
|
77 |
+
|
78 |
+
Complementary details:
|
79 |
+
{complementary_details}
|
80 |
+
|
81 |
+
Fact:
|
82 |
+
{fact}
|
83 |
+
"""
|
84 |
+
)
|