Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +22 -22
src/streamlit_app.py
CHANGED
|
@@ -4,42 +4,42 @@ import requests
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
-
from transformers import MarianMTModel, MarianTokenizer
|
| 8 |
|
| 9 |
# Configure the Streamlit page
|
| 10 |
st.set_page_config(page_title="📍 BharatPulse - Local News & Sentiment", layout="wide")
|
| 11 |
|
| 12 |
# --- User Authentication Functions ---
|
| 13 |
# IMPORTANT: For production, this 'users.json' file will not persist across deployments
|
| 14 |
-
# on cloud platforms like Streamlit Community Cloud.
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
USERS_FILE = "/tmp/users.json"
|
| 18 |
|
| 19 |
def load_users():
|
| 20 |
"""Loads user data from the JSON file."""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
def save_users(users):
|
| 36 |
"""Saves user data to the JSON file."""
|
| 37 |
try:
|
| 38 |
with open(USERS_FILE, "w") as f:
|
| 39 |
json.dump(users, f, indent=2)
|
|
|
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
-
st.error(f"An unexpected error occurred while saving users: {e}")
|
| 42 |
-
|
| 43 |
|
| 44 |
def register_user(username, email, password, location):
|
| 45 |
"""Registers a new user."""
|
|
@@ -52,7 +52,8 @@ def register_user(username, email, password, location):
|
|
| 52 |
"password": password, # In a real app, hash passwords!
|
| 53 |
"location": location
|
| 54 |
}
|
| 55 |
-
save_users(users)
|
|
|
|
| 56 |
return True, "Registration successful. You can now log in."
|
| 57 |
|
| 58 |
def login_user(email, password):
|
|
@@ -61,7 +62,6 @@ def login_user(email, password):
|
|
| 61 |
user = users.get(email)
|
| 62 |
if user and user["password"] == password: # In a real app, compare hashed hashed passwords!
|
| 63 |
return True, user
|
| 64 |
-
# Modified error message to guide the user
|
| 65 |
return False, "Invalid email or password. If you don't have an account, please go to the 'Register' tab to create one."
|
| 66 |
|
| 67 |
# --- MarianMT Model for Translation ---
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 8 |
|
| 9 |
# Configure the Streamlit page
|
| 10 |
st.set_page_config(page_title="📍 BharatPulse - Local News & Sentiment", layout="wide")
|
| 11 |
|
| 12 |
# --- User Authentication Functions ---
|
| 13 |
# IMPORTANT: For production, this 'users.json' file will not persist across deployments
|
| 14 |
+
# on cloud platforms like Streamlit Community Cloud. For persistent user data,
|
| 15 |
+
# a proper database (e.g., Firebase Firestore, PostgreSQL) is REQUIRED.
|
| 16 |
+
# If you are seeing PermissionError even with /tmp, your environment does not allow file writes.
|
| 17 |
+
USERS_FILE = "/tmp/users.json" # Attempt to use /tmp for write permissions
|
| 18 |
|
| 19 |
def load_users():
|
| 20 |
"""Loads user data from the JSON file."""
|
| 21 |
+
users_data = {}
|
| 22 |
+
if os.path.exists(USERS_FILE):
|
| 23 |
+
try:
|
| 24 |
+
with open(USERS_FILE, "r") as f:
|
| 25 |
+
users_data = json.load(f)
|
| 26 |
+
except json.JSONDecodeError:
|
| 27 |
+
st.error("Error: users.json file is corrupted. Starting with an empty user database.")
|
| 28 |
+
except PermissionError:
|
| 29 |
+
st.error("Permission Error: Cannot read users.json. Your environment might restrict file access. For persistent user data, a database is required.")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
st.error(f"An unexpected error occurred while loading users: {e}. For persistent user data, a database is required.")
|
| 32 |
+
return users_data
|
|
|
|
| 33 |
|
| 34 |
def save_users(users):
|
| 35 |
"""Saves user data to the JSON file."""
|
| 36 |
try:
|
| 37 |
with open(USERS_FILE, "w") as f:
|
| 38 |
json.dump(users, f, indent=2)
|
| 39 |
+
except PermissionError:
|
| 40 |
+
st.error("Permission Error: Cannot write to users.json. Your environment might restrict file access. For persistent user data, a database is required.")
|
| 41 |
except Exception as e:
|
| 42 |
+
st.error(f"An unexpected error occurred while saving users: {e}. For persistent user data, a database is required.")
|
|
|
|
| 43 |
|
| 44 |
def register_user(username, email, password, location):
|
| 45 |
"""Registers a new user."""
|
|
|
|
| 52 |
"password": password, # In a real app, hash passwords!
|
| 53 |
"location": location
|
| 54 |
}
|
| 55 |
+
save_users(users) # Attempt to save
|
| 56 |
+
# The success of saving will be indicated by the absence of an error from save_users
|
| 57 |
return True, "Registration successful. You can now log in."
|
| 58 |
|
| 59 |
def login_user(email, password):
|
|
|
|
| 62 |
user = users.get(email)
|
| 63 |
if user and user["password"] == password: # In a real app, compare hashed hashed passwords!
|
| 64 |
return True, user
|
|
|
|
| 65 |
return False, "Invalid email or password. If you don't have an account, please go to the 'Register' tab to create one."
|
| 66 |
|
| 67 |
# --- MarianMT Model for Translation ---
|