Spaces:
Sleeping
Sleeping
Julian-Hans
commited on
Commit
•
2c9e5e4
1
Parent(s):
fa554aa
added tests and test workflow, small cleanup
Browse files- .github/workflows/sync.yml +5 -2
- .github/workflows/test.yml +43 -0
- Case-Study-1/.DS_Store +0 -0
- app.py +0 -11
- config.py +13 -1
- requirements.txt +3 -0
- test_blip_image_caption_large.py +16 -0
- test_musicgen_small.py +26 -0
- test_phi3_mini_4k_instruct.py +20 -0
.github/workflows/sync.yml
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
name: Sync to Hugging Face hub
|
2 |
on:
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
5 |
|
6 |
# to run this workflow manually from the Actions tab
|
7 |
workflow_dispatch:
|
|
|
1 |
name: Sync to Hugging Face hub
|
2 |
on:
|
3 |
+
workflow_run:
|
4 |
+
workflows: ["Run Pytest Tests"] # This must match the name of your test workflow
|
5 |
+
types:
|
6 |
+
- completed
|
7 |
+
|
8 |
|
9 |
# to run this workflow manually from the Actions tab
|
10 |
workflow_dispatch:
|
.github/workflows/test.yml
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ----ATTRIBUTION-START----
|
2 |
+
# LLM: Github Copilot
|
3 |
+
# PROMPT: i have written tests. i run them like this pytest test_blip_image_caption_large.py test_phi3_mini_4k_instruct.py test_musicgen_small.py - help me create a github runner that runs these tests - it also needs to create the environment variable "HF_API_TOKEN". it is added to the github repo under the name HF_API_TOKEN
|
4 |
+
# EDITS: /
|
5 |
+
|
6 |
+
name: Run Pytest Tests
|
7 |
+
|
8 |
+
# Triggers the workflow on push or pull request to the main branch
|
9 |
+
on:
|
10 |
+
push:
|
11 |
+
branches:
|
12 |
+
- main
|
13 |
+
pull_request:
|
14 |
+
branches:
|
15 |
+
- main
|
16 |
+
|
17 |
+
jobs:
|
18 |
+
test:
|
19 |
+
runs-on: ubuntu-latest
|
20 |
+
|
21 |
+
env:
|
22 |
+
# Create the HF_API_TOKEN environment variable from the repository secrets
|
23 |
+
HF_API_TOKEN: ${{ secrets.HF_API_TOKEN }}
|
24 |
+
|
25 |
+
steps:
|
26 |
+
- name: Checkout code
|
27 |
+
uses: actions/checkout@v3
|
28 |
+
|
29 |
+
- name: Set up Python
|
30 |
+
uses: actions/setup-python@v4
|
31 |
+
with:
|
32 |
+
python-version: "3.x" # Set your preferred Python version here
|
33 |
+
|
34 |
+
- name: Install dependencies
|
35 |
+
run: |
|
36 |
+
python -m pip install --upgrade pip
|
37 |
+
pip install -r requirements.txt # Ensure you have a requirements.txt in your repo
|
38 |
+
|
39 |
+
- name: Run Pytest tests
|
40 |
+
run: |
|
41 |
+
pytest test_blip_image_caption_large.py test_phi3_mini_4k_instruct.py test_musicgen_small.py
|
42 |
+
|
43 |
+
# -----ATTRIBUTION-END-----
|
Case-Study-1/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
app.py
CHANGED
@@ -4,7 +4,6 @@ import logging as log
|
|
4 |
import time
|
5 |
import uuid
|
6 |
import gradio as gr
|
7 |
-
import os
|
8 |
|
9 |
# local imports
|
10 |
from blip_image_caption_large import Blip_Image_Caption_Large
|
@@ -30,16 +29,6 @@ class Image_To_Music:
|
|
30 |
self.caption_generation_duration = -1
|
31 |
self.description_generation_duration = -1
|
32 |
self.music_generation_duration = -1
|
33 |
-
self.create_output_folder()
|
34 |
-
|
35 |
-
|
36 |
-
# ----ATTRIBUTION-START----
|
37 |
-
# LLM: Github Copilot
|
38 |
-
# PROMPT: create an output folder for the generated audio files
|
39 |
-
# EDITS: /
|
40 |
-
def create_output_folder(self):
|
41 |
-
os.makedirs(config.AUDIO_DIR, exist_ok=True)
|
42 |
-
# -----ATTRIBUTION-END-----
|
43 |
|
44 |
def caption_image(self, image_path):
|
45 |
log.info("Captioning Image...")
|
|
|
4 |
import time
|
5 |
import uuid
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
# local imports
|
9 |
from blip_image_caption_large import Blip_Image_Caption_Large
|
|
|
29 |
self.caption_generation_duration = -1
|
30 |
self.description_generation_duration = -1
|
31 |
self.music_generation_duration = -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def caption_image(self, image_path):
|
34 |
log.info("Captioning Image...")
|
config.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import logging as log
|
|
|
3 |
log.basicConfig(level=log.INFO)
|
4 |
|
5 |
IMAGE_CAPTION_MODEL = "Salesforce/blip-image-captioning-large"
|
@@ -17,7 +18,18 @@ MUSICGEN_MAX_NEW_TOKENS = 256 # 5 seconds of audio
|
|
17 |
AUDIO_DIR = "Case-Study-1/data/"
|
18 |
|
19 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
|
|
20 |
if HF_API_TOKEN:
|
21 |
log.info(f"Read HF_API_TOKEN: {HF_API_TOKEN[0:4]}...")
|
22 |
else:
|
23 |
-
print("HF_API_TOKEN not found in environment variables.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import logging as log
|
3 |
+
|
4 |
log.basicConfig(level=log.INFO)
|
5 |
|
6 |
IMAGE_CAPTION_MODEL = "Salesforce/blip-image-captioning-large"
|
|
|
18 |
AUDIO_DIR = "Case-Study-1/data/"
|
19 |
|
20 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
21 |
+
|
22 |
if HF_API_TOKEN:
|
23 |
log.info(f"Read HF_API_TOKEN: {HF_API_TOKEN[0:4]}...")
|
24 |
else:
|
25 |
+
print("HF_API_TOKEN not found in environment variables.")
|
26 |
+
|
27 |
+
# ----ATTRIBUTION-START----
|
28 |
+
# LLM: Github Copilot
|
29 |
+
# PROMPT: create an output folder for the generated audio files
|
30 |
+
# EDITS: /
|
31 |
+
def create_output_folder():
|
32 |
+
os.makedirs(AUDIO_DIR, exist_ok=True)
|
33 |
+
# -----ATTRIBUTION-END-----
|
34 |
+
|
35 |
+
create_output_folder()
|
requirements.txt
CHANGED
@@ -20,6 +20,7 @@ httpx==0.27.2
|
|
20 |
huggingface-hub==0.24.6
|
21 |
idna==3.8
|
22 |
importlib_resources==6.4.5
|
|
|
23 |
Jinja2==3.1.4
|
24 |
kiwisolver==1.4.7
|
25 |
markdown-it-py==3.0.0
|
@@ -33,12 +34,14 @@ orjson==3.10.7
|
|
33 |
packaging==24.1
|
34 |
pandas==2.2.2
|
35 |
pillow==10.4.0
|
|
|
36 |
psutil==6.0.0
|
37 |
pydantic==2.9.1
|
38 |
pydantic_core==2.23.3
|
39 |
pydub==0.25.1
|
40 |
Pygments==2.18.0
|
41 |
pyparsing==3.1.4
|
|
|
42 |
python-dateutil==2.9.0.post0
|
43 |
python-multipart==0.0.9
|
44 |
pytz==2024.2
|
|
|
20 |
huggingface-hub==0.24.6
|
21 |
idna==3.8
|
22 |
importlib_resources==6.4.5
|
23 |
+
iniconfig==2.0.0
|
24 |
Jinja2==3.1.4
|
25 |
kiwisolver==1.4.7
|
26 |
markdown-it-py==3.0.0
|
|
|
34 |
packaging==24.1
|
35 |
pandas==2.2.2
|
36 |
pillow==10.4.0
|
37 |
+
pluggy==1.5.0
|
38 |
psutil==6.0.0
|
39 |
pydantic==2.9.1
|
40 |
pydantic_core==2.23.3
|
41 |
pydub==0.25.1
|
42 |
Pygments==2.18.0
|
43 |
pyparsing==3.1.4
|
44 |
+
pytest==8.3.3
|
45 |
python-dateutil==2.9.0.post0
|
46 |
python-multipart==0.0.9
|
47 |
pytz==2024.2
|
test_blip_image_caption_large.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from blip_image_caption_large import Blip_Image_Caption_Large
|
2 |
+
|
3 |
+
# Test the local image caption pipeline with wikipedia image
|
4 |
+
def test_blip_image_caption_local_model():
|
5 |
+
image_caption_model = Blip_Image_Caption_Large()
|
6 |
+
image_path = "https://upload.wikimedia.org/wikipedia/commons/8/8f/Students_taking_computerized_exam.jpg"
|
7 |
+
result = image_caption_model.caption_image(image_path, use_local_caption=True)
|
8 |
+
assert result == "several people sitting at desks with computers in a classroom"
|
9 |
+
|
10 |
+
# Test the image caption API with wikipedia image
|
11 |
+
def test_blip_image_caption_api():
|
12 |
+
image_caption_model = Blip_Image_Caption_Large()
|
13 |
+
image_path = "https://upload.wikimedia.org/wikipedia/commons/8/8f/Students_taking_computerized_exam.jpg"
|
14 |
+
result = image_caption_model.caption_image(image_path, use_local_caption=False)
|
15 |
+
assert result == "several people sitting at desks with computers in a classroom"
|
16 |
+
|
test_musicgen_small.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from musicgen_small import Musicgen_Small
|
2 |
+
|
3 |
+
import config
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Test the local Musicgen_Small class with a 5 second music generation and assert file creation
|
7 |
+
def test_musicgen_small_local_model():
|
8 |
+
musicgen_model = Musicgen_Small()
|
9 |
+
prompt = "a very testy song, perfect to test the music generation model"
|
10 |
+
audio_path = f"{config.AUDIO_DIR}/test_musicgen_small_local.wav"
|
11 |
+
musicgen_model.generate_music(prompt, audio_path, use_local_musicgen=True)
|
12 |
+
assert os.path.exists(audio_path)
|
13 |
+
assert os.path.getsize(audio_path) > 0
|
14 |
+
os.remove(audio_path)
|
15 |
+
assert not os.path.exists(audio_path)
|
16 |
+
|
17 |
+
# Test the Musicgen_Small API with a 30 second music generation and assert file creation
|
18 |
+
def test_musicgen_small_api():
|
19 |
+
musicgen_model = Musicgen_Small()
|
20 |
+
prompt = "a very testy song, perfect to test the music generation model"
|
21 |
+
audio_path = f"{config.AUDIO_DIR}/test_musicgen_small_api.wav"
|
22 |
+
musicgen_model.generate_music(prompt, audio_path, use_local_musicgen=False)
|
23 |
+
assert os.path.exists(audio_path)
|
24 |
+
assert os.path.getsize(audio_path) > 0
|
25 |
+
os.remove(audio_path)
|
26 |
+
assert not os.path.exists(audio_path)
|
test_phi3_mini_4k_instruct.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from phi3_mini_4k_instruct import Phi3_Mini_4k_Instruct
|
2 |
+
|
3 |
+
# Test the local Phi3_Mini_4k_Instruct Model with default values
|
4 |
+
def test_phi3_mini_4k_instruct_local():
|
5 |
+
phi3_mini_4k_instruct = Phi3_Mini_4k_Instruct()
|
6 |
+
messages = [
|
7 |
+
{"role": "system", "content": "You are an image caption to song description converter with a deep understanding of Music and Art. You are given the caption of an image. Your task is to generate a textual description of a musical piece that fits the caption. The description should be detailed and vivid, and should include the genre, mood, instruments, tempo, and other relevant information about the music. You should also use your knowledge of art and visual aesthetics to create a musical piece that complements the image. Only output the description of the music, without any explanation or introduction. Be concise."},
|
8 |
+
{"role": "user", "content": "several people sitting at desks with computers in a classroom"},
|
9 |
+
]
|
10 |
+
generated_description = phi3_mini_4k_instruct.generate_text(messages, use_local_llm=True)
|
11 |
+
assert isinstance(generated_description, str) and generated_description != ""
|
12 |
+
|
13 |
+
def test_phi3_mini_4k_instruct_api():
|
14 |
+
phi3_mini_4k_instruct = Phi3_Mini_4k_Instruct()
|
15 |
+
messages = [
|
16 |
+
{"role": "system", "content": "You are an image caption to song description converter with a deep understanding of Music and Art. You are given the caption of an image. Your task is to generate a textual description of a musical piece that fits the caption. The description should be detailed and vivid, and should include the genre, mood, instruments, tempo, and other relevant information about the music. You should also use your knowledge of art and visual aesthetics to create a musical piece that complements the image. Only output the description of the music, without any explanation or introduction. Be concise."},
|
17 |
+
{"role": "user", "content": "several people sitting at desks with computers in a classroom"},
|
18 |
+
]
|
19 |
+
generated_description = phi3_mini_4k_instruct.generate_text(messages, use_local_llm=False)
|
20 |
+
assert isinstance(generated_description, str) and generated_description != ""
|