Skip to content

Commit

Permalink
Return random values
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-piles committed Apr 25, 2024
1 parent 80dc8c3 commit a47f7cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import random
import sys
from os.path import exists
from pathlib import Path
Expand All @@ -16,7 +17,7 @@
app = FastAPI()

data_path = Path("data.json")
options_path = Path("options.json")
params_path = Path("options.json")


@app.get("/info")
Expand Down Expand Up @@ -73,9 +74,13 @@ async def get_suggestions(tenant: str, extractor_id: str):
predictions_data = json.loads(data_path.read_text()) if exists(data_path) else list()

suggestions_list = list()
values = json.loads(options_path.read_text()) if exists(options_path) else list()
params = json.loads(params_path.read_text()) if exists(params_path) else dict()
all_values = params["options"] if params and "options" in params else list()
multi_value = params["multi_value"] if params and "multi_value" in params else False

for prediction_data in predictions_data:
values_count = random.randint(1, len(all_values)) if multi_value else 1
values = random.sample(all_values, k=values_count) if all_values else list()
suggestions_list.append(
Suggestion(
tenant=tenant,
Expand All @@ -93,7 +98,7 @@ async def get_suggestions(tenant: str, extractor_id: str):
if exists(data_path):
os.remove(data_path)

if exists(options_path):
os.remove(options_path)
if exists(params_path):
os.remove(params_path)

return json.dumps(suggestions_list)
4 changes: 2 additions & 2 deletions worker_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from rsmq.consumer import RedisSMQConsumer
from rsmq import RedisSMQ, cmd

from app import options_path
from app import params_path
from data.MetadataExtractionTask import MetadataExtractionTask
from data.ResultsMessage import ResultsMessage

Expand All @@ -28,7 +28,7 @@ def process(self, id, message, rc, ts):
task = MetadataExtractionTask(**message)

if task.params.options:
options_path.write_text(json.dumps([task.params.options[-1]]))
params_path.write_text(json.dumps(task.params))

data_url = f"http://127.0.0.1:5056/get_suggestions/{task.tenant}/{task.params.id}"

Expand Down

0 comments on commit a47f7cc

Please sign in to comment.