Skip to content

Commit

Permalink
Roast IG (#409)
Browse files Browse the repository at this point in the history
* Adding OpenAI & Update IG test

* Adding RoastingIG

* Update workflow
  • Loading branch information
animemoeus authored Sep 24, 2024
1 parent df8b0b4 commit 41226b5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jobs:
echo "WAIFU_DISCORD_REFRESH_URL_BOT_TOKEN=${{ secrets.WAIFU_DISCORD_REFRESH_URL_BOT_TOKEN }}" >> .envs/.local/.django
cat .envs/.local/.django
echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .envs/.local/.django
cat .envs/.local/.django
echo "WIREGUARD_HOST=localhost" >> .env
echo "WIREGUARD_PASSWORD=password" >> .env
cat .env
Expand Down
4 changes: 4 additions & 0 deletions backend/utils/openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.conf import settings
from openai import OpenAI

openai_client = OpenAI(api_key=settings.OPENAI_API_KEY)
3 changes: 3 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,6 @@
# instagram
INSTAGRAM_API_URL = env.str("INSTAGRAM_API_URL", default="")
INSTAGRAM_API_KEY = env.str("INSTAGRAM_API_KEY", default="")

# OpenAI
OPENAI_API_KEY = env.str("OPENAI_API_KEY", default="")
Empty file added instagram/tests/__init__.py
Empty file.
8 changes: 2 additions & 6 deletions instagram/tests.py → instagram/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.test import TestCase

from .models import Story as InstagramStory
from .models import User as InstagramUser
from instagram.models import Story as InstagramStory
from instagram.models import User as InstagramUser


class InstagramTestCase(TestCase):
Expand Down Expand Up @@ -97,7 +97,3 @@ def test_save_user_stories(self):
self.assertIsNotNone(story.story_created_at, "The story_created_at should not be None")
self.assertIsNotNone(story.thumbnail, "The thumbnail should not be None")
self.assertIsNotNone(story.media, "The media should not be None")


class TestInstaloader(TestCase):
pass
19 changes: 19 additions & 0 deletions instagram/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test import TestCase

from instagram.utils import InstagramAPI, RoastingIG


class TestGetInstagramRoastingText(TestCase):
def setUp(self):
self.instagram_api = InstagramAPI()
self.instagram_user_data = self.instagram_api.get_user_info_v2("arter_tendean")

def test_get_profile_picture_keywords(self):
profile_picture_keywords = RoastingIG.get_profile_picture_keywords(
self.instagram_user_data.get("profile_pic_url")
)
self.assertIsNotNone(profile_picture_keywords, "Should return profile picture keywords")

def test_get_instagram_roasting_text(self):
roasting_text = RoastingIG.get_instagram_roasting_text(self.instagram_user_data)
self.assertIsNotNone(roasting_text, "Should return roasting text")
80 changes: 80 additions & 0 deletions instagram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.conf import settings
from rest_framework import status

from backend.utils.openai import openai_client


class InstagramAPI:
def __init__(self):
Expand Down Expand Up @@ -56,3 +58,81 @@ def user_profile_picture_upload_location(instance, filename):

def user_stories_upload_location(instance, filename):
return f"instagram/user/{instance.user.username}/stories/{filename}"


class RoastingIG:
client = openai_client
model = "gpt-4o-mini-2024-07-18"

@classmethod
def get_profile_picture_keywords(cls, url: str) -> str:
response = cls.client.chat.completions.create(
model=cls.model,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "get the keywords from image, separate by comma"},
{
"type": "image_url",
"image_url": {
"url": url,
},
},
],
}
],
max_tokens=300,
)

return response.choices[0].message.content

@classmethod
def format_user_data(cls, data: dict) -> dict:
profile_picture_keywords = cls.get_profile_picture_keywords(data.get("profile_pic_url"))

return {
"profile_picture_keywords": profile_picture_keywords,
"nama_lengkap": data.get("full_name", ""),
"username": data.get("username"),
"is_private_account": data.get("is_private"),
"biography": data.get("biography", ""),
"links_in_profile": [link.get("url") for link in data.get("bio_links", []) if link.get("url")],
"jumlah_follower": data.get("follower_count"),
"jumlah_akun_yang_diikuti": data.get("following_count"),
"jumlah_postingan": data.get("media_count"),
}

@classmethod
def get_instagram_roasting_text(cls, data: str) -> str:
formatted_user_data = cls.format_user_data(data)

completion = cls.client.chat.completions.create(
model=cls.model,
messages=[
{
"role": "system",
"content": "You are a greatest roasting assistant. You can give a criticism for every things",
},
{
"role": "user",
"content": f"""
berikan roasting dengan kejam,menyindir, serta menyakitkan dalam bahasa gaul untuk profile dengan data berikut:
{formatted_user_data}
roasting foto profilnya
roasting username dan atau namanya
roasting jumlah following, follower dan post
roasting kategori profilenya jika ada, tuduh dia ngaku ngaku
roasting biografinya jika ada, jika kosong: berikan roasting yang berhubungan dengan no life
roasting info lainnya juga, jangan lupa pake emoji
jangan pake list, langsung teks roastingnya
jangan ada key dari json yang muncul
""",
},
],
)

return completion.choices[0].message.content
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ pixivpy3==3.7.5 # https://github.com/upbit/pixivpy
pyscord-storage==2.0.3 # https://github.com/animemoeus/pyscord-storage
django-health-check==3.18.3 # https://github.com/revsys/django-health-check
saiyaku==2023.12.11 # https://github.com/animemoeus/saiyaku
openai==1.47.1 # https://pypi.org/project/openai/

0 comments on commit 41226b5

Please sign in to comment.