blind1234 commited on
Commit
0814ed4
·
verified ·
1 Parent(s): 203ac1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -96
app.py CHANGED
@@ -15,8 +15,11 @@ if not os.path.exists(ADMIN_FILE):
15
  json.dump(default_admin, f)
16
 
17
  def load_admin_credentials():
18
- with open(ADMIN_FILE, "r") as f:
19
- return json.load(f)
 
 
 
20
 
21
  def save_admin_credentials(credentials):
22
  with open(ADMIN_FILE, "w") as f:
@@ -26,143 +29,124 @@ def get_admin_usernames():
26
  credentials = load_admin_credentials()
27
  return list(credentials.keys())
28
 
 
 
 
29
  # Function to handle user feedback submission
30
  def submit_feedback(admin_username, message):
 
 
 
31
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
32
  user_feedback = pd.DataFrame({
33
  "timestamp": [timestamp],
34
  "admin": [admin_username],
35
  "message": [message]
36
  })
37
- user_feedback.to_csv("user_feedback.csv", mode='a', header=False, index=False)
38
- return "Your feedback has been successfully submitted to the selected administrator."
 
 
 
 
 
 
39
 
40
  # Function to create new admin account
41
- def create_admin(new_username, new_password):
42
- if not new_username or not new_password:
43
  return "Please fill in both username and password fields."
44
 
45
- credentials = load_admin_credentials()
46
- if new_username in credentials:
47
- return "This username is already taken. Please choose a different username."
 
 
 
 
 
 
48
 
49
- credentials[new_username] = hashlib.sha256(new_password.encode()).hexdigest()
50
- save_admin_credentials(credentials)
51
- return f"Success! New administrator account for '{new_username}' has been created. You can now log in using these credentials."
52
 
53
  # Function to view feedback messages
54
  def view_messages(username, password):
55
- credentials = load_admin_credentials()
56
- hashed_password = hashlib.sha256(password.encode()).hexdigest()
57
- if username not in credentials or credentials[username] != hashed_password:
58
- return "Login failed. Please check your username and password and try again."
59
 
60
  try:
61
- df = pd.read_csv("user_feedback.csv", names=["timestamp", "admin", "message"])
 
 
 
 
 
 
 
 
 
62
  admin_messages = df[df['admin'] == username]
 
63
  if admin_messages.empty:
64
- return "You have no messages at this time."
 
65
  return admin_messages.to_string()
66
- except FileNotFoundError:
67
- return "No messages found in the system."
68
  except Exception as e:
69
- return f"An error occurred while retrieving messages: {str(e)}"
70
 
71
  # Create Gradio interface
72
  with gr.Blocks() as demo:
73
- gr.Markdown("# Feedback Management System")
74
- gr.Markdown("Welcome to the Feedback Management System. Please select a tab below to continue.")
75
 
76
- with gr.Tab("Submit Feedback", elem_id="submit_tab"):
77
- gr.Markdown("### Submit Your Feedback")
78
- gr.Markdown("Please select an administrator and enter your feedback message below.")
79
  admin_select = gr.Dropdown(
80
  choices=get_admin_usernames(),
81
- label="Select Administrator",
82
- info="Choose the administrator who should receive your feedback",
83
- elem_id="admin_select"
84
  )
85
  feedback_input = gr.Textbox(
86
- label="Your Feedback Message",
87
- lines=3,
88
- placeholder="Type your feedback here",
89
- info="Enter your feedback message in detail",
90
- elem_id="feedback_input"
91
- )
92
- submit_button = gr.Button("Submit Feedback", elem_id="submit_button")
93
- feedback_output = gr.Textbox(
94
- label="Submission Status",
95
- interactive=False,
96
- elem_id="feedback_status"
97
  )
 
 
 
98
  submit_button.click(
99
- fn=submit_feedback,
100
- inputs=[admin_select, feedback_input],
101
  outputs=feedback_output
102
  )
103
 
104
- with gr.Tab("Administrator Login", elem_id="login_tab"):
105
- gr.Markdown("### Administrator Login")
106
- gr.Markdown("Please log in with your administrator credentials to view messages.")
107
- login_username = gr.Dropdown(
108
- choices=get_admin_usernames(),
109
- label="Administrator Username",
110
- info="Select your username from the list",
111
- elem_id="login_username"
112
- )
113
- login_password = gr.Textbox(
114
- label="Password",
115
- type="password",
116
- placeholder="Enter your password",
117
- info="Enter your administrator password",
118
- elem_id="login_password"
119
- )
120
- view_messages_btn = gr.Button("View Messages", elem_id="view_messages_button")
121
- messages_output = gr.Textbox(
122
- label="Messages",
123
- lines=10,
124
- interactive=False,
125
- elem_id="messages_display"
126
- )
127
- view_messages_btn.click(
128
  fn=view_messages,
129
  inputs=[login_username, login_password],
130
  outputs=messages_output
131
  )
132
 
133
- with gr.Tab("Create Administrator Account", elem_id="create_account_tab"):
134
- gr.Markdown("### Create New Administrator Account")
135
- gr.Markdown("Fill in the fields below to create a new administrator account.")
136
- new_username = gr.Textbox(
137
- label="New Administrator Username",
138
- placeholder="Enter desired username",
139
- info="Choose a unique username for the new administrator account",
140
- elem_id="new_username"
141
- )
142
- new_password = gr.Textbox(
143
- label="New Administrator Password",
144
- type="password",
145
- placeholder="Enter desired password",
146
- info="Choose a secure password for the account",
147
- elem_id="new_password"
148
- )
149
- create_button = gr.Button(
150
- "Create Administrator Account",
151
- elem_id="create_account_button"
152
- )
153
- create_output = gr.Textbox(
154
- label="Account Creation Status",
155
- interactive=False,
156
- elem_id="creation_status"
157
- )
158
  create_button.click(
159
  fn=create_admin,
160
  inputs=[new_username, new_password],
161
  outputs=create_output
 
 
 
 
162
  )
163
 
164
- gr.Markdown("### Need Help?")
165
- gr.Markdown("If you need assistance, please contact system support.")
166
-
167
- # Launch the Gradio app
168
- demo.launch(show_error=True)
 
15
  json.dump(default_admin, f)
16
 
17
  def load_admin_credentials():
18
+ try:
19
+ with open(ADMIN_FILE, "r") as f:
20
+ return json.load(f)
21
+ except:
22
+ return {"admin": hashlib.sha256("admin123".encode()).hexdigest()}
23
 
24
  def save_admin_credentials(credentials):
25
  with open(ADMIN_FILE, "w") as f:
 
29
  credentials = load_admin_credentials()
30
  return list(credentials.keys())
31
 
32
+ def refresh_admin_list():
33
+ return gr.Dropdown(choices=get_admin_usernames())
34
+
35
  # Function to handle user feedback submission
36
  def submit_feedback(admin_username, message):
37
+ if not admin_username or not message:
38
+ return "Please fill in both admin selection and message."
39
+
40
  timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
41
  user_feedback = pd.DataFrame({
42
  "timestamp": [timestamp],
43
  "admin": [admin_username],
44
  "message": [message]
45
  })
46
+
47
+ # Create file with headers if it doesn't exist
48
+ if not os.path.exists("user_feedback.csv"):
49
+ user_feedback.to_csv("user_feedback.csv", index=False)
50
+ else:
51
+ user_feedback.to_csv("user_feedback.csv", mode='a', header=False, index=False)
52
+
53
+ return "Feedback submitted successfully!"
54
 
55
  # Function to create new admin account
56
+ def create_admin(username, password):
57
+ if not username or not password:
58
  return "Please fill in both username and password fields."
59
 
60
+ try:
61
+ credentials = load_admin_credentials()
62
+
63
+ if username in credentials:
64
+ return "Username already exists. Please choose a different username."
65
+
66
+ credentials[username] = hashlib.sha256(password.encode()).hexdigest()
67
+ save_admin_credentials(credentials)
68
+ return "Account created successfully! You can now log in with your credentials."
69
 
70
+ except Exception as e:
71
+ return f"Error creating account: {str(e)}"
 
72
 
73
  # Function to view feedback messages
74
  def view_messages(username, password):
75
+ if not username or not password:
76
+ return "Please provide both username and password."
 
 
77
 
78
  try:
79
+ credentials = load_admin_credentials()
80
+ hashed_password = hashlib.sha256(password.encode()).hexdigest()
81
+
82
+ if username not in credentials or credentials[username] != hashed_password:
83
+ return "Invalid credentials. Please try again."
84
+
85
+ if not os.path.exists("user_feedback.csv"):
86
+ return "No messages found."
87
+
88
+ df = pd.read_csv("user_feedback.csv")
89
  admin_messages = df[df['admin'] == username]
90
+
91
  if admin_messages.empty:
92
+ return "No messages found for your account."
93
+
94
  return admin_messages.to_string()
95
+
 
96
  except Exception as e:
97
+ return f"Error retrieving messages: {str(e)}"
98
 
99
  # Create Gradio interface
100
  with gr.Blocks() as demo:
101
+ gr.Markdown("# Feedback System")
 
102
 
103
+ with gr.Tab("Submit Feedback"):
 
 
104
  admin_select = gr.Dropdown(
105
  choices=get_admin_usernames(),
106
+ label="Select Admin",
107
+ interactive=True
 
108
  )
109
  feedback_input = gr.Textbox(
110
+ label="Your Feedback",
111
+ placeholder="Enter your feedback here",
112
+ lines=3
 
 
 
 
 
 
 
 
113
  )
114
+ submit_button = gr.Button("Submit")
115
+ feedback_output = gr.Textbox(label="Status")
116
+
117
  submit_button.click(
118
+ fn=submit_feedback,
119
+ inputs=[admin_select, feedback_input],
120
  outputs=feedback_output
121
  )
122
 
123
+ with gr.Tab("Admin Login"):
124
+ login_username = gr.Textbox(label="Username")
125
+ login_password = gr.Textbox(label="Password", type="password")
126
+ view_button = gr.Button("View Messages")
127
+ messages_output = gr.Textbox(label="Messages", lines=10)
128
+
129
+ view_button.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  fn=view_messages,
131
  inputs=[login_username, login_password],
132
  outputs=messages_output
133
  )
134
 
135
+ with gr.Tab("Create Account"):
136
+ new_username = gr.Textbox(label="Username")
137
+ new_password = gr.Textbox(label="Password", type="password")
138
+ create_button = gr.Button("Create Account")
139
+ create_output = gr.Textbox(label="Status")
140
+
141
+ # Create account and update admin lists
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  create_button.click(
143
  fn=create_admin,
144
  inputs=[new_username, new_password],
145
  outputs=create_output
146
+ ).then(
147
+ fn=refresh_admin_list,
148
+ inputs=None,
149
+ outputs=admin_select
150
  )
151
 
152
+ demo.launch(share=True)