shareen1 commited on
Commit
ea6bc8a
·
verified ·
1 Parent(s): 8cef7ca

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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()