Andi5986 commited on
Commit
c2999dd
·
1 Parent(s): 123db68

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ from sklearn.linear_model import LinearRegression
5
+ import random
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+
9
+ st.title('Oracle Function Simulation')
10
+
11
+ # Oracle function
12
+ def 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_complexity
16
+ + weights[1] * ether_price
17
+ + weights[2] * active_users
18
+ + weights[3] * solved_tasks
19
+ + weights[4] * unsolved_tasks
20
+ + weights[5] * user_kpis
21
+ + weights[6] * service_level_agreements
22
+ )
23
+
24
+ # Get historical data for Ether
25
+ url = "https://api.coingecko.com/api/v3/coins/ethereum/market_chart"
26
+ params = {"vs_currency": "usd", "days": "1095"} # 1095 days is approximately 3 years
27
+ response = requests.get(url, params=params)
28
+ data = response.json()
29
+
30
+ # Convert the price data to a Pandas DataFrame
31
+ df = pd.DataFrame(data['prices'], columns=['time', 'price'])
32
+ df['time'] = pd.to_datetime(df['time'], unit='ms')
33
+
34
+ # Generate mock data for the oracle function and simulate the last 3 years
35
+ oracle_outputs = []
36
+ variables = {'task_complexity': [], 'ether_price': [], 'active_users': [], 'solved_tasks': [], 'unsolved_tasks': [], 'user_kpis': [], 'service_level_agreements': []}
37
+ for _ 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 price
55
+ model = LinearRegression()
56
+ model.fit(df['price'].values.reshape(-1, 1), oracle_outputs)
57
+
58
+ # Resample the price data to monthly data and calculate average price for each month
59
+ df['oracle_output'] = oracle_outputs
60
+ df.set_index('time', inplace=True)
61
+ monthly_df = df.resample('M').mean()
62
+
63
+ # Predict the oracle output for each average monthly price
64
+ monthly_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 time
67
+ st.subheader('Predicted Oracle Output and Ether Price Over Time')
68
+ st.line_chart(monthly_df[['predicted_oracle_output', 'price']])
69
+
70
+ # Display a scatter plot with linear relation between Predicted Oracle output and Ether price
71
+ st.subheader('Predicted Oracle output vs Ether price')
72
+ plt.figure(figsize=(8,6))
73
+ plt.scatter(monthly_df['predicted_oracle_output'], monthly_df['price'])
74
+ m, b = np.polyfit(monthly_df['predicted_oracle_output'], monthly_df['price'], 1)
75
+ plt.plot(monthly_df['predicted_oracle_output'], m*monthly_df['predicted_oracle_output'] + b, color='red')
76
+ plt.xlabel('Predicted Oracle Output')
77
+ plt.ylabel('Ether Price')
78
+ st.pyplot(plt)
79
+
80
+ # Display tables showing average values of the variables over time
81
+ st.subheader('Average Values of the Variables Over Time')
82
+ for var in variables:
83
+ st.write(f"{var}: {sum(variables[var])/len(variables[var])}")
84
+