File size: 4,662 Bytes
a537178
6113e88
593ffbb
efaf1df
6113e88
8570956
6113e88
 
462bf87
741de78
711729c
e413191
593ffbb
6113e88
efaf1df
c6200d7
efaf1df
c6200d7
6113e88
c2ba00e
c6200d7
c2ba00e
 
1dd989d
5a75808
 
 
 
 
 
 
 
 
1dd989d
c2ba00e
 
 
 
 
 
1dd989d
c2ba00e
6113e88
 
a566cd3
6113e88
025cdfe
dcace2c
593ffbb
efaf1df
593ffbb
 
 
462bf87
593ffbb
 
a566cd3
24bc888
d457362
 
1a10e5a
a3c31c2
 
 
 
1a10e5a
a3c31c2
 
 
 
0bf3bf7
ed735b9
 
 
 
 
12db9e7
8570956
 
 
 
6113e88
8570956
6113e88
8570956
 
 
 
 
 
1dd989d
8570956
178dcf0
6113e88
 
 
 
 
 
 
 
 
 
a537178
 
6113e88
27d8b8d
8ea45e7
 
 
 
 
 
 
 
a537178
6113e88
 
 
 
 
 
 
178dcf0
6113e88
 
 
 
a537178
c9663b3
6113e88
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import gradio as gr
from gradio.themes import Soft
import os
from dotenv import load_dotenv
from transcript import transcribe_audio  # Import the transcription function
from metadata import info
import numpy as np
import requests
from scipy.io.wavfile import write
from groq import Groq
import json
from genius import get_song_info_from_lyrics

# Load environment variables from .env file
load_dotenv()
client = Groq(
    api_key=os.environ["GROQ_API_KEY"],
)

def song_history(message):
    # question = "Tell me the history or a fun fact of the song (in a single line!): " + message
    completion = client.chat.completions.create(
        model="llama3-8b-8192",
        messages=[
            {
                "role": "system",
                "content": "You are a professor of music at Berklee College of Music."
            },
            # Set a user message for the assistant to respond to.
            {
                "role": "user",
                "content": "Please explain the historical and musical significance of " + message + ". Please also break down the instruments used in the song. Keep it just one line!",
            }
        ],
        temperature=1,
        max_completion_tokens=1024,
        top_p=1,
        stream=True,
        stop=None,
    )
    return completion.choices[0].message.content

def analyze_song(mp3_file):
    # Call the transcription function and get the transcription
    # Pass the mp3 filename to transcript.py

    os.makedirs("out", exist_ok=True)
    write('test.wav', mp3_file[0], mp3_file[1])
    data = {
        'api_token': os.environ["AUDD_API"],
        'return': 'apple_music,spotify',
    }
    files = {
        'file': open('test.wav', 'rb'),
    }
    result = requests.post('https://api.audd.io/', data=data, files=files)
    transcription = transcribe_audio('test.wav')
    if result.json()['result']['artist'] is not None:
        artist = result.json()['result']['artist']
        title = result.json()['result']['title']
        # genre = result.json()['result']['apple_music']['genreNames']
        try:
            genre_list = result.json()['result']['apple_music']['genreNames']
        except:
            genre_list = ['']
        genre = ", ".join(genre_list)
        try:
            image_url = result.json()['result']['spotify']['album']['images'][0]['url']
        except:
            image_url = ""
    else:
        image_url = ""
        genre = ""
        genius_response = get_song_info_from_lyrics(transcription)
        title = genius_response["title"]
        artist = genius_response["artist"]
    # else:
    song_data = json.loads(info(title, artist))
    if (genre == None):
        genre = song_data["genre"]
    # print(song_data)
    # Placeholder for song analysis logic
    instrumentation = str(song_data["instrumentation"])[1:-2].replace('\'', '')
    lyrics = transcription  # Use the transcription as lyrics
    tempo_key = song_data["tempo"] + ", " + song_data["key"]
    history = song_data["historical"]

    # instrumentation = "Sample Instrumentation"
    # lyrics = transcription  # Use the transcription as lyrics
    # tempo_key = "Sample Tempo/Key"
    # history = "History"
    # history = song_history(title)
    return title, artist, genre, instrumentation, lyrics, tempo_key, history, image_url  # Return the mp3 file for replay

# Custom CSS to set a fun musical theme image as background
css = """
body {
    background-image: url('https://example.com/path/to/your/musical-theme-image.jpg'); /* Replace with your image URL */
    background-size: cover; /* Cover the entire background */
    background-repeat: no-repeat; /* Prevent repeating the image */
    background-position: center; /* Center the image */
}
"""

demo = gr.Interface(
    fn=analyze_song,
    inputs=gr.Audio(label="Record Audio", sources=["upload", "microphone"],
    waveform_options=gr.WaveformOptions(
        waveform_color="#01C6FF",
        waveform_progress_color="#0066B4",
        skip_length=2,
        show_controls=False,
        ),
    ),  # Record audio in MP3 format
    
    outputs=[
        gr.Textbox(label="Title"),
        gr.Textbox(label="Artist"),
        gr.Textbox(label="Genre"),
        gr.Textbox(label="Instrumentation"),
        gr.Textbox(label="Lyrics"),
        gr.Textbox(label="Tempo/Key"),
        gr.Textbox(label="History"),
        # gr.Audio(label="Replay Recorded Audio"),  # Add an output for replaying the recorded audio
        gr.Image(label="Cover")  # Gives the Cover image
    ],
    theme=Soft(),  # Apply the Soft theme
    title="Concert Buddy πŸ˜„πŸŽ΅πŸͺ©"  # Apply custom CSS for background image
)

demo.launch()