ezequiellopez commited on
Commit
b21b232
·
1 Parent(s): 3b86501

rename main

Browse files
Files changed (2) hide show
  1. Dockerfile +1 -1
  2. app/app.py +105 -0
Dockerfile CHANGED
@@ -39,4 +39,4 @@ USER user
39
  EXPOSE 7860
40
 
41
  # Command to run the FastAPI application using uvicorn
42
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
39
  EXPOSE 7860
40
 
41
  # Command to run the FastAPI application using uvicorn
42
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app/app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import required libraries
2
+ from fastapi import FastAPI, HTTPException
3
+ #import redis
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ from modules.redistribute import redistribute, insert_element_at_position
8
+ from modules.models.api import Input, Output, NewItem, UUID
9
+ from modules.database import BoostDatabase, UserDatabase, User
10
+
11
+
12
+ # Load environment variables from .env file
13
+ load_dotenv('../.env')
14
+
15
+ # Access environment variables
16
+ redis_port = os.getenv("REDIS_PORT")
17
+ fastapi_port = os.getenv("FASTAPI_PORT")
18
+
19
+
20
+ print("Redis port:", redis_port)
21
+ print("FastAPI port:", fastapi_port)
22
+
23
+ app = FastAPI()
24
+ boost_db = BoostDatabase('data/boost_bank.csv')
25
+ user_db = UserDatabase()
26
+
27
+
28
+ # Define a health check endpoint
29
+ @app.get("/")
30
+ async def health_check():
31
+ return {"status": "ok"}
32
+
33
+ # Define FastAPI routes and logic
34
+ @app.post("/rerank/")
35
+ async def rerank_items(input_data: Input) -> Output:
36
+ # who is the user?
37
+ user = input_data.session.user_id
38
+ date = input_data.session.current_time
39
+ platform = input_data.session.platform
40
+ items = input_data.items
41
+ # TODO consider sampling them?
42
+
43
+ print(items)
44
+ reranked_ids, first_topic, insertion_pos = redistribute(items=items)
45
+ #reranked_ids = [ for id_ in reranked_ids]
46
+ print("here!")
47
+ print(reranked_ids)
48
+
49
+ user_in_db = user_db.get_user(user_id=user)
50
+
51
+ # if user already exists -> has boosting records
52
+ if user_in_db:
53
+ # has been boosted today?
54
+ if user_in_db.is_boosted_today():
55
+ # return only reranked items, no insertion
56
+ return Output(reranked_ids=reranked_ids, new_items=[])
57
+ # user exists and not boosted today yet
58
+ else:
59
+ new_items = []
60
+ boosts_received = user_in_db.boosts
61
+ # there was some civic content in the batch
62
+ if first_topic != "non-civic":
63
+ fetched_boost = boost_db.get_random_boost(topic=first_topic,
64
+ platform=platform,
65
+ blacklist_ids=boosts_received)
66
+ user_db.add_boost_to_user(user_id=user, boost=fetched_boost)
67
+ user_db.update_user_boosted_today(user_id=user, date=date)
68
+
69
+ # insert boost before first civic in batch
70
+ reranked_ids = insert_element_at_position(lst=reranked_ids,
71
+ element=UUID(fetched_boost['id']),
72
+ position=insertion_pos)
73
+
74
+ return Output(ranked_ids=reranked_ids, new_items=[NewItem(id=UUID(fetched_boost["id"]), url=fetched_boost["url"])])
75
+
76
+ # no civic content to boost on
77
+ else:
78
+ return Output(ranked_ids=reranked_ids, new_items=[])
79
+
80
+ # user doesn't exist
81
+ else:
82
+ print(first_topic)
83
+ print(platform)
84
+ if first_topic != "non-civic":
85
+ fetched_boost = boost_db.get_random_boost(topic=first_topic,
86
+ platform=platform,
87
+ blacklist_ids=[])
88
+ print(fetched_boost)
89
+ print(type(fetched_boost))
90
+ user_db.add_user(user_id=user,
91
+ user=User(user_id=user, last_boost=date, boosts=[fetched_boost]))
92
+
93
+ # insert boost before first civic in batch
94
+ reranked_ids = insert_element_at_position(lst=reranked_ids,
95
+ element=UUID(fetched_boost['id']),
96
+ position=insertion_pos)
97
+
98
+
99
+ return Output(ranked_ids=reranked_ids, new_items=[NewItem(id=UUID(fetched_boost["id"]), url=fetched_boost["url"])])
100
+
101
+ # no civic content to boost on
102
+ else:
103
+ print("there")
104
+ return Output(ranked_ids=reranked_ids, new_items=[])
105
+