-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
232 lines (189 loc) · 8.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import streamlit as st
from PIL import Image
import pandas as pd
import openai
import plotly.express as px
# Local imports
from subgraphs.subgraph import SubGraph
from nlp_models.gpt import GPT
from nlp_models.gpt_training import add_graphQL_examples
from metamask_component import metamask_component
from keys.private_keys import OPENAI_PRIVATE_KEY, THEGRAPH_API_KEY
from etherscan.etherscan_data import Etherscan_scan
# -----------------------------------------------------------
# Aux Functions
# -----------------------------------------------------------
def clean_query(raw_query):
subgraph = raw_query.split("{")[0]
query = raw_query.split(subgraph)[-1].replace("'", '"')
return subgraph, query
def query_subgraph(raw_query):
# GPT3 part missing! add it here plz
subgraph, query = clean_query(raw_query)
subgraph = SubGraph(subgraph, api_key=THEGRAPH_API_KEY)
return subgraph.run_query(query), query
def prettify_json(ugly_json):
for data_point in ugly_json["data"]:
st.subheader(data_point)
if isinstance(ugly_json["data"][data_point], list):
pretty_df = pd.DataFrame(ugly_json["data"][data_point])
st.dataframe(pretty_df)
st.download_button(
"Download", pretty_df.to_csv(index=False), mime="text/csv"
)
else:
st.write(ugly_json["data"][data_point])
# CSS
with open("styles/style.css", "r") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
@st.cache
def load_gpt3_model():
openai.api_key = OPENAI_PRIVATE_KEY
gpt = GPT(engine="davinci", temperature=0.5, max_tokens=100)
add_graphQL_examples(gpt)
return gpt
def json_to_df(raw_json):
df = pd.DataFrame(columns=["token","similar_symbol","percentage"])
for x in raw_json.keys():
pre = {}
pre["token"] = x
for y in raw_json[x].keys():
if y == "percentage":
raw_json[x][y] = round( float(raw_json[x][y] * 100),2)
pre[y] = raw_json[x][y]
df = df.append(pre, ignore_index=True)
df = df.rename(columns={"percentage": "similarity", "token": "token_owner", "similar_symbol": "similar_token"})
return df
# -----------------------------------------------------------
# Main Section
# -----------------------------------------------------------
# GPT
gpt = load_gpt3_model()
# Metamask
value = metamask_component(account_results="hello there")
subgraph_response = {}
query = ""
actual_query = ""
image = Image.open(
"/Users/gerardogodfreyc/Documents/Proyectos/ETH_Hackaton/eth-mexico/media/waphl_logo.png"
)
st.image(image, use_column_width=True)
st.markdown("## Search:")
text_query = st.text_input(
label="",
placeholder="give me the id, exact, decimals and simple for the first 7 metrics in the mStable protocol subgraph",
)
query_action = st.button("Run Search")
if query_action: # if pressed
if value:
#raw_query = gpt.submit_request(text_query).choices[0].text.split("output: ")[-1] # gpt
raw_query = "uniswap-v2{pairs(first: 9, where: {reserveETH_gt: '100000'}, orderBy: reserveETH, orderDirection: desc) {reserveUSD}}"
subgraph_response, actual_query = query_subgraph(raw_query)
st.code(actual_query)
results_tab, json_results_tab, query_code_tab = st.tabs(["Results", "Code Results", "Query"])
# Tabs section
with results_tab:
if subgraph_response:
prettify_json(subgraph_response)
# We need to make the prettify_json func
with json_results_tab:
st.json(subgraph_response)
with query_code_tab:
st.code(actual_query)
else:
st.error('Connect the wallet', icon="🚨")
#Balance
with st.expander("Balance and recomenndations"):
if value :
st.markdown("### Wallet:")
st.write(value)
c1 = Etherscan_scan(value,testing=True)
st.markdown("### Balance:")
st.write(c1.get_json_balance())
st.markdown("### Recomendation:")
raw_json = {
'GEL': {'similar_symbol': 'APE', 'percentage': 0.39872121810913086},
'SUSHI': {'similar_symbol': 'UNI', 'percentage': 0.5800848007202148},
'SNX': {'similar_symbol': 'HEX', 'percentage': 0.34170082211494446},
'DAI': {'similar_symbol': 'APE', 'percentage': 0.5156382918357849},
'APE': {'similar_symbol': 'DAI', 'percentage': 0.5156382918357849}
}
df_data = json_to_df(raw_json)
st.markdown("#### Description similarity:")
table, plot,recommendation = st.tabs(["table", "plot","recommendation"])
with table:
st.table(data= df_data)
with plot:
fig = px.bar(df_data, x='token_owner', y='similarity', color='similar_token')
st.plotly_chart(fig)
with recommendation:
df_data = df_data.sort_values(by='similarity', ascending=False)
row_1=df_data.iloc[0]
a = row_1['token_owner']
b = row_1['similar_token']
c = row_1['similarity']
dont = ["HEX","BNB","USDT","SHIBA","APE","WBTC","Matic","DAI"]
st.write(f"For this model, we find that the cryptocurrency {a} you have is a {c} similar to {b}.")
if b in dont:
st.write(f"{b} It does not have a subgraph in The graph !!! Look for browsing in it on this page:")
st.write("https://thegraph.com/docs/en/cookbook/quick-start/")
else:
st.write(f"{b} It does have a subgraph in The graph !!! Look for browsing in it on this page ")
raw_json2 = {
'GEL': {'similar_symbol': 'LDO', 'percentage': 0.21330898721810916},
'SUSHI': {'similar_symbol': 'CRV', 'percentage': 0.44848007202148},
'SNX': {'similar_symbol': 'COMP', 'percentage': 0.00770082211494446},
'DAI': {'similar_symbol': 'USDC', 'percentage': 0.3575156382918849},
'APE': {'similar_symbol': 'POOL', 'percentage': 0.6156382918357849}
}
df_data2 = json_to_df(raw_json2)
st.markdown("#### Similar Protocol:")
table, plot,recommendation = st.tabs(["table", "plot","recommendation"])
with table:
st.table(data= df_data2)
with plot:
fig = px.bar(df_data2, x='token_owner', y='similarity', color='similar_token')
st.plotly_chart(fig)
with recommendation:
df_data2 = df_data2.sort_values(by='similarity', ascending=False)
row_1=df_data2.iloc[0]
a = row_1['token_owner']
b = row_1['similar_token']
c = row_1['similarity']
dont = ["HEX","BNB","USDT","SHIBA","APE","WBTC","Matic","DAI"]
st.write(f"For this model, we find that the cryptocurrency {a} you have is a {c} similar to {b}.")
if b in dont:
st.write(f"{b} It does not have a subgraph in The graph !!! Look for browsing in it on this page:")
st.write("https://thegraph.com/docs/en/cookbook/quick-start/")
else:
st.write(f"{b} It does have a subgraph in The graph !!! Look for browsing in it on this page ")
raw_json3 = {
'GEL': {'similar_symbol': 'BAL', 'percentage': 0.67239891308121810},
'SUSHI': {'similar_symbol': 'COMP', 'percentage': 0.4720214805800808},
'SNX': {'similar_symbol': 'MANA', 'percentage': 0.37149400824121446},
'DAI': {'similar_symbol': 'SNX', 'percentage': 0.2951818357563849},
'APE': {'similar_symbol': 'BNB', 'percentage': 0.5235781569183849}
}
df_data3 = json_to_df(raw_json3)
st.markdown("#### Social Trends :")
table, plot,recommendation = st.tabs(["table", "plot","recommendation"])
with table:
st.table(data= df_data3)
with plot:
fig = px.bar(df_data3, x='token_owner', y='similarity', color='similar_token')
st.plotly_chart(fig)
with recommendation:
df_data3 = df_data3.sort_values(by='similarity', ascending=False)
row_1=df_data3.iloc[0]
a = row_1['token_owner']
b = row_1['similar_token']
c = row_1['similarity']
dont = ["HEX","BNB","USDT","SHIBA","APE","WBTC","Matic","DAI"]
st.write(f"For this model, we find that the cryptocurrency {a} you have is a {c} similar to {b}.")
if b in dont:
st.write(f"{b} It does not have a subgraph in The graph !!! Look for browsing in it on this page:")
st.write("https://thegraph.com/docs/en/cookbook/quick-start/")
else:
st.write(f"{b} It does have a subgraph in The graph !!! Look for browsing in it on this page ")
else:
st.write("Sin datos actualizados")