import sqlite3 import streamlit as st # Disease criticality points (1 to 10) disease_points = { 'Neonatal Diarrhea': 7, 'Pneumonia': 9, 'Navel Ill': 6, 'Cryptosporidiosis': 8, 'Joint Ill': 7, 'Coccidiosis': 6, 'Anaplasmosis': 8, 'Theileriosis': 9, 'Internal Parasites': 5, 'External Parasites': 4, 'Lameness / Foot Rot': 7, 'Pink Eye': 4, 'Mastitis': 8, 'Ketosis': 7, 'Milk Fever': 8, 'Johne’s': 10, 'FMD': 10, 'Bloat': 9, 'Hardware Disease': 9, 'Lumpy Skin Disease': 8, 'Brucellosis': 9, 'Tuberculosis': 9, 'Retained Placenta': 6, 'Metritis': 7, 'Udder Edema': 5, 'Dystocia': 6, 'Prolapse Abortion (IBR, Brucella, Lepto)': 9 } # SQLite Database setup def create_table(): conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute(''' CREATE TABLE IF NOT EXISTS cattle ( RFID TEXT PRIMARY KEY, Breed TEXT, Age INTEGER, Tag_ID TEXT, Health_History TEXT, Health_Score INTEGER ) ''') conn.commit() conn.close() # Function to calculate health score based on diseases def calculate_health_score(selected_diseases): health_score = 100 for disease in selected_diseases: health_score -= disease_points.get(disease, 0) health_score = max(0, health_score) return health_score # Function to register a new cow def register_cow(): st.header("Register a New Cow") rfid = st.text_input("Enter RFID Tag Number", key="rfid") if not rfid: st.warning("RFID is a mandatory field. Please enter an RFID number.") return conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute("SELECT * FROM cattle WHERE RFID = ?", (rfid,)) existing_cow = c.fetchone() conn.close() if existing_cow: st.warning(f"Cow with RFID {rfid} already exists.") return breed = st.selectbox("Select Breed", ['Girolando', 'Jersey', 'Holstein Friesian (HF)', 'Sahiwal', 'Kankrej', 'Gir / Gyr', 'Red Sindhi']) age = st.number_input("Enter Age (in years)", min_value=0) tag_id = st.text_input("Enter Tag ID") st.subheader("Enter Health History") health_status = st.selectbox("Current Health Status", ['Healthy', 'Sick', 'Under Treatment']) selected_diseases = [] if health_status in ['Sick', 'Under Treatment']: selected_diseases = st.multiselect("Select Disease(s)", list(disease_points.keys())) previous_illness = st.selectbox("Previous Illness", ['No', 'Yes']) previous_diseases = [] if previous_illness == 'Yes': previous_diseases = st.multiselect("Select Previous Disease(s)", list(disease_points.keys())) reproductive_status = st.selectbox("Reproductive Status", ['Not Applicable', 'Estrus Period', 'Gestation Period', 'Adult/Milking Stage', 'Pregnant/Calving Stage']) reproductive_cycles = st.number_input("No. of Reproductive Cycles", min_value=0, value=0) descriptive_analysis = st.text_area("Enter Descriptive Analysis", "Provide any additional details...") health_score = calculate_health_score(selected_diseases) if health_status in ['Sick', 'Under Treatment'] else 100 health_score_color = "green" if health_score >= 86 else "yellow" if health_score >= 70 else "red" health_status_color = "green" if health_status == "Healthy" else "yellow" if health_status == "Under Treatment" else "red" if st.button("Register Cow"): health_history = f"Current Health Status: {health_status}\n" \ f"Diseases: {', '.join(selected_diseases) if health_status in ['Sick', 'Under Treatment'] else 'None'}\n" \ f"Previous Illness: {previous_illness}\n" \ f"Previous Diseases: {', '.join(previous_diseases) if previous_illness == 'Yes' else 'None'}\n" \ f"Reproductive Status: {reproductive_status}\n" \ f"No. of Reproductive Cycles: {reproductive_cycles}\n" \ f"Descriptive Analysis: {descriptive_analysis}" conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute(''' INSERT INTO cattle (RFID, Breed, Age, Tag_ID, Health_History, Health_Score) VALUES (?, ?, ?, ?, ?, ?) ''', (rfid, breed, age, tag_id, health_history, health_score)) conn.commit() conn.close() st.success(f"Cow with RFID {rfid} has been registered successfully!") st.markdown(f"

Current Health Status: {health_status}

", unsafe_allow_html=True) st.markdown(f"

Health Score: {health_score}

", unsafe_allow_html=True) # Function to edit or update an existing cow's profile def update_cow(): st.header("Edit or Update Cow Profile") rfid = st.text_input("Enter RFID Tag Number to Update", key="update_rfid") if not rfid: st.warning("RFID is a mandatory field. Please enter an RFID number.") return conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute("SELECT * FROM cattle WHERE RFID = ?", (rfid,)) cow_info = c.fetchone() conn.close() if cow_info: st.write("Current Information:") st.write(f"**Breed**: {cow_info[1]}") st.write(f"**Age**: {cow_info[2]} years") st.write(f"**Tag ID**: {cow_info[3]}") st.write(f"**Health History**: {cow_info[4]}") st.write(f"**Health Score**: {cow_info[5]}") # Pre-fill the form with the existing data new_breed = st.selectbox("Select New Breed", ['Girolando', 'Jersey', 'Holstein Friesian (HF)', 'Sahiwal', 'Kankrej', 'Gir / Gyr', 'Red Sindhi'], index=['Girolando', 'Jersey', 'Holstein Friesian (HF)', 'Sahiwal', 'Kankrej', 'Gir / Gyr', 'Red Sindhi'].index(cow_info[1])) new_age = st.number_input("Enter New Age (in years)", min_value=0, value=cow_info[2]) new_tag_id = st.text_input("Enter New Tag ID", value=cow_info[3]) st.subheader("Enter Health History") # Extracting Health Status and Diseases new_health_status = st.selectbox("Current Health Status", ['Healthy', 'Sick', 'Under Treatment'], index=['Healthy', 'Sick', 'Under Treatment'].index(cow_info[4].split('\n')[0].split(": ")[1])) new_selected_diseases = [] if new_health_status in ['Sick', 'Under Treatment']: existing_diseases = cow_info[4].split('\n')[1].split(": ")[1] existing_diseases = existing_diseases.split(", ") if existing_diseases != 'None' else [] new_selected_diseases = st.multiselect("Select Disease(s)", list(disease_points.keys()), default=existing_diseases) new_previous_illness = st.selectbox("Previous Illness", ['No', 'Yes'], index=['No', 'Yes'].index(cow_info[4].split('\n')[2].split(": ")[1])) new_previous_diseases = [] # Fixing the Previous Disease selection if new_previous_illness == 'Yes': existing_previous_diseases = cow_info[4].split('\n')[2].split(": ")[1] # Ensure the values passed as default are part of the available diseases list existing_previous_diseases = existing_previous_diseases.split(", ") if existing_previous_diseases != 'None' else [] # Only provide valid options that exist in the disease list new_previous_diseases = st.multiselect("Select Previous Disease(s)", list(disease_points.keys()), default=[disease for disease in existing_previous_diseases if disease in disease_points.keys()]) # Reproductive Status handling (as string) reproductive_status_list = ['Not Applicable', 'Estrus Period', 'Gestation Period', 'Adult/Milking Stage', 'Pregnant/Calving Stage'] reproductive_status_value = cow_info[4].split('\n')[3].split(": ")[1] if cow_info[4].split('\n')[3].split(": ")[1] in reproductive_status_list else 'Not Applicable' new_reproductive_status = st.selectbox("Reproductive Status", reproductive_status_list, index=reproductive_status_list.index(reproductive_status_value)) # No. of Reproductive Cycles (parse it correctly as an integer) try: new_reproductive_cycles = st.number_input("No. of Reproductive Cycles", min_value=0, value=int(cow_info[4].split('\n')[4].split(": ")[1])) except ValueError: new_reproductive_cycles = st.number_input("No. of Reproductive Cycles", min_value=0, value=0) new_descriptive_analysis = st.text_area("Enter Descriptive Analysis", cow_info[4].split('\n')[5].split(": ")[1]) if st.button("Update Cow"): new_health_history = f"Current Health Status: {new_health_status}\n" \ f"Diseases: {', '.join(new_selected_diseases) if new_health_status in ['Sick', 'Under Treatment'] else 'None'}\n" \ f"Previous Illness: {new_previous_illness}\n" \ f"Previous Diseases: {', '.join(new_previous_diseases) if new_previous_illness == 'Yes' else 'None'}\n" \ f"Reproductive Status: {new_reproductive_status}\n" \ f"No. of Reproductive Cycles: {new_reproductive_cycles}\n" \ f"Descriptive Analysis: {new_descriptive_analysis}" new_health_score = calculate_health_score(new_selected_diseases) if new_health_status in ['Sick', 'Under Treatment'] else 100 conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute(''' UPDATE cattle SET Breed = ?, Age = ?, Tag_ID = ?, Health_History = ?, Health_Score = ? WHERE RFID = ? ''', (new_breed, new_age, new_tag_id, new_health_history, new_health_score, rfid)) conn.commit() conn.close() st.success(f"Cow with RFID {rfid} has been updated successfully!") else: st.warning(f"No cow found with RFID {rfid}.") # Function to view cattle profiles def view_profile(): st.header("View Cow Profile") rfid = st.text_input("Enter RFID Tag Number to View Profile", key="view_rfid") if rfid: conn = sqlite3.connect('cattle_data.db') c = conn.cursor() c.execute("SELECT * FROM cattle WHERE RFID = ?", (rfid,)) cow_info = c.fetchone() conn.close() if cow_info: st.write(f"**Breed**: {cow_info[1]}") st.write(f"**Age**: {cow_info[2]} years") st.write(f"**Tag ID**: {cow_info[3]}") st.write(f"**Health History**: {cow_info[4]}") st.write(f"**Health Score**: {cow_info[5]}") else: st.warning(f"No cow found with RFID {rfid}.") # Main app layout def main(): st.title("RFID Cattle Profile Generator") menu = ["Register Cow", "View Cow Profile", "Edit Cow Profile"] choice = st.sidebar.selectbox("Select an Option", menu) # Add the "Master Data" link to the sidebar st.sidebar.markdown("[Master Data](https://yougandar-mastersheetcows.hf.space/?embed=true&embed_options=show_toolbar#92ef4034)") if choice == "Register Cow": register_cow() elif choice == "View Cow Profile": view_profile() elif choice == "Edit Cow Profile": update_cow() # Create the table when the app starts create_table() # Run the app if __name__ == "__main__": main()