Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
|
|
3 |
import hashlib
|
4 |
import json
|
5 |
import os
|
|
|
6 |
|
7 |
# Initialize admin credentials file if it doesn't exist
|
8 |
ADMIN_FILE = "admin_credentials.json"
|
@@ -21,59 +22,66 @@ def save_admin_credentials(credentials):
|
|
21 |
with open(ADMIN_FILE, "w") as f:
|
22 |
json.dump(credentials, f)
|
23 |
|
|
|
|
|
|
|
|
|
24 |
# Function to handle user feedback submission
|
25 |
-
def submit_feedback(message):
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
27 |
user_feedback.to_csv("user_feedback.csv", mode='a', header=False, index=False)
|
28 |
return "Feedback submitted successfully!"
|
29 |
|
30 |
-
# Function to verify admin login
|
31 |
-
def verify_login(username, password):
|
32 |
-
credentials = load_admin_credentials()
|
33 |
-
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
34 |
-
if username in credentials and credentials[username] == hashed_password:
|
35 |
-
return True, "Login successful!"
|
36 |
-
return False, "Invalid credentials!"
|
37 |
-
|
38 |
# Function to create new admin account
|
39 |
-
def create_admin(
|
40 |
credentials = load_admin_credentials()
|
41 |
-
|
42 |
-
# Verify current admin password
|
43 |
-
if hashlib.sha256(admin_password.encode()).hexdigest() not in credentials.values():
|
44 |
-
return "Invalid admin password!"
|
45 |
-
|
46 |
-
if username in credentials:
|
47 |
return "Username already exists!"
|
48 |
|
49 |
-
credentials[
|
50 |
save_admin_credentials(credentials)
|
51 |
-
return "New admin account created successfully!"
|
52 |
|
53 |
# Function to view feedback messages
|
54 |
def view_messages(username, password):
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
try:
|
60 |
-
df = pd.read_csv("user_feedback.csv", names=["message"])
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Create Gradio interface
|
66 |
with gr.Blocks() as demo:
|
67 |
gr.Markdown("# Feedback System")
|
68 |
|
69 |
with gr.Tab("Submit Feedback"):
|
70 |
-
|
|
|
71 |
submit_button = gr.Button("Submit Feedback")
|
72 |
feedback_output = gr.Textbox(label="Result")
|
73 |
-
submit_button.click(
|
|
|
|
|
|
|
|
|
74 |
|
75 |
with gr.Tab("Admin Login"):
|
76 |
-
login_username = gr.
|
77 |
login_password = gr.Textbox(label="Password", type="password")
|
78 |
view_messages_btn = gr.Button("View Messages")
|
79 |
messages_output = gr.Textbox(label="Messages", lines=10)
|
@@ -83,16 +91,15 @@ with gr.Blocks() as demo:
|
|
83 |
outputs=messages_output
|
84 |
)
|
85 |
|
86 |
-
with gr.Tab("Create Admin
|
87 |
-
new_username = gr.Textbox(label="New Username")
|
88 |
-
new_password = gr.Textbox(label="New Password", type="password")
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
create_admin_btn.click(
|
93 |
fn=create_admin,
|
94 |
-
inputs=[new_username, new_password
|
95 |
-
outputs=
|
96 |
)
|
97 |
|
98 |
# Launch the Gradio app
|
|
|
3 |
import hashlib
|
4 |
import json
|
5 |
import os
|
6 |
+
from datetime import datetime
|
7 |
|
8 |
# Initialize admin credentials file if it doesn't exist
|
9 |
ADMIN_FILE = "admin_credentials.json"
|
|
|
22 |
with open(ADMIN_FILE, "w") as f:
|
23 |
json.dump(credentials, f)
|
24 |
|
25 |
+
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 "Feedback submitted successfully!"
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Function to create new admin account
|
41 |
+
def create_admin(new_username, new_password):
|
42 |
credentials = load_admin_credentials()
|
43 |
+
if new_username in credentials:
|
|
|
|
|
|
|
|
|
|
|
44 |
return "Username already exists!"
|
45 |
|
46 |
+
credentials[new_username] = hashlib.sha256(new_password.encode()).hexdigest()
|
47 |
save_admin_credentials(credentials)
|
48 |
+
return f"New admin account '{new_username}' created successfully!"
|
49 |
|
50 |
# Function to view feedback messages
|
51 |
def view_messages(username, password):
|
52 |
+
credentials = load_admin_credentials()
|
53 |
+
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
54 |
+
if username not in credentials or credentials[username] != hashed_password:
|
55 |
+
return "Invalid credentials!"
|
56 |
|
57 |
try:
|
58 |
+
df = pd.read_csv("user_feedback.csv", names=["timestamp", "admin", "message"])
|
59 |
+
admin_messages = df[df['admin'] == username]
|
60 |
+
if admin_messages.empty:
|
61 |
+
return "No messages found for your account."
|
62 |
+
return admin_messages.to_string()
|
63 |
+
except FileNotFoundError:
|
64 |
+
return "No messages found."
|
65 |
+
except Exception as e:
|
66 |
+
return f"Error reading messages: {str(e)}"
|
67 |
|
68 |
# Create Gradio interface
|
69 |
with gr.Blocks() as demo:
|
70 |
gr.Markdown("# Feedback System")
|
71 |
|
72 |
with gr.Tab("Submit Feedback"):
|
73 |
+
admin_select = gr.Dropdown(choices=get_admin_usernames(), label="Select Admin")
|
74 |
+
feedback_input = gr.Textbox(label="Your Feedback", lines=3)
|
75 |
submit_button = gr.Button("Submit Feedback")
|
76 |
feedback_output = gr.Textbox(label="Result")
|
77 |
+
submit_button.click(
|
78 |
+
fn=submit_feedback,
|
79 |
+
inputs=[admin_select, feedback_input],
|
80 |
+
outputs=feedback_output
|
81 |
+
)
|
82 |
|
83 |
with gr.Tab("Admin Login"):
|
84 |
+
login_username = gr.Dropdown(choices=get_admin_usernames(), label="Admin Username")
|
85 |
login_password = gr.Textbox(label="Password", type="password")
|
86 |
view_messages_btn = gr.Button("View Messages")
|
87 |
messages_output = gr.Textbox(label="Messages", lines=10)
|
|
|
91 |
outputs=messages_output
|
92 |
)
|
93 |
|
94 |
+
with gr.Tab("Create New Admin"):
|
95 |
+
new_username = gr.Textbox(label="New Admin Username")
|
96 |
+
new_password = gr.Textbox(label="New Admin Password", type="password")
|
97 |
+
create_button = gr.Button("Create New Admin")
|
98 |
+
create_output = gr.Textbox(label="Result")
|
99 |
+
create_button.click(
|
|
|
100 |
fn=create_admin,
|
101 |
+
inputs=[new_username, new_password],
|
102 |
+
outputs=create_output
|
103 |
)
|
104 |
|
105 |
# Launch the Gradio app
|