File size: 3,409 Bytes
a0f3c62
 
 
9786a27
b55e08d
 
a0f3c62
9786a27
 
 
 
 
 
 
 
a0f3c62
 
a5baca0
a0f3c62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b55e08d
a0f3c62
 
 
 
 
 
 
 
 
 
 
 
b55e08d
5199940
 
 
b55e08d
 
 
 
 
 
 
a0f3c62
 
 
 
5199940
a0f3c62
b55e08d
a0f3c62
 
5199940
a0f3c62
5199940
a0f3c62
b55e08d
 
 
 
 
 
a0f3c62
 
 
 
5199940
b55e08d
 
 
 
a0f3c62
b55e08d
 
a0f3c62
 
 
 
 
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
import gradio as gr
import torch
import random
import os
import time
import threading
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from huggingface_hub import login

# Login to Hugging Face using token from environment variable
hf_token = os.getenv("HUGGINGFACE_TOKEN")
if hf_token:
    login(token=hf_token)
else:
    raise ValueError("HUGGINGFACE_TOKEN environment variable not set.")

# Load the trained model and tokenizer
model_name = "chaitravi/hate-speech-classifier"  # Replace with your actual Hugging Face model repo
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

# Predefined usernames and chat messages for a game scenario
usernames = ["ShadowSlayer", "DragonKnight", "PixelMage", "CyberRogue", "PhantomArcher"]
game_responses = [
    "I need backup at the base!",
    "Watch out for enemies on the left!",
    "Let's team up and attack together.",
    "Great shot! That was amazing!",
    "We need to capture the objective now!",
    "Healing incoming, stay close!",
    "I got eliminated, need a revive!",
    "Nice strategy, let's keep it up!"
]

# Function for classification
def classify_message(message):
    inputs = tokenizer(message, padding="max_length", truncation=True, return_tensors="pt").to(device)
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        prediction = torch.argmax(logits, dim=1).item()
    return "Hate speech/Offensive" if prediction == 1 else "Not hate speech/Offensive"

# Chat simulation function
def chat_interface(history, user_message=""):
    if history is None:
        history = []
    
    # Process user input
    if user_message:
        classification = classify_message(user_message)
        blurred_message = "****" if classification == "Hate speech/Offensive" else user_message
        history.append({"role": "user", "content": f"You: {blurred_message}"})
    
    # Generate automated response
    username = random.choice(usernames)
    new_message = random.choice(game_responses)
    classification = classify_message(new_message)
    blurred_message = "****" if classification == "Hate speech/Offensive" else new_message
    history.append({"role": "user", "content": f"{username}: {blurred_message}"})
    
    # Generate bot response
    bot_username = "GameMaster"
    bot_response = random.choice(game_responses)
    history.append({"role": "assistant", "content": f"{bot_username}: {bot_response}"})
    
    return history

# Background chat simulation
def simulate_chat(chatbot):
    while True:
        chatbot.update(chat_interface(chatbot.value))
        time.sleep(2)

# Create Gradio interface
def main():
    with gr.Blocks() as app:
        gr.Markdown("# Game Chat Hate Speech Detection Simulator")
        chatbot = gr.Chatbot(type="messages")
        user_input = gr.Textbox(label="Enter your message")
        log_box = gr.Textbox(label="Classification Log", interactive=False)
        
        user_input.submit(chat_interface, inputs=[chatbot, user_input], outputs=[chatbot])
        
        # Start chat simulation in a separate thread
        threading.Thread(target=simulate_chat, args=(chatbot,), daemon=True).start()
    
    app.launch()

if __name__ == "__main__":
    main()