From 18e43129fe1ad2409d2447cc55828d8e3374f134 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sun, 27 Aug 2023 04:15:52 +0300 Subject: [PATCH] final updates and fixes --- ml/openai_handle.py | 4 ++-- pitch_deck_generator/decks/api/serializers.py | 23 +++++++++--------- pitch_deck_generator/decks/api/views.py | 2 +- .../decks/migrations/0009_pitchdeck_images.py | 18 ++++++++++++++ pitch_deck_generator/decks/models.py | 1 + pitch_deck_generator/decks/services.py | 2 +- pitch_deck_generator/decks/signals.py | 5 +++- pitch_deck_generator/decks/tasks.py | 24 +++++++++++++++---- 8 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 pitch_deck_generator/decks/migrations/0009_pitchdeck_images.py diff --git a/ml/openai_handle.py b/ml/openai_handle.py index 4280026..0c23c9f 100644 --- a/ml/openai_handle.py +++ b/ml/openai_handle.py @@ -112,8 +112,8 @@ def create_hints(description: str, stage: int): ].group() if not len(filtered_content): raise ValueError(f"answer doesnt pass validation, {filtered_content}") - except: - raise ValueError(f"answer doesnt pass validation, {filtered_content}") + except Exception: + print(f"answer doesnt pass validation") content = literal_eval(filtered_content) for assertion_statement in assertions[stage]: assert assertion_statement(content) diff --git a/pitch_deck_generator/decks/api/serializers.py b/pitch_deck_generator/decks/api/serializers.py index df5fc36..e39f14e 100644 --- a/pitch_deck_generator/decks/api/serializers.py +++ b/pitch_deck_generator/decks/api/serializers.py @@ -17,11 +17,12 @@ class BasePitchDeckSerializer(serializers.ModelSerializer): class Meta: model = PitchDeck - fields = ["id", "name", "description", "logo", "created", "updated"] + fields = ["id", "name", "description", "logo", "created", "images", "updated"] extra_kwargs = { "id": {"read_only": True}, "logo": {"read_only": True}, "name": {"read_only": True}, + "images": {"read_only": True}, "created": {"read_only": True}, "updated": {"read_only": True}, } @@ -138,8 +139,8 @@ def validate(self, data): raise serializers.ValidationError("Value to found") if not isinstance(answer[slug], (int, float)): raise serializers.ValidationError("Incorrect type") - if not (params["min_value"] <= answer[slug] <= params["max_value"]): - raise serializers.ValidationError("Number is too big or too small") + # if not (params["min_value"] <= answer[slug] <= params["max_value"]): + # raise serializers.ValidationError("Number is too big or too small") case "multiple_range": scrollbars = {} for el in params["scrollbars"]: @@ -149,14 +150,14 @@ def validate(self, data): raise serializers.ValidationError(f"Value {slug} to found") if not isinstance(answer[slug], (int, float)): raise serializers.ValidationError(f"Incorrect {slug} type") - if not ( - scrollbars[slug]["min_value"] - <= answer[slug] - <= scrollbars[slug]["max_value"] - ): - raise serializers.ValidationError( - f"Number is too big or too small for {slug}" - ) + # if not ( + # scrollbars[slug]["min_value"] + # <= answer[slug] + # <= scrollbars[slug]["max_value"] + # ): + # raise serializers.ValidationError( + # f"Number is too big or too small for {slug}" + # ) case "select": if answer not in params["options"]: raise serializers.ValidationError("No such option") diff --git a/pitch_deck_generator/decks/api/views.py b/pitch_deck_generator/decks/api/views.py index 4749bb5..f7ff6ea 100644 --- a/pitch_deck_generator/decks/api/views.py +++ b/pitch_deck_generator/decks/api/views.py @@ -91,7 +91,7 @@ class GetDeckPresentationDataApiView(generics.GenericAPIView): 10: ["how_much_investments", "time_to_spend", "investments_sold"], 11: ["company_value", "future_value", "time_to_spend"], 12: ["aims"], - 13: ["images"], + 13: ["images", "links"], } def get(self, request, *args, **kwargs): diff --git a/pitch_deck_generator/decks/migrations/0009_pitchdeck_images.py b/pitch_deck_generator/decks/migrations/0009_pitchdeck_images.py new file mode 100644 index 0000000..9a99a9a --- /dev/null +++ b/pitch_deck_generator/decks/migrations/0009_pitchdeck_images.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.4 on 2023-08-26 23:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("decks", "0008_pdftopptxstorage_alter_question_type"), + ] + + operations = [ + migrations.AddField( + model_name="pitchdeck", + name="images", + field=models.JSONField(null=True), + ), + ] diff --git a/pitch_deck_generator/decks/models.py b/pitch_deck_generator/decks/models.py index 808b8d8..f8be63b 100644 --- a/pitch_deck_generator/decks/models.py +++ b/pitch_deck_generator/decks/models.py @@ -11,6 +11,7 @@ class PitchDeck(models.Model): # generated info logo = models.ImageField(upload_to="logos/", null=True, blank=True) + images = models.JSONField(null=True) styles = models.FileField(upload_to="styles/", null=True, blank=True) presentation = models.FileField(upload_to="logos/", null=True, blank=True) meta_info = models.JSONField(default=dict, null=True, blank=True) diff --git a/pitch_deck_generator/decks/services.py b/pitch_deck_generator/decks/services.py index 85ef850..9c4396d 100644 --- a/pitch_deck_generator/decks/services.py +++ b/pitch_deck_generator/decks/services.py @@ -15,7 +15,7 @@ def get_image_mokeup(image_path): ) else: q = mockup.generate( - template_id=mockup.templates[0].id, + template_id="d95345535d98fcc6f4030e1d3ca62052", screenshot_path=image_path, color=mockup.templates[0].colors[0].color, ) diff --git a/pitch_deck_generator/decks/signals.py b/pitch_deck_generator/decks/signals.py index 49b7d3b..e25a7b9 100644 --- a/pitch_deck_generator/decks/signals.py +++ b/pitch_deck_generator/decks/signals.py @@ -34,7 +34,7 @@ def question_answer_create(sender, instance: QuestionAnswer, created, **kwargs): kwargs={"pk": instance.deck.pk}, countdown=1 ) elif instance.question.inner_tag == "names": - instance.deck.name = instance.answer + instance.deck.name = instance.answer if instance.answer else None instance.deck.save() elif instance.question.inner_tag in ["finance_model"]: qenerate_answer_qr.apply_async(kwargs={"pk": instance.pk}, countdown=5) @@ -50,6 +50,9 @@ def question_answer_update(sender, instance: QuestionAnswer, **kwargs): generate_numeric_values.apply_async( kwargs={"pk": instance.deck.pk}, countdown=1 ) + elif instance.question.inner_tag == "names": + instance.deck.name = instance.answer if instance.answer else None + instance.deck.save() elif instance.question.inner_tag in ["finance_model"]: qenerate_answer_qr.apply_async(kwargs={"pk": instance.pk}, countdown=5) elif instance.question.inner_tag == "images": diff --git a/pitch_deck_generator/decks/tasks.py b/pitch_deck_generator/decks/tasks.py index 5ec43e4..97555aa 100644 --- a/pitch_deck_generator/decks/tasks.py +++ b/pitch_deck_generator/decks/tasks.py @@ -15,7 +15,7 @@ ) from pitch_deck_generator.decks.services import get_image_mokeup -ML_HOST = "https://forty-eggs-slide.loca.lt/" +ML_HOST = "https://small-teeth-own.loca.lt/" data_types = { "names": ("text", 1), @@ -56,10 +56,11 @@ @shared_task def run_pitch_deck_calculation(pk: int): - generate_pitch_deck_name.apply_async(kwargs={"pk": pk}) - generate_known_values.apply_async(kwargs={"pk": pk}) + generate_pitch_deck_name.apply_async(kwargs={"pk": pk}, countdown=1) + generate_known_values.apply_async(kwargs={"pk": pk}, countdown=1) for i in range(3): - generate_batch_hints.apply_async(kwargs={"pk": pk, "num": i}, delay=1) + generate_batch_hints.apply_async(kwargs={"pk": pk, "num": i}, countdown=1) + generate_pitch_deck_images.apply_async(kwargs={"pk": pk}, countdown=1) @shared_task @@ -74,6 +75,19 @@ def generate_pitch_deck_name(pk: int): ) +@shared_task +def generate_pitch_deck_images(pk: int): + pitch_deck = PitchDeck.objects.get(pk=pk) + req = requests.post( + ML_HOST + "img-search", + json={"body": pitch_deck.description}, + ) + print(req.text) + data = req.json() + pitch_deck.images = data + pitch_deck.save() + + @shared_task def generate_known_values(pk: int): pitch_deck = PitchDeck.objects.get(pk=pk) @@ -81,6 +95,7 @@ def generate_known_values(pk: int): ML_HOST + "search", json={"body": pitch_deck.description}, ) + print(req.text) data = req.json() _, question_id = data_types["competitors"] QuestionDeckHint.objects.create( @@ -134,6 +149,7 @@ def generate_numeric_values(pk: int): "type": type, }, ) + print(req.text) data = req.json() for el in data: question_type, question_id = data_types[el["type"]]