Skip to content

Instantly share code, notes, and snippets.

View rian-dolphin's full-sized avatar

Rian Dolphin rian-dolphin

  • University College Dublin
  • Dublin, Ireland
View GitHub Profile
def extract_dict_from_json_llm_response(text):
text = "{"+"{".join([xi.strip() for xi in text.split("{")[1:]])
text = "}".join([xi.strip() for xi in text.split("}")[:-1]])+"}"
return eval(text)
def extract_python_dict_from_response(text):
"""
LLM response formatted as python dicts.
Args:
#-- Find index of max sharpe portfolio
max_sharpe_index = np.argmax(mean_variance_pairs[:,0]/mean_variance_pairs[:,1]**0.5)
#-- Add max sharpe portfolio as a seperate point to the plot
fig.add_trace(go.Scatter(x=mean_variance_pairs[max_sharpe_index,1]**0.5, y=mean_variance_pairs[max_sharpe_index,0],
marker=dict(color='red', size=14),
mode='markers'))
# -- Create the figure in Plotly
fig = px.scatter(
filtered_df,
x="gdpPercap",
y="lifeExp",
size="pop",
color="continent",
hover_name="country",
log_x=log_x_choice,
size_max=60,
# -- Read in the data
df = px.data.gapminder()
# -- Apply the year filter given by the user
filtered_df = df[(df.year == year_choice)]
# -- Apply the continent filter
if continent_choice != "All":
filtered_df = filtered_df[filtered_df.continent == continent_choice]
# -- Get the user input
year_col, continent_col, log_x_col = st.columns([5, 5, 5])
with year_col:
year_choice = st.slider(
"What year would you like to examine?",
min_value=1952,
max_value=2007,
step=5,
value=2007,
)
# -- Create three columns
col1, col2, col3 = st.columns([5, 5, 20])
# -- Put the image in the middle column
# - Commented out here so that the file will run without having the image downloaded
# with col2:
# st.image("streamlit.png", width=200)
# -- Put the title in the last column
with col3:
st.title("Streamlit Demo")
# -- We use the first column here as a dummy to add a space to the left
import streamlit as st
import plotly.express as px
@rian-dolphin
rian-dolphin / streamlit_app.py
Last active June 30, 2023 08:22
Streamlit Demo
import streamlit as st
import plotly.express as px
st.set_page_config(layout="wide")
# -- Create three columns
col1, col2, col3 = st.columns([5, 5, 20])
# -- Put the image in the middle column
# - Commented out here so that the file will run without having the image downloaded
# with col2:
x = [dt.datetime.date(d) for d in df.index]
fig = plt.figure(figsize=(10,5))
plt.title('Walmart Quarterly Earnings')
plt.ylabel('Earnings (Billions)')
plt.grid(True)
plt.plot(x[:-len(predictions)],
df.Earnings[:-len(predictions)],
"b-")
plt.plot(x[-len(predictions):],
df.Earnings[-len(predictions):],
model.eval()
with torch.no_grad():
predictions, _ = model(train_scaled[-train_periods:], None)
#-- Apply inverse transform to undo scaling
predictions = scaler.inverse_transform(np.array(predictions.reshape(-1,1)))