Init Repo
Browse files- Home.py +16 -0
- pages/1_FitInOne.py +118 -0
- pages/2_Chatbot.py +44 -0
- requirements.txt +5 -0
Home.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.set_page_config(
|
| 4 |
+
page_title="Hello",
|
| 5 |
+
page_icon="👋",
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
st.write("# Welcome to SPACE! 👋")
|
| 9 |
+
|
| 10 |
+
st.sidebar.success("Select a demo above.")
|
| 11 |
+
|
| 12 |
+
st.markdown(
|
| 13 |
+
"""
|
| 14 |
+
Hello, a simple demo from SPACE.
|
| 15 |
+
"""
|
| 16 |
+
)
|
pages/1_FitInOne.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from sklearn.metrics import r2_score
|
| 6 |
+
|
| 7 |
+
st.title("Fit Your Data")
|
| 8 |
+
|
| 9 |
+
default_data = {
|
| 10 |
+
"X": [1, 2, 3, 4, 5],
|
| 11 |
+
"Y": [2.2, 4.4, 6.5, 8.0, 10.1],
|
| 12 |
+
"Select": [True, True, True, True, True]
|
| 13 |
+
}
|
| 14 |
+
data = pd.DataFrame(default_data)
|
| 15 |
+
|
| 16 |
+
with st.sidebar:
|
| 17 |
+
st.subheader("Enter Your Data")
|
| 18 |
+
user_data = st.data_editor(data, num_rows="dynamic", key="data_editor")
|
| 19 |
+
fit_type = st.radio(
|
| 20 |
+
"Choose the Type of Fit",
|
| 21 |
+
options=["Logarithmic", "Linear", "Linearithmic", "Quadratic", "Cubic", "Exponential"],
|
| 22 |
+
index=0
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
selected_data = user_data[user_data["Select"]]
|
| 27 |
+
x = np.array(selected_data["X"], dtype=float)
|
| 28 |
+
y = np.array(selected_data["Y"], dtype=float)
|
| 29 |
+
|
| 30 |
+
if len(x) < 2 and len(y) < 2:
|
| 31 |
+
st.warning("Please enter at least 2 data points.")
|
| 32 |
+
st.stop()
|
| 33 |
+
except ValueError:
|
| 34 |
+
st.error("Invalid data entered. Please ensure all values are numeric.")
|
| 35 |
+
st.stop()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
if fit_type == "Logarithmic":
|
| 39 |
+
try:
|
| 40 |
+
log_x = np.log(x)
|
| 41 |
+
coefficients = np.polyfit(log_x, y , 1)
|
| 42 |
+
y_fit = coefficients[0] * log_x + coefficients[1]
|
| 43 |
+
r2 = r2_score(y, y_fit)
|
| 44 |
+
equation = f"y = {coefficients[0]:.2f}*log(x) + {coefficients[1]:.2f}"
|
| 45 |
+
except ValueError:
|
| 46 |
+
st.error("Logarithmic fit failed. Ensure all X values are positive.")
|
| 47 |
+
st.stop()
|
| 48 |
+
|
| 49 |
+
elif fit_type == "Linear":
|
| 50 |
+
degree = 1
|
| 51 |
+
coefficients = np.polyfit(x, y, degree)
|
| 52 |
+
y_fit = np.polyval(coefficients, x)
|
| 53 |
+
r2 = r2_score(y, y_fit)
|
| 54 |
+
equation = f"y = {coefficients[0]:.2f}*x + {coefficients[1]:.2f}"
|
| 55 |
+
|
| 56 |
+
elif fit_type == "Linearithmic":
|
| 57 |
+
try:
|
| 58 |
+
x_log_x = x * np.log(x)
|
| 59 |
+
A = np.column_stack((x_log_x, x, np.ones_like(x)))
|
| 60 |
+
coefficients, _, _, _ = np.linalg.lstsq(A, y, rcond=None)
|
| 61 |
+
a, b, c = coefficients
|
| 62 |
+
y_fit = a * x_log_x + b * x + c
|
| 63 |
+
r2 = r2_score(y, y_fit)
|
| 64 |
+
equation = f"y = {a:.2f}*x*log(x) + {b:.2f}*x + {c:.2f}"
|
| 65 |
+
except ValueError:
|
| 66 |
+
st.error("Linearithmic fir failed. Ensure all X values are positive.")
|
| 67 |
+
st.stop()
|
| 68 |
+
|
| 69 |
+
elif fit_type == "Quadratic":
|
| 70 |
+
degree = 2
|
| 71 |
+
coefficients = np.polyfit(x, y, degree)
|
| 72 |
+
y_fit = np.polyval(coefficients, x)
|
| 73 |
+
r2 = r2_score(y, y_fit)
|
| 74 |
+
equation = f"y = {coefficients[0]:.2f}*x² + {coefficients[1]:.2f}*x + {coefficients[2]:.2f}"
|
| 75 |
+
|
| 76 |
+
elif fit_type == "Cubic":
|
| 77 |
+
degree = 3
|
| 78 |
+
coefficients = np.polyfit(x, y, degree)
|
| 79 |
+
y_fit = np.polyval(coefficients, x)
|
| 80 |
+
r2 = r2_score(y, y_fit)
|
| 81 |
+
equation = f"y = {coefficients[0]:.2f}*x³ + {coefficients[1]:.2f}*x² + {coefficients[2]:.2f}*x + {coefficients[3]:.2f}"
|
| 82 |
+
|
| 83 |
+
elif fit_type == "Exponential":
|
| 84 |
+
try:
|
| 85 |
+
log_y = np.log(y)
|
| 86 |
+
coefficients = np.polyfit(x, log_y, 1)
|
| 87 |
+
a = np.exp(coefficients[1])
|
| 88 |
+
b = coefficients[0]
|
| 89 |
+
y_fit = a * np.exp(b * x)
|
| 90 |
+
r2 = r2_score(y, y_fit)
|
| 91 |
+
equation = f"y = {a:.2f}*exp({b:.2f}*x)"
|
| 92 |
+
except ValueError:
|
| 93 |
+
st.error("Exponential fit failed. Ensure all Y values are positive.")
|
| 94 |
+
st.stop()
|
| 95 |
+
|
| 96 |
+
x_smooth = np.linspace(min(x), max(x), 500)
|
| 97 |
+
if fit_type == "Logarithmic":
|
| 98 |
+
y_smooth = coefficients[0] * np.log(x_smooth) + coefficients[1]
|
| 99 |
+
elif fit_type == "Linearithmic":
|
| 100 |
+
y_smooth = a * x_smooth * np.log(x_smooth) + b * x_smooth + c
|
| 101 |
+
elif fit_type == "Exponential":
|
| 102 |
+
y_smooth = a * np.exp(b * x_smooth)
|
| 103 |
+
else:
|
| 104 |
+
y_smooth = np.polyval(coefficients, x_smooth)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
fig, ax = plt.subplots()
|
| 108 |
+
ax.scatter(x, y, color="red", label="Original Data")
|
| 109 |
+
ax.plot(x_smooth, y_smooth, color="blue", label=f"{fit_type} Fit (R²={r2:.2f})")
|
| 110 |
+
ax.set_xlabel("X-axis")
|
| 111 |
+
ax.set_ylabel("Y-axis")
|
| 112 |
+
ax.legend()
|
| 113 |
+
ax.set_title("Fit")
|
| 114 |
+
|
| 115 |
+
st.pyplot(fig)
|
| 116 |
+
|
| 117 |
+
st.write(f"**Fitted Equation**: {equation}")
|
| 118 |
+
st.write(f"**R² Value**: {r2:.4f}")
|
pages/2_Chatbot.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
with st.sidebar:
|
| 5 |
+
IFC_API_KEY = st.text_input("HF access tokens", key="chat_bot_api_key", type="password")
|
| 6 |
+
model = st.radio(
|
| 7 |
+
"Choose a model to chat with",
|
| 8 |
+
["Qwen/Qwen2.5-72B-Instruct", "meta-llama/Llama-3.3-70B-Instruct", "Qwen/QwQ-32B-Preview"],
|
| 9 |
+
captions=[
|
| 10 |
+
"By Qwen",
|
| 11 |
+
"By Meta",
|
| 12 |
+
"By Qwen",
|
| 13 |
+
],
|
| 14 |
+
)
|
| 15 |
+
temperature = st.slider("Temperature", 0.01, 0.99, 0.5)
|
| 16 |
+
top_p = st.slider("Top_p", 0.01, 0.99, 0.7)
|
| 17 |
+
max_tokens = st.slider("Max Tokens", 128, 4096, 2048)
|
| 18 |
+
|
| 19 |
+
st.title("💬 Chatbot")
|
| 20 |
+
st.caption(" A HF chatbot powered by HF")
|
| 21 |
+
if "messages" not in st.session_state:
|
| 22 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you?"}]
|
| 23 |
+
|
| 24 |
+
for msg in st.session_state.messages:
|
| 25 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 26 |
+
|
| 27 |
+
if prompt := st.chat_input():
|
| 28 |
+
if not IFC_API_KEY:
|
| 29 |
+
st.info("Please add your access token to continue.")
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
client = OpenAI(api_key=IFC_API_KEY)
|
| 33 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 34 |
+
st.chat_message("user").write(prompt)
|
| 35 |
+
response = client.chat.completions.create(
|
| 36 |
+
model=model,
|
| 37 |
+
messages = st.session_state.messages,
|
| 38 |
+
temperature = temperature,
|
| 39 |
+
max_tokens = max_tokens,
|
| 40 |
+
top_p = top_p,
|
| 41 |
+
)
|
| 42 |
+
msg = response.choices[0].message.content
|
| 43 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
| 44 |
+
st.chat_message("assistant").write(msg)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
matplotlib
|
| 3 |
+
numpy
|
| 4 |
+
scikitlearn
|
| 5 |
+
openai
|