Tobias Geisler commited on
Commit
61c774d
·
1 Parent(s): 0a440b6
Files changed (2) hide show
  1. database.py +139 -0
  2. german_profanity.txt +1 -0
database.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # database.py
2
+ import os
3
+ from sqlalchemy import create_engine, Column, Integer, String, Text, Boolean, DateTime, func
4
+ from sqlalchemy.ext.declarative import declarative_base
5
+ from sqlalchemy.orm import sessionmaker
6
+ from utils import get_secret
7
+ import random
8
+ from better_profanity import profanity
9
+ import logging
10
+
11
+ # Database connection
12
+ DB_USER = get_secret("DB_USER")
13
+ DB_PASSWORD = get_secret("DB_PASSWORD")
14
+ DB_HOST = get_secret("DB_HOST")
15
+ DB_NAME = get_secret("DB_NAME")
16
+ DB_PORT = get_secret("DB_PORT", "3306")
17
+
18
+ DATABASE_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
19
+
20
+ logging.basicConfig(level=logging.INFO)
21
+ logger = logging.getLogger(__name__)
22
+
23
+ try:
24
+ logger.info("Connecting to database at %s", DB_HOST)
25
+ engine = create_engine(DATABASE_URL)
26
+ # Test the connection
27
+ with engine.connect() as connection:
28
+ logger.info("Successfully connected to the database")
29
+ except Exception as e:
30
+ logger.error(f"Error connecting to the database: {str(e)}")
31
+ raise e
32
+
33
+ #engine = create_engine(DATABASE_URL)
34
+ SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
35
+
36
+ Base = declarative_base()
37
+
38
+ class Chatbot(Base):
39
+ __tablename__ = "chatbots"
40
+
41
+ id = Column(Integer, primary_key=True, index=True)
42
+ chatbot_id = Column(String(50), unique=True, index=True, nullable=False)
43
+ name = Column(String(100), nullable=False)
44
+ custom_instruction = Column(Text, nullable=False)
45
+ is_active = Column(Boolean, default=True)
46
+ created_at = Column(DateTime(timezone=True), server_default=func.now())
47
+ updated_at = Column(DateTime(timezone=True), onupdate=func.now())
48
+
49
+ # Create tables
50
+ Base.metadata.create_all(bind=engine)
51
+
52
+ # Load German profanity words
53
+ german_profanity = set()
54
+ with open('german_profanity.txt', 'r', encoding='utf-8') as f:
55
+ german_profanity = set(word.strip().lower() for word in f)
56
+
57
+ profanity.add_censor_words(german_profanity)
58
+
59
+ def filter_profanity(text):
60
+ return profanity.censor(text)
61
+
62
+ def generate_chatbot_id():
63
+ adjectives = [
64
+ 'happy', 'clever', 'bright', 'shiny', 'fluffy', 'gentle', 'brave', 'calm',
65
+ 'kind', 'joyful', 'radiant', 'sparkling', 'cheerful', 'gracious', 'elegant',
66
+ 'vivacious', 'serene', 'vibrant', 'splendid', 'charismatic', 'delightful',
67
+ 'blissful', 'generous', 'charming', 'dazzling', 'glowing', 'harmonious',
68
+ 'jovial', 'luminous', 'majestic', 'mellow', 'noble', 'optimistic',
69
+ 'passionate', 'playful', 'resilient', 'spirited', 'tranquil', 'upbeat',
70
+ 'valiant', 'whimsical', 'witty', 'zealous', 'admirable', 'affectionate',
71
+ 'brilliant', 'courteous', 'devoted', 'ecstatic', 'faithful', 'gleeful'
72
+ ]
73
+
74
+ nouns = [
75
+ 'elephant', 'penguin', 'lion', 'dolphin', 'koala', 'panda', 'tiger', 'whale',
76
+ 'butterfly', 'eagle', 'peacock', 'unicorn', 'phoenix', 'otter', 'swan',
77
+ 'chameleon', 'ladybug', 'flamingo', 'puppy', 'kitten', 'fawn', 'hummingbird',
78
+ 'koala', 'firefly', 'bunny', 'goldfish', 'puffin', 'orca', 'red panda', 'turtle',
79
+ 'parrot', 'owl', 'seahorse', 'hedgehog', 'sloth', 'duckling', 'starfish',
80
+ 'gazelle', 'panther', 'robin', 'seal', 'lynx', 'jellyfish', 'gecko',
81
+ 'kangaroo', 'lemur', 'meerkat', 'platypus', 'quokka', 'squirrel', 'toucan'
82
+ ]
83
+
84
+ return f"{random.choice(adjectives)}-{random.choice(nouns)}-{random.randint(100, 999)}"
85
+
86
+ def create_chatbot(name, custom_instruction):
87
+ db = SessionLocal()
88
+ try:
89
+ chatbot_id = generate_chatbot_id()
90
+ new_chatbot = Chatbot(chatbot_id=chatbot_id, name=name, custom_instruction=custom_instruction)
91
+ db.add(new_chatbot)
92
+ db.commit()
93
+ db.refresh(new_chatbot)
94
+ return new_chatbot
95
+ finally:
96
+ db.close()
97
+
98
+ def get_chatbot(chatbot_id):
99
+ db = SessionLocal()
100
+ try:
101
+ return db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id, Chatbot.is_active == True).first()
102
+ finally:
103
+ db.close()
104
+
105
+ def update_chatbot(chatbot_id, name=None, custom_instruction=None, is_active=None):
106
+ db = SessionLocal()
107
+ try:
108
+ chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
109
+ if chatbot:
110
+ if name:
111
+ chatbot.name = name
112
+ if custom_instruction:
113
+ chatbot.custom_instruction = custom_instruction
114
+ if is_active is not None:
115
+ chatbot.is_active = is_active
116
+ db.commit()
117
+ db.refresh(chatbot)
118
+ return chatbot
119
+ finally:
120
+ db.close()
121
+
122
+ def delete_chatbot(chatbot_id):
123
+ db = SessionLocal()
124
+ try:
125
+ chatbot = db.query(Chatbot).filter(Chatbot.chatbot_id == chatbot_id).first()
126
+ if chatbot:
127
+ db.delete(chatbot)
128
+ db.commit()
129
+ return True
130
+ return False
131
+ finally:
132
+ db.close()
133
+
134
+ def get_all_chatbots():
135
+ db = SessionLocal()
136
+ try:
137
+ return db.query(Chatbot).all()
138
+ finally:
139
+ db.close()
german_profanity.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ Arsch