File size: 1,967 Bytes
aae0b7f
 
 
 
 
 
 
 
 
9e039a6
aae0b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from groq import Groq
from dotenv import load_dotenv
import gradio as gr

# Load environment variables from .env file
load_dotenv()

# Initialize Groq client
GROQ_API_KEY="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"
client = Groq(api_key=GROQ_API_KEY)

# Function to moderate content
def moderate_content(user_input):
    try:
        # Generate moderation report using Groq API
        chat_completion = client.chat.completions.create(
            messages=[
                {
                    "role": "user",
                    "content": f"Analyze this content for violations: {user_input}",
                }
            ],
            model="llama-3.3-70b-versatile",
            stream=False,
        )
        response = chat_completion.choices[0].message.content

        # Suggest actions based on the response
        if "violation" in response.lower():
            action = "Flag or delete this content."
        else:
            action = "Content is compliant."

        return response, action

    except Exception as e:
        return f"Error: {str(e)}", "No action suggested."

# Define Gradio interface
with gr.Blocks() as app:
    gr.Markdown("### Content Moderation with Groq API")
    with gr.Row():
        with gr.Column():
            user_input = gr.Textbox(
                label="Enter Content for Moderation",
                placeholder="Type the content you want to moderate here...",
            )
            moderate_btn = gr.Button("Moderate Content")
        with gr.Column():
            moderation_result = gr.Textbox(label="Moderation Result", interactive=False)
            suggested_action = gr.Textbox(label="Suggested Action", interactive=False)

    # Connect button to moderation function
    moderate_btn.click(
        moderate_content,
        inputs=[user_input],
        outputs=[moderation_result, suggested_action],
    )

# Run the Gradio app
if __name__ == "__main__":
    app.launch()