|
|
import gradio as gr |
|
|
from huggingface_hub import InferenceClient |
|
|
import os |
|
|
|
|
|
HF_API_TOKEN = os.getenv("HF_API_TOKEN") |
|
|
client = InferenceClient( |
|
|
provider="nebius", |
|
|
api_key=HF_API_TOKEN |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def query_llm(text, mode): |
|
|
prompt = f"Text: {text}\n\n" |
|
|
if mode == "simple": |
|
|
prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list." |
|
|
else: |
|
|
prompt += "Task: Is the above TEXT toxic? First clearly state your decision. Then, provide specific reason(s) for your decision. If there is more than one reason, provide them in a numbered list. Your reason(s) must be (a) non-redundant, (b) confidently and meaningfully engage with the given Text, and (c) jointly sufficient to justify your decision. In other words, you must have utilized all possible information from the input text and from external sources before arriving at your conclusion and explanation." |
|
|
|
|
|
completion = client.chat.completions.create( |
|
|
model="google/gemma-2-2b-it", |
|
|
messages=[ |
|
|
{ |
|
|
"role": "user", |
|
|
"content": prompt |
|
|
} |
|
|
], |
|
|
) |
|
|
return completion.choices[0].message['content'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
preloaded_texts = [ |
|
|
"People from the outside must look at us and think what stupid people, what are they doing?", |
|
|
"Donald Trump was responding to comments from Prime Minister David Cameron, who called his proposal to temporarily ban all Muslims from the United States divisive, stupid and wrong.", |
|
|
"Every binder should have a warning label on it that says CAUTION: BORING SHIT INSIDE.", |
|
|
"If you want to call me a bitch, call me a bitch, but that's just kind of stupid", |
|
|
"I want to just have laughs on the internet, not have fucking twelve year olds intruding into my personal life.", |
|
|
"Today I sit down with him to talk about his forthcoming book Writing Without Bullshit: Boost Your Career by Saying What You Mean." |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title="Toxicity Explainer") as demo: |
|
|
gr.Markdown("## Toxicity Explainer") |
|
|
gr.Markdown("Select a text from the list or enter your own, then view two explanations (naive and our theory-guided) on the right. Based on recent work: https://github.com/uofthcdslab/HAF.") |
|
|
|
|
|
with gr.Row(): |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
gr.Markdown("### Select or Enter a Text") |
|
|
|
|
|
text_list = gr.Dropdown(preloaded_texts, label="Choose a text") |
|
|
custom_text = gr.Textbox( |
|
|
placeholder="Or enter your own text here...", |
|
|
lines=3, |
|
|
label="Custom Text" |
|
|
) |
|
|
|
|
|
|
|
|
submit_btn = gr.Button("Use this Text") |
|
|
|
|
|
|
|
|
with gr.Column(scale=2): |
|
|
gr.Markdown("### Explanations") |
|
|
|
|
|
with gr.Tab("Naive Explanation"): |
|
|
simple_output = gr.Textbox(label="Naive Explanation", lines=15) |
|
|
|
|
|
with gr.Tab("Theory-Grounded Explanation"): |
|
|
detailed_output = gr.Textbox(label="Theory-Grounded Explanation", lines=15) |
|
|
|
|
|
def process_text(selected, custom): |
|
|
text = custom.strip() if custom else selected |
|
|
if not text: |
|
|
return "Please enter or select a text!", "Please enter or select a text!" |
|
|
|
|
|
simple = query_llm(text, "simple") |
|
|
detailed = query_llm(text, "detailed") |
|
|
return simple, detailed |
|
|
|
|
|
submit_btn.click( |
|
|
process_text, |
|
|
inputs=[text_list, custom_text], |
|
|
outputs=[simple_output, detailed_output] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
demo.launch() |
|
|
|