Spaces:
Paused
Paused
Download user_info.py from freelancerrhinobi/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/spaces/freelancerrhinobi/chatbot/resolve/main/user_info.py
- Command line
-
hf download hf://spaces/freelancerrhinobi/chatbot/user_info.py
-
curl -L -o user_info.py https://huggingface.co/spaces/freelancerrhinobi/chatbot/resolve/main/user_info.py
4.7 kB
| import firebase_admin | |
| from firebase_admin import credentials, firestore | |
| import datetime | |
| import pytz | |
| import os | |
| cred = credentials.Certificate({ | |
| "type": os.getenv("type"), | |
| "project_id": os.getenv("project_id"), | |
| "private_key_id":os.getenv("private_key_id") , | |
| "private_key": os.getenv('private_key').replace('\\n', '\n'), | |
| "client_email": os.getenv('client_email'), | |
| "client_id": os.getenv('client_id'), | |
| "auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
| "token_uri": "https://oauth2.googleapis.com/token", | |
| "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
| "client_x509_cert_url": os.getenv('client_x509_cert_url'), | |
| "universe_domain": "googleapis.com" | |
| }) | |
| # cred = credentials.Certificate('./key.json') | |
| firebase_admin.initialize_app(cred) | |
| db = firestore.client() | |
| usa_eastern_timezone = pytz.timezone('America/New_York') | |
| current_datetime = datetime.datetime.now(usa_eastern_timezone) | |
| def get_db(): | |
| return db | |
| # Define a function to update user data by userid | |
| def update_user_by_userid(userid, field_updates): | |
| try: | |
| # Create a reference to the "users" collection | |
| users_ref = db.collection('users') | |
| # Query for the user with the specified userid | |
| query = users_ref.where('userid', '==', userid).limit(1) | |
| user_docs = query.stream() | |
| # Assuming there's only one matching document (limit=1) | |
| for doc in user_docs: | |
| user_ref = doc.reference | |
| user_ref.update(field_updates) | |
| # print(f"User with userid {userid} updatedg successfully.") | |
| return True | |
| print(f"No user found with userid {userid}.") | |
| return False | |
| except Exception as e: | |
| print(f"Error updating user: {e}") | |
| return False | |
| def get_user_by_userid(userid): | |
| # Create a reference to the "users" collection | |
| users_ref = db.collection('users') | |
| # Query for the user with the specified userid | |
| query = users_ref.where('userid', '==', userid).limit(1) | |
| user_docs = query.stream() | |
| # Assuming there's only one matching document (limit=1) | |
| for doc in user_docs: | |
| user_data = doc.to_dict() | |
| return user_data | |
| def user_info(userid,usermail): | |
| users_ref = db.collection('users') | |
| user_in_db = get_user_by_userid(userid) | |
| if user_in_db: | |
| # print("User found:") | |
| if user_in_db['isNewUser']: | |
| # print('New user found. do any initialization you want.') | |
| user_in_db['isNewUser'] =False | |
| if (user_in_db['last_accessed_date'].date()<current_datetime.date()) and (user_in_db["membership"]=="free"): | |
| # print("free membership initialization started") | |
| user_in_db['chatgpt_tokens'] =1 | |
| user_in_db['gemini_tokens'] = 5 | |
| else: | |
| if ((user_in_db['last_accessed_date'].date()<current_datetime.date()) and (current_datetime<user_in_db['SubscriptionEndDate'])) : | |
| # print("new day initialization started") | |
| if (user_in_db["membership"]=="Base Yearly") or (user_in_db["membership"]=='Base Monthly') : | |
| print("Base membership initialization started") | |
| user_in_db['chatgpt_tokens'] =10 | |
| user_in_db['gemini_tokens'] = 10 | |
| if (user_in_db["membership"]=="Professional Yearly") or (user_in_db["membership"]=='Professional Monthly') : | |
| print("Professional membership initialization started") | |
| user_in_db['chatgpt_tokens'] =20 | |
| user_in_db['gemini_tokens'] = 50 | |
| if (user_in_db["membership"]=="Enterprise Yearly") or (user_in_db["membership"]=='Enterprise Monthly') : | |
| print("Enterprise membership initialization started") | |
| user_in_db['chatgpt_tokens'] =100 | |
| user_in_db['gemini_tokens'] = 200 | |
| update_user_by_userid(userid=userid,field_updates=user_in_db) | |
| # print("new day initialization ended") | |
| user_in_db['last_accessed_date'] =current_datetime | |
| result = update_user_by_userid(userid=userid,field_updates=user_in_db) | |
| if not result: | |
| print('error occured while updating the user info') | |
| else: | |
| print(f"No user found with userid {userid}. creating new user") | |
| userinfo = { | |
| 'isNewUser':True, | |
| 'email': usermail, | |
| 'userid':userid, | |
| 'membership':'free', | |
| 'last_accessed_date':current_datetime, | |
| 'chatgpt_tokens':5, | |
| 'gemini_tokens':5 | |
| } | |
| users_ref.add(userinfo) | |
| user_in_db = userinfo | |
| # print(user_in_db) | |
| return user_in_db | |