add three new ‘Slangify’ chat styles and remove the previous TA’s functions.

#1
by Zimabluee - opened
.github/workflows/check.yml CHANGED
@@ -13,4 +13,4 @@ jobs:
13
  - name: Check large files
14
  uses: ActionsDesk/lfs-warning@v2.0
15
  with:
16
- filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
 
13
  - name: Check large files
14
  uses: ActionsDesk/lfs-warning@v2.0
15
  with:
16
+ filesizelimit: 10485760 # this is 10MB so we can sync to HF Spaces
.github/workflows/{sync.yml → main.yml} RENAMED
@@ -1,10 +1,7 @@
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:
@@ -20,4 +17,4 @@ jobs:
20
  - name: Push to hub
21
  env:
22
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
23
- run: git push -f https://oxmraz-mldo:$HF_TOKEN@huggingface.co/spaces/Group17WPIMLDO24/Case-Study-1 main
 
1
  name: Sync to Hugging Face hub
2
  on:
3
+ push:
4
+ branches: [main]
 
 
 
5
 
6
  # to run this workflow manually from the Actions tab
7
  workflow_dispatch:
 
17
  - name: Push to hub
18
  env:
19
  HF_TOKEN: ${{ secrets.HF_TOKEN }}
20
+ run: git push https://oxmraz-mldo:$HF_TOKEN@huggingface.co/spaces/Group17WPIMLDO24/Case-Study-1 main
.github/workflows/setupaccess.yml DELETED
@@ -1,25 +0,0 @@
1
- name: Setup Access
2
-
3
- on:
4
- workflow_dispatch: # Manual trigger
5
-
6
- jobs:
7
- deploy:
8
- runs-on: ubuntu-latest
9
-
10
- steps:
11
- - name: Checkout Repository
12
- uses: actions/checkout@v3
13
-
14
- - name: Install expect
15
- run: sudo apt-get install expect
16
-
17
- - name: Add permission for script to run
18
- run: chmod +x setupaccess.sh
19
-
20
- - name: Run Bash Scripts
21
- env:
22
- PASSPHRASE_GROUP17: ${{ secrets.PASSPHRASE_GROUP17 }}
23
- GROUP17_PUBLICKKEY: ${{ secrets.GROUP17_PUBLICKKEY }}
24
- GROUP17_PRIVATEKEY: ${{ secrets.GROUP17_PRIVATEKEY }}
25
- run: expect setupaccess.exp "$PASSPHRASE_GROUP17" # this one has code triggering setupaccess.sh, so command ultimately runs multiple scripts :)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.github/workflows/test.yml DELETED
@@ -1,45 +0,0 @@
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
- workflow_dispatch: # Manual trigger
17
-
18
-
19
- jobs:
20
- test:
21
- runs-on: ubuntu-latest
22
-
23
- env:
24
- # Create the HF_API_TOKEN environment variable from the repository secrets
25
- HF_API_TOKEN: ${{ secrets.HF_API_TOKEN }}
26
-
27
- steps:
28
- - name: Checkout code
29
- uses: actions/checkout@v3
30
-
31
- - name: Set up Python
32
- uses: actions/setup-python@v4
33
- with:
34
- python-version: "3.x" # Set your preferred Python version here
35
-
36
- - name: Install dependencies
37
- run: |
38
- python -m pip install --upgrade pip
39
- pip install -r requirements.txt # Ensure you have a requirements.txt in your repo
40
-
41
- - name: Run Pytest tests
42
- run: |
43
- pytest test_blip_image_caption_large.py test_phi3_mini_4k_instruct.py test_musicgen_small.py
44
-
45
- # -----ATTRIBUTION-END-----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.gitignore DELETED
@@ -1,5 +0,0 @@
1
- /__pycache__
2
- *.wav
3
- .pytest_cache
4
- /audio_data
5
- .cache
 
 
 
 
 
 
Case-Study-1/.DS_Store DELETED
Binary file (6.15 kB)
 
README.md CHANGED
@@ -1,19 +1,12 @@
1
  ---
2
- title: Case-Study-1 - Image-To-Music
3
- emoji: 🎼
4
- colorFrom: gray
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- ## Case-Study-1: Image-To-Music 🎼
13
-
14
- An image to music converter, built with the following models:
15
- - https://huggingface.co/Salesforce/blip-image-captioning-large for Image Captioning
16
- - https://huggingface.co/microsoft/Phi-3-mini-4k-instruct for Audio Prompt generation with Caption
17
- - https://huggingface.co/facebook/musicgen-small for Music Generation
18
-
19
- Currently supports .jpg, .jpeg, and .png!
 
1
  ---
2
+ title: CS553_Example
3
+ emoji: 💬
4
+ colorFrom: yellow
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,164 +1,158 @@
1
- # external imports
2
- import gc
3
- import logging as log
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
10
- from phi3_mini_4k_instruct import Phi3_Mini_4k_Instruct
11
- from musicgen_small import Musicgen_Small
12
- import config
13
-
14
- log.basicConfig(level=log.INFO)
15
-
16
-
17
- class Image_To_Music:
18
- def __init__(self, use_local_caption=False, use_local_llm=False, use_local_musicgen=False):
19
-
20
- self.use_local_llm = use_local_llm
21
- self.use_local_caption = use_local_caption
22
- self.use_local_musicgen = use_local_musicgen
23
-
24
- self.image_path = None
25
- self.generated_caption = None
26
- self.generated_description = None
27
- self.audio_path = config.AUDIO_DIR + str(uuid.uuid4()) + ".wav"
28
-
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...")
35
- caption_start_time = time.time()
36
-
37
- # load model
38
- self.image_caption_model = Blip_Image_Caption_Large()
39
-
40
- self.image_path = image_path
41
- self.generated_caption = self.image_caption_model.caption_image(self.image_path, self.use_local_caption)
42
-
43
- # delete model to free up ram
44
- del self.image_caption_model
45
- gc.collect()
46
-
47
- self.caption_generation_duration = time.time() - caption_start_time
48
- log.info(f"Captioning Complete in {self.caption_generation_duration:.2f} seconds: {self.generated_caption} - used local model: {self.use_local_caption}")
49
- return self.generated_caption
50
-
51
- def generate_description(self):
52
- log.info("Generating Music Description...")
53
- description_start_time = time.time()
54
-
55
- # load model
56
- self.text_generation_model = Phi3_Mini_4k_Instruct()
57
-
58
- messages = [
59
- {"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."},
60
- {"role": "user", "content": self.generated_caption},
61
- ]
62
- self.generated_description = self.text_generation_model.generate_text(messages, self.use_local_llm)
63
-
64
- # delete model to free up ram
65
- del self.text_generation_model
66
- gc.collect()
67
-
68
- self.description_generation_duration = time.time() - description_start_time
69
- log.info(f"Description Generation Complete in {self.description_generation_duration:.2f} seconds: {self.generated_description} - used local model: {self.use_local_llm}")
70
- return self.generated_description
71
-
72
- def generate_music(self):
73
- log.info("Generating Music...")
74
- music_start_time = time.time()
75
-
76
- # load model
77
- self.music_generation_model = Musicgen_Small()
78
-
79
- self.music_generation_model.generate_music(self.generated_description, self.audio_path, self.use_local_musicgen)
80
-
81
- # delete model to free up ram
82
- del self.music_generation_model
83
- gc.collect()
84
-
85
- self.music_generation_duration = time.time() - music_start_time
86
- log.info(f"Music Generation Complete in {self.music_generation_duration:.2f} seconds: {self.audio_path} - used local model: {self.use_local_musicgen}")
87
- return self.audio_path
88
-
89
- def get_durations(self):
90
- return f"Caption Generation Time: {self.caption_generation_duration:.2f} seconds\nDescription Generation Time: {self.description_generation_duration:.2f} seconds\nMusic Generation Time: {self.music_generation_duration:.2f} seconds\nTotal Time: {self.caption_generation_duration + self.description_generation_duration + self.music_generation_duration:.2f} seconds"
91
-
92
- def run_yield(self, image_path):
93
-
94
- self.caption_image(image_path)
95
- yield [self.generated_caption, None, None, None]
96
- self.generate_description()
97
- yield [self.generated_caption, self.generated_description, None, None]
98
- self.generate_music()
99
- yield [self.generated_caption, self.generated_description, self.audio_path, None]
100
- return [self.generated_caption, self.generated_description, self.audio_path,self.get_durations()]
101
-
102
- def run(self, image_path):
103
- self.caption_image(image_path)
104
- self.generate_description()
105
- self.generate_music()
106
- return [self.generated_caption, self.generated_description, self.audio_path, self.get_durations()]
107
-
108
-
109
- def run_image_to_music(image_path, llm_max_new_tokens, llm_temperature, llm_top_p, musicgen_max_seconds, use_local_caption, use_local_llm, use_local_musicgen):
110
- config.LLM_MAX_NEW_TOKENS = llm_max_new_tokens
111
- config.LLM_TEMPERATURE = llm_temperature
112
- config.LLM_TOP_P = llm_top_p
113
- config.MUSICGEN_MAX_NEW_TOKENS = musicgen_max_seconds * 51
114
- itm = Image_To_Music(use_local_caption=use_local_caption, use_local_llm=use_local_llm, use_local_musicgen=use_local_musicgen)
115
- return itm.run(image_path)
116
-
117
- # Gradio UI
118
- def gradio():
119
- # Define Gradio Interface, information from (https://www.gradio.app/docs/chatinterface)
120
- with gr.Blocks() as demo:
121
- gr.Markdown("<h1 style='text-align: center;'> ⛺ Image to Music Generator 🎼</h1>")
122
- image_input = gr.Image(type="filepath", label="Upload Image")
123
-
124
-
125
- # ----ATTRIBUTION-START----
126
- # LLM: ChatGPT4o
127
- # PROMPT: i need 3 checkbox fields that pass booleans to the run_image_to_music function. it should be "Use local Image Captioning" "Use local LLM" "Use local Music Generation". please make it a nice parameter selector
128
- # EDITS: /
129
-
130
- # Checkbox parameters
131
- with gr.Row():
132
- local_captioning = gr.Checkbox(label="Use local Image Captioning", value=False)
133
- local_llm = gr.Checkbox(label="Use local LLM", value=False)
134
- local_music_gen = gr.Checkbox(label="Use local Music Generation", value=False)
135
- # -----ATTRIBUTION-END-----
136
-
137
- # ----ATTRIBUTION-START----
138
- # LLM: ChatGPT4o
139
- # PROMPT: Now, I need sliders for the different models that are used in the product:\n LLM_MAX_NEW_TOKENS = 50\nLLM_TEMPERATURE = 0.7\nLLM_TOP_P = 0.95\nMUSICGEN_MAX_NEW_TOKENS = 256 # 256 = 5 seconds of audio\n they should be in a hidden menu that opens when I click on "advanced options"\nPlease label them for the end user and fit them nicely in the following UI: <code>
140
- # EDITS: added interactive flags
141
- # Advanced options with sliders
142
- with gr.Accordion("Advanced Options", open=False):
143
- gr.Markdown("<h3>LLM Settings</h3>")
144
- llm_max_new_tokens = gr.Slider(1, 200, value=50, step=1, label="LLM Max Tokens", interactive=True)
145
- llm_temperature = gr.Slider(0.0, 1.0, value=0.7, step=0.01, label="LLM Temperature", interactive=True)
146
- llm_top_p = gr.Slider(0.01, 0.99, value=0.95, step=0.01, label="LLM Top P", interactive=True)
147
-
148
- gr.Markdown("<h3>Music Generation Settings</h3>")
149
- musicgen_max_seconds = gr.Slider(1, 30, value=5, step=1, label="MusicGen Duration in Seconds (local model only)", interactive=True)
150
- # -----ATTRIBUTION-END-----
151
-
152
- with gr.Row():
153
- caption_output = gr.Textbox(label="Image Caption")
154
- music_description_output = gr.Textbox(label="Music Description")
155
- durations = gr.Textbox(label="Processing Times", interactive=False, placeholder="Time statistics will appear here")
156
-
157
- music_output = gr.Audio(label="Generated Music")
158
- # Button to trigger the process
159
- generate_button = gr.Button("Generate Music")
160
- generate_button.click(fn=run_image_to_music, inputs=[image_input, llm_max_new_tokens, llm_temperature, llm_top_p, musicgen_max_seconds, local_captioning, local_llm, local_music_gen], outputs=[caption_output, music_description_output, music_output, durations])
161
- # Launch Gradio app
162
- demo.launch(server_port=config.SERVICE_PORT, server_name=config.SERVER_NAME)
163
-
164
- gradio()
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import torch
4
+ from transformers import pipeline
5
+
6
+ # Inference client setup
7
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+ pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dtype=torch.bfloat16, device_map="auto")
9
+
10
+ # Global flag to handle cancellation
11
+ stop_inference = False
12
+
13
+ def style_response(style, response):
14
+ """Modify response style based on the selected style."""
15
+ if style == "Nautical Marauder":
16
+ response = response.replace("you", "ye").replace("hello", "ahoy").replace("friend", "matey")
17
+ response = response.replace("is", "be").replace("my", "me").replace("the", "th'").replace("am", "be")
18
+ elif style == "Elizabethan Prose":
19
+ response = response.replace("you", "thou").replace("are", "art").replace("is", "be").replace("my", "mine")
20
+ response = response.replace("your", "thy").replace("the", "thee").replace("has", "hath").replace("do", "doth")
21
+ elif style == "Cyber Elite":
22
+ response = response.replace("e", "3").replace("a", "4").replace("t", "7").replace("o", "0").replace("i", "1")
23
+ return response
24
+
25
+ def get_css(style):
26
+ """Return corresponding CSS based on the selected style."""
27
+ if style == "Nautical Marauder":
28
+ return """
29
+ body {
30
+ background-color: #2b2b2b;
31
+ font-family: 'Trebuchet MS', sans-serif;
32
+ color: #f4e9c9;
33
+ background-image: url('https://www.transparenttextures.com/patterns/old-map.png');
34
+ }
35
+ .gradio-container {
36
+ background: rgba(0, 0, 0, 0.7);
37
+ border: 2px solid #d4af37;
38
+ box-shadow: 0 4px 8px rgba(255, 255, 255, 0.1);
39
+ }
40
+ .gr-chat {
41
+ font-size: 16px;
42
+ color: #f4e9c9;
43
+ }
44
+ """
45
+ elif style == "Elizabethan Prose":
46
+ return """
47
+ body {
48
+ background-color: #f5f0e1;
49
+ font-family: 'Dancing Script', cursive;
50
+ color: #5c4033;
51
+ background-image: url('https://www.transparenttextures.com/patterns/old-paper.png');
52
+ }
53
+ .gradio-container {
54
+ background: rgba(255, 255, 255, 0.9);
55
+ border: 2px solid #a0522d;
56
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
57
+ }
58
+ .gr-chat {
59
+ font-size: 18px;
60
+ color: #5c4033;
61
+ }
62
+ """
63
+ elif style == "Cyber Elite":
64
+ return """
65
+ body {
66
+ background-color: #000000;
67
+ font-family: 'Courier New', Courier, monospace;
68
+ color: #00ff00;
69
+ }
70
+ .gradio-container {
71
+ background: #1a1a1a;
72
+ border: 2px solid #00ff00;
73
+ box-shadow: 0 4px 8px rgba(0, 255, 0, 0.3);
74
+ }
75
+ .gr-chat {
76
+ font-size: 16px;
77
+ color: #00ff00;
78
+ }
79
+ """
80
+ else:
81
+ # Default style
82
+ return """
83
+ body {
84
+ background-color: #f0f0f0;
85
+ font-family: 'Arial', sans-serif;
86
+ color: #333;
87
+ }
88
+ .gradio-container {
89
+ background: white;
90
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
91
+ border-radius: 10px;
92
+ }
93
+ .gr-chat {
94
+ font-size: 16px;
95
+ color: #333;
96
+ }
97
+ """
98
+
99
+ def respond(message, history: list[tuple[str, str]], style="Standard Conversational"):
100
+ global stop_inference
101
+ stop_inference = False # Reset cancellation flag
102
+
103
+ # Initialize history if it's None
104
+ if history is None:
105
+ history = []
106
+
107
+ # API-based inference
108
+ messages = [{"role": "user", "content": message}]
109
+
110
+ response = ""
111
+ for message_chunk in client.chat_completion(
112
+ messages,
113
+ max_tokens=512, # Default max tokens for response
114
+ stream=True,
115
+ temperature=0.7, # Default temperature
116
+ top_p=0.95, # Default top-p
117
+ ):
118
+ if stop_inference:
119
+ response = "Inference cancelled."
120
+ yield history + [(message, response)]
121
+ return
122
+ token = message_chunk.choices[0].delta.content
123
+ response += token
124
+ yield history + [(message, style_response(style, response))] # Apply selected style to the response
125
+
126
+
127
+ def cancel_inference():
128
+ global stop_inference
129
+ stop_inference = True
130
+
131
+ # Define the interface
132
+ with gr.Blocks() as demo:
133
+ gr.Markdown("<h1 style='text-align: center;'>🌟 Fancy AI Chatbot 🌟</h1>")
134
+ gr.Markdown("Interact with the AI chatbot using customizable settings below.")
135
+
136
+ chat_history = gr.Chatbot(label="Chat")
137
+
138
+ user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
139
+
140
+ cancel_button = gr.Button("Cancel Inference", variant="danger")
141
+
142
+ # New feature: Style selection with more formal names
143
+ style_selection = gr.Dropdown(
144
+ label="Response Style",
145
+ choices=["Standard Conversational", "Nautical Marauder", "Elizabethan Prose", "Cyber Elite"],
146
+ value="Standard Conversational"
147
+ )
148
+
149
+ def apply_css(style):
150
+ return get_css(style)
151
+
152
+ # Adjusted to ensure history is maintained and passed correctly
153
+ user_input.submit(respond, [user_input, chat_history, style_selection], chat_history)
154
+ style_selection.change(apply_css, style_selection, gr.CSS())
155
+ cancel_button.click(cancel_inference)
156
+
157
+ if __name__ == "__main__":
158
+ demo.launch(share=False) # Remove share=True because it's not supported on HF Spaces
 
attribution_example.py DELETED
@@ -1,8 +0,0 @@
1
- # Example Code Attribution for AI-Generated Code
2
-
3
- # ----ATTRIBUTION-START----
4
- # LLM: Github Copilot
5
- # PROMPT: write a hello world example
6
- # EDITS: changed the wording to make it more personal
7
- print("Hello, World! This is your Copilot speaking!")
8
- # -----ATTRIBUTION-END-----
 
 
 
 
 
 
 
 
 
blip_image_caption_large.py DELETED
@@ -1,29 +0,0 @@
1
- # external imports
2
- from transformers import pipeline
3
- from huggingface_hub import InferenceClient
4
-
5
- # local imports
6
- import config
7
-
8
- class Blip_Image_Caption_Large:
9
- def __init__(self):
10
- pass
11
-
12
- def caption_image(self, image_path, use_local_caption):
13
- if use_local_caption:
14
- return self.caption_image_local_pipeline(image_path)
15
- else:
16
- return self.caption_image_api(image_path)
17
-
18
- def caption_image_local_pipeline(self, image_path):
19
- self.local_pipeline = pipeline("image-to-text", model=config.IMAGE_CAPTION_MODEL)
20
- result = self.local_pipeline(image_path)[0]['generated_text']
21
- return result
22
-
23
- def caption_image_api(self, image_path):
24
- client = InferenceClient(config.IMAGE_CAPTION_MODEL, token=config.HF_API_TOKEN)
25
- try:
26
- result = client.image_to_text(image_path).generated_text
27
- except Exception as e:
28
- result = f"Error: {e}"
29
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
config.py DELETED
@@ -1,38 +0,0 @@
1
- import os
2
- import logging as log
3
-
4
- log.basicConfig(level=log.INFO)
5
-
6
- SERVICE_PORT = 7860
7
- SERVER_NAME = "0.0.0.0"
8
-
9
- IMAGE_CAPTION_MODEL = "Salesforce/blip-image-captioning-large"
10
-
11
- LLM_MODEL = "microsoft/Phi-3-mini-4k-instruct"
12
- LLM_MAX_LENGTH = 50
13
- LLM_MAX_NEW_TOKENS = 50
14
- LLM_TEMPERATURE = 0.7
15
- LLM_TOP_P = 0.95
16
-
17
- MUSICGEN_MODEL = "facebook/musicgen-small"
18
- MUSICGEN_MODEL_API_URL = f"https://api-inference.huggingface.co/models/{MUSICGEN_MODEL}"
19
- MUSICGEN_MAX_NEW_TOKENS = 256 # 5 seconds of audio
20
-
21
- AUDIO_DIR = "Case-Study-1/data/"
22
-
23
- HF_API_TOKEN = os.getenv("HF_API_TOKEN")
24
-
25
- if HF_API_TOKEN:
26
- log.info(f"Read HF_API_TOKEN: {HF_API_TOKEN[0:4]}...")
27
- else:
28
- print("HF_API_TOKEN not found in environment variables.")
29
-
30
- # ----ATTRIBUTION-START----
31
- # LLM: Github Copilot
32
- # PROMPT: create an output folder for the generated audio files
33
- # EDITS: /
34
- def create_output_folder():
35
- os.makedirs(AUDIO_DIR, exist_ok=True)
36
- # -----ATTRIBUTION-END-----
37
-
38
- create_output_folder()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
data/Students_taking_computerized_exam.jpg DELETED
Binary file (329 kB)
 
musicgen_small.py DELETED
@@ -1,48 +0,0 @@
1
- # external imports
2
- from transformers import pipeline
3
- from io import BytesIO
4
- import requests
5
- import scipy
6
-
7
- # local imports
8
- import config
9
-
10
- class Musicgen_Small:
11
- def __init__(self):
12
- pass
13
-
14
- def generate_music(self, prompt, audio_path, use_local_musicgen):
15
- if use_local_musicgen:
16
- self.generate_music_local_pipeline(prompt, audio_path)
17
- else:
18
- self.generate_music_api(prompt, audio_path)
19
-
20
- def generate_music_local_pipeline(self, prompt, audio_path):
21
- self.local_pipeline = pipeline("text-to-audio", model=config.MUSICGEN_MODEL)
22
- music = self.local_pipeline(prompt, forward_params={"do_sample": True, "max_new_tokens": config.MUSICGEN_MAX_NEW_TOKENS})
23
- scipy.io.wavfile.write(audio_path, rate=music["sampling_rate"], data=music["audio"])
24
-
25
- def generate_music_api(self, prompt, audio_path):
26
- headers = {"Authorization": f"Bearer {config.HF_API_TOKEN}"}
27
- payload = {
28
- "inputs": prompt
29
- }
30
-
31
- response = requests.post(config.MUSICGEN_MODEL_API_URL, headers=headers, json=payload)
32
-
33
- # ----ATTRIBUTION-START----
34
- # LLM: ChatGPT4o
35
- # PROMPT: please save the audio to a .wav file
36
- # EDITS: changed variables to match the code
37
-
38
- # Convert the byte content into an audio array
39
- try:
40
- audio_buffer = BytesIO(response.content)
41
- # Use scipy to save the audio, assuming it's a WAV format audio stream
42
- # If it's raw PCM audio, you would need to decode it first.
43
- with open(audio_path, "wb") as f:
44
- f.write(audio_buffer.read())
45
- # -----ATTRIBUTION-END-----
46
- except Exception as e:
47
- print(f"Error: {e}")
48
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
phi3_mini_4k_instruct.py DELETED
@@ -1,45 +0,0 @@
1
- # external imports
2
- from transformers import pipeline
3
- from huggingface_hub import InferenceClient
4
- import torch
5
- # local imports
6
- import config
7
- from llama_cpp import Llama
8
-
9
-
10
- class Phi3_Mini_4k_Instruct:
11
- def __init__(self):
12
- pass
13
-
14
- def generate_text(self, messages, use_local_llm):
15
- if use_local_llm:
16
- return self.generate_text_llama_cpp(messages)
17
- else:
18
- return self.generate_text_api(messages)
19
-
20
- def generate_text_llama_cpp(self, messages):
21
- model = Llama.from_pretrained(
22
- repo_id="microsoft/Phi-3-mini-4k-instruct-gguf",
23
- filename="Phi-3-mini-4k-instruct-q4.gguf"
24
- )
25
- response = model.create_chat_completion(messages)
26
- generated_message = response['choices'][0]['message']['content']
27
-
28
- return generated_message
29
-
30
- def generate_text_local_pipeline(self, messages):
31
- self.local_pipeline = pipeline("text-generation", model=config.LLM_MODEL, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto")
32
- self.local_pipeline.model.config.max_length = config.LLM_MAX_LENGTH
33
- self.local_pipeline.model.config.max_new_tokens = config.LLM_MAX_NEW_TOKENS
34
- self.local_pipeline.model.config.temperature = config.LLM_TEMPERATURE
35
- self.local_pipeline.model.config.top_p = config.LLM_TOP_P
36
- result = self.local_pipeline(messages)[-1]['generated_text'][-1]['content']
37
- return result
38
-
39
- def generate_text_api(self, messages):
40
- client = InferenceClient(config.LLM_MODEL, token=config.HF_API_TOKEN)
41
- try:
42
- result = client.chat_completion(messages, max_tokens=config.LLM_MAX_NEW_TOKENS, temperature=config.LLM_TEMPERATURE, top_p=config.LLM_TOP_P).choices[0].message.content
43
- except Exception as e:
44
- result = f"Error: {e}"
45
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,74 +1,5 @@
1
- accelerate==0.34.2
2
- aiofiles==23.2.1
3
- annotated-types==0.7.0
4
- anyio==4.4.0
5
- certifi==2024.8.30
6
- charset-normalizer==3.3.2
7
- click==8.1.7
8
- contourpy==1.3.0
9
- cycler==0.12.1
10
- fastapi==0.114.2
11
- ffmpy==0.4.0
12
- filelock==3.16.0
13
- fonttools==4.53.1
14
- fsspec==2024.9.0
15
- gradio==4.44.0
16
- gradio_client==1.3.0
17
- h11==0.14.0
18
- httpcore==1.0.5
19
- httpx==0.27.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
- llama_cpp_python==0.3.1
27
- markdown-it-py==3.0.0
28
- MarkupSafe==2.1.5
29
- matplotlib==3.9.2
30
- mdurl==0.1.2
31
- mpmath==1.3.0
32
- networkx==3.3
33
- numpy==2.1.1
34
- orjson==3.10.7
35
- packaging==24.1
36
- pandas==2.2.2
37
- pillow==10.4.0
38
- pluggy==1.5.0
39
- psutil==6.0.0
40
- pydantic==2.9.1
41
- pydantic_core==2.23.3
42
- pydub==0.25.1
43
- Pygments==2.18.0
44
- pyparsing==3.1.4
45
- pytest==8.3.3
46
- python-dateutil==2.9.0.post0
47
- python-multipart==0.0.9
48
- pytz==2024.2
49
- PyYAML==6.0.2
50
- regex==2024.9.11
51
- requests==2.32.3
52
- rich==13.8.1
53
- ruff==0.6.5
54
- safetensors==0.4.5
55
- scipy==1.14.1
56
- semantic-version==2.10.0
57
- shellingham==1.5.4
58
- six==1.16.0
59
- sniffio==1.3.1
60
- starlette==0.38.5
61
- sympy==1.13.2
62
- tokenizers==0.19.1
63
- tomlkit==0.12.0
64
- torch==2.4.1
65
- torchaudio==2.4.1
66
- torchvision==0.19.1
67
- tqdm==4.66.5
68
- transformers==4.44.2
69
- typer==0.12.5
70
- typing_extensions==4.12.2
71
- tzdata==2024.1
72
- urllib3==2.2.2
73
- uvicorn==0.30.6
74
- websockets==12.0
 
1
+ huggingface_hub==0.23.*
2
+ gradio==4.39.*
3
+ torch==2.4.*
4
+ transformers==4.43.*
5
+ accelerate==0.33.*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setup.sh DELETED
@@ -1,52 +0,0 @@
1
- #!/bin/bash
2
-
3
- VENV_DIR="venv"
4
-
5
- #ensure package list
6
- sudo add-apt-repository -y universe
7
- #ensure python and requirements is installed
8
- sudo apt install -qq -y python3-venv
9
- sudo apt install -qq -y python3-pip
10
- sudo apt install -y build-essential
11
- sudo apt install -y gcc g++
12
- sudo apt install -y screen
13
-
14
-
15
- # Check if the virtual environment exists
16
- if [ ! -d "$VENV_DIR" ]; then
17
- echo "Virtual environment not found. Creating a new one..."
18
- # Create a virtual environment
19
- python3 -m venv "$VENV_DIR"
20
- echo "Virtual environment created."
21
-
22
- else
23
- echo "Virtual environment found."
24
- fi
25
-
26
- # Activate the virtual environment
27
- source "$VENV_DIR/bin/activate"
28
- echo "Virtual environment $VENV_DIR activated."
29
-
30
- pip install --upgrade pip
31
-
32
-
33
- if git pull | grep -q 'Already up to date.'; then
34
- echo "Repository is up to date. Proceeding with setup."
35
-
36
- else
37
- echo "Repository updated successfully. Proceeding to next step."
38
- fi
39
-
40
- echo "Checking if http://127.0.0.1:7860 is running..."
41
- if curl -s --head http://127.0.0.1:7860 | grep "200 OK" > /dev/null; then
42
- echo "URL is running.No further action required. Exiting."
43
- exit 0 # Exit script since the service is already running
44
- else
45
- echo "URL is not running.Proceeding with setup."
46
- # Install dependencies and run the application
47
- pip install -r requirements.txt
48
-
49
- screen -S "app" -d -m bash -c 'python3 app.py'
50
- fi
51
- deactivate
52
- exit 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setupaccess.exp DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/expect -f
2
- set PASSPHRASE_GROUP17 [lindex $argv 0]
3
- spawn ./setupaccess.sh
4
- set timeout 5
5
- expect "The authenticity of host"
6
- send "yes\r"
7
- expect "Enter passphrase for key 'group17':\r"
8
- send "$PASSPHRASE_GROUP17\r"
9
- expect "Enter passphrase for key 'group17':\r"
10
- send "$PASSPHRASE_GROUP17\r"
11
- expect "Enter passphrase for key 'group17':\r"
12
- send "$PASSPHRASE_GROUP17\r"
13
- expect eof
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setupaccess.sh DELETED
@@ -1,45 +0,0 @@
1
- #!/bin/bash
2
-
3
- touch group17.pub
4
- echo "$GROUP17_PUBLICKKEY" > group17.pub
5
- echo "setupaccess.sh: make group17.pub file"
6
-
7
- touch group17
8
- echo "$GROUP17_PRIVATEKEY" > group17
9
- echo "setupaccess.sh: make group17 file"
10
-
11
- chmod 600 group17
12
- echo "setupaccess.sh: change permissions of group17 file"
13
-
14
- ssh-keygen -R "[paffenroth-23.dyn.wpi.edu]:22017"
15
- echo "setupaccess.sh: remove known host keys for the server to avoid the REMOTE HOST IDENTIFICATION HAS CHANGED error"
16
-
17
- cat group17.pub > authorized_keys
18
- echo "setupaccess.sh: make an authorized_keys file with group17.pub as an authorized key"
19
-
20
- rm group17.pub
21
- echo "setupaccess.sh: remove group17.pub file from host"
22
-
23
- scpOutput=$(scp -o StrictHostKeyChecking=no -i group17 -P 22017 authorized_keys student-admin@paffenroth-23.dyn.wpi.edu:/home/student-admin/.ssh 2>&1)
24
- echo "setupaccess.sh: try to copy authorized_keys file to server"
25
-
26
- if [[ "$scpOutput" = *"student-admin@paffenroth-23.dyn.wpi.edu: Permission denied (publickey)."* ]];
27
- then touch student-admin_key
28
- echo "$STUDENT_ADMIN_KEY" > student-admin_key
29
- echo "setupaccess.sh: make student-admin_key file"
30
- scp -o StrictHostKeyChecking=no -i student-admin_key -P 22017 authorized_keys student-admin@paffenroth-23.dyn.wpi.edu:/home/student-admin/.ssh
31
- echo "setupaccess.sh: copied authorized_keys file to server with student-admin_key"
32
- rm student-admin_key
33
- echo "setupaccess.sh: remove student-admin_key from host"
34
- else
35
- echo "setupaccess.sh: copied authorized_keys file to server with our private key"
36
- fi
37
-
38
- rm authorized_keys
39
- echo "setupaccess.sh: remove authorized_keys file from host"
40
-
41
- ssh -p 22017 -i group17 -o StrictHostKeyChecking=no student-admin@paffenroth-23.dyn.wpi.edu
42
- echo "setupaccess.sh: try to ssh in"
43
-
44
- rm group17
45
- echo "setupaccess.sh: remove group17 file from host"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
test_blip_image_caption_large.py DELETED
@@ -1,16 +0,0 @@
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 = "data/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 = "data/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 DELETED
@@ -1,26 +0,0 @@
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 DELETED
@@ -1,20 +0,0 @@
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 != ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tmp.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ ponytails are really cool 3