# A simple Linear Regression example for Celsius to Fahrenheit conversion with TensorFlow import tensorflow as tf import numpy as np import streamlit as st import matplotlib.pyplot as plt # Streamlit UI st.title('Celsius to Fahrenheit Conversion with TensorFlow') # Define the model model = tf.keras.Sequential([ tf.keras.layers.Dense(units=1, input_shape=[1]) ]) # Compile the model with an optimizer and loss function model.compile(optimizer='sgd', loss='mse') # Training data (Celsius to Fahrenheit) celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float) # Celsius fahrenheit = np.array([-40, 14, 32, 46.4, 59, 71.6, 100.4], dtype=float) # Corresponding Fahrenheit # Display example input and output st.write("Example Celsius values (input):", celsius) st.write("Corresponding Fahrenheit values (output):", fahrenheit) # User input for the Celsius value to predict Fahrenheit input_celsius = st.number_input('Enter Celsius value:', value=0.0, format="%.1f") # User input for epochs epochs = st.sidebar.slider("Number of epochs", 10, 500, 10) # Button to train the model and make prediction if st.button('Train Model and Predict Fahrenheit'): with st.spinner('Training...'): model.fit(celsius, fahrenheit, epochs=epochs) st.success('Training completed!') # Make prediction predicted_fahrenheit = model.predict([input_celsius]) st.write(f'For input of {input_celsius}°C, the predicted Fahrenheit value is {predicted_fahrenheit[0][0]:.1f}°F') # Predictions for visualization predictions = model.predict(celsius) # Plotting plt.scatter(celsius, fahrenheit, label='Actual Conversion') plt.plot(celsius, predictions, color='red', label='Predicted Conversion') plt.xlabel('Celsius') plt.ylabel('Fahrenheit') plt.legend() st.pyplot(plt)