Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import hashlib | |
import json | |
import os | |
from datetime import datetime | |
# Initialize admin credentials file if it doesn't exist | |
ADMIN_FILE = "admin_credentials.json" | |
if not os.path.exists(ADMIN_FILE): | |
default_admin = { | |
"admin": hashlib.sha256("admin123".encode()).hexdigest() | |
} | |
with open(ADMIN_FILE, "w") as f: | |
json.dump(default_admin, f) | |
def load_admin_credentials(): | |
try: | |
with open(ADMIN_FILE, "r") as f: | |
return json.load(f) | |
except: | |
return {"admin": hashlib.sha256("admin123".encode()).hexdigest()} | |
def save_admin_credentials(credentials): | |
with open(ADMIN_FILE, "w") as f: | |
json.dump(credentials, f) | |
def get_admin_usernames(): | |
credentials = load_admin_credentials() | |
return list(credentials.keys()) | |
def refresh_admin_list(): | |
return gr.Dropdown(choices=get_admin_usernames()) | |
def submit_feedback(admin_username, message): | |
if not admin_username or not message: | |
return "Please fill in both admin selection and message." | |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
user_feedback = pd.DataFrame({ | |
"timestamp": [timestamp], | |
"admin": [admin_username], | |
"message": [message] | |
}) | |
if not os.path.exists("user_feedback.csv"): | |
user_feedback.to_csv("user_feedback.csv", index=False) | |
else: | |
user_feedback.to_csv("user_feedback.csv", mode='a', header=False, index=False) | |
return "Feedback submitted successfully!" | |
def auto_create_or_login(username, password): | |
if not username or not password: | |
return "Please enter both username and password." | |
try: | |
credentials = load_admin_credentials() | |
hashed_password = hashlib.sha256(password.encode()).hexdigest() | |
# If user exists, check password | |
if username in credentials: | |
if credentials[username] == hashed_password: | |
# Login successful | |
if not os.path.exists("user_feedback.csv"): | |
return "Login successful. No messages found." | |
df = pd.read_csv("user_feedback.csv") | |
admin_messages = df[df['admin'] == username] | |
if admin_messages.empty: | |
return "Login successful. No messages found for your account." | |
return f"Login successful. Your messages:\n\n{admin_messages.to_string()}" | |
else: | |
return "Incorrect password for existing account." | |
# If user doesn't exist, create new account | |
credentials[username] = hashed_password | |
save_admin_credentials(credentials) | |
return f"New account created successfully for {username}! No messages yet." | |
except Exception as e: | |
return f"Error: {str(e)}" | |
with gr.Blocks() as demo: | |
gr.Markdown("# Feedback System") | |
with gr.Tab("Submit Feedback"): | |
admin_select = gr.Dropdown( | |
choices=get_admin_usernames(), | |
label="Select Admin", | |
interactive=True | |
) | |
feedback_input = gr.Textbox( | |
label="Your Feedback", | |
placeholder="Enter your feedback here", | |
lines=3 | |
) | |
submit_button = gr.Button("Submit") | |
feedback_output = gr.Textbox(label="Status") | |
submit_button.click( | |
fn=submit_feedback, | |
inputs=[admin_select, feedback_input], | |
outputs=feedback_output | |
) | |
with gr.Tab("Admin Login/Register"): | |
gr.Markdown(""" | |
### Admin Login or Register | |
Enter your username and password: | |
- If account exists: You will be logged in | |
- If account doesn't exist: A new account will be created automatically | |
""") | |
login_username = gr.Textbox( | |
label="Username", | |
placeholder="Enter username" | |
) | |
login_password = gr.Textbox( | |
label="Password", | |
type="password", | |
placeholder="Enter password" | |
) | |
login_button = gr.Button("Login or Create Account") | |
messages_output = gr.Textbox(label="Status and Messages", lines=10) | |
login_button.click( | |
fn=auto_create_or_login, | |
inputs=[login_username, login_password], | |
outputs=messages_output | |
).then( | |
fn=refresh_admin_list, | |
inputs=None, | |
outputs=admin_select | |
) | |
demo.launch() |