AjithKSenthil commited on
Commit
0611f13
·
verified ·
1 Parent(s): e5b9c49

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +50 -10
chatbot.py CHANGED
@@ -40,9 +40,14 @@ def create_table():
40
  if conn:
41
  cur = conn.cursor()
42
  create_table_query = '''
 
 
 
 
 
43
  CREATE TABLE IF NOT EXISTS chat_transcripts (
44
  id SERIAL PRIMARY KEY,
45
- user_id VARCHAR(255) NOT NULL,
46
  transcript TEXT NOT NULL,
47
  timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
48
  );
@@ -66,6 +71,39 @@ def store_transcript(user_id, transcript):
66
  cur.close()
67
  conn.close()
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  # Initialize the table
70
  create_table()
71
 
@@ -99,18 +137,12 @@ def chatbot(user_id, input):
99
  store_transcript(user_id, conversation)
100
  return conversation
101
 
102
- def authenticate(username, password):
103
- # Replace with actual authentication logic
104
- if username == "user" and password == "pass":
105
- return username
106
- else:
107
- return None
108
-
109
  with gr.Blocks() as demo:
110
  username = gr.Textbox(label="Username")
111
  password = gr.Textbox(label="Password", type="password")
112
  login_button = gr.Button("Login")
113
-
 
114
  chat_interface = gr.Interface(
115
  fn=chatbot,
116
  inputs=[gr.Textbox(label="User ID"), gr.Textbox(lines=7, label="Chat with AttachmentBot")],
@@ -118,7 +150,7 @@ with gr.Blocks() as demo:
118
  title="AttachmentBot",
119
  description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'."
120
  )
121
-
122
  def login(username, password):
123
  user_id = authenticate(username, password)
124
  if user_id:
@@ -126,6 +158,14 @@ with gr.Blocks() as demo:
126
  else:
127
  return gr.update(visible=False), "", "Invalid credentials"
128
 
 
 
 
 
 
 
 
129
  login_button.click(login, inputs=[username, password], outputs=[chat_interface, gr.Variable(), gr.Textbox(visible=False)])
 
130
 
131
  demo.launch()
 
40
  if conn:
41
  cur = conn.cursor()
42
  create_table_query = '''
43
+ CREATE TABLE IF NOT EXISTS users (
44
+ id SERIAL PRIMARY KEY,
45
+ username VARCHAR(255) UNIQUE NOT NULL,
46
+ password VARCHAR(255) NOT NULL
47
+ );
48
  CREATE TABLE IF NOT EXISTS chat_transcripts (
49
  id SERIAL PRIMARY KEY,
50
+ user_id INTEGER REFERENCES users(id),
51
  transcript TEXT NOT NULL,
52
  timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
53
  );
 
71
  cur.close()
72
  conn.close()
73
 
74
+ # Register new user
75
+ def register_user(username, password):
76
+ conn = connect_db()
77
+ if conn:
78
+ cur = conn.cursor()
79
+ insert_query = '''
80
+ INSERT INTO users (username, password)
81
+ VALUES (%s, %s) RETURNING id;
82
+ '''
83
+ cur.execute(insert_query, (username, password))
84
+ user_id = cur.fetchone()[0]
85
+ conn.commit()
86
+ cur.close()
87
+ conn.close()
88
+ return user_id
89
+ return None
90
+
91
+ # Authenticate user
92
+ def authenticate(username, password):
93
+ conn = connect_db()
94
+ if conn:
95
+ cur = conn.cursor()
96
+ select_query = '''
97
+ SELECT id FROM users WHERE username = %s AND password = %s;
98
+ '''
99
+ cur.execute(select_query, (username, password))
100
+ user_id = cur.fetchone()
101
+ cur.close()
102
+ conn.close()
103
+ if user_id:
104
+ return user_id[0]
105
+ return None
106
+
107
  # Initialize the table
108
  create_table()
109
 
 
137
  store_transcript(user_id, conversation)
138
  return conversation
139
 
 
 
 
 
 
 
 
140
  with gr.Blocks() as demo:
141
  username = gr.Textbox(label="Username")
142
  password = gr.Textbox(label="Password", type="password")
143
  login_button = gr.Button("Login")
144
+ register_button = gr.Button("Register")
145
+
146
  chat_interface = gr.Interface(
147
  fn=chatbot,
148
  inputs=[gr.Textbox(label="User ID"), gr.Textbox(lines=7, label="Chat with AttachmentBot")],
 
150
  title="AttachmentBot",
151
  description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'."
152
  )
153
+
154
  def login(username, password):
155
  user_id = authenticate(username, password)
156
  if user_id:
 
158
  else:
159
  return gr.update(visible=False), "", "Invalid credentials"
160
 
161
+ def register(username, password):
162
+ user_id = register_user(username, password)
163
+ if user_id:
164
+ return gr.update(visible=True), user_id, "Registration successful, you can now login."
165
+ else:
166
+ return gr.update(visible=False), "", "Registration failed, try a different username."
167
+
168
  login_button.click(login, inputs=[username, password], outputs=[chat_interface, gr.Variable(), gr.Textbox(visible=False)])
169
+ register_button.click(register, inputs=[username, password], outputs=[chat_interface, gr.Variable(), gr.Textbox(visible=False)])
170
 
171
  demo.launch()