Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import streamlit as stimport requestsimport pandas as pdfrom sklearn.linear_model import LinearRegressionimport randomimport matplotlib.pyplot as pltimport numpy as np
st.title('Oracle Function Simulation')
# Oracle functiondef oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements): weights = [random.random() for _ in range(7)] return ( weights[0] * task_complexity + weights[1] * ether_price + weights[2] * active_users + weights[3] * solved_tasks + weights[4] * unsolved_tasks + weights[5] * user_kpis + weights[6] * service_level_agreements )
# Get historical data for Etherurl = "https://api.coingecko.com/api/v3/coins/ethereum/market_chart"params = {"vs_currency": "usd", "days": "1095"} # 1095 days is approximately 3 yearsresponse = requests.get(url, params=params)data = response.json()
# Convert the price data to a Pandas DataFramedf = pd.DataFrame(data['prices'], columns=['time', 'price'])df['time'] = pd.to_datetime(df['time'], unit='ms')
# Generate mock data for the oracle function and simulate the last 3 yearsoracle_outputs = []variables = {'task_complexity': [], 'ether_price': [], 'active_users': [], 'solved_tasks': [], 'unsolved_tasks': [], 'user_kpis': [], 'service_level_agreements': []}for _ in range(len(df)): task_complexity = random.randint(1, 10) active_users = random.randint(1, 10000) solved_tasks = random.randint(1, 1000) unsolved_tasks = random.randint(1, 1000) user_kpis = random.uniform(0.1, 1) service_level_agreements = random.uniform(0.1, 1) ether_price = df.iloc[_]['price'] oracle_outputs.append(oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements)) variables['task_complexity'].append(task_complexity) variables['ether_price'].append(ether_price) variables['active_users'].append(active_users) variables['solved_tasks'].append(solved_tasks) variables['unsolved_tasks'].append(unsolved_tasks) variables['user_kpis'].append(user_kpis) variables['service_level_agreements'].append(service_level_agreements)
# Train a linear regression model to adjust the oracle output based on Ether pricemodel = LinearRegression()model.fit(df['price'].values.reshape(-1, 1), oracle_outputs)
# Resample the price data to monthly data and calculate average price for each monthdf['oracle_output'] = oracle_outputsdf.set_index('time', inplace=True)monthly_df = df.resample('M').mean()
# Predict the oracle output for each average monthly pricemonthly_df['predicted_oracle_output'] = model.predict(monthly_df['price'].values.reshape(-1, 1))
# Display a line chart of the predicted oracle output and Ether price over timest.subheader('Predicted Oracle Output and Ether Price Over Time')st.line_chart(monthly_df[['predicted_oracle_output', 'price']])
# Display a scatter plot with linear relation between Predicted Oracle output and Ether pricest.subheader('Predicted Oracle output vs Ether price')plt.figure(figsize=(8,6))plt.scatter(monthly_df['predicted_oracle_output'], monthly_df['price'])m, b = np.polyfit(monthly_df['predicted_oracle_output'], monthly_df['price'], 1)plt.plot(monthly_df['predicted_oracle_output'], m*monthly_df['predicted_oracle_output'] + b, color='red')plt.xlabel('Predicted Oracle Output')plt.ylabel('Ether Price')st.pyplot(plt)
# Display tables showing average values of the variables over timest.subheader('Average Values of the Variables Over Time')for var in variables: st.write(f"{var}: {sum(variables[var])/len(variables[var])}")
app.pyAdded+84