Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
from langchain.llms import OpenAI as LangChainOpenAI
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain_core.runnables import RunnablePassthrough
|
5 |
from langchain.chains import LLMChain
|
|
|
|
|
6 |
import requests
|
7 |
import numpy as np
|
8 |
import os
|
@@ -14,7 +16,9 @@ from scipy.io import wavfile
|
|
14 |
from pydub import AudioSegment
|
15 |
|
16 |
# Initialize OpenAI clients
|
17 |
-
|
|
|
|
|
18 |
openai_client = OpenAI()
|
19 |
|
20 |
# Create a prompt template for the story
|
@@ -25,8 +29,8 @@ Topic: {topic}
|
|
25 |
Story:
|
26 |
"""
|
27 |
|
28 |
-
story_prompt = PromptTemplate(
|
29 |
-
|
30 |
|
31 |
# Create a prompt template for the music based on the story prompt
|
32 |
music_template = """
|
@@ -43,11 +47,12 @@ Story Topic: {topic}
|
|
43 |
Music Prompt:
|
44 |
"""
|
45 |
|
46 |
-
music_prompt = PromptTemplate(
|
47 |
|
48 |
# Create LLMChains
|
49 |
-
story_chain = LLMChain(llm=llm, prompt=story_prompt)
|
50 |
-
music_chain = LLMChain(llm=llm, prompt=music_prompt)
|
|
|
51 |
|
52 |
# Configuration for music generation
|
53 |
def get_config():
|
@@ -151,7 +156,7 @@ def generate_story_and_music(topic):
|
|
151 |
with tempfile.TemporaryDirectory() as temp_dir:
|
152 |
try:
|
153 |
# Generate the story
|
154 |
-
story = story_chain.
|
155 |
|
156 |
# Ensure the story has 5 paragraphs
|
157 |
paragraphs = story.split('\n\n')
|
@@ -166,7 +171,7 @@ def generate_story_and_music(topic):
|
|
166 |
|
167 |
try:
|
168 |
# Generate the music prompt based on the topic
|
169 |
-
final_music_prompt = music_chain.
|
170 |
except Exception as e:
|
171 |
print(f"Error generating music prompt: {e}")
|
172 |
final_music_prompt = "Piano and strings creating a melancholic yet hopeful mood with moderate energy in a contemporary classical style"
|
|
|
1 |
import gradio as gr
|
2 |
+
# from langchain.llms import OpenAI as LangChainOpenAI
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain_core.runnables import RunnablePassthrough
|
5 |
from langchain.chains import LLMChain
|
6 |
+
from langchain_openai import ChatOpenAI
|
7 |
+
|
8 |
import requests
|
9 |
import numpy as np
|
10 |
import os
|
|
|
16 |
from pydub import AudioSegment
|
17 |
|
18 |
# Initialize OpenAI clients
|
19 |
+
|
20 |
+
llm = ChatOpenAI(temperature=0, model_name="gpt-4o-mini")
|
21 |
+
# llm = LangChainOpenAI(model_name="gpt-4o-mini")
|
22 |
openai_client = OpenAI()
|
23 |
|
24 |
# Create a prompt template for the story
|
|
|
29 |
Story:
|
30 |
"""
|
31 |
|
32 |
+
story_prompt = PromptTemplate.from_template(story_template)
|
33 |
+
story_chain = (RunnablePassthrough() | story_prompt | llm)
|
34 |
|
35 |
# Create a prompt template for the music based on the story prompt
|
36 |
music_template = """
|
|
|
47 |
Music Prompt:
|
48 |
"""
|
49 |
|
50 |
+
music_prompt = PromptTemplate.from_template(music_template)
|
51 |
|
52 |
# Create LLMChains
|
53 |
+
# story_chain = LLMChain(llm=llm, prompt=story_prompt)
|
54 |
+
# music_chain = LLMChain(llm=llm, prompt=music_prompt)
|
55 |
+
music_chain = (RunnablePassthrough() | music_prompt | llm)
|
56 |
|
57 |
# Configuration for music generation
|
58 |
def get_config():
|
|
|
156 |
with tempfile.TemporaryDirectory() as temp_dir:
|
157 |
try:
|
158 |
# Generate the story
|
159 |
+
story = story_chain.invoke({"topic":topic}).content
|
160 |
|
161 |
# Ensure the story has 5 paragraphs
|
162 |
paragraphs = story.split('\n\n')
|
|
|
171 |
|
172 |
try:
|
173 |
# Generate the music prompt based on the topic
|
174 |
+
final_music_prompt = music_chain.invoke({"topic":topic}).content.strip()
|
175 |
except Exception as e:
|
176 |
print(f"Error generating music prompt: {e}")
|
177 |
final_music_prompt = "Piano and strings creating a melancholic yet hopeful mood with moderate energy in a contemporary classical style"
|