Skip to content

Instantly share code, notes, and snippets.

@lpabon
Created June 27, 2023 18:18
Show Gist options
  • Save lpabon/62393fb27c288ce77971cf7472be89d4 to your computer and use it in GitHub Desktop.
Save lpabon/62393fb27c288ce77971cf7472be89d4 to your computer and use it in GitHub Desktop.
diff --git a/ai_presenter/ai_presenter.py b/ai_presenter/ai_presenter.py
index 0da30d6..170412c 100644
--- a/ai_presenter/ai_presenter.py
+++ b/ai_presenter/ai_presenter.py
@@ -10,63 +10,18 @@ class AIPresenter:
self.generator = g
def run(self):
- self.__run_text_ai()
- self.__run_voice_ai()
-
- def __run_text_ai(self):
- logging.info("running text ai")
- # calls get_text and puts it into textai
- textai = self.generator.get_text()
- # sends textai to chatgtp config
- # creates a message string variable
- message = self.database.config.ai_config.chatgptconfig.style
- # goes through each actor
- for key, actor in self.database.actors.items():
- message += f'{actor.name} is a {actor.age} year old' + \
- f' {actor.gender}, {actor.description}. '
- # sends message to ai in given format
- # textai.send(message)
-
config = self.database.get_config()
text_ai_file = config.get_ai_config().get_text_ai_filename()
-
with open(text_ai_file, 'w') as file:
textai = self.generator.get_text()
for key, scene in self.database.scenes.items():
- logging.info(f"******** \nWorking on scene: {scene.name} in " +
+ logging.info(f"Working on scene: {scene.name} in " +
f"{scene.location}")
- output = self.__get_scene_text(scene, textai, message)
- message = ''
+ output = textai.generate(scene)
file.write(output + '\n')
logging.info(f'got back from textai: {output}')
- def __run_voice_ai(self):
+ # Generate voice ai
voiceai = VoiceAI(self.database)
voiceconfig = VoiceConfig()
voiceai.generate('text_ai.txt', 'voice_ai.txt', voiceconfig)
-
- def __get_scene_text(self, scene, textai, message=''):
- # go through each scene
- for dialogue in scene.dialogue:
- actor = dialogue['actor']
- text = dialogue['text']
- message += f'{actor} says, \"{text}\". '
- # This message tells ChatGPT to expand on the scene input
- # as well as the format in which to return as JSON
- message += (
- "Return a json, formatted so they contain characters, "
- "and dialogue. characters to start, contain their "
- "name, personality, and emotion. dialogue contains "
- "speaker, emotion, and message. Make sure to "
- "expand on the given text, adding dialogue, "
- "detail, and emotion."
- )
- output = textai.send(message)
- return output
- # chatGPT is fricking amazing
- # working idea rn is feed chat gpt the scene in the format of
- # "{character, description}* are at scene in location.
- # {character says, dialogue}*. Build scene from this and make
- # sure to include lots of intense dialogue and details.
- # provide voice ai with input(filename), output(filename),
- # and configuration(which voice)
diff --git a/ai_presenter/database.py b/ai_presenter/database.py
index 08d2417..5b45787 100644
--- a/ai_presenter/database.py
+++ b/ai_presenter/database.py
@@ -2,15 +2,20 @@ from ai_presenter.config.config import Config
class Database:
- def __init__(self, actors=None, scenes=None, locations=None, config=None):
+ def __init__(self, actors=None, scenes=None, locations=None, config=None, alldata=None):
self.actors = actors
self.scenes = scenes
self.locations = locations
self.config = config
+ self.alldata = alldata
def get_config(self) -> Config:
return self.config
+ def get_data(self):
+ return self.alldata
+
+
class Actor:
def __init__(self, data):
@@ -28,6 +33,10 @@ class Scene:
self.location = data['location']
self.actors = data['actors']
self.dialogue = data['dialogue']
+ self.alldata = data
+
+ def to_map(self):
+ return self.alldata
class Location:
diff --git a/ai_presenter/reader.py b/ai_presenter/reader.py
index c0c3e6c..50cc663 100644
--- a/ai_presenter/reader.py
+++ b/ai_presenter/reader.py
@@ -39,7 +39,8 @@ class Reader:
return Database(config=self.config,
actors=self.actors,
scenes=self.scenes,
- locations=self.locations)
+ locations=self.locations
+ alldata=self.data)
def print(self):
yaml.dump(self.data, default_flow_style=False)
diff --git a/ai_presenter/text_ai/base.py b/ai_presenter/text_ai/base.py
index 673cded..411e9be 100644
--- a/ai_presenter/text_ai/base.py
+++ b/ai_presenter/text_ai/base.py
@@ -1,4 +1,4 @@
-from ai_presenter.database import Database
+from ai_presenter.database import Database, Scene
class TextAi:
@@ -7,5 +7,5 @@ class TextAi:
self.scenes = db.scenes
self.locations = db.locations
- def send(self, text) -> str:
+ def generate(self, s: Scene) -> str:
pass
diff --git a/ai_presenter/text_ai/chatgpt.py b/ai_presenter/text_ai/chatgpt.py
index 83938be..2c1439f 100644
--- a/ai_presenter/text_ai/chatgpt.py
+++ b/ai_presenter/text_ai/chatgpt.py
@@ -3,7 +3,7 @@ import os
import openai
import logging
from ai_presenter.text_ai.base import TextAi
-from ai_presenter.database import Database
+from ai_presenter.database import Database, Scene
from ai_presenter.tools.json_trim import json_trim
# import ai_presenter.config.env_vars
@@ -11,17 +11,35 @@ from ai_presenter.tools.json_trim import json_trim
class TextChatGPT(TextAi):
def __init__(self, db: Database):
self.db = db
+
+ self.messages = [
+ {"roles" : "system" },
+ {"roles" : "assistant"},
+ ]
+
+ self.user_message= {}
+ self.user_message['actors'] = self.db.get_data()['actors']
+
openai.api_key = os.getenv("CHATGPT_APIKEY")
# config = self.db.get_config()
# openai.api_key = config.get_ai_config().get_chatgpt_api_key()
- def send(self, text) -> str:
+ def generate(self, s: Scene) -> str:
+ self.user_message['scene'] = s.alldata()
+ self.messages.append(
+ {"roles": "user", "content": json.dumps(self.user_message)}
+ )
completion = openai.ChatCompletion.create(
- model="gpt-3.5-turbo",
- messages=[
- {"role": "system", "content": text}
- ]
+ #model="gpt-3.5-turbo",
+ mode="gpt-4",
+ messages=self.messages,
)
+
+ # clear for next time
+ self.messages = []
+ self.user_message = {}
+
+ # result
resp = completion.choices[0].message.content
logging.info("Recieved " + resp)
resp = json_trim(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment