Upload 2 files
Browse filesFiles changed (2)
- oracle.pyAdded+83
- requirements.txtAdded+52
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import 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])}")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152altair==5.0.1attrs==23.1.0blinker==1.6.2cachetools==5.3.1certifi==2023.5.7charset-normalizer==3.1.0click==8.1.3contourpy==1.0.7cycler==0.11.0decorator==5.1.1fonttools==4.39.4gitdb==4.0.10GitPython==3.1.31idna==3.4importlib-metadata==6.6.0Jinja2==3.1.2jsonschema==4.17.3kiwisolver==1.4.4markdown-it-py==2.2.0MarkupSafe==2.1.3matplotlib==3.7.1mdurl==0.1.2numpy==1.24.3packaging==23.1pandas==2.0.2Pillow==9.5.0protobuf==4.23.2pyarrow==12.0.0pydeck==0.8.1b0Pygments==2.15.1Pympler==1.0.1pyparsing==3.0.9pyrsistent==0.19.3python-dateutil==2.8.2pytz==2023.3pytz-deprecation-shim==0.1.0.post0requests==2.31.0rich==13.4.1six==1.16.0sklearn==0.0.post5smmap==5.0.0streamlit==1.23.1tenacity==8.2.2toml==0.10.2toolz==0.12.0tornado==6.3.2typing_extensions==4.6.3tzdata==2023.3tzlocal==4.3urllib3==2.0.3validators==0.20.0zipp==3.15.0oracle.pyAdded+83
requirements.txtAdded+52