Andi5986/Oracle
0
1import streamlit as st2import requests3import pandas as pd4from sklearn.linear_model import LinearRegression5import random6import matplotlib.pyplot as plt7import numpy as np8 9st.title('Oracle Function Simulation')10 11# Oracle function12def oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements):13 weights = [random.random() for _ in range(7)]14 return (15 weights[0] * task_complexity16 + weights[1] * ether_price17 + weights[2] * active_users18 + weights[3] * solved_tasks19 + weights[4] * unsolved_tasks20 + weights[5] * user_kpis21 + weights[6] * service_level_agreements22 )23 24# Get historical data for Ether25url = "https://api.coingecko.com/api/v3/coins/ethereum/market_chart"26params = {"vs_currency": "usd", "days": "1095"} # 1095 days is approximately 3 years27response = requests.get(url, params=params)28data = response.json()29 30# Convert the price data to a Pandas DataFrame31df = pd.DataFrame(data['prices'], columns=['time', 'price'])32df['time'] = pd.to_datetime(df['time'], unit='ms')33 34# Generate mock data for the oracle function and simulate the last 3 years35oracle_outputs = []36variables = {'task_complexity': [], 'ether_price': [], 'active_users': [], 'solved_tasks': [], 'unsolved_tasks': [], 'user_kpis': [], 'service_level_agreements': []}37for _ in range(len(df)):38 task_complexity = random.randint(1, 10)39 active_users = random.randint(1, 10000)40 solved_tasks = random.randint(1, 1000)41 unsolved_tasks = random.randint(1, 1000)42 user_kpis = random.uniform(0.1, 1)43 service_level_agreements = random.uniform(0.1, 1)44 ether_price = df.iloc[_]['price']45 oracle_outputs.append(oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements))46 variables['task_complexity'].append(task_complexity)47 variables['ether_price'].append(ether_price)48 variables['active_users'].append(active_users)49 variables['solved_tasks'].append(solved_tasks)50 variables['unsolved_tasks'].append(unsolved_tasks)51 variables['user_kpis'].append(user_kpis)52 variables['service_level_agreements'].append(service_level_agreements)53 54# Train a linear regression model to adjust the oracle output based on Ether price55model = LinearRegression()56model.fit(df['price'].values.reshape(-1, 1), oracle_outputs)57 58# Resample the price data to monthly data and calculate average price for each month59df['oracle_output'] = oracle_outputs60df.set_index('time', inplace=True)61monthly_df = df.resample('M').mean()62 63# Predict the oracle output for each average monthly price64monthly_df['predicted_oracle_output'] = model.predict(monthly_df['price'].values.reshape(-1, 1))65 66# Display a line chart of the predicted oracle output and Ether price over time67st.subheader('Predicted Oracle Output and Ether Price Over Time')68st.line_chart(monthly_df[['predicted_oracle_output', 'price']])69 70# Display a scatter plot with linear relation between Predicted Oracle output and Ether price71st.subheader('Predicted Oracle output vs Ether price')72plt.figure(figsize=(8,6))73plt.scatter(monthly_df['predicted_oracle_output'], monthly_df['price'])74m, b = np.polyfit(monthly_df['predicted_oracle_output'], monthly_df['price'], 1)75plt.plot(monthly_df['predicted_oracle_output'], m*monthly_df['predicted_oracle_output'] + b, color='red')76plt.xlabel('Predicted Oracle Output')77plt.ylabel('Ether Price')78st.pyplot(plt)79 80# Display tables showing average values of the variables over time81st.subheader('Average Values of the Variables Over Time')82for var in variables:83 st.write(f"{var}: {sum(variables[var])/len(variables[var])}")84 