Spaces:
Sleeping
Sleeping
MikaelYu-I
commited on
Commit
•
53e9fb6
1
Parent(s):
3e9959e
poem_gen
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import openai
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
# Set the OpenAI API key
|
7 |
+
os.environ["OPENAI_API_KEY"] = "sk-proj-yCa8xuZojVBCMM0r7q3hT3BlbkFJwjPh2CN3opBzjby3hg2J"
|
8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
+
|
10 |
+
# Define the prompt template
|
11 |
+
def generate_poem(theme):
|
12 |
+
response = openai.ChatCompletion.create(
|
13 |
+
model="gpt-3.5-turbo",
|
14 |
+
messages=[
|
15 |
+
{"role": "system", "content": "You are a poet."},
|
16 |
+
{"role": "user", "content": f"Write a poem about {theme}"},
|
17 |
+
],
|
18 |
+
max_tokens=150,
|
19 |
+
temperature=0.7,
|
20 |
+
)
|
21 |
+
return response['choices'][0]['message']['content'].strip()
|
22 |
+
|
23 |
+
# Function to chain prompts
|
24 |
+
def chain_prompts(initial_theme):
|
25 |
+
initial_output = generate_poem(initial_theme)
|
26 |
+
refined_prompt = f"Expand on this idea: {initial_output}"
|
27 |
+
refined_output = generate_poem(refined_prompt)
|
28 |
+
return refined_output
|
29 |
+
|
30 |
+
# Streamlit app
|
31 |
+
st.title("Poetry Generator")
|
32 |
+
theme = st.text_input("Enter a theme or prompt for your poem:")
|
33 |
+
|
34 |
+
# Generate poem button
|
35 |
+
if st.button("Generate Poem"):
|
36 |
+
poem = chain_prompts(theme)
|
37 |
+
st.write(poem)
|