File size: 9,615 Bytes
fc016ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# === Gradio Demo App: gradio_app.py ===
# This script creates a user-friendly web interface to demonstrate the
# multimodal moderation capabilities of the main FastAPI server.
#
# It interacts with the /v3/moderations endpoint.
# --------------------------------------------------------------------

import base64
import os
import json
import logging

import gradio as gr
import httpx
from dotenv import load_dotenv

# --- Configuration ---
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
load_dotenv()

# The URL of your running FastAPI server.
# It's crucial to set this in your .env file for deployment.
API_BASE_URL = os.environ.get("API_BASE_URL", "")
MODERATION_ENDPOINT = f"{API_BASE_URL}/v3/moderations"

# --- NEW: Full list of Whisper V3 supported languages ---
# Mapping user-friendly names to ISO 639-1 codes
WHISPER_LANGUAGES = {
    "English": "en", "Chinese": "zh", "German": "de", "Spanish": "es", "Russian": "ru",
    "Korean": "ko", "French": "fr", "Japanese": "ja", "Portuguese": "pt", "Turkish": "tr",
    "Polish": "pl", "Catalan": "ca", "Dutch": "nl", "Arabic": "ar", "Swedish": "sv",
    "Italian": "it", "Indonesian": "id", "Hindi": "hi", "Finnish": "fi", "Vietnamese": "vi",
    "Hebrew": "he", "Ukrainian": "uk", "Greek": "el", "Malay": "ms", "Czech": "cs",
    "Romanian": "ro", "Danish": "da", "Hungarian": "hu", "Tamil": "ta", "Norwegian": "no",
    "Thai": "th", "Urdu": "ur", "Croatian": "hr", "Bulgarian": "bg", "Lithuanian": "lt",
    "Latin": "la", "Maori": "mi", "Malayalam": "ml", "Welsh": "cy", "Slovak": "sk",
    "Telugu": "te", "Persian": "fa", "Latvian": "lv", "Bengali": "bn", "Serbian": "sr",
    "Azerbaijani": "az", "Slovenian": "sl", "Kannada": "kn", "Estonian": "et", "Macedonian": "mk",
    "Breton": "br", "Basque": "eu", "Icelandic": "is", "Armenian": "hy", "Nepali": "ne",
    "Mongolian": "mn", "Bosnian": "bs", "Kazakh": "kk", "Albanian": "sq", "Swahili": "sw",
    "Galician": "gl", "Marathi": "mr", "Punjabi": "pa", "Sinhala": "si", "Khmer": "km",
    "Shona": "sn", "Yoruba": "yo", "Somali": "so", "Afrikaans": "af", "Occitan": "oc",
    "Georgian": "ka", "Belarusian": "be", "Tajik": "tg", "Sindhi": "sd", "Gujarati": "gu",
    "Amharic": "am", "Yiddish": "yi", "Lao": "lo", "Uzbek": "uz", "Faroese": "fo",
    "Haitian Creole": "ht", "Pashto": "ps", "Turkmen": "tk", "Nynorsk": "nn", "Maltese": "mt",
    "Sanskrit": "sa", "Luxembourgish": "lb", "Myanmar (Burmese)": "my", "Tibetan": "bo",
    "Tagalog": "tl", "Malagasy": "mg", "Assamese": "as", "Tatar": "tt", "Hawaiian": "haw",
    "Lingala": "ln", "Hausa": "ha", "Bashkir": "ba", "Javanese": "jw", "Sundanese": "su",
}
# Sort languages alphabetically for the dropdown
SORTED_LANGUAGES = dict(sorted(WHISPER_LANGUAGES.items()))


# --- Helper Function ---
def file_to_base64(filepath: str) -> str:
    """Reads a file and converts it to a base64 encoded string."""
    if not filepath:
        return None
    try:
        with open(filepath, "rb") as f:
            encoded_string = base64.b64encode(f.read()).decode("utf-8")
        return encoded_string
    except Exception as e:
        logging.error(f"Failed to convert file {filepath} to base64: {e}")
        return None

# --- Core Logic ---
def moderate_content(text_input, image_input, video_input, audio_input, language_full_name):
    """
    Prepares the payload, calls the moderation API, and formats the response.
    """
    if not any([text_input, image_input, video_input, audio_input]):
        return "Please provide at least one input (text, image, video, or audio).", None

    logging.info("Preparing payload for moderation API...")
    payload = {
        "model": "nai-moderation-latest" # This is the model name expected by our API
    }

    if text_input:
        payload["input"] = text_input
    
    # Gradio provides file paths; we need to convert them to base64
    image_b64 = file_to_base64(image_input)
    if image_b64:
        payload["image"] = image_b64

    video_b64 = file_to_base64(video_input)
    if video_b64:
        payload["video"] = video_b64

    audio_b64 = file_to_base64(audio_input)
    if audio_b64:
        payload["voice"] = audio_b64
        # --- NEW: Add selected language to the payload ---
        language_code = SORTED_LANGUAGES.get(language_full_name, "en") # Default to 'en' if not found
        payload["language"] = language_code
        logging.info(f"Audio detected. Using language: {language_full_name} ({language_code})")


    logging.info(f"Sending request to {MODERATION_ENDPOINT} with inputs: {list(payload.keys())}")
    
    summary_output = "An error occurred. Please check the logs."
    full_response_output = {}

    try:
        # Using a synchronous client is simpler for this Gradio function
        with httpx.Client(timeout=180.0) as client:
            response = client.post(MODERATION_ENDPOINT, json=payload)
            response.raise_for_status() # Raises an exception for 4xx/5xx errors
            
            data = response.json()
            full_response_output = data
            
            if not data.get("results"):
                summary_output = "API returned an empty result. This might happen if media processing fails (e.g., a video with no frames)."
                return summary_output, full_response_output

            # The v3 endpoint returns a single aggregated result
            result = data["results"][0]
            
            # Format a nice, human-readable summary
            status = "🚨 FLAGGED 🚨" if result["flagged"] else "βœ… SAFE βœ…"
            reason = result.get("reason") or "N/A"
            transcribed = result.get("transcribed_text") or "N/A"

            # Create a clean list of flagged categories
            flagged_categories = [cat for cat, flagged in result.get("categories", {}).items() if flagged]
            categories_str = ", ".join(flagged_categories) if flagged_categories else "None"

            summary_output = f"""
            **Moderation Status:** {status}
            ---
            **Reason:** {reason}
            ---
            **Flagged Categories:** {categories_str}
            ---
            **Transcribed Text (from audio):**
            {transcribed}
            """
            logging.info("Successfully received and parsed moderation response.")

    except httpx.HTTPStatusError as e:
        error_details = e.response.text
        summary_output = f"HTTP Error: {e.response.status_code}\n\nCould not connect to the moderation service or the service returned an error.\n\nDetails:\n{error_details}"
        logging.error(f"HTTP Status Error: {error_details}")
    except httpx.RequestError as e:
        summary_output = f"Request Error: Could not connect to the API server at {API_BASE_URL}.\nPlease ensure the server is running and the URL is correct."
        logging.error(f"Request Error: {e}")
    except Exception as e:
        summary_output = f"An unexpected error occurred: {str(e)}"
        logging.error(f"Unexpected Error: {e}", exc_info=True)
        
    return summary_output, full_response_output

# --- Gradio Interface ---
with gr.Blocks(theme=gr.themes.Soft(), css="footer {display: none !important}") as demo:
    gr.Markdown(
        """
        # πŸ€– Multimodal Content Moderation Demo
        This demo uses a custom API server to perform advanced content moderation.
        You can provide any combination of text, image, video, and audio. The system will analyze all inputs together.
        """
    )

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### 1. Provide Your Content")
            text_input = gr.Textbox(label="Text Input", lines=4, placeholder="Enter any text here...")
            image_input = gr.Image(label="Image Input", type="filepath")
            video_input = gr.Video(label="Video Input")
            audio_input = gr.Audio(label="Voice/Audio Input", type="filepath")
            
            # --- NEW: Language selection dropdown ---
            language_input = gr.Dropdown(
                label="Audio Language (if providing audio)",
                choices=list(SORTED_LANGUAGES.keys()),
                value="English",
                interactive=True
            )

            submit_button = gr.Button("Moderate Content", variant="primary")
        
        with gr.Column(scale=2):
            gr.Markdown("### 2. See the Results")
            result_output = gr.Markdown(label="Moderation Summary")
            full_response_output = gr.JSON(label="Full API Response")

    submit_button.click(
        fn=moderate_content,
        # --- UPDATED: Add language_input to the list ---
        inputs=[text_input, image_input, video_input, audio_input, language_input],
        outputs=[result_output, full_response_output]
    )
    
    gr.Examples(
        examples=[
            ["This is a test of the system with safe text.", None, None, None, "English"],
            ["I am going to kill the process on my computer.", None, None, None, "English"],
        ],
        # --- UPDATED: Add language_input to the list ---
        inputs=[text_input, image_input, video_input, audio_input, language_input],
        outputs=[result_output, full_response_output],
        fn=moderate_content
    )


if __name__ == "__main__":
    logging.info(f"Connecting to API server at: {API_BASE_URL}")
    if API_BASE_URL == "http://127.0.0.1:8000":
        logging.warning("API_BASE_URL is set to the default local address. Make sure this is correct or set it in your .env file.")
    demo.launch(server_name="0.0.0.0", server_port=7860)