Monday, 30 November 2026

Streamlit and ML Integration

 Hi All,

How to integrate diabetes prediction model with Streamlit interface in python


Part1 - Model creation - createmodel.py


# train_model.py

import pandas as pd
import numpy as np
import joblib

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

from sklearn.metrics import (
    accuracy_score,
    confusion_matrix,
    classification_report,
    roc_auc_score
)

# 1. Load dataset
df = pd.read_csv("diabetes.csv")

print("Dataset Shape:", df.shape)
print(df.head())

# 2. Define input features
features = [
    "Pregnancies",
    "Glucose",
    "BloodPressure",
    "SkinThickness",
    "Insulin",
    "BMI",
    "DiabetesPedigreeFunction",
    "Age"
]

X = df[features].copy()
y = df["Outcome"]

# 3. Replace invalid zero values with NaN
# Zero is possible for Pregnancies, so do not replace it.

zero_columns = [
    "Glucose",
    "BloodPressure",
    "SkinThickness",
    "Insulin",
    "BMI"
]

X[zero_columns] = X[zero_columns].replace(0, np.nan)

# 4. Split dataset into training and testing
X_train, X_test, y_train, y_test = train_test_split(
    X,
    y,
    test_size=0.20,
    random_state=42,
    stratify=y
)

# 5. Create machine learning pipeline
model = Pipeline([
    ("imputer", SimpleImputer(strategy="median")),
    ("scaler", StandardScaler()),
    ("classifier", LogisticRegression(max_iter=1000))
])

# 6. Train model
model.fit(X_train, y_train)

print("\nModel Training Completed!")

# 7. Make predictions
y_pred = model.predict(X_test)

# 8. Evaluate model
accuracy = accuracy_score(y_test, y_pred)

print("\nModel Accuracy:", round(accuracy * 100, 2), "%")

print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))

print("\nClassification Report:")
print(classification_report(y_test, y_pred))

y_probability = model.predict_proba(X_test)[:, 1]

print(
    "ROC-AUC:",
    round(roc_auc_score(y_test, y_probability), 3)
)

# 9. Save trained model
joblib.dump(model, "diabetes_model.pkl")

print("\nModel saved successfully!")
print("File: diabetes_model.pkl")


Part 2 - Test model - testdiabetes.py


# app.py

import streamlit as st
import pandas as pd
import joblib

# 1. Page configuration
st.set_page_config(
    page_title="Diabetes Prediction",
    page_icon="🩺",
    layout="centered"
)

# 2. Load saved model
model = joblib.load("diabetes_model.pkl")

# 3. Application title
st.title("🩺 Diabetes Prediction System")

st.write(
    "Enter patient details below to estimate "
    "the diabetes classification."
)

st.divider()

# 4. Patient input form
with st.form("diabetes_form"):

    st.subheader("Patient Information")

    col1, col2 = st.columns(2)

    with col1:

        pregnancies = st.number_input(
            "Pregnancies",
            min_value=0,
            max_value=20,
            value=0
        )

        glucose = st.number_input(
            "Glucose Level",
            min_value=0.0,
            max_value=300.0,
            value=120.0
        )

        blood_pressure = st.number_input(
            "Blood Pressure",
            min_value=0.0,
            max_value=200.0,
            value=70.0
        )

        skin_thickness = st.number_input(
            "Skin Thickness",
            min_value=0.0,
            max_value=100.0,
            value=20.0
        )

    with col2:

        insulin = st.number_input(
            "Insulin Level",
            min_value=0.0,
            max_value=1000.0,
            value=80.0
        )

        bmi = st.number_input(
            "BMI",
            min_value=0.0,
            max_value=100.0,
            value=25.0
        )

        diabetes_pedigree = st.number_input(
            "Diabetes Pedigree Function",
            min_value=0.0,
            max_value=3.0,
            value=0.5
        )

        age = st.number_input(
            "Age",
            min_value=1,
            max_value=120,
            value=30
        )

    st.divider()

    submit = st.form_submit_button(
        "Predict Diabetes",
        use_container_width=True
    )

# 5. Prediction
if submit:

    # Create input dataframe
    input_data = pd.DataFrame([{
        "Pregnancies": pregnancies,
        "Glucose": glucose,
        "BloodPressure": blood_pressure,
        "SkinThickness": skin_thickness,
        "Insulin": insulin,
        "BMI": bmi,
        "DiabetesPedigreeFunction": diabetes_pedigree,
        "Age": age
    }])

    # Predict class
    prediction = model.predict(input_data)[0]

    # Predict probability
    probability = model.predict_proba(
        input_data
    )[0][1]

    st.divider()

    st.subheader("Prediction Result")

    st.metric(
        "Estimated Model Probability",
        f"{probability * 100:.2f}%"
    )

    if prediction == 1:

        st.error(
            "Model Prediction: Positive "
            "for Diabetes Classification"
        )

    else:

        st.success(
            "Model Prediction: Negative "
            "for Diabetes Classification"
        )

    st.info(
        "This result is generated by a machine learning "
        "model and is not a medical diagnosis."
    )

Saturday, 29 November 2025

Prompt Engineering using Jemini

 HI all

Question : Prompt engineering code in Python which accepts some prompt and get the response from Gemini using the API key

Step1 : Go to command mode and type - pip install google-genai

Step2. Go to Python IDLE and paste the below given code modified with your API key

Answer code

---------------------

from google import genai

from google.genai.errors import APIError


# NOTE: Hardcoding API keys is generally discouraged in production environments

# for security reasons. Environment variables or secret management tools are preferred.

# This is done here only to fulfill the specific request.


HARDCODED_API_KEY ="Your API code generated from Google AI  Studio"


def initialize_gemini(api_key: str):

    """Initializes the Gemini client using the hardcoded API key."""

    try:

        # Pass the API key directly to the Client constructor

        client = genai.Client(api_key=api_key)

        return client

    except Exception as e:

        # Note: If the API key is invalid or permissions are wrong, an error

        # might be raised here or during the first API call.

        print(f"Error initializing client: {e}")

        return None


def run_prompt_engineering_lesson(client):

    """

    Demonstrates prompt engineering using a Few-Shot Prompting technique.

    The goal is to teach the model a new, specific output format.

    """

    if not client:

        print("Cannot run lesson because the Gemini client failed to initialize.")

        return


    print("--- 📚 Prompt Engineering Lesson: Few-Shot Prompting ---")

    

    # --- 1. The Prompt ---

    # We provide a few examples ("shots") to guide the model's behavior.

    # The goal is to make it translate casual terms into formal business language.

    prompt = """

    **Instructions:** You are an expert business communication consultant.

    Your task is to translate casual, everyday phrases into formal, professional business language.


    **Examples (Few-Shots):**

    1. Casual: "We messed up the schedule."

    Formal: "We encountered an unforeseen discrepancy in the project timeline."

    2. Casual: "Can you email me the slides?"

    Formal: "Kindly forward the presentation deck via electronic mail."

    3. Casual: "Let's catch up later today."

    Formal: "I propose we schedule a debriefing session later this afternoon."


    **Your Turn (The Query):**

    4. Casual: "The project's going nowhere fast."

    Formal:"""

    prompt=input('Enter a query')


    print("\n[Input Prompt Sent to Gemini Model]:")

    print("----------------------------------------------------------------")

    print(prompt.strip())

    print("----------------------------------------------------------------")


    # --- 2. API Call ---

    # We use a powerful model like gemini-2.5-flash

    try:

        response = client.models.generate_content(

            model='gemini-2.5-flash',

            contents=prompt,

            # Adjusting temperature for more consistent responses.

            config={"temperature": 0.2} 

        )


        # --- 3. Output ---

        print("\n[Model Response Received]:")

        print("----------------------------------------------------------------")

        

        # We process the response text to extract only the formal translation

        full_text = response.text

        if "Formal:" in full_text:

            # Attempt to split to get the translation following the last "Formal:" marker

            formal_translation = full_text.split("Formal:")[-1].strip()

        else:

            # If the model only returned the answer, just strip it

            formal_translation = full_text.strip()

            

        print(formal_translation)

        print("----------------------------------------------------------------")


        print("\n**✨ Prompt Engineering Concept Demonstrated: Few-Shot Learning.**")

        print("By providing examples, we taught the model a specific format (Casual -> Formal) without explicit coding.")


    except APIError as e:

        print(f"\nAn API error occurred: {e}")

        print("Check your API key and ensure you haven't exceeded the free tier quota.")

    except Exception as e:

        print(f"\nAn unexpected error occurred: {e}")



if __name__ == "__main__":

    gemini_client = initialize_gemini(HARDCODED_API_KEY)

    if gemini_client:

        run_prompt_engineering_lesson(gemini_client)


Saturday, 6 April 2024

Save tokenizer ,model and load

 Hi all

-----------------------------------------

from google.colab import drive
drive.mount('/content/drive')
cd /content/drive/MyDrive/HUPHealth

import joblib

file_path = "model.joblib"

joblib.dump(model, file_path)

-------------------------------------------

from tensorflow.keras.models import load_model

emotion_model = load_model("HUPYogadeep.h5") 

--------------------------------------

 from keras.models import load_model
 hupmodel= load_model("hup_sentimental_lstm.h5")
------------------------------
from keras.models import load_model
model.save("hupmodel18.h5")
-------------------------------
from sklearn.preprocessing import LabelEncoder
import joblib

labelencoder = LabelEncoder()

y = labelencoder.fit_transform(df['cyberbullying_type'])
joblib.dump(labelencoder, 'huplabelencoder.pkl')
from tensorflow.keras.utils import to_categorical

y1 = to_categorical(y)
--------------------------------------------
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import pandas as pd
import re
import joblib

nltk.download('stopwords')
port_stem = PorterStemmer()
def stemming(content):
    stemmed_content = re.sub('[^a-zA-Z]',' ',content)
    stemmed_content = stemmed_content.lower()
    stemmed_content = stemmed_content.split()
    stemmed_content = [port_stem.stem(word) for word in stemmed_content if not word in stopwords.words('english')]
    stemmed_content = ' '.join(stemmed_content)
    return stemmed_content

hupstr=input('Enter the tweet')
df1 = pd.DataFrame({'text': [hupstr]})
df1['text']  = df1['text'].apply(stemming)

from keras.models import load_model
##hupmodel= load_model("hupmodel17.h5")
hupmodel= load_model("my_model.keras")


loaded_vectorizer = joblib.load('huptokenizer.joblib')
X2 = loaded_vectorizer.texts_to_sequences(df1['text'])
X2 = pad_sequences(X2,maxlen=337)


hupprediction1 = hupmodel.predict_on_batch(np.stack(X2))
labelencoder = joblib.load('huplabelencoder.pkl')
huplabel1 = labelencoder.inverse_transform(np.argmax(hupprediction1, axis=1))
hupresult1 = ''.join(huplabel1)
print(hupresult1)
------------
hupprediction1 = model.predict([data3]).argmax(axis=1)
OR
hupprediction1 = model.predict_on_batch(np.stack(data3))

Saturday, 16 March 2024

Text to Vector , Preprocessing and Loading to mode

 Hi

import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import pandas as pd
import re


nltk.download('stopwords')
port_stem = PorterStemmer()
def stemming(content):
    stemmed_content = re.sub('[^a-zA-Z]',' ',content)
    stemmed_content = stemmed_content.lower()
    stemmed_content = stemmed_content.split()
    stemmed_content = [port_stem.stem(word) for word in stemmed_content if not word in stopwords.words('english')]
    stemmed_content = ' '.join(stemmed_content)
    return stemmed_content

hupstr=input('Enter the tweet')
df1 = pd.DataFrame({'text': [hupstr]})
df1['text']  = df1['text'].apply(stemming)

from keras.models import load_model
hupmodel= load_model("hupmodel17.h5")


loaded_vectorizer = joblib.load('huptokenizer.joblib')
X2 = loaded_vectorizer.texts_to_sequences(df1['text'])
X2 = pad_sequences(X2,maxlen=337)

#X_test_transformed = loaded_vectorizer.transform(hupinput)
#X_test_dense = X_test_transformed.toarray()
hupprediction1 = hupmodel.predict_on_batch(np.stack(X2))
labelencoder = joblib.load('huplabelencoder.pkl')
huplabel1 = labelencoder.inverse_transform(np.argmax(hupprediction1, axis=1))
hupresult1 = ''.join(huplabel1)
print(hupresult1)

Print Tensorflow version

 Hi 

Try this


import tensorflow as tf print(tf.__version__) import keras print(keras.__version__)

Tuesday, 12 March 2024

How Flask file shows templates and images

 Hi

Normal Flask code look like

--------------

from flask import Flask, redirect, url_for, request
app = Flask(__name__)
 
 
@app.route('/success/<name>')
def success(name):
    return 'Hi %s' % name
 
 
@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        user = request.form['name']
        return redirect(url_for('success', name=user))
    else:
        user = request.args.get('name')
        return redirect(url_for('success', name=user))
 
 
if __name__ == '__main__':
    app.run(debug=True)

--------------

If you need template page, your flask file should return the following code

return render_template('template.html', result=hupresult)

Your template.html should be saved inside templates folder

and the code template.html should look like


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Flask Image Example</title>

</head>

<body>

    <h1>{{ result }}</h1>


    <!-- Display the image from the static folder -->

    <img src="{{ url_for('static', filename='bg.jpg') }}" alt="Your Image">

</body>

</html>


The image bg.jpg should be saved inside 'static' folder