blind1234 commited on
Commit
d4173df
·
verified ·
1 Parent(s): 4662992

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -12
app.py CHANGED
@@ -1,24 +1,99 @@
1
  import gradio as gr
2
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Function to handle user feedback submission
5
  def submit_feedback(message):
6
- # Append the message to the user feedback DataFrame
7
- user_feedback = pd.DataFrame({"message": [message]})
8
- user_feedback.to_csv("user_feedback.csv", mode='a', header=False, index=False)
9
  return "Feedback submitted successfully!"
10
 
11
- # Create a Gradio interface for submitting feedback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  with gr.Blocks() as demo:
13
- # Textbox for user to input feedback
14
- feedback_input = gr.Textbox(label="Your Feedback")
15
- # Button to submit feedback
16
- submit_button = gr.Button("Submit Feedback")
17
- # Textbox to display the result of the submission
18
- feedback_output = gr.Textbox(label="Result")
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Set up the event listener for the submit button
21
- submit_button.click(fn=submit_feedback, inputs=feedback_input, outputs=feedback_output)
 
 
 
 
 
 
 
 
 
22
 
23
  # Launch the Gradio app
24
  demo.launch(show_error=True)
 
1
  import gradio as gr
2
  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"
9
+ if not os.path.exists(ADMIN_FILE):
10
+ default_admin = {
11
+ "admin": hashlib.sha256("admin123".encode()).hexdigest()
12
+ }
13
+ with open(ADMIN_FILE, "w") as f:
14
+ json.dump(default_admin, f)
15
+
16
+ def load_admin_credentials():
17
+ with open(ADMIN_FILE, "r") as f:
18
+ return json.load(f)
19
+
20
+ 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
+ user_feedback = pd.DataFrame({"message": [message]})
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(username, password, admin_password):
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[username] = hashlib.sha256(password.encode()).hexdigest()
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
+ login_success, message = verify_login(username, password)
56
+ if not login_success:
57
+ return message
58
+
59
+ try:
60
+ df = pd.read_csv("user_feedback.csv", names=["message"])
61
+ return df.to_string()
62
+ except:
63
+ return "No messages found or error reading messages."
64
+
65
+ # Create Gradio interface
66
  with gr.Blocks() as demo:
67
+ gr.Markdown("# Feedback System")
68
+
69
+ with gr.Tab("Submit Feedback"):
70
+ feedback_input = gr.Textbox(label="Your Feedback")
71
+ submit_button = gr.Button("Submit Feedback")
72
+ feedback_output = gr.Textbox(label="Result")
73
+ submit_button.click(fn=submit_feedback, inputs=feedback_input, outputs=feedback_output)
74
+
75
+ with gr.Tab("Admin Login"):
76
+ login_username = gr.Textbox(label="Username")
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)
80
+ view_messages_btn.click(
81
+ fn=view_messages,
82
+ inputs=[login_username, login_password],
83
+ outputs=messages_output
84
+ )
85
 
86
+ with gr.Tab("Create Admin Account"):
87
+ new_username = gr.Textbox(label="New Username")
88
+ new_password = gr.Textbox(label="New Password", type="password")
89
+ admin_password = gr.Textbox(label="Current Admin Password", type="password")
90
+ create_admin_btn = gr.Button("Create Admin Account")
91
+ create_admin_output = gr.Textbox(label="Result")
92
+ create_admin_btn.click(
93
+ fn=create_admin,
94
+ inputs=[new_username, new_password, admin_password],
95
+ outputs=create_admin_output
96
+ )
97
 
98
  # Launch the Gradio app
99
  demo.launch(show_error=True)