File size: 4,551 Bytes
4431af9
a122c34
d4173df
 
 
c3b41a9
d4173df
 
 
 
 
 
 
 
 
 
 
0814ed4
 
 
 
 
d4173df
 
 
 
4662992
c3b41a9
 
 
 
0814ed4
 
 
c3b41a9
0814ed4
 
 
c3b41a9
 
 
 
 
 
0814ed4
 
 
 
 
 
 
4662992
f357f34
0814ed4
f357f34
203ac1d
0814ed4
 
f357f34
0814ed4
f357f34
0814ed4
f357f34
 
 
 
 
 
 
 
 
 
 
 
 
 
0814ed4
f357f34
 
0814ed4
f357f34
0814ed4
c3b41a9
f357f34
d4173df
4662992
0814ed4
d4173df
0814ed4
203ac1d
 
0814ed4
 
203ac1d
 
0814ed4
 
 
203ac1d
0814ed4
 
 
c3b41a9
0814ed4
 
c3b41a9
 
d4173df
f357f34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0814ed4
f357f34
 
d4173df
 
0814ed4
 
 
 
d4173df
4662992
f357f34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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()