File size: 3,976 Bytes
94a621a
3ea3319
94a621a
 
 
3ea3319
08c411e
 
3ea3319
 
94a621a
43fcdf6
94a621a
0e92063
 
94a621a
 
 
0e92063
94a621a
08c411e
 
 
 
 
 
 
 
3b95ad2
08c411e
 
94a621a
 
0e92063
94a621a
0e92063
8d507fa
 
94a621a
 
 
 
 
 
 
 
 
 
0e92063
 
7ea7201
94a621a
 
 
 
0e92063
94a621a
0e92063
 
 
94a621a
0e92063
94a621a
 
 
0e92063
94a621a
 
 
 
 
0e92063
3df5e69
94a621a
0e92063
3df5e69
94a621a
0e92063
 
 
 
94a621a
0e92063
 
94a621a
 
 
0e92063
 
94a621a
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
)

# -------------------------
# API Call - Based on recent work: https://github.com/uofthcdslab/HAF
# -------------------------
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
# -------------------------
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."
]


# -------------------------
# Gradio Blocks UI
# -------------------------
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():
        # Left pane
        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"
            )

            # Button to confirm selection
            submit_btn = gr.Button("Use this Text")

        # Right pane
        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]
    )


# Run app
demo.launch()