artintel235 commited on
Commit
580c204
·
verified ·
1 Parent(s): 4ddaea3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import streamlit as st
2
  import firebase_admin
3
- from firebase_admin import credentials, auth
4
  import os
5
  import json
 
6
  # Load Firebase credentials from Hugging Face Secrets
7
  firebase_creds = os.getenv("FIREBASE_CREDENTIALS")
8
 
@@ -10,10 +11,13 @@ if firebase_creds:
10
  firebase_creds = json.loads(firebase_creds)
11
  else:
12
  st.error("Firebase credentials not found. Please check your secrets.")
 
13
  # Initialize Firebase (only once)
14
  if not firebase_admin._apps:
15
  cred = credentials.Certificate(firebase_creds) # Replace with your Firebase service account key
16
- firebase_admin.initialize_app(cred)
 
 
17
 
18
  # Initialize session state
19
  if "logged_in" not in st.session_state:
@@ -51,6 +55,19 @@ def logout_callback():
51
  st.session_state.current_user = None
52
  st.info("Logged out successfully!")
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Registration form
55
  def registration_form():
56
  with st.form("Registration"):
@@ -78,6 +95,8 @@ def main_app():
78
  # Display greeting in real-time
79
  if name:
80
  st.write(f"Hello {name}!")
 
 
81
 
82
  # Logout button
83
  if st.button("Logout", on_click=logout_callback):
 
1
  import streamlit as st
2
  import firebase_admin
3
+ from firebase_admin import credentials, auth, db
4
  import os
5
  import json
6
+
7
  # Load Firebase credentials from Hugging Face Secrets
8
  firebase_creds = os.getenv("FIREBASE_CREDENTIALS")
9
 
 
11
  firebase_creds = json.loads(firebase_creds)
12
  else:
13
  st.error("Firebase credentials not found. Please check your secrets.")
14
+
15
  # Initialize Firebase (only once)
16
  if not firebase_admin._apps:
17
  cred = credentials.Certificate(firebase_creds) # Replace with your Firebase service account key
18
+ firebase_admin.initialize_app(cred, {
19
+ 'databaseURL': 'https://creative-623ef-default-rtdb.firebaseio.com/' # Replace with your Firebase Realtime Database URL
20
+ })
21
 
22
  # Initialize session state
23
  if "logged_in" not in st.session_state:
 
55
  st.session_state.current_user = None
56
  st.info("Logged out successfully!")
57
 
58
+ # Function to store text in Firebase Realtime Database
59
+ def store_text_in_db(user_id, text):
60
+ try:
61
+ ref = db.reference(f'users/{user_id}/texts')
62
+ new_text_ref = ref.push()
63
+ new_text_ref.set({
64
+ 'text': text,
65
+ 'timestamp': firebase_admin.db.ServerValue.TIMESTAMP
66
+ })
67
+ st.success("Text saved successfully!")
68
+ except Exception as e:
69
+ st.error(f"Failed to save text: {e}")
70
+
71
  # Registration form
72
  def registration_form():
73
  with st.form("Registration"):
 
95
  # Display greeting in real-time
96
  if name:
97
  st.write(f"Hello {name}!")
98
+ # Store the entered text in Firebase Realtime Database
99
+ store_text_in_db(st.session_state.current_user, name)
100
 
101
  # Logout button
102
  if st.button("Logout", on_click=logout_callback):