-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (53 loc) · 2.41 KB
/
main.py
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
# -*- coding: utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
import os
from discord.ext import commands
from dotenv import load_dotenv, find_dotenv
from model import FakeNewsAgent
import validators
load_dotenv(find_dotenv())
# Instantiate model with endpoint
agent = FakeNewsAgent(endpoint_url=os.getenv('ENDPOINT_URL'))
# Instantiate bot
bot = commands.Bot(command_prefix="$")
@bot.event
async def on_ready():
logger.info("Bot is ready")
@bot.event
async def on_message(message):
if message.author != bot.user:
if message.content.startswith('$'):
await bot.process_commands(message)
else:
response = f"Hi {message.author}! Ich bin Bubblestitch, dein Fake News Agent! " \
"Du kannst mich verwenden, um News Artikel (auf Deutsch!) hinsichtlich Fake News " \
"bewerten zu lassen. Dazu verwende einfach das Kommando '$score <URL zum Artikel>' " \
"mit einer URL zu einem Artikel. Ich liefere dir einen Fake-Score zurück, welcher zwischen 0 und 1 liegt. " \
"Je höher der Score sich bei 1 befindet, desto eher geht meine Einschätzung davon aus, dass der "\
"Artikel den du mir gegeben hast Fake News beinhaltet. Versuchs doch einfach!"
await message.channel.send(response)
@bot.command()
async def score(ctx, url):
# Check if url is valid
if not validators.url(str(url)):
# Generate response
response = "Sorry, aber das war keine gültige URL! Check nochmal deine Eingabe und versuchs nochmal!"
logger.info(f"'{url}' war keine gültige URL")
else:
logger.info(f"Got {url} to score")
# Feed endpoint with url and get fake score
raw = agent.predict(url)
fake_score = round(raw['data'][0][1], 2)
# Generate som interpretations for the user
if fake_score < 0.4:
interpretation_str = "Der Artikel sieht für mich nicht nach Fake News aus."
elif fake_score >= 0.4 and fake_score <= 0.8:
interpretation_str = "Der Artikel könnte Fake sein, bin mir aber nicht sicher."
elif fake_score > 0.8:
interpretation_str = "Hey Achtung, das sieht nach Fake News aus!"
# Generate response
response = f"Dieser Artikel hat einen Fake-Score von {fake_score}. {interpretation_str}"
await ctx.send(response)
bot.run(os.getenv('BOT_TOKEN'))