Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +104 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,106 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 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 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from backend import db_connection # Import db_connection
|
| 4 |
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Summarizer model
|
| 7 |
+
# -----------------------------
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_summarizer():
|
| 10 |
+
return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 11 |
+
|
| 12 |
+
summarizer = load_summarizer()
|
| 13 |
+
|
| 14 |
+
# -----------------------------
|
| 15 |
+
# Functions
|
| 16 |
+
# -----------------------------
|
| 17 |
+
def signup():
|
| 18 |
+
st.title("📝 Signup Page")
|
| 19 |
+
# name = st.text_input("Enter your Name") # Removed name input
|
| 20 |
+
email = st.text_input("Enter your Email")
|
| 21 |
+
username = st.text_input("Choose a Username")
|
| 22 |
+
password = st.text_input("Choose a Password", type="password")
|
| 23 |
+
|
| 24 |
+
if st.button("Signup"):
|
| 25 |
+
if email and username and password: # Removed 'name'
|
| 26 |
+
# Use db_connection to create user
|
| 27 |
+
if db_connection.create_user(email, username, password): # Removed 'name' argument
|
| 28 |
+
st.success("✅ Account created! Please log in now.")
|
| 29 |
+
st.session_state.page = "login"
|
| 30 |
+
st.rerun()
|
| 31 |
+
else:
|
| 32 |
+
st.error("⚠️ An account with this username or email already exists. Please choose another.")
|
| 33 |
+
else:
|
| 34 |
+
st.error("⚠️ Please fill in all fields.")
|
| 35 |
+
|
| 36 |
+
def login():
|
| 37 |
+
st.title("🔒 Login Page")
|
| 38 |
+
username = st.text_input("Username")
|
| 39 |
+
password = st.text_input("Password", type="password")
|
| 40 |
+
|
| 41 |
+
if st.button("Login"):
|
| 42 |
+
# Use db_connection to validate user
|
| 43 |
+
user = db_connection.validate_user(username, password)
|
| 44 |
+
if user:
|
| 45 |
+
st.session_state.logged_in = True
|
| 46 |
+
st.session_state.username = user['username'] # Store username in session
|
| 47 |
+
st.session_state.user_role = user['role'] # Store user role in session
|
| 48 |
+
st.success("✅ Login successful! Redirecting...")
|
| 49 |
+
st.session_state.page = "dashboard"
|
| 50 |
+
st.rerun()
|
| 51 |
+
else:
|
| 52 |
+
st.error("❌ Invalid username or password")
|
| 53 |
+
|
| 54 |
+
if st.button("Go to Signup"):
|
| 55 |
+
st.session_state.page = "signup"
|
| 56 |
+
st.rerun()
|
| 57 |
+
|
| 58 |
+
def summarizer_page():
|
| 59 |
+
st.title("📄 Text Summarizer")
|
| 60 |
+
|
| 61 |
+
text = st.text_area("Enter the text you want to summarize:", height=200)
|
| 62 |
+
|
| 63 |
+
if st.button("Summarize"):
|
| 64 |
+
if text.strip():
|
| 65 |
+
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
|
| 66 |
+
st.subheader("✨ Summary:")
|
| 67 |
+
st.write(summary[0]['summary_text'])
|
| 68 |
+
else:
|
| 69 |
+
st.error("⚠️ Please enter some text to summarize.")
|
| 70 |
+
|
| 71 |
+
if st.button("Logout"):
|
| 72 |
+
st.session_state.logged_in = False
|
| 73 |
+
st.session_state.page = "login" # Ensure it redirects to login after logout
|
| 74 |
+
st.rerun()
|
| 75 |
+
|
| 76 |
+
# -----------------------------
|
| 77 |
+
# Main App
|
| 78 |
+
# -----------------------------
|
| 79 |
+
def main():
|
| 80 |
+
if "logged_in" not in st.session_state:
|
| 81 |
+
st.session_state.logged_in = False
|
| 82 |
+
if "page" not in st.session_state:
|
| 83 |
+
st.session_state.page = "login"
|
| 84 |
+
|
| 85 |
+
if st.session_state.page == "signup":
|
| 86 |
+
signup()
|
| 87 |
+
elif st.session_state.page == "login":
|
| 88 |
+
login()
|
| 89 |
+
elif st.session_state.page == "dashboard" and st.session_state.logged_in: # New dashboard page
|
| 90 |
+
st.title("Welcome to Dashboard!") # Placeholder for dashboard content
|
| 91 |
+
# In a real app, you'd import and call a dashboard function here
|
| 92 |
+
if st.button("Go to Summarizer"): # Example navigation to summarizer
|
| 93 |
+
st.session_state.page = "summarizer"
|
| 94 |
+
st.rerun()
|
| 95 |
+
if st.button("Logout"): # Logout from dashboard
|
| 96 |
+
st.session_state.logged_in = False
|
| 97 |
+
st.session_state.page = "login"
|
| 98 |
+
st.rerun()
|
| 99 |
+
elif st.session_state.page == "summarizer" and st.session_state.logged_in:
|
| 100 |
+
summarizer_page()
|
| 101 |
+
else:
|
| 102 |
+
st.session_state.page = "login"
|
| 103 |
+
login()
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|