Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,7 +32,6 @@ def get_admin_usernames():
|
|
| 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."
|
|
@@ -44,7 +43,6 @@ def submit_feedback(admin_username, message):
|
|
| 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:
|
|
@@ -52,51 +50,39 @@ def submit_feedback(admin_username, message):
|
|
| 52 |
|
| 53 |
return "Feedback submitted successfully!"
|
| 54 |
|
| 55 |
-
|
| 56 |
-
def create_admin(username, password):
|
| 57 |
if not username or not password:
|
| 58 |
-
return "Please
|
| 59 |
|
| 60 |
try:
|
| 61 |
credentials = load_admin_credentials()
|
|
|
|
| 62 |
|
|
|
|
| 63 |
if username in credentials:
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
| 67 |
save_admin_credentials(credentials)
|
| 68 |
-
return "
|
| 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
|
| 98 |
|
| 99 |
-
# Create Gradio interface
|
| 100 |
with gr.Blocks() as demo:
|
| 101 |
gr.Markdown("# Feedback System")
|
| 102 |
|
|
@@ -120,33 +106,33 @@ with gr.Blocks() as demo:
|
|
| 120 |
outputs=feedback_output
|
| 121 |
)
|
| 122 |
|
| 123 |
-
with gr.Tab("Admin Login"):
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
-
fn=
|
| 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(
|
|
|
|
| 32 |
def refresh_admin_list():
|
| 33 |
return gr.Dropdown(choices=get_admin_usernames())
|
| 34 |
|
|
|
|
| 35 |
def submit_feedback(admin_username, message):
|
| 36 |
if not admin_username or not message:
|
| 37 |
return "Please fill in both admin selection and message."
|
|
|
|
| 43 |
"message": [message]
|
| 44 |
})
|
| 45 |
|
|
|
|
| 46 |
if not os.path.exists("user_feedback.csv"):
|
| 47 |
user_feedback.to_csv("user_feedback.csv", index=False)
|
| 48 |
else:
|
|
|
|
| 50 |
|
| 51 |
return "Feedback submitted successfully!"
|
| 52 |
|
| 53 |
+
def auto_create_or_login(username, password):
|
|
|
|
| 54 |
if not username or not password:
|
| 55 |
+
return "Please enter both username and password."
|
| 56 |
|
| 57 |
try:
|
| 58 |
credentials = load_admin_credentials()
|
| 59 |
+
hashed_password = hashlib.sha256(password.encode()).hexdigest()
|
| 60 |
|
| 61 |
+
# If user exists, check password
|
| 62 |
if username in credentials:
|
| 63 |
+
if credentials[username] == hashed_password:
|
| 64 |
+
# Login successful
|
| 65 |
+
if not os.path.exists("user_feedback.csv"):
|
| 66 |
+
return "Login successful. No messages found."
|
| 67 |
+
|
| 68 |
+
df = pd.read_csv("user_feedback.csv")
|
| 69 |
+
admin_messages = df[df['admin'] == username]
|
| 70 |
+
|
| 71 |
+
if admin_messages.empty:
|
| 72 |
+
return "Login successful. No messages found for your account."
|
| 73 |
+
|
| 74 |
+
return f"Login successful. Your messages:\n\n{admin_messages.to_string()}"
|
| 75 |
+
else:
|
| 76 |
+
return "Incorrect password for existing account."
|
| 77 |
|
| 78 |
+
# If user doesn't exist, create new account
|
| 79 |
+
credentials[username] = hashed_password
|
| 80 |
save_admin_credentials(credentials)
|
| 81 |
+
return f"New account created successfully for {username}! No messages yet."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
+
return f"Error: {str(e)}"
|
| 85 |
|
|
|
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Feedback System")
|
| 88 |
|
|
|
|
| 106 |
outputs=feedback_output
|
| 107 |
)
|
| 108 |
|
| 109 |
+
with gr.Tab("Admin Login/Register"):
|
| 110 |
+
gr.Markdown("""
|
| 111 |
+
### Admin Login or Register
|
| 112 |
+
Enter your username and password:
|
| 113 |
+
- If account exists: You will be logged in
|
| 114 |
+
- If account doesn't exist: A new account will be created automatically
|
| 115 |
+
""")
|
| 116 |
+
login_username = gr.Textbox(
|
| 117 |
+
label="Username",
|
| 118 |
+
placeholder="Enter username"
|
| 119 |
+
)
|
| 120 |
+
login_password = gr.Textbox(
|
| 121 |
+
label="Password",
|
| 122 |
+
type="password",
|
| 123 |
+
placeholder="Enter password"
|
| 124 |
+
)
|
| 125 |
+
login_button = gr.Button("Login or Create Account")
|
| 126 |
+
messages_output = gr.Textbox(label="Status and Messages", lines=10)
|
| 127 |
|
| 128 |
+
login_button.click(
|
| 129 |
+
fn=auto_create_or_login,
|
| 130 |
inputs=[login_username, login_password],
|
| 131 |
outputs=messages_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
).then(
|
| 133 |
fn=refresh_admin_list,
|
| 134 |
inputs=None,
|
| 135 |
outputs=admin_select
|
| 136 |
)
|
| 137 |
|
| 138 |
+
demo.launch()
|