Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Initialize Groq client
|
| 10 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
+
|
| 12 |
+
# Function to moderate content
|
| 13 |
+
def moderate_content(user_input):
|
| 14 |
+
try:
|
| 15 |
+
# Generate moderation report using Groq API
|
| 16 |
+
chat_completion = client.chat.completions.create(
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": f"Analyze this content for violations: {user_input}",
|
| 21 |
+
}
|
| 22 |
+
],
|
| 23 |
+
model="llama-3.3-70b-versatile",
|
| 24 |
+
stream=False,
|
| 25 |
+
)
|
| 26 |
+
response = chat_completion.choices[0].message.content
|
| 27 |
+
|
| 28 |
+
# Suggest actions based on the response
|
| 29 |
+
if "violation" in response.lower():
|
| 30 |
+
action = "Flag or delete this content."
|
| 31 |
+
else:
|
| 32 |
+
action = "Content is compliant."
|
| 33 |
+
|
| 34 |
+
return response, action
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}", "No action suggested."
|
| 38 |
+
|
| 39 |
+
# Define Gradio interface
|
| 40 |
+
with gr.Blocks() as app:
|
| 41 |
+
gr.Markdown("### Content Moderation with Groq API")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column():
|
| 44 |
+
user_input = gr.Textbox(
|
| 45 |
+
label="Enter Content for Moderation",
|
| 46 |
+
placeholder="Type the content you want to moderate here...",
|
| 47 |
+
)
|
| 48 |
+
moderate_btn = gr.Button("Moderate Content")
|
| 49 |
+
with gr.Column():
|
| 50 |
+
moderation_result = gr.Textbox(label="Moderation Result", interactive=False)
|
| 51 |
+
suggested_action = gr.Textbox(label="Suggested Action", interactive=False)
|
| 52 |
+
|
| 53 |
+
# Connect button to moderation function
|
| 54 |
+
moderate_btn.click(
|
| 55 |
+
moderate_content,
|
| 56 |
+
inputs=[user_input],
|
| 57 |
+
outputs=[moderation_result, suggested_action],
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Run the Gradio app
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.launch()
|