-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathchat.py
74 lines (56 loc) · 2.12 KB
/
chat.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
import os
import replicate
import streamlit as st
from dotenv import load_dotenv
from elevenlabs import generate
from langchain import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import OpenAI
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
eleven_api_key = os.getenv("ELEVEN_API_KEY")
llm = OpenAI(temperature=0.9)
def generate_story(text):
"""Generate a story using the langchain library and OpenAI's GPT-3 model."""
prompt = PromptTemplate(
input_variables=["text"],
template="""
You are a fun and seasoned storyteller. Generate a story for me about {text}.
"""
)
story = LLMChain(llm=llm, prompt=prompt)
return story.run(text=text)
def generate_audio(text, voice):
"""Convert the generated story to audio using the Eleven Labs API."""
audio = generate(text=text, voice=voice, api_key=eleven_api_key)
return audio
def generate_images(story_text):
"""Generate images using the story text using the Replicate API."""
output = replicate.run(
"stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
input={"prompt": story_text}
)
return output
def app():
st.title("Story Storm")
with st.form(key='my_form'):
text = st.text_input(
"Enter a word to generate a story",
max_chars=None,
type="default",
placeholder="Enter a word to generate a story",
)
options = ["Bella", "Antoni", "Arnold", "Adam", "Domi", "Elli", "Josh", "Rachel", "Sam"]
voice = st.selectbox("Select a voice", options)
if st.form_submit_button("Submit"):
with st.spinner('Generating story...'):
story_text = generate_story(text)
audio = generate_audio(story_text, voice)
st.audio(audio, format='audio/mp3')
images = generate_images(story_text)
for item in images:
st.image(item)
if not text or not voice:
st.info("Please enter a word and select a voice")
if __name__ == '__main__':
app()