|
| 1 | +import json |
| 2 | +import asyncio |
| 3 | +import numpy as np |
| 4 | +from redis import Redis |
| 5 | +from datetime import datetime, timedelta |
| 6 | +from news_fetcher import fetch_and_normalize_news_data |
| 7 | +from weight_calculator import calculate_weight |
| 8 | +from embedding_generator import generate_embedding |
| 9 | +from features_dataset_initializer import initialize_features_dataset |
| 10 | +from features_predictor import predict_growth |
| 11 | + |
| 12 | + |
| 13 | +def get_key(article): |
| 14 | + return f"article::{article['id']}" |
| 15 | + |
| 16 | + |
| 17 | +def get_article(r, key): |
| 18 | + return json.loads(r.execute_command("JSON.GET", key)) |
| 19 | + |
| 20 | + |
| 21 | +def set_article_field(r, key, field, value): |
| 22 | + return r.execute_command("JSON.SET", key, f"$.{field}", value) |
| 23 | + |
| 24 | + |
| 25 | +async def update_article_weight(r, key): |
| 26 | + article = get_article(r, key) |
| 27 | + weight = calculate_weight(article) |
| 28 | + set_article_field(r, key, "weight", weight) |
| 29 | + |
| 30 | + |
| 31 | +async def update_article_embedding(r, key): |
| 32 | + article = get_article(r, key) |
| 33 | + content = article.get("title", "") |
| 34 | + embedding = str(generate_embedding(content).tolist()) |
| 35 | + set_article_field(r, key, "embedding", embedding) |
| 36 | + |
| 37 | + |
| 38 | +def get_timestamp(days_ago=10): |
| 39 | + return int((datetime.now() - timedelta(days=days_ago)).timestamp()) |
| 40 | + |
| 41 | + |
| 42 | +def create_index(r): |
| 43 | + return r.execute_command( |
| 44 | + "FT.CREATE news_idx ON JSON PREFIX 1 article:: SCHEMA $.id AS id NUMERIC $.published_at AS published_at NUMERIC SORTABLE $.votes.negative AS votes_negative NUMERIC $.votes.positive AS votes_positive NUMERIC $.votes.important AS votes_important NUMERIC $.currencies[*].code AS currency_code TAG $.weight AS weight NUMERIC" |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def create_news_vector_index(r): |
| 49 | + return r.execute_command( |
| 50 | + "FT.CREATE news_vector_idx ON JSON PREFIX 1 article:: SCHEMA $.id AS id NUMERIC $.published_at AS published_at NUMERIC SORTABLE $.title AS title TEXT $.embedding AS embedding VECTOR FLAT 6 TYPE FLOAT32 DIM 384 DISTANCE_METRIC COSINE" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def create_features_index(r): |
| 55 | + return r.execute_command( |
| 56 | + "FT.CREATE features_idx ON JSON PREFIX 1 week:: SCHEMA $.currency_code AS currency_code TAG $.features AS features VECTOR FLAT 6 TYPE FLOAT32 DIM 5 DISTANCE_METRIC COSINE $.label AS label NUMERIC" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +async def get_most_mentioned_cryptocurrencies(r, from_timestamp, top=10): |
| 61 | + return r.execute_command( |
| 62 | + "FT.AGGREGATE", |
| 63 | + "news_idx", |
| 64 | + f"@published_at:[{from_timestamp} +inf]", |
| 65 | + "GROUPBY", |
| 66 | + 1, |
| 67 | + "@currency_code", |
| 68 | + "REDUCE", |
| 69 | + "COUNT", |
| 70 | + 0, |
| 71 | + "AS", |
| 72 | + "mentions_count", |
| 73 | + "SORTBY", |
| 74 | + 2, |
| 75 | + "@mentions_count", |
| 76 | + "DESC", |
| 77 | + "LIMIT", |
| 78 | + 0, |
| 79 | + top, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +async def get_cryptocurrency_recent_mentions(r, currency, from_timestamp, limit=10): |
| 84 | + return r.execute_command( |
| 85 | + "FT.SEARCH", |
| 86 | + "news_idx", |
| 87 | + f"@currency_code:{{{currency}}} @published_at:[{from_timestamp} +inf]", |
| 88 | + "SORTBY", |
| 89 | + "published_at", |
| 90 | + "DESC", |
| 91 | + "LIMIT", |
| 92 | + 0, |
| 93 | + limit, |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +async def get_cryptocurrency_votes(r, currency, from_timestamp): |
| 98 | + return r.execute_command( |
| 99 | + "FT.AGGREGATE", |
| 100 | + "news_idx", |
| 101 | + f"@currency_code:{{{currency}}} @published_at:[{from_timestamp} +inf]", |
| 102 | + "GROUPBY", |
| 103 | + 1, |
| 104 | + "@currency_code", |
| 105 | + "REDUCE", |
| 106 | + "SUM", |
| 107 | + 1, |
| 108 | + "@votes_positive", |
| 109 | + "AS", |
| 110 | + "total_positive_votes", |
| 111 | + "REDUCE", |
| 112 | + "SUM", |
| 113 | + 1, |
| 114 | + "@votes_negative", |
| 115 | + "AS", |
| 116 | + "total_negative_votes", |
| 117 | + "REDUCE", |
| 118 | + "SUM", |
| 119 | + 1, |
| 120 | + "@votes_important", |
| 121 | + "AS", |
| 122 | + "total_important_votes", |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +async def get_similar_articles(r, query_text, from_timestamp, top=10): |
| 127 | + query_embedding = generate_embedding(query_text).tobytes() |
| 128 | + return r.execute_command( |
| 129 | + "FT.SEARCH", |
| 130 | + "news_vector_idx", |
| 131 | + f"@published_at:[{from_timestamp} +inf] =>[KNN {top} @embedding $query AS score]", |
| 132 | + "PARAMS", |
| 133 | + 2, |
| 134 | + "query", |
| 135 | + query_embedding, |
| 136 | + "SORTBY", |
| 137 | + "score", |
| 138 | + "ASC", |
| 139 | + "LIMIT", |
| 140 | + 0, |
| 141 | + top, |
| 142 | + "RETURN", |
| 143 | + 3, |
| 144 | + "title", |
| 145 | + "published_at", |
| 146 | + "score", |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +def to_dict(result): |
| 151 | + if (len(result) < 2): |
| 152 | + return {} |
| 153 | + return {k.decode(): v.decode() for k, v in zip(result[1][::2], result[1][1::2])} |
| 154 | + |
| 155 | + |
| 156 | +async def compute_features(r, currency, from_timestamp): |
| 157 | + recent_mensions = await get_cryptocurrency_recent_mentions(r, currency, from_timestamp, 1000) |
| 158 | + |
| 159 | + count_news = float(recent_mensions[0]) |
| 160 | + |
| 161 | + votes = await get_cryptocurrency_votes(r, currency, from_timestamp) |
| 162 | + |
| 163 | + votes_dict = to_dict(votes) |
| 164 | + |
| 165 | + sum_positive = float(votes_dict.get("total_positive_votes", 0)) |
| 166 | + sum_negative = float(votes_dict.get("total_negative_votes", 0)) |
| 167 | + |
| 168 | + mean_positive = sum_positive / count_news if count_news > 0.0 else 0.0 |
| 169 | + mean_negative = sum_negative / count_news if count_news > 0.0 else 0.0 |
| 170 | + |
| 171 | + features = [count_news, sum_positive, sum_negative, mean_positive, mean_negative] #etc. |
| 172 | + return np.array(features, dtype=np.float32) |
| 173 | + |
| 174 | + |
| 175 | +async def cryptocurrency_will_grow(r, currency, from_timestamp): |
| 176 | + features = await compute_features(r, currency, from_timestamp) |
| 177 | + return await predict_growth(r, currency, features) |
| 178 | + |
| 179 | + |
| 180 | +async def main(): |
| 181 | + # Connect to DragonflyDB |
| 182 | + r = Redis(host="localhost", port=6379) |
| 183 | + |
| 184 | + r.execute_command("FLUSHALL") |
| 185 | + |
| 186 | + news_data = fetch_and_normalize_news_data() |
| 187 | + |
| 188 | + # Store the news data in DragonflyDB |
| 189 | + for article in news_data: |
| 190 | + key = get_key(article) |
| 191 | + value = json.dumps(article) |
| 192 | + r.execute_command("JSON.SET", key, "$", value) |
| 193 | + |
| 194 | + for article in news_data: |
| 195 | + key = get_key(article) |
| 196 | + await update_article_weight(r, key) |
| 197 | + |
| 198 | + for article in news_data: |
| 199 | + key = get_key(article) |
| 200 | + await update_article_embedding(r, key) |
| 201 | + |
| 202 | + create_index(r) |
| 203 | + create_news_vector_index(r) |
| 204 | + |
| 205 | + timestamp_ten_days_ago = 1700000000 |
| 206 | + |
| 207 | + # Get the most mentioned cryptocurrencies |
| 208 | + print("Most mentioned cryptocurrencies:") |
| 209 | + print(await get_most_mentioned_cryptocurrencies(r, timestamp_ten_days_ago)) |
| 210 | + print() |
| 211 | + |
| 212 | + # Get recent mentions of a cryptocurrency |
| 213 | + print("Recent mentions of BTC:") |
| 214 | + print(await get_cryptocurrency_recent_mentions(r, "BTC", timestamp_ten_days_ago, 2)) |
| 215 | + print() |
| 216 | + |
| 217 | + # Get votes for a cryptocurrency |
| 218 | + print("Votes for BTC:") |
| 219 | + print(await get_cryptocurrency_votes(r, "BTC", timestamp_ten_days_ago)) |
| 220 | + print() |
| 221 | + |
| 222 | + # Get similar articles |
| 223 | + print("Similar articles to 'Bitcoin crash':") |
| 224 | + print(await get_similar_articles(r, "Bitcoin crash", timestamp_ten_days_ago, 2)) |
| 225 | + print() |
| 226 | + |
| 227 | + print("Similar articles to 'Ethereum rally':") |
| 228 | + print(await get_similar_articles(r, "Ethereum rally", timestamp_ten_days_ago, 2)) |
| 229 | + print() |
| 230 | + |
| 231 | + print("Similar articles to 'Ripple lawsuit':") |
| 232 | + print(await get_similar_articles(r, "Ripple lawsuit", timestamp_ten_days_ago, 2)) |
| 233 | + print() |
| 234 | + |
| 235 | + # Compute features for a cryptocurrency |
| 236 | + initialize_features_dataset(r) |
| 237 | + create_features_index(r) |
| 238 | + |
| 239 | + timestamp_seven_days_ago = 1730000000 |
| 240 | + |
| 241 | + # Predict if a cryptocurrency will grow |
| 242 | + will_grow = await cryptocurrency_will_grow(r, "BTC", timestamp_seven_days_ago) |
| 243 | + print(f"Will BTC grow in next seven days? {"Yes" if will_grow else "No"}") |
| 244 | + |
| 245 | + |
| 246 | +if __name__ == "__main__": |
| 247 | + asyncio.run(main()) |
0 commit comments