Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,128 @@
|
|
| 1 |
-
from simple_salesforce import Salesforce
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
# Authenticate with Salesforce
|
| 4 |
sf = Salesforce(username='surendra@sathkrutha.com',
|
| 5 |
password='Lavanyanaga@123',
|
| 6 |
security_token='z7Wvk6mys7n8XjqbYKf3bwBh7')
|
| 7 |
-
import bcrypt
|
| 8 |
|
|
|
|
|
|
|
| 9 |
def hash_password(password):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# This will generate a hash that may exceed 60 characters
|
| 11 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 12 |
|
|
@@ -24,17 +140,25 @@ def signup(name, email, phone, password):
|
|
| 24 |
return "Signup successful! You can now login."
|
| 25 |
except Exception as e:
|
| 26 |
return f"Error during signup: {str(e)}"
|
|
|
|
|
|
|
| 27 |
def login(email, password):
|
| 28 |
-
|
|
|
|
| 29 |
result = sf.query(query)
|
|
|
|
| 30 |
if len(result['records']) == 0:
|
| 31 |
-
return "Invalid email or password."
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
else:
|
| 37 |
-
return "Invalid email or password."
|
|
|
|
| 38 |
|
| 39 |
import gradio as gr
|
| 40 |
def signup_page(name, email, phone, password):
|
|
@@ -93,4 +217,4 @@ with gr.Blocks() as app:
|
|
| 93 |
home_interface.render()
|
| 94 |
|
| 95 |
# Launch the app
|
| 96 |
-
app.launch()
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
|
| 3 |
+
import bcrypt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from simple_salesforce import Salesforce
|
| 6 |
# Authenticate with Salesforce
|
| 7 |
sf = Salesforce(username='surendra@sathkrutha.com',
|
| 8 |
password='Lavanyanaga@123',
|
| 9 |
security_token='z7Wvk6mys7n8XjqbYKf3bwBh7')
|
|
|
|
| 10 |
|
| 11 |
+
|
| 12 |
+
# Function to Hash Password
|
| 13 |
def hash_password(password):
|
| 14 |
+
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 15 |
+
|
| 16 |
+
# Function to Verify Password
|
| 17 |
+
def verify_password(plain_password, hashed_password):
|
| 18 |
+
return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8'))
|
| 19 |
+
|
| 20 |
+
# Function for Signup
|
| 21 |
+
def signup(name, email, phone, password):
|
| 22 |
+
hashed_password = hash_password(password) # Hash the plain text password
|
| 23 |
+
try:
|
| 24 |
+
sf.User_Login__c.create({
|
| 25 |
+
'Name__c': name,
|
| 26 |
+
'Email__c': email,
|
| 27 |
+
'Phone__c': phone,
|
| 28 |
+
'Password__c': hashed_password
|
| 29 |
+
})
|
| 30 |
+
return "Signup successful! You can now login."
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error during signup: {str(e)}"
|
| 33 |
+
|
| 34 |
+
# Function for Login
|
| 35 |
+
def login(email, password):
|
| 36 |
+
try:
|
| 37 |
+
# Query Salesforce for the user's hashed password
|
| 38 |
+
query = f"SELECT Name__c, Email__c, Phone__c, Password__c FROM User_Login__c WHERE Email__c = '{email}'"
|
| 39 |
+
result = sf.query(query)
|
| 40 |
+
|
| 41 |
+
if len(result['records']) == 0:
|
| 42 |
+
return "Invalid email or password.", None, None, None
|
| 43 |
+
|
| 44 |
+
user = result['records'][0]
|
| 45 |
+
hashed_password = user['Password__c']
|
| 46 |
+
|
| 47 |
+
# Compare the entered password with the stored hash
|
| 48 |
+
if verify_password(password, hashed_password):
|
| 49 |
+
return "Login successful!", user['Name__c'], user['Email__c'], user['Phone__c']
|
| 50 |
+
else:
|
| 51 |
+
return "Invalid email or password.", None, None, None
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error during login: {str(e)}", None, None, None
|
| 54 |
+
|
| 55 |
+
# Function for Home Page
|
| 56 |
+
def home_page(name, email, phone):
|
| 57 |
+
return f"Welcome, {name}! Your email is {email} and your phone number is {phone}."
|
| 58 |
+
|
| 59 |
+
# Gradio Signup Interface
|
| 60 |
+
def signup_page(name, email, phone, password):
|
| 61 |
+
response = signup(name, email, phone, password)
|
| 62 |
+
return response
|
| 63 |
+
|
| 64 |
+
signup_interface = gr.Interface(
|
| 65 |
+
fn=signup_page,
|
| 66 |
+
inputs=[
|
| 67 |
+
gr.Textbox(label="Name"),
|
| 68 |
+
gr.Textbox(label="Email"),
|
| 69 |
+
gr.Textbox(label="Phone"),
|
| 70 |
+
gr.Textbox(label="Password", type="password"),
|
| 71 |
+
],
|
| 72 |
+
outputs=gr.Textbox(label="Signup Status"),
|
| 73 |
+
title="Signup Page"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Gradio Login Interface
|
| 77 |
+
def login_page(email, password):
|
| 78 |
+
login_status, name, email, phone = login(email, password)
|
| 79 |
+
if login_status == "Login successful!":
|
| 80 |
+
return login_status, name, email, phone
|
| 81 |
+
else:
|
| 82 |
+
return login_status, "Error", "Error", "Error"
|
| 83 |
+
|
| 84 |
+
login_interface = gr.Interface(
|
| 85 |
+
fn=login_page,
|
| 86 |
+
inputs=[
|
| 87 |
+
gr.Textbox(label="Email"),
|
| 88 |
+
gr.Textbox(label="Password", type="password"),
|
| 89 |
+
],
|
| 90 |
+
outputs=[
|
| 91 |
+
gr.Textbox(label="Login Status"),
|
| 92 |
+
gr.Textbox(label="Name"),
|
| 93 |
+
gr.Textbox(label="Email"),
|
| 94 |
+
gr.Textbox(label="Phone"),
|
| 95 |
+
],
|
| 96 |
+
title="Login Page"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Gradio Home Interface
|
| 100 |
+
home_interface = gr.Interface(
|
| 101 |
+
fn=home_page,
|
| 102 |
+
inputs=[
|
| 103 |
+
gr.Textbox(label="Name"),
|
| 104 |
+
gr.Textbox(label="Email"),
|
| 105 |
+
gr.Textbox(label="Phone"),
|
| 106 |
+
],
|
| 107 |
+
outputs=gr.Textbox(label="Welcome Message"),
|
| 108 |
+
title="Home Page"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Combine All Pages in Gradio
|
| 112 |
+
with gr.Blocks() as app:
|
| 113 |
+
with gr.Tab("Signup"):
|
| 114 |
+
signup_interface.render()
|
| 115 |
+
|
| 116 |
+
with gr.Tab("Login"):
|
| 117 |
+
login_interface.render()
|
| 118 |
+
|
| 119 |
+
with gr.Tab("Home"):
|
| 120 |
+
home_interface.render()
|
| 121 |
+
|
| 122 |
+
# Launch the Gradio App
|
| 123 |
+
app.launch()
|
| 124 |
+
|
| 125 |
+
"""def hash_password(password):
|
| 126 |
# This will generate a hash that may exceed 60 characters
|
| 127 |
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
| 128 |
|
|
|
|
| 140 |
return "Signup successful! You can now login."
|
| 141 |
except Exception as e:
|
| 142 |
return f"Error during signup: {str(e)}"
|
| 143 |
+
|
| 144 |
+
|
| 145 |
def login(email, password):
|
| 146 |
+
# Query Salesforce for the user's hashed password
|
| 147 |
+
query = f"SELECT Name__c, Email__c, Phone__c, Password__c FROM User_Login__c WHERE Email__c = '{email}'"
|
| 148 |
result = sf.query(query)
|
| 149 |
+
|
| 150 |
if len(result['records']) == 0:
|
| 151 |
+
return "Invalid email or password.", None, None, None
|
| 152 |
|
| 153 |
+
user = result['records'][0]
|
| 154 |
+
hashed_password = user['Password__c']
|
| 155 |
+
|
| 156 |
+
# Compare the entered password with the stored hash
|
| 157 |
+
if bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8')):
|
| 158 |
+
return "Login successful!", user['Name__c'], user['Email__c'], user['Phone__c']
|
| 159 |
else:
|
| 160 |
+
return "Invalid email or password.", None, None, None
|
| 161 |
+
|
| 162 |
|
| 163 |
import gradio as gr
|
| 164 |
def signup_page(name, email, phone, password):
|
|
|
|
| 217 |
home_interface.render()
|
| 218 |
|
| 219 |
# Launch the app
|
| 220 |
+
app.launch()"""
|