-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
77 lines (63 loc) · 2.13 KB
/
app.py
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
import gradio as gr
import scipy
from datasets import load_dataset
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from transformers import AutoProcessor, MusicgenForConditionalGeneration, pipeline
def imageToText(url):
image_to_text = pipeline(
"image-to-text", model="Salesforce/blip-image-captioning-large"
)
text = image_to_text(url)
return text[0]["generated_text"]
def storyGeneratorGPT(user_input):
template = """
You are a music story teller;
You can suggest music that suits the scenario;
The suggested music should include the genre of the music as well as the style where it is inpired from;
The suggestion should be no more than 20 words.
CONTEXT: {scenario}
STORY:
"""
prompt = PromptTemplate(template=template, input_variables=["scenario"])
prompt.format(scenario=user_input)
story_chain = LLMChain(
llm=OpenAI(model_name="gpt-3.5-turbo", temperature=1),
prompt=prompt,
verbose=True,
)
story = story_chain.run(user_input)
# print(story)
return story
def generate(text):
print("generate..")
print(text)
processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
inputs = processor(
# audio=load_input(),
text=[text],
padding=True,
return_tensors="pt",
)
audio_values = model.generate(**inputs, max_new_tokens=256) # 256
sampling_rate = model.config.audio_encoder.sampling_rate
resultFile = "musicgen_out.wav"
scipy.io.wavfile.write(
resultFile,
rate=sampling_rate,
data=audio_values[0, 0].numpy(),
)
return resultFile
series_1 = gr.Interface(
fn=imageToText,
inputs="pil",
outputs="text",
examples=["beatles.png"],
)
series_2 = gr.Interface(fn=storyGeneratorGPT, inputs="text", outputs="text")
series_3 = gr.Interface(fn=generate, inputs="text", outputs="video")
demo = gr.Series(series_1, series_2, series_3)
if __name__ == "__main__":
demo.launch()