Skip to content

Commit

Permalink
.env usage
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSoleCasale committed May 5, 2024
1 parent b9cee56 commit 1fdfdea
Show file tree
Hide file tree
Showing 19 changed files with 20 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__/
*.pyc
OPENAI_API_KEY.txt
replicate_api.txt
.env
Data/PngRealSet
Data/version2.csv
Binary file removed Data/ExampleData/CoolChairs.mp4
Binary file not shown.
Binary file removed Data/ExampleData/audio.mp3
Binary file not shown.
Binary file modified Data/ExampleData/input.mp3
Binary file not shown.
Binary file added Data/ExampleData/output.mp4
Binary file not shown.
Binary file removed Data/ExampleData07_02_27_374653.mp4
Binary file not shown.
Binary file removed Data/ExampleData07_03_25_320022.mp4
Binary file not shown.
Binary file removed Data/ExampleData07_08_24_401930.mp4
Binary file not shown.
Binary file removed Data/ExampleData07_17_18_216397.mp4
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion OPENAI_API_KEY.txt

This file was deleted.

6 changes: 4 additions & 2 deletions modules/im2tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import pandas as pd
from PIL import Image
import torch
from transformers import BlipProcessor as Bp
from transformers import BlipForConditionalGeneration as BConGen
from tqdm import tqdm
Expand All @@ -21,10 +22,11 @@ def obtain_df_with_text(data_path : str, verbose : bool=False ) -> pd.DataFrame:
"""
Given a path to a folder with images, returns a DataFrame with the images and their descriptions.
"""
device = "cuda" if torch.cuda.is_available() else "cpu"
warnings.simplefilter(action='ignore', category=FutureWarning)
processor = Bp.from_pretrained("Salesforce/blip-image-captioning-large")
# Add .to("cuda") to run in GPU
model = BConGen.from_pretrained("Salesforce/blip-image-captioning-large")
model = BConGen.from_pretrained("Salesforce/blip-image-captioning-large").to(device)
# We use this text to condition the image text generation
seed1 = "a photography of"
# We store here the images and their descriptions
Expand All @@ -36,7 +38,7 @@ def obtain_df_with_text(data_path : str, verbose : bool=False ) -> pd.DataFrame:
img_path = os.path.join(data_path, img).replace("\\", "/")
raw_image = Image.open(img_path).convert('RGB')
# Add .to("cuda") to run in GPU
inputs = processor(raw_image, seed1, return_tensors="pt")
inputs = processor(raw_image, seed1, return_tensors="pt").to(device)
out = model.generate(**inputs, max_new_tokens=20)
capt = processor.decode(out[0], skip_special_tokens=True)
imtx.loc[len(imtx.index)] = [img_path, capt]
Expand Down
18 changes: 6 additions & 12 deletions modules/iris_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,15 @@ def description_search(self, query: str, top_n: int, name: str = 'gallery_images


if __name__ == '__main__':
OPENAI_API_KEY = open('OPENAI_API_KEY.txt').read().strip()
iris = IrisDB(Openai_client = OpenAI(api_key=OPENAI_API_KEY))
iris = IrisDB(Openai_client = OpenAI())

if not iris.table_exists():
# df = pd.DataFrame({
# 'image_path': ['image1.jpg', 'image2.jpg', 'image3.jpg'],
# 'description': ['a cat', 'a dog', 'a car']
# })
df = pd.read_csv('Data/version2.csv')
df.columns = ['image_path', 'description']
df = pd.read_csv('Data/version2.csv')
df.columns = ['image_path', 'description']

iris.init_table()
iris.insert_df_to_table(df)
iris.init_table()
iris.insert_df_to_table(df)

description_search = "music"
description_search = "beard"
results = iris.description_search(description_search, top_n=3)
# limit prints of pd.DataFrame to 240 characters
pd.set_option('display.max_colwidth', 240)
Expand Down
10 changes: 6 additions & 4 deletions modules/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from openai import OpenAI
import pandas as pd
import os
from dotenv import load_dotenv

load_dotenv()

def generate_videoclip(
openai_client : OpenAI,
Expand All @@ -18,7 +21,7 @@ def generate_videoclip(
Given a query, generates a video clip with images and a song.
"""
if not os.environ.get("REPLICATE_API_TOKEN"):
os.environ["REPLICATE_API_TOKEN"] = open('replicate_api.txt').read().strip()
raise Exception("Please set the REPLICATE_API_TOKEN environment variable.")

db_output_df = DB.description_search(query=query, top_n=amount_images)
image_paths = db_output_df['path'].tolist()
Expand All @@ -31,8 +34,7 @@ def generate_videoclip(


if __name__ == '__main__':
OPENAI_API_KEY = open('OPENAI_API_KEY.txt').read().strip()
Openai_client = OpenAI(api_key=OPENAI_API_KEY)
Openai_client = OpenAI()
DB = IrisDB(Openai_client = Openai_client)
if not DB.table_exists():
df = pd.read_csv('Data/version2.csv')
Expand All @@ -42,4 +44,4 @@ def generate_videoclip(
DB.insert_df_to_table(df)

query = 'Cool chairs'
generate_videoclip(Openai_client, DB, query, duration=10, video_path='Data/ExampleData/output4.mp4', amount_images=6)
generate_videoclip(Openai_client, DB, query, duration=10, video_path='Data/ExampleData/output.mp4', amount_images=6)
2 changes: 1 addition & 1 deletion modules/song_prompt_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def create_song_prompt(query : str, image_descriptions : list[str], openai_clien
if __name__ == '__main__':
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
openai_client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
openai_client = OpenAI()
query = "Sadness"
image_descriptions = ["Long winter nights", "A lonely person", "A rainy day"]
print(create_song_prompt(query, image_descriptions, openai_client))
1 change: 0 additions & 1 deletion replicate_api.txt

This file was deleted.

0 comments on commit 1fdfdea

Please sign in to comment.