Spaces:
Runtime error
Runtime error
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import random
|
4 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
+
|
6 |
+
# Load the trained model and tokenizer
|
7 |
+
model_name = "your-username/hate-speech-classifier" # Replace with your actual Hugging Face model repo
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
model.to(device)
|
12 |
+
model.eval()
|
13 |
+
|
14 |
+
# Predefined usernames and chat messages for a game scenario
|
15 |
+
usernames = ["ShadowSlayer", "DragonKnight", "PixelMage", "CyberRogue", "PhantomArcher"]
|
16 |
+
game_responses = [
|
17 |
+
"I need backup at the base!",
|
18 |
+
"Watch out for enemies on the left!",
|
19 |
+
"Let's team up and attack together.",
|
20 |
+
"Great shot! That was amazing!",
|
21 |
+
"We need to capture the objective now!",
|
22 |
+
"Healing incoming, stay close!",
|
23 |
+
"I got eliminated, need a revive!",
|
24 |
+
"Nice strategy, let's keep it up!"
|
25 |
+
]
|
26 |
+
|
27 |
+
# Function for classification
|
28 |
+
def classify_message(message):
|
29 |
+
inputs = tokenizer(message, padding="max_length", truncation=True, return_tensors="pt").to(device)
|
30 |
+
with torch.no_grad():
|
31 |
+
outputs = model(**inputs)
|
32 |
+
logits = outputs.logits
|
33 |
+
prediction = torch.argmax(logits, dim=1).item()
|
34 |
+
return "Hate speech/Offensive" if prediction == 1 else "Not hate speech/Offensive"
|
35 |
+
|
36 |
+
# Chat simulation function
|
37 |
+
def chat_interface(history, _):
|
38 |
+
username = random.choice(usernames)
|
39 |
+
new_message = random.choice(game_responses)
|
40 |
+
classification = classify_message(new_message)
|
41 |
+
blurred_message = "****" if classification == "Hate speech/Offensive" else new_message
|
42 |
+
history.append((f"{username}: {new_message}", f"{username}: {blurred_message}"))
|
43 |
+
|
44 |
+
# Generate automated game response
|
45 |
+
bot_username = "GameMaster"
|
46 |
+
bot_response = random.choice(game_responses)
|
47 |
+
history.append((f"{bot_username}: {bot_response}", f"{bot_username}: {bot_response}"))
|
48 |
+
|
49 |
+
return history, history
|
50 |
+
|
51 |
+
# Create Gradio interface
|
52 |
+
def main():
|
53 |
+
with gr.Blocks() as app:
|
54 |
+
gr.Markdown("# Game Chat Hate Speech Detection Simulator")
|
55 |
+
chatbot = gr.Chatbot()
|
56 |
+
submit = gr.Button("Generate Message")
|
57 |
+
|
58 |
+
submit.click(chat_interface, inputs=[chatbot, None], outputs=[chatbot, chatbot])
|
59 |
+
|
60 |
+
app.launch()
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
main()
|