Spaces:
Sleeping
Sleeping
Upload 12 files
Browse files- app_ui/__init__.py +0 -0
- app_ui/__pycache__/__init__.cpython-311.pyc +0 -0
- app_ui/__pycache__/admin_page.cpython-311.pyc +0 -0
- app_ui/__pycache__/app.cpython-311.pyc +0 -0
- app_ui/admin_page.py +74 -0
- app_ui/app.py +195 -0
- app_ui/index.py +164 -0
- app_ui/profile.png +0 -0
- app_ui/utils/__init__.py +0 -0
- app_ui/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- app_ui/utils/__pycache__/utils.cpython-311.pyc +0 -0
- app_ui/utils/utils.py +227 -0
app_ui/__init__.py
ADDED
|
File without changes
|
app_ui/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (162 Bytes). View file
|
|
|
app_ui/__pycache__/admin_page.cpython-311.pyc
ADDED
|
Binary file (4.48 kB). View file
|
|
|
app_ui/__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (7.19 kB). View file
|
|
|
app_ui/admin_page.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import base64
|
| 4 |
+
from utils.utils import __get_new_user,app_config_col,encrypt_with_password,__encode, __update_user_config, __generate_password
|
| 5 |
+
|
| 6 |
+
def load_user_config():
|
| 7 |
+
|
| 8 |
+
st.sidebar.header("Options")
|
| 9 |
+
page = st.sidebar.radio("Select Option", ["Validate New User", "Show User Details"],label_visibility="collapsed")
|
| 10 |
+
|
| 11 |
+
if page=="Validate New User":
|
| 12 |
+
new_users = __get_new_user()
|
| 13 |
+
|
| 14 |
+
# Initialize step index in session state
|
| 15 |
+
if "index" not in st.session_state:
|
| 16 |
+
st.session_state.index = 0
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# --- Sidebar Profile Section ---
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
st.title("User Information")
|
| 23 |
+
|
| 24 |
+
# Stop if finished
|
| 25 |
+
if st.session_state.index >= len(new_users):
|
| 26 |
+
st.success("User validation completed")
|
| 27 |
+
if st.button("Restart"):
|
| 28 |
+
st.session_state.index = 0
|
| 29 |
+
st.rerun()
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
# Show current item
|
| 33 |
+
current_user = new_users[st.session_state.index]
|
| 34 |
+
st.info("Username: **{}**".format(current_user.get("username")))
|
| 35 |
+
st.info("Email: **{}**".format(current_user.get("email")))
|
| 36 |
+
st.info("Token: **{}**".format(current_user.get("token")))
|
| 37 |
+
st.info("Validated By: **{}**".format(current_user.get("validated_by","Validation Not Completed")))
|
| 38 |
+
is_email_validated=st.selectbox(label="Is Email Validation Completed?",options=["Y", "N"])
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# User action area
|
| 42 |
+
st.write("Perform your action below:")
|
| 43 |
+
|
| 44 |
+
action_done = st.checkbox("TokenID is send to email:{}".format(current_user.get("email")))
|
| 45 |
+
|
| 46 |
+
# Next button
|
| 47 |
+
if st.button("Next"):
|
| 48 |
+
|
| 49 |
+
if not action_done:
|
| 50 |
+
st.warning("Please complete the action before continuing!")
|
| 51 |
+
else:
|
| 52 |
+
current_user['token']="1111"
|
| 53 |
+
current_user['validated_by']=st.session_state.user
|
| 54 |
+
current_user['_is_validated']= "Y" if is_email_validated=="Y" else "N"
|
| 55 |
+
current_user['_is_email_validated'] = is_email_validated
|
| 56 |
+
|
| 57 |
+
salt_bytes=base64.b64encode(app_config_col.get('SALT_KEY').encode("utf-8"))
|
| 58 |
+
app_api_key=app_config_col.get("APP_API_KEY")
|
| 59 |
+
enc = encrypt_with_password(app_api_key, salt_bytes, current_user)
|
| 60 |
+
|
| 61 |
+
__update_user_config(key=__encode(userinfo=current_user),value=enc)
|
| 62 |
+
|
| 63 |
+
st.session_state.index += 1
|
| 64 |
+
st.rerun()
|
| 65 |
+
|
| 66 |
+
if page=="Show User Details":
|
| 67 |
+
st.info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
|
| 68 |
+
|
| 69 |
+
if st.sidebar.button("Logout"):
|
| 70 |
+
st.session_state.logged_in = False
|
| 71 |
+
st.session_state.user = None
|
| 72 |
+
st.session_state.show_audio = False
|
| 73 |
+
st.session_state.bg_started = False
|
| 74 |
+
st.rerun()
|
app_ui/app.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
| 5 |
+
from src.story_gen import generate_story
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import threading
|
| 8 |
+
import time
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
# ------------------- WAV CLEANUP CONFIG -------------------
|
| 12 |
+
WAV_FOLDER = "." # Change to your folder name
|
| 13 |
+
DELETE_AFTER_SECONDS = 300
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def delete_wav_files():
|
| 17 |
+
"""Delete .wav files older than 1 hour."""
|
| 18 |
+
if not os.path.exists(WAV_FOLDER):
|
| 19 |
+
return
|
| 20 |
+
|
| 21 |
+
current_time = time.time()
|
| 22 |
+
|
| 23 |
+
for filename in os.listdir(WAV_FOLDER):
|
| 24 |
+
if filename.endswith(".wav"):
|
| 25 |
+
file_path = os.path.join(WAV_FOLDER, filename)
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
file_age = current_time - os.path.getmtime(file_path)
|
| 29 |
+
|
| 30 |
+
if file_age > DELETE_AFTER_SECONDS:
|
| 31 |
+
os.remove(file_path)
|
| 32 |
+
print(f"[CLEANER] Deleted (older than 1 hr): {file_path}")
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"[CLEANER] Error deleting {file_path}: {e}")
|
| 36 |
+
|
| 37 |
+
def background_cleaner():
|
| 38 |
+
"""Runs every 1 minute."""
|
| 39 |
+
while True:
|
| 40 |
+
delete_wav_files()
|
| 41 |
+
time.sleep(60)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def load_app():
|
| 47 |
+
# Start background cleaner only once
|
| 48 |
+
if "bg_started" not in st.session_state:
|
| 49 |
+
|
| 50 |
+
threading.Thread(target=background_cleaner, daemon=True).start()
|
| 51 |
+
st.session_state.bg_started = True
|
| 52 |
+
print("[CLEANER] Background WAV cleaner started.")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# # --- Sidebar Profile Section ---
|
| 56 |
+
# profile_image_path = os.path.join(os.path.dirname(__file__), "profile.png")
|
| 57 |
+
|
| 58 |
+
# # Read image safely
|
| 59 |
+
# encoded_img = None
|
| 60 |
+
# if os.path.exists(profile_image_path):
|
| 61 |
+
# with open(profile_image_path, "rb") as img_file:
|
| 62 |
+
# encoded_img = base64.b64encode(img_file.read()).decode()
|
| 63 |
+
|
| 64 |
+
# # Sidebar HTML (centered layout)
|
| 65 |
+
# sidebar_html = f"""
|
| 66 |
+
# <div style="text-align:center; margin-top:20px;">
|
| 67 |
+
# <img src="data:image/png;base64,{encoded_img if encoded_img else ''}"
|
| 68 |
+
# style="width:110px; height:110px; border-radius:50%; object-fit:cover; border:2px solid #ddd;">
|
| 69 |
+
# <div style="margin-top:10px; font-size:16px; font-weight:600; color:#333;">
|
| 70 |
+
# {"User:{}".format(st.session_state['user'])}
|
| 71 |
+
# </div>
|
| 72 |
+
# </div>
|
| 73 |
+
# """
|
| 74 |
+
# st.sidebar.markdown(sidebar_html, unsafe_allow_html=True)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# # Centered logout button
|
| 78 |
+
# logout_style = """
|
| 79 |
+
# <style>
|
| 80 |
+
# div.stButton > button:first-child {
|
| 81 |
+
# display: block;
|
| 82 |
+
# margin: 10px auto;
|
| 83 |
+
# padding: 0.5rem 1rem;
|
| 84 |
+
# border-radius: 8px;
|
| 85 |
+
# font-size: 15px;
|
| 86 |
+
# }
|
| 87 |
+
# </style>
|
| 88 |
+
# """
|
| 89 |
+
# st.sidebar.markdown(logout_style, unsafe_allow_html=True)
|
| 90 |
+
st.sidebar.header("Options")
|
| 91 |
+
page = st.sidebar.radio("Select Option", ["Story Using One Line Plot", "Story Using Custom Plot"],label_visibility="collapsed")
|
| 92 |
+
|
| 93 |
+
if st.sidebar.button("Logout"):
|
| 94 |
+
st.session_state.logged_in = False
|
| 95 |
+
st.session_state.user = None
|
| 96 |
+
st.session_state.show_audio = False
|
| 97 |
+
st.session_state.bg_started = False
|
| 98 |
+
st.rerun()
|
| 99 |
+
|
| 100 |
+
if page == "Story Using One Line Plot":
|
| 101 |
+
# ------------------- STREAMLIT APP -------------------
|
| 102 |
+
st.set_page_config(
|
| 103 |
+
page_title="AI Story Generator",
|
| 104 |
+
page_icon="📚",
|
| 105 |
+
layout="wide" # wide works best with no-scroll layouts
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# ------------------- CSS: Fit Window + No Scroll -------------------
|
| 109 |
+
css = """
|
| 110 |
+
<style>
|
| 111 |
+
|
| 112 |
+
/* Remove scrolling */
|
| 113 |
+
html, body, [class*="css"] {
|
| 114 |
+
height: 100%;
|
| 115 |
+
overflow: hidden !important;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
/* Reduce padding so content fits */
|
| 119 |
+
.block-container {
|
| 120 |
+
padding-top: 1rem !important;
|
| 121 |
+
padding-bottom: 1rem !important;
|
| 122 |
+
max-width: 900px; /* keeps layout centered */
|
| 123 |
+
margin: auto;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
/* Title spacing adjustment */
|
| 127 |
+
h1 {
|
| 128 |
+
margin-top: 0 !important;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
</style>
|
| 132 |
+
"""
|
| 133 |
+
st.markdown(css, unsafe_allow_html=True)
|
| 134 |
+
|
| 135 |
+
# ------------------- UI -------------------
|
| 136 |
+
st.title("📚 AI Story Generator")
|
| 137 |
+
st.write("Enter a story plot and let AI turn it into a full story!")
|
| 138 |
+
|
| 139 |
+
language = st.selectbox(
|
| 140 |
+
"Select language for audio:",
|
| 141 |
+
[
|
| 142 |
+
("English", "en"),
|
| 143 |
+
("Hindi", "hi"),
|
| 144 |
+
("Tamil", "ta"),
|
| 145 |
+
("Telugu", "te"),
|
| 146 |
+
("Bengali", "bn"),
|
| 147 |
+
("Gujarati", "gu"),
|
| 148 |
+
("Kannada", "kn"),
|
| 149 |
+
("Malayalam", "ml")
|
| 150 |
+
],
|
| 151 |
+
format_func=lambda x: x[0]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
plot = st.text_area("Enter your story plot:", height=150)
|
| 155 |
+
|
| 156 |
+
display_story = st.checkbox("Display Story")
|
| 157 |
+
|
| 158 |
+
generate = st.button("Generate Story")
|
| 159 |
+
|
| 160 |
+
config={}
|
| 161 |
+
config['lang']=language
|
| 162 |
+
config['user']=st.session_state['user']
|
| 163 |
+
if generate:
|
| 164 |
+
if not plot.strip():
|
| 165 |
+
st.warning("Please enter a story plot.")
|
| 166 |
+
else:
|
| 167 |
+
with st.spinner("Generating story... please wait ⏳"):
|
| 168 |
+
|
| 169 |
+
response = generate_story(story_plot=plot, config=config)
|
| 170 |
+
|
| 171 |
+
story = response.get("story")
|
| 172 |
+
fileName = response.get("fileName")
|
| 173 |
+
|
| 174 |
+
# Hold space for final audio output
|
| 175 |
+
st.session_state["audio_file"] = fileName
|
| 176 |
+
st.session_state["story_text"] = story
|
| 177 |
+
st.session_state["show_audio"] = True
|
| 178 |
+
|
| 179 |
+
if st.session_state.get("show_audio", False):
|
| 180 |
+
|
| 181 |
+
st.markdown("---")
|
| 182 |
+
st.subheader("🔊 Generated Story Audio")
|
| 183 |
+
|
| 184 |
+
f = Path(st.session_state["audio_file"])
|
| 185 |
+
audio_bytes = f.read_bytes()
|
| 186 |
+
st.audio(audio_bytes, format="audio/wav")
|
| 187 |
+
|
| 188 |
+
if display_story:
|
| 189 |
+
st.markdown("---")
|
| 190 |
+
st.subheader("📖 Full Story")
|
| 191 |
+
st.write(st.session_state["story_text"])
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if page == "Story Using Custom Plot":
|
| 195 |
+
st.info("Story Using Custom Plot Feature development in progress")
|
app_ui/index.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from utils.utils import check_admin_login
|
| 3 |
+
import asyncio
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
def _index():
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Auth Page",
|
| 9 |
+
page_icon="🔐",
|
| 10 |
+
layout="wide"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# threading.Thread(target=__add_user_config, daemon=True).start()
|
| 14 |
+
# print("[CONFIG] Background user config update started.")
|
| 15 |
+
|
| 16 |
+
# --------------------------------------
|
| 17 |
+
# Initialize session state
|
| 18 |
+
# --------------------------------------
|
| 19 |
+
if "logged_in" not in st.session_state:
|
| 20 |
+
st.session_state.logged_in = False
|
| 21 |
+
|
| 22 |
+
# --------------------------------------
|
| 23 |
+
# If user is logged in → show main app
|
| 24 |
+
# --------------------------------------
|
| 25 |
+
if st.session_state.logged_in and st.session_state.admin_login:
|
| 26 |
+
from admin_page import load_user_config
|
| 27 |
+
load_user_config()
|
| 28 |
+
st.stop()
|
| 29 |
+
|
| 30 |
+
if st.session_state.logged_in:
|
| 31 |
+
from app import load_app
|
| 32 |
+
load_app()
|
| 33 |
+
st.stop() # important: stop further execution
|
| 34 |
+
|
| 35 |
+
# --------------------------------------
|
| 36 |
+
# Otherwise show login UI
|
| 37 |
+
# --------------------------------------
|
| 38 |
+
st.sidebar.header("Options")
|
| 39 |
+
page = st.sidebar.radio("Select Option", ["Login", "Sign Up", "Admin Login"],label_visibility="collapsed")
|
| 40 |
+
|
| 41 |
+
# LOGIN PAGE
|
| 42 |
+
if page == "Login":
|
| 43 |
+
try:
|
| 44 |
+
st.subheader("🔐 Login")
|
| 45 |
+
|
| 46 |
+
# New checkbox
|
| 47 |
+
is_first_login = st.checkbox("Is First Login")
|
| 48 |
+
|
| 49 |
+
# Common inputs
|
| 50 |
+
username = st.text_input("Username")
|
| 51 |
+
password = st.text_input("Password", type="password")
|
| 52 |
+
|
| 53 |
+
# Conditional field
|
| 54 |
+
token = None
|
| 55 |
+
if is_first_login:
|
| 56 |
+
token = st.text_input("Token")
|
| 57 |
+
|
| 58 |
+
if st.button("Login"):
|
| 59 |
+
from utils.utils import is_authenticated
|
| 60 |
+
|
| 61 |
+
userinfo = {
|
| 62 |
+
"username": username,
|
| 63 |
+
"_access_key": password
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
# Add token only if first login
|
| 67 |
+
if is_first_login:
|
| 68 |
+
userinfo["token"] = token
|
| 69 |
+
|
| 70 |
+
statuscode, message = asyncio.run(is_authenticated(userinfo=userinfo))
|
| 71 |
+
|
| 72 |
+
if statuscode == 0:
|
| 73 |
+
st.session_state.admin_login=False
|
| 74 |
+
st.session_state.logged_in = True
|
| 75 |
+
st.session_state.user = username
|
| 76 |
+
st.rerun()
|
| 77 |
+
load_app()
|
| 78 |
+
else:
|
| 79 |
+
st.error(message)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error(e)
|
| 82 |
+
st.error("Server Error while Login, please try after sometime")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# Sign Up
|
| 86 |
+
|
| 87 |
+
if page == "Sign Up":
|
| 88 |
+
try:
|
| 89 |
+
from utils.utils import _create_user
|
| 90 |
+
st.header("🧾 Sign Up")
|
| 91 |
+
|
| 92 |
+
# ---------- Reset ONLY when coming to this page ----------
|
| 93 |
+
if "current_page" not in st.session_state:
|
| 94 |
+
st.session_state.current_page = "Sign Up"
|
| 95 |
+
|
| 96 |
+
if st.session_state.current_page != "Sign Up":
|
| 97 |
+
# reset fields BEFORE widgets are drawn
|
| 98 |
+
st.session_state.email = ""
|
| 99 |
+
st.session_state.username = ""
|
| 100 |
+
st.session_state.password = ""
|
| 101 |
+
st.session_state.current_page = "Sign Up"
|
| 102 |
+
|
| 103 |
+
# ---------- Widgets ----------
|
| 104 |
+
email = st.text_input("Email", key="email")
|
| 105 |
+
username = st.text_input("Username", key="username")
|
| 106 |
+
password = st.text_input("Password", type="password", key="password")
|
| 107 |
+
|
| 108 |
+
user_info = {
|
| 109 |
+
"username": username,
|
| 110 |
+
"email": email,
|
| 111 |
+
"_access_key": password
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
all_filled = email.strip() and username.strip() and password.strip()
|
| 115 |
+
|
| 116 |
+
if all_filled:
|
| 117 |
+
if st.button("Create User"):
|
| 118 |
+
statuscode, message = asyncio.run(_create_user(userinfo=user_info))
|
| 119 |
+
if statuscode == 0:
|
| 120 |
+
st.success(message)
|
| 121 |
+
time.sleep(3)
|
| 122 |
+
# Reset BEFORE next rerun, not after widgets created
|
| 123 |
+
st.session_state.current_page = ""
|
| 124 |
+
|
| 125 |
+
st.rerun()
|
| 126 |
+
|
| 127 |
+
else:
|
| 128 |
+
st.error(message)
|
| 129 |
+
else:
|
| 130 |
+
st.button("Create User", disabled=True)
|
| 131 |
+
|
| 132 |
+
except Exception as e:
|
| 133 |
+
st.error(e)
|
| 134 |
+
st.warning("Server is facing too many request, please try after sometime")
|
| 135 |
+
|
| 136 |
+
if page == "Admin Login":
|
| 137 |
+
try:
|
| 138 |
+
st.subheader("🔐 Admin Login")
|
| 139 |
+
# Common inputs
|
| 140 |
+
username = st.text_input("Username")
|
| 141 |
+
password = st.text_input("Password", type="password")
|
| 142 |
+
if st.button("Admin Login"):
|
| 143 |
+
from utils.utils import check_admin_login
|
| 144 |
+
|
| 145 |
+
userinfo = {
|
| 146 |
+
"username": username,
|
| 147 |
+
"password": password
|
| 148 |
+
}
|
| 149 |
+
statuscode, message = asyncio.run(check_admin_login(userinfo=userinfo))
|
| 150 |
+
if statuscode:
|
| 151 |
+
st.session_state.logged_in = True
|
| 152 |
+
st.session_state.admin_login = True
|
| 153 |
+
st.session_state.user = username
|
| 154 |
+
st.rerun()
|
| 155 |
+
load_user_config()
|
| 156 |
+
else:
|
| 157 |
+
st.error(message)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
st.error(e)
|
| 160 |
+
st.error("Server Error while Login, please try after sometime")
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
_index()
|
app_ui/profile.png
ADDED
|
app_ui/utils/__init__.py
ADDED
|
File without changes
|
app_ui/utils/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (168 Bytes). View file
|
|
|
app_ui/utils/__pycache__/utils.cpython-311.pyc
ADDED
|
Binary file (13.9 kB). View file
|
|
|
app_ui/utils/utils.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
import ast
|
| 5 |
+
import base64
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
| 9 |
+
from cryptography.hazmat.primitives import hashes
|
| 10 |
+
from cryptography.fernet import Fernet
|
| 11 |
+
import hashlib
|
| 12 |
+
import base64
|
| 13 |
+
import random
|
| 14 |
+
import string
|
| 15 |
+
import certifi
|
| 16 |
+
from pymongo import MongoClient
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#DB utils
|
| 20 |
+
|
| 21 |
+
load_dotenv(override=True)
|
| 22 |
+
uri = os.getenv('MONGO_DB_URL')
|
| 23 |
+
client = MongoClient(uri,tlsCAFile=certifi.where())
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def __load_user_config():
|
| 27 |
+
db=client[os.getenv('CONFIG_DB')]
|
| 28 |
+
collection=db[os.getenv('USER_CONFIG')]
|
| 29 |
+
user_config=list(collection.find({},{"_id":0}))
|
| 30 |
+
return user_config
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def __load_app_config():
|
| 34 |
+
db=client[os.getenv('CONFIG_DB')]
|
| 35 |
+
collection=db[os.getenv('APP_CONFIG')]
|
| 36 |
+
app_config=list(collection.find({}))[0].get("_id")
|
| 37 |
+
return app_config
|
| 38 |
+
|
| 39 |
+
def __add_user_config(key,value):
|
| 40 |
+
try:
|
| 41 |
+
db=client[os.getenv('CONFIG_DB')]
|
| 42 |
+
collection=db[os.getenv('USER_CONFIG')]
|
| 43 |
+
doc = {
|
| 44 |
+
"user_key": key,
|
| 45 |
+
"user_config": value,
|
| 46 |
+
}
|
| 47 |
+
result=collection.insert_one(document=doc)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
raise Exception
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def __update_user_config(key,value):
|
| 53 |
+
db=client[os.getenv('CONFIG_DB')]
|
| 54 |
+
collection=db[os.getenv('USER_CONFIG')]
|
| 55 |
+
collection.update_one(
|
| 56 |
+
{"user_key":key},
|
| 57 |
+
{ "$set": { "user_config": value } }
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
app_config_col=__load_app_config()
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def __get_user_info(user_key):
|
| 64 |
+
user_config_col=__load_user_config()
|
| 65 |
+
user_info=[user for user in user_config_col if user.get('user_key')==user_key]
|
| 66 |
+
return user_info
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
#encode-decode utils
|
| 70 |
+
|
| 71 |
+
def __drive_key(master_password: str, salt: bytes) -> bytes:
|
| 72 |
+
kdf = PBKDF2HMAC(
|
| 73 |
+
algorithm=hashes.SHA256(),
|
| 74 |
+
length=32,
|
| 75 |
+
salt=salt,
|
| 76 |
+
iterations=390000,
|
| 77 |
+
)
|
| 78 |
+
return base64.urlsafe_b64encode(kdf.derive(master_password.encode()))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def encrypt_with_password(master_password: str, salt:bytes,user_info: dict):
|
| 82 |
+
key = __drive_key(master_password, salt)
|
| 83 |
+
token = Fernet(key).encrypt(user_info.__str__().encode())
|
| 84 |
+
token=token.decode("utf-8")
|
| 85 |
+
return token
|
| 86 |
+
|
| 87 |
+
def decrypt_with_password(master_password: str, salt: bytes, encrypted_text: bytes):
|
| 88 |
+
key = __drive_key(master_password, salt)
|
| 89 |
+
decrypted = Fernet(key).decrypt(encrypted_text)
|
| 90 |
+
return decrypted.decode()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def __encode(userinfo,k='email'):
|
| 94 |
+
h = hashlib.sha256(userinfo.get(k).encode()).hexdigest()
|
| 95 |
+
random.seed(h)
|
| 96 |
+
letters = string.ascii_letters
|
| 97 |
+
encoded = ''.join(random.choice(letters) for _ in range(32))
|
| 98 |
+
if k=='email':
|
| 99 |
+
return "kahini_lok_user_"+encoded
|
| 100 |
+
else:
|
| 101 |
+
return encoded
|
| 102 |
+
|
| 103 |
+
def __decode_user_config(encrypted_txt):
|
| 104 |
+
salt_bytes=base64.b64encode(app_config_col.get("SALT_KEY").encode("utf-8"))
|
| 105 |
+
app_api_key=app_config_col.get("APP_API_KEY")
|
| 106 |
+
userconfig=decrypt_with_password(master_password=app_api_key,
|
| 107 |
+
salt=salt_bytes,
|
| 108 |
+
encrypted_text=encrypted_txt)
|
| 109 |
+
return userconfig
|
| 110 |
+
|
| 111 |
+
def __generate_password(length=10):
|
| 112 |
+
characters = string.ascii_letters + string.digits
|
| 113 |
+
return ''.join(random.choice(characters) for _ in range(length))
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
#app utils
|
| 118 |
+
async def is_authenticated(userinfo):
|
| 119 |
+
|
| 120 |
+
username_encode=__encode(userinfo=userinfo,k="username")
|
| 121 |
+
|
| 122 |
+
user=__get_user_info(user_key=username_encode)
|
| 123 |
+
if len(user)==1:
|
| 124 |
+
|
| 125 |
+
encode_uuid=user[0].get("user_config")
|
| 126 |
+
user_config=__get_user_info(user_key=encode_uuid)
|
| 127 |
+
if len(user_config)==1:
|
| 128 |
+
|
| 129 |
+
userconfig=ast.literal_eval(__decode_user_config(user_config[0].get('user_config')))
|
| 130 |
+
|
| 131 |
+
if userconfig.get("_is_validated")=="N":
|
| 132 |
+
return 1, "User validation is in progress, Please try after sometime with tokenID send to your email"
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
if userconfig.get("_access_key")==userinfo.get("_access_key"):
|
| 136 |
+
if userconfig['_is_email_validated']=="N":
|
| 137 |
+
return 1, "Email validation is in progress, Please try after sometime with tokenID send to your email"
|
| 138 |
+
if userconfig['_is_email_validated']=="Y" and userconfig['_is_first_login']=="Y":
|
| 139 |
+
if userconfig.get("token")!=userinfo.get("token"):
|
| 140 |
+
return 1, "Invaild token OTP , please try with token send to your email"
|
| 141 |
+
else:
|
| 142 |
+
salt_bytes=base64.b64encode(app_config_col.get("SALT_KEY").encode("utf-8"))
|
| 143 |
+
app_api_key=app_config_col.get("APP_API_KEY")
|
| 144 |
+
userconfig['_is_first_login']='N'
|
| 145 |
+
enc=encrypt_with_password(master_password=app_api_key,salt=salt_bytes,user_info=userconfig)
|
| 146 |
+
__update_user_config(key=encode_uuid,value=enc)
|
| 147 |
+
return 0, "Successful Login"
|
| 148 |
+
|
| 149 |
+
return 0, "Successful Login"
|
| 150 |
+
|
| 151 |
+
if userconfig.get("_access_key")!=userinfo.get("_access_key"):
|
| 152 |
+
return 1, "Invalid password"
|
| 153 |
+
else:
|
| 154 |
+
return 1, "Username does not exists"
|
| 155 |
+
|
| 156 |
+
else:
|
| 157 |
+
return 1, "Username does not exists"
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def is_valid_email(email):
|
| 161 |
+
vaild_domain=['gmail','yahoo','outlook']
|
| 162 |
+
domain=email.split(".")[0].split("@")[1]
|
| 163 |
+
provider=email.split(".")[1]
|
| 164 |
+
if domain in vaild_domain and provider == 'com':
|
| 165 |
+
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
|
| 166 |
+
return re.match(pattern, email) is not None
|
| 167 |
+
else:
|
| 168 |
+
return False
|
| 169 |
+
|
| 170 |
+
def is_user_exists(userinfo:dict,k):
|
| 171 |
+
user=__get_user_info(__encode(userinfo=userinfo,k=k))
|
| 172 |
+
if len(user)>0:
|
| 173 |
+
return 1
|
| 174 |
+
else:
|
| 175 |
+
return 0
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
async def _create_user(userinfo):
|
| 179 |
+
|
| 180 |
+
try:
|
| 181 |
+
|
| 182 |
+
if not is_valid_email(userinfo.get("email")):
|
| 183 |
+
return 1, "Invaild email ID"
|
| 184 |
+
|
| 185 |
+
if is_user_exists(userinfo=userinfo,k="email")==0:
|
| 186 |
+
if is_user_exists(userinfo=userinfo,k="username")==0:
|
| 187 |
+
salt_bytes=base64.b64encode(app_config_col.get('SALT_KEY').encode("utf-8"))
|
| 188 |
+
app_api_key=app_config_col.get("APP_API_KEY")
|
| 189 |
+
|
| 190 |
+
userinfo['_is_validated']="N"
|
| 191 |
+
userinfo['_is_email_validated']="N"
|
| 192 |
+
userinfo['_is_first_login']='Y'
|
| 193 |
+
userinfo['token']=__generate_password()
|
| 194 |
+
|
| 195 |
+
enc = encrypt_with_password(app_api_key, salt_bytes, userinfo)
|
| 196 |
+
__add_user_config(key=__encode(userinfo=userinfo),value=enc)
|
| 197 |
+
__add_user_config(key=__encode(userinfo=userinfo,k='username'),value=__encode(userinfo=userinfo))
|
| 198 |
+
|
| 199 |
+
return 0, """Account created successfully!\nYour username is {}.Login credentials will be delivered to {} within 2–3 hours, Use it for 1st time login""".format(userinfo.get("username"),
|
| 200 |
+
userinfo.get("email"))
|
| 201 |
+
|
| 202 |
+
else:
|
| 203 |
+
return 1, """Username already exists, please use different username"""
|
| 204 |
+
else:
|
| 205 |
+
return 1, """User already exists with this email, please used different email"""
|
| 206 |
+
except Exception as e:
|
| 207 |
+
#TODO
|
| 208 |
+
return 1, """Unable to create user, please try after sometime"""
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def __get_new_user():
|
| 213 |
+
|
| 214 |
+
new_user_list=[]
|
| 215 |
+
|
| 216 |
+
for user in __load_user_config():
|
| 217 |
+
if user.get("user_key").startswith("kahini_lok_user"):
|
| 218 |
+
new_user_list.append(ast.literal_eval(__decode_user_config(encrypted_txt=user.get("user_config"))))
|
| 219 |
+
|
| 220 |
+
return [new_user for new_user in new_user_list if new_user['_is_validated']=="N"]
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
async def check_admin_login(userinfo):
|
| 224 |
+
if userinfo.get("username")==app_config_col.get("APP_ADMIN") and userinfo.get("password")==app_config_col.get("ADMIN_KEY"):
|
| 225 |
+
return True, "Successful Login"
|
| 226 |
+
else:
|
| 227 |
+
return False, "Invalid credentials"
|