Spaces:
Sleeping
Sleeping
shaileshjadhavSS commited on
Commit ·
cd570f9
1
Parent(s): cbf81e5
Added UI and backend for bot
Browse filesFiles changed (11)
- .gitignoreAdded+7
- app.pyAdded+265
- model/prompts.pyAdded+45
- model/utils.pyAdded+45
- requirements.txtAdded+70
- settings/__init__.pyAdded+1
- settings/base.pyAdded+24
- user_auth/__init__.pyAdded
- user_auth/auth_manager.pyAdded+23
- user_auth/user.pyAdded+18
- user_auth/user_manager.pyAdded+59
1234567**/__pycache__/*.env*.jpg*.jpeg*.mp3app.log*.png123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265import osimport streamlit as stfrom streamlit_chat import messagefrom model.utils import create_model, setup_logger, generate_contentimport google.generativeai as genaiimport base64from settings.base import setup_logger, GOOGLE_API_KEYimport jsonfrom datetime import datetime
from user_auth.user_manager import UserManagerfrom user_auth.auth_manager import AuthManager
# Set up logginglogger = setup_logger()
# Initialize session state variablesif 'history' not in st.session_state: st.session_state.history = []if 'chat_session' not in st.session_state: st.session_state.chat_session = Noneif 'authenticated' not in st.session_state: st.session_state.authenticated = Falseif 'email' not in st.session_state: st.session_state.email = ""if 'profile' not in st.session_state: st.session_state.profile = Noneif 'model' not in st.session_state: st.session_state.model = None
def save_chat_history(user_name, chat_history): """ Save chat history to a file in a user-specific directory with a timestamp. """ base_dir = "chat_history" user_dir = os.path.join(base_dir, user_name) os.makedirs(user_dir, exist_ok=True) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") file_path = os.path.join(user_dir, f"session_{timestamp}.json") # Prepare a serializable version of chat_history serializable_history = [] for entry in chat_history: if entry['role'] == 'user': user_entry = { 'role': 'user', 'parts': entry['parts'][1] if len(entry['parts']) > 1 else entry['parts'][0] } serializable_history.append(user_entry) elif entry['role'] == 'model': model_entry = { 'role': 'model', 'parts': entry['parts'] } serializable_history.append(model_entry) with open(file_path, "w") as f: json.dump(serializable_history, f, indent=4) logger.info(f"Chat history saved to {file_path}")
def authenticate_user(email, password): """ Authenticate user using AuthManager. """ user_manager = UserManager() auth_manager = AuthManager(user_manager) authenticated = auth_manager.authenticate_user(email, password) return authenticated
def conversation_chat(file_path, text_prompt): """ Handle the conversation with the chat session using the provided file path and text prompt. """ temp_dir = "temp" os.makedirs(temp_dir, exist_ok=True) try: user_input = text_prompt if text_prompt else "" if file_path: temp_file_path = os.path.join(temp_dir, file_path.name) with open(temp_file_path, "wb") as f: f.write(file_path.read()) # Write the uploaded file to temp location
logger.info(f"Successfully saved uploaded file: {file_path.name}") file = genai.upload_file(path=temp_file_path, display_name=file_path.name) user_entry = { "role": "user", "parts": [file, user_input] } else: user_entry = { "role": "user", "parts": [user_input] } st.session_state.history.append(user_entry)
response_text = generate_content(st.session_state.model, st.session_state.history) bot_entry = { "role": "model", "parts": response_text } st.session_state.history.append(bot_entry)
logger.info("Conversation successfully processed") except Exception as e: logger.error(f"Error processing file: {e}") st.session_state.history.append("Error")
def display_chat(): """ Display chat input and responses. """ profile = st.session_state.profile st.title("Wellness Bot 🦾🤖") chat_container = st.container() upload_container = st.container()
clear_chat_button = st.button('Clear Chat') if st.button('Logout'):
# Save chat history before logging out save_chat_history(st.session_state.email, st.session_state.history)
st.session_state.authenticated = False st.session_state.email = "" st.session_state.history = [] st.session_state.chat_session = None st.session_state.profile = None st.session_state.model = None st.rerun()
with upload_container: with st.form(key='chat_form', clear_on_submit=True): file_path = st.file_uploader("Upload an image or audio of a meal:", type=["jpg", "jpeg", "png", "mpeg", "mp3", "wav", "ogg", "mp4"]) text_prompt = st.text_input("Type Here...") submit_button = st.form_submit_button(label='Send ⬆️')
if submit_button: conversation_chat(file_path, text_prompt) if clear_chat_button: st.session_state.history = [] st.session_state.chat_session = None
with chat_container: message(f"Hey {profile['name']}! I'm here to assist you with your meals. Let's make healthy eating a breeze. Feel free to upload an image/video/audio of your meal or ask any questions about nutrition, dietary needs, and meal planning. Together, we'll achieve your goals and ensure you stay healthy and happy.", avatar_style="bottts") for i, entry in enumerate(st.session_state.history): if entry['role'] == 'user': if len(entry['parts']) > 1: uploaded_file = entry['parts'][0] if uploaded_file.mime_type.startswith("image/"): file_name= uploaded_file.display_name with open(os.path.join("temp", file_name), "rb") as file: encoded_img = base64.b64encode(file.read()).decode('utf-8') img_html = f'<img src="data:image/png;base64,{encoded_img}" width="200" style="margin-top: 5px;"/>' st.markdown(f""" <div style="display: flex; justify-content: flex-end; margin-bottom: 10px;"> <div style="max-width: 300px; background-color: #E8E8E8; border-radius: 10px; padding: 10px; position: relative;"> <div style="text-align: right;"> {img_html} </div> </div> </div> """, unsafe_allow_html=True) else: message(uploaded_file.display_name, is_user=True, key=f"{i}_user", avatar_style="adventurer")
if entry['parts'][1] != "": message(entry['parts'][1], is_user=True, key=f"{i}_user", avatar_style="adventurer") else: message(entry['parts'][0], is_user=True, key=f"{i}_user", avatar_style="adventurer") elif entry['role'] == 'model': message(entry['parts'], key=str(i), avatar_style="bottts")
def main(): """ Main function to run the Streamlit app. """ user_manager = UserManager() auth_manager = AuthManager(user_manager) st.set_page_config(page_title="Wellness Bot 🦾🤖", page_icon=":fork_and_knife:")
if st.session_state.authenticated: if st.session_state.profile is None or st.session_state.model is None: st.session_state.profile = user_manager.get_user(st.session_state.email).profile st.session_state.model = create_model(GOOGLE_API_KEY, st.session_state.profile)
display_chat() else: st.title("Wellness Bot 🦾🤖 \n\nLogin/SignUp")
tab1, tab2 = st.tabs(["Login", "Sign Up"])
with tab1: st.header("Login") email = st.text_input("Email", key="login_email") password = st.text_input("Password", type="password", key="login_password") if st.button("Login"): if auth_manager.authenticate_user(email, password): st.success("Login successful!") st.session_state.authenticated = True st.session_state.email = email st.rerun() else: st.error("Invalid email or password")
with tab2: st.header("Create Account") email = st.text_input("Email", key="signup_email") password = st.text_input("Password", type="password", key="signup_password") confirm_password = st.text_input("Confirm Password", type="password", key="signup_confirm_password")
name = st.text_input("Name") age = st.number_input("Age", min_value=1, max_value=120) gender = st.selectbox("Gender", ["Male", "Female", "Other"]) height = st.number_input("Height (cm)", min_value=30, max_value=300) weight = st.number_input("Weight (kg)", min_value=1, max_value=500) location = st.text_input("Location") allergies = st.text_input("Allergies (comma-separated)") spec_diet_pref = st.text_input("Special Dietary Preferences") primary_goal = st.text_area("Primary Goal") health_condition = st.text_area("Health Condition") activity_level = st.selectbox("Activity Level", ["Sedentary", "Lightly Active", "Moderately Active", "Very Active"]) daily_calorie_intake = st.number_input("Daily Calorie Intake", min_value=1, max_value=10000)
if st.button("Sign Up"): if password != confirm_password: st.error("Passwords do not match") else: profile = { "name": name, "age": age, "gender": gender, "height": height, "weight": weight, "location": location, "allergies": allergies.split(','), "spec_diet_pref": spec_diet_pref, "primary_goal": primary_goal, "health_condition": health_condition, "activity_level": activity_level, "daily_calorie_intake": daily_calorie_intake } success, message = user_manager.create_user(email, password, profile) if success: st.success(message) else: st.error(message)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445def chatbot_prompt(profile): age = profile.get('age', 'unknown') gender = profile.get('gender', 'unknown') location = profile.get('location', 'unknown') spec_diet_pref = profile.get('spec_diet_pref', 'unknown') primary_goal = profile.get('primary_goal', 'unknown') health_condition = profile.get('health_condition', 'unknown') activity_level = profile.get('activity_level', 'unknown') daily_calorie_intake = profile.get('daily_calorie_intake', 'unknown')
return(f"""You are a highly knowledgeable nutritionist. You provide expert advice on meal planning, dietary needs, nutritional values, and healthy eating habits. You avoid answering any questions that are not related to meals, food, nutrition, or diet. Keep your responses focused on offering practical and evidence-based nutritional guidance. Keep your tone motivational and supportive. when you provide your expert advice always consider user details which are as follows and explain in what way it is tailored for the user: Age: {age},Gender: {gender},Location: {location}, Special Diet Preference: {spec_diet_pref}, Primary Goal: {primary_goal}, Health Condition: {health_condition} Activity Level: {activity_level}, Daily Calorie Intake: {daily_calorie_intake} calories Chat Flow: 1. Ask the user which meal of the day is uploading (breakfast, lunch, dinner, or snack). 2. Identify, mention what you could identified and ask required follow up questions as needed to provide a proper response a. if user uploads an image: - analyze the image to identify the meal name, ingredients, and detailed and necessary nutritional information including portion of each nutrient with its metric. \n\nMeal Analysis:\n\nMeal Name:\n[Identified Meal Name]\n\nIdentified Ingredient:\nIngredient 1\nIngredient 2\nIngredient 3\n...\n\nNutritional value Information (per serving) - If the image is not of a meal, respond with 'Please add a valid image.' If the image or meal details are unclear or incomplete, ask specific questions for clarification. If the image appears fake, respond with 'Please add an original meal image.' b. if user uploads audio: - transcribe the audio and identify the meal related information mentioned and reply to the user the transcribed text and get a confirmation. - respond with the identified meal name, ingredients and thier quantity (if not mentioned ask the user to provide), provide the necessary nutritional information in detail including portion of each nutrient with its metric. \n\nMeal Analysis:\n\nMeal Name:\n[Identified Meal Name]\n\nIdentified Ingredient:\nIngredient 1\nIngredient 2\nIngredient 3\n...\n\nNutritional value Information (per serving) - If the audio is not of a meal, respond with 'Please provide a valid meal description.' If the audio or meal details are unclear or incomplete, ask specific questions for clarification. If the audio appears fake, respond with 'Please provide proper clear audio.' c. if user uploads video: - analyse the video uploaded and identify the meal related information mentioned or visible in the frames and reply to the user the transcription and get a confirmation. - If the video has audio, transcribe the audio and identify all the meal related information mentioned (it could be the meal name or the ingredients) and reply to the user the transcribed text and get a confirmation. - respond with the identified meal name, ingredients, and detailed and necessary nutritional information including portion of each nutrient with its metric. \n\nMeal Analysis:\n\nMeal Name:\n[Identified Meal Name]\n\nIdentified Ingredient:\nIngredient 1\nIngredient 2\nIngredient 3\n...\n\nNutritional Information (per serving) - if the video is not a meal or the meal details are unclear or incomplete or if the video has no audio or the audio does not mention any meal , ask specific questions for clarification. If the video appears fake, respond with 'Please provide a valid meal video.' d. if user begins the conversation with a text input of their meal - Ask additional questions to gather information required to provide a proper response. 3. Additional Information Needed:\\n(If applicable) Please provide more details on:\\n- each ingredient clarification(size, type/kind)\\n- Specific cooking instructions (e.g., fried, baked) 4. Please confirm if the identified details are correct. -If correct, rewrite the identified details in a structured format and suggest a healthy alternative meal with detailed nutritional information and explanation why it is a better alternative. 4. Based on identified ingredients and nutritional information, suggest better alternatives cosidering the user's profile information, that would help improve the user's health or achiever goals, with detailed nutritional information and explanation why is this a better alternative. Healthy Alternative Suggestion:\\n\\n\"\n Ingredients:\\nIngredient 1\\nIngredient 2\\nIngredient 3\\n...\\n\\n\"\n Nutritional Information (per serving):\\n\\n 5. Then suggest the next healthy meal with a detailed ingredient list , recipe and nutritional value information per serving, explanation on how the suggested meal is beneficial for the user's health or goals. Also, help User keep track of their calorie intake by providing a calorie intake breakdown of the current meal ,suggested next meal and their future meals of the day\"\n
Every time you provide something, please confirm from the user, If any details are incorrect, please ask for additional information and always consider user details provided.\"\n""")
123456789101112131415161718192021222324252627282930313233343536373839404142434445import osimport google.generativeai as genaifrom model.prompts import chatbot_promptfrom settings.base import setup_logger
logger = setup_logger()
def create_model(api_key, profile): """ Create a GenerativeModel instance using the provided API key. """ generation_config = { "temperature": 0.8, "top_p": 0.95, "top_k": 64, "max_output_tokens": 8192, "response_mime_type": "text/plain", }
try: genai.configure(api_key=api_key) model = genai.GenerativeModel( model_name="gemini-1.5-pro", generation_config=generation_config, system_instruction= chatbot_prompt(profile), ) logger.info("Model created successfully") return model except Exception as e: logger.error(f"Error creating model: {e}") return None
def generate_content(model, message): """ Send a message to the chat session and get the response. """ try: # msg = f"Query:{message}/n/n Every time you provide something please confirm from the user if the identified details are correct. If they are correct, suggest the next healthy meal with the detailed recipe. If any details are incorrect, please ask for additional information." response = model.generate_content(message) logger.info(f"Generated content for message") return response.text except Exception as e: logger.error(f"Error generating content for message: {e}") return f"Error generating content: {e}"12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970altair==5.3.0annotated-types==0.7.0attrs==23.2.0bcrypt==4.1.3blinker==1.8.2cachelib==0.13.0cachetools==5.3.3certifi==2024.6.2charset-normalizer==3.3.2click==8.1.7Flask==3.0.3Flask-Session==0.8.0gitdb==4.0.11GitPython==3.1.43google-ai-generativelanguage==0.6.6google-api-core==2.19.1google-api-python-client==2.134.0google-auth==2.30.0google-auth-httplib2==0.2.0google-generativeai==0.7.1googleapis-common-protos==1.63.2grpcio==1.64.1grpcio-status==1.62.2httplib2==0.22.0idna==3.7itsdangerous==2.2.0Jinja2==3.1.4jsonschema==4.22.0jsonschema-specifications==2023.12.1markdown-it-py==3.0.0MarkupSafe==2.1.5mdurl==0.1.2msgspec==0.18.6numpy==2.0.0packaging==24.1pandas==2.2.2pillow==10.3.0proto-plus==1.24.0protobuf==4.25.3psycopg2-binary==2.9.9pyarrow==16.1.0pyasn1==0.6.0pyasn1_modules==0.4.0pydantic==2.7.4pydantic_core==2.18.4pydeck==0.9.1Pygments==2.18.0pyparsing==3.1.2python-dateutil==2.9.0.post0pytz==2024.1referencing==0.35.1requests==2.32.3rich==13.7.1rpds-py==0.18.1rsa==4.9six==1.16.0smmap==5.0.1streamlit==1.36.0streamlit-chat==0.1.1tenacity==8.4.2toml==0.10.2toolz==0.12.1tornado==6.4.1tqdm==4.66.4typing_extensions==4.12.2tzdata==2024.1uritemplate==4.1.1urllib3==2.2.2watchdog==4.0.1Werkzeug==3.0.31from settings.base import *123456789101112131415161718192021222324import osimport logging
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
DB_NAME = os.environ.get("DB_NAME")DB_USER = os.environ.get("DB_USER")DB_PASSWORD = os.environ.get("DB_PASSWORD") DB_HOST = os.environ.get("DB_HOST")DB_PORT = os.environ.get("DB_PORT")
def setup_logger(): logger = logging.getLogger('app_logger') logger.setLevel(logging.DEBUG) # Avoid adding multiple handlers if the logger already has handlers if not logger.hasHandlers(): fh = logging.FileHandler('debug.log', mode='w') fh.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logger.addHandler(fh)
return logger1234567891011121314151617181920212223from user_auth.user_manager import UserManagerimport loggingfrom settings.base import setup_logger
logger = setup_logger()
class AuthManager: def __init__(self, user_manager): self.user_manager = user_manager
def authenticate_user(self, email, password): user = self.user_manager.get_user(email) if user: logger.info(f'Attempting to authenticate user: {user.email}') if user.check_password(password): logger.info(f'User authenticated successfully: {user.email}') return True else: logger.error(f'Invalid password for user: {user.email}') else: logger.error(f'User not found: {email}') return False123456789101112131415161718import bcrypt
class User: def __init__(self, email, password, profile): self.email = email self.password_hash = self._hash_password(password) self.password = password self.profile = profile
def _hash_password(self, password): hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) return hashed_password
def check_password(self, password): entered_password = password.encode('utf-8') stored_pwd = self.password.encode('utf-8') return bcrypt.checkpw(entered_password, stored_pwd) 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import psycopg2from user_auth.user import Userfrom settings.base import setup_logger, DB_HOST, DB_NAME, DB_PASSWORD, DB_PORT, DB_USER
logger = setup_logger()
class UserManager: def __init__(self): self.conn = psycopg2.connect( dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST, port=DB_PORT ) self.cursor = self.conn.cursor()
def create_user(self, email, password, profile): if self.get_user(email): return False, "User already exists!" user = User(email, password, profile) try: self.cursor.execute( """ INSERT INTO users (username, password, name, age, gender, height, weight, location, allergies, spec_diet_pref, primary_goal, health_condition, activity_level, daily_calorie_intake) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """, (email, user.password_hash.decode("utf-8"), profile['name'], profile['age'], profile['gender'], profile['height'], profile['weight'], profile['location'], profile['allergies'], profile['spec_diet_pref'], profile['primary_goal'], profile['health_condition'], profile['activity_level'], profile['daily_calorie_intake']) ) self.conn.commit() logger.info(f'User created successfully: {email}') return True, "Account created successfully!" except Exception as e: self.conn.rollback() logger.error(f'Error creating user: {e}') return False, str(e)
def get_user(self, email): self.cursor.execute("SELECT * FROM users WHERE username = %s", (email,)) result = self.cursor.fetchone() if result: profile = { "name": result[3], "age": result[4], "gender": result[5], "height": result[6], "weight": result[7], "location": result[8], "allergies": result[9], "spec_diet_pref": result[10], "primary_goal": result[11], "health_condition": result[12], "activity_level": result[13], "daily_calorie_intake": result[14] } logger.info(f'User retrieved from database: {result[1]}') return User(email=result[1], password=result[1], profile=profile) return None
def close(self): self.cursor.close() self.conn.close().gitignoreAdded+7
app.pyAdded+265
model/prompts.pyAdded+45
model/utils.pyAdded+45
requirements.txtAdded+70
settings/__init__.pyAdded+1
settings/base.pyAdded+24
user_auth/__init__.pyAdded
user_auth/auth_manager.pyAdded+23
user_auth/user.pyAdded+18
user_auth/user_manager.pyAdded+59