Spaces:
Sleeping
Sleeping
import os | |
import json | |
import requests | |
import gradio as gr | |
from langchain.schema import SystemMessage | |
from langchain.chat_models import ChatGooglePalm | |
from langchain.agents import initialize_agent | |
from langchain.agents import AgentType | |
from langchain.tools import Tool | |
hf_api_key = os.environ['HF_API_KEY'] | |
google_key = os.environ['GOOGLE_API_KEY'] | |
def query(payload, model_id, api_token=hf_api_key): | |
headers = {"Authorization": f"Bearer {api_token}"} | |
API_URL = f"https://api-inference.huggingface.co/models/{model_id}" | |
options = {"use_cache": True, "wait_for_model": True} | |
payload = {"inputs":payload, "options":options} | |
response = requests.post(API_URL, headers=headers, json=payload) | |
return json.loads(response.content.decode("utf-8")) | |
flicc_model = "fzanartu/flicc" | |
cards_model = "crarojasca/BinaryAugmentedCARDS" | |
def fallacy_detector(text): | |
response = query(text, model_id=cards_model) | |
if response[0][0].get('label') == 'Contrarian': | |
response = query(text, model_id=flicc_model) | |
result = response[0][0].get('label') | |
else: | |
result = 'There is no fallacy detected in your text' | |
return result | |
system_message = SystemMessage( | |
content = """You are an expert climate analyst, who provide precise and consise responses to climate change misinformation. | |
Please make sure you complete the objective above with the following rules: | |
1/ Provide precise and concise replies to climate change misinformation using a structured "hamburger-style" response, including the myth, the fallacy, and the fact. | |
2/ You should use the provided fallacy detector tool to identify the correct fallacy contained in the text. | |
3/ Hamburger-Style Response: Your responses should follow a structured format commonly known as the "hamburger-style." This includes: | |
Top Bun (Myth): Start with the misinformation related to climate change. | |
Meat (Fallacy): Utilize the provided fallacy detector tool to identify the specific fallacy in the misinformation. Explain why the misinformation is incorrect, addressing the fallacious reasoning present. | |
Bottom Bun (Fact): End with a clear and concise presentation of the factual information that debunks the climate change myth. | |
Your task is complete once you have written all the elements of the hamburger-style response. | |
""" | |
) | |
tools = [ | |
Tool( | |
name="fallacy_detector", | |
func=fallacy_detector, | |
description="useful when you need to detect a fallacy" | |
), | |
] | |
agent_kwargs = { | |
"system_message": system_message, | |
} | |
llm = ChatGooglePalm(temperature=0.0, model_name="models/chat-bison-001", google_api_key=google_key) | |
agent = initialize_agent( | |
tools, | |
llm, | |
agent=AgentType.OPENAI_FUNCTIONS, | |
verbose=False, | |
agent_kwargs=agent_kwargs, | |
) | |
def get_reply(text): | |
response = agent.run(text) | |
return response | |
demo = gr.Interface(fn=get_reply, inputs="text", outputs="text") | |
demo.launch() |