File size: 3,442 Bytes
b86ba0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709e8f1
b86ba0c
 
 
 
 
 
709e8f1
b86ba0c
 
 
709e8f1
b86ba0c
 
 
 
 
 
 
 
 
 
 
 
 
709e8f1
b86ba0c
 
 
 
 
 
 
 
 
 
 
 
709e8f1
b86ba0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709e8f1
 
b86ba0c
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
import gradio as gr
import torch
import json
from pathlib import Path
from model import (
    GreesyGPT, 
    generate_moderation, 
    ReasoningMode, 
    OutputFormat, 
    DEVICE,
    describe_reasoning_modes
)

# 1. Initialize Model
model = GreesyGPT()
weights_path = Path("greesy_gpt.pt")

if weights_path.exists():
    model.load_state_dict(torch.load(weights_path, map_location=DEVICE))
    print(f"Loaded weights from {weights_path}")
else:
    print("Warning: No trained weights found. Using fresh initialization.")

model.to(DEVICE)
model.eval()

def moderate(text, mode_str, format_str):
    if not text.strip():
        return "Please enter some text to analyze.", ""

    mode = ReasoningMode(mode_str.lower())
    fmt = OutputFormat(format_str.lower())

    result = generate_moderation(
        model, 
        prompt=text, 
        mode=mode, 
        output_format=fmt
    )

    verdict_output = result["verdict_fmt"]
    if fmt == OutputFormat.JSON:
        verdict_output = f"```json\n{json.dumps(verdict_output, indent=2)}\n```"
    
    thinking_process = result.get("thinking", "No reasoning generated.")
    
    return verdict_output, thinking_process

# 2. Build Gradio UI
theme = gr.themes.Soft(primary_hue="orange", secondary_hue="gray")

with gr.Blocks(theme=theme, title="GreesyGPT Content Moderation") as demo:
    gr.Markdown("# 🛡️ GreesyGPT Content Moderation")
    gr.Markdown("Reasoning-based safety model using chain-of-thought deliberation.")
    
    with gr.Row():
        with gr.Column(scale=2):
            input_text = gr.Textbox(
                label="Message to Review",
                placeholder="Type the message you want to moderate here...",
                lines=5
            )
            
            with gr.Row():
                mode_dropdown = gr.Dropdown(
                    choices=[m.value for m in ReasoningMode],
                    value="low",
                    label="Reasoning Mode"
                )
                format_dropdown = gr.Dropdown(
                    choices=[f.value for f in OutputFormat],
                    value="markdown",
                    label="Output Format"
                )
            
            submit_btn = gr.Button("Analyze Content", variant="primary")

        with gr.Column(scale=3):
            output_verdict = gr.Markdown(label="Verdict")
            
            # FIXED: Changed Expander to Accordion
            with gr.Accordion("View Internal Reasoning (Thinking Process)", open=False):
                output_thinking = gr.Textbox(
                    label="Chain of Thought",
                    interactive=False,
                    lines=10
                )

    gr.Examples(
        examples=[
            ["You're so stupid, nobody likes you.", "medium", "markdown"],
            ["How do I fix a bug in my Python code?", "none", "markdown"],
            ["CONGRATULATIONS! You won a $1000 gift card! Click here!", "low", "json"],
        ],
        inputs=[input_text, mode_dropdown, format_dropdown]
    )

    # FIXED: Changed Expander to Accordion
    with gr.Accordion("System Information / Reasoning Mode Definitions", open=False):
        gr.Code(describe_reasoning_modes(), language="text")

    submit_btn.click(
        fn=moderate,
        inputs=[input_text, mode_dropdown, format_dropdown],
        outputs=[output_verdict, output_thinking]
    )

if __name__ == "__main__":
    demo.launch()