bizvideoschool commited on
Commit
1cfedfa
·
verified ·
1 Parent(s): 5960c5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,34 +1,38 @@
1
- import openai
2
  import os
 
3
  import streamlit as st
4
  from tenacity import retry, wait_fixed, stop_after_attempt
5
 
6
- # Set OpenAI API key from environment variables
7
- openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
  # Define the initial system message
10
- initial_messages = [{"role": "system", "content": """Please act as a marketing expert for real estate agents. Your role is
 
 
11
  to generate topic summary ideas for social media videos. Follow these steps in this order:
12
  1. Before you execute any steps, consider the last input from the user as a suggestion for the types of topics you should create if
13
  they submit one. If they don't submit a topic idea then assume they would like ideas for marketing videos for a real estate agent.
14
  2. Generate 100 total ideas for videos a real estate agent should make. Some should be ideas
15
  for simple marketing videos, creative social media content, educational videos, and a few that are outside the box.
16
- Reply with the 10 overall best ideas. Include a short, up to 2 sentence long description of each idea. Do not return all 100 ideas."""}]
 
17
 
18
- @retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
19
  def call_openai_api(messages):
20
  """
21
  Call OpenAI's ChatCompletion API with retries for transient errors.
22
  """
23
  try:
24
- response = openai.ChatCompletion.create(
25
- model="gpt-3.5-turbo",
26
  messages=messages,
27
- max_tokens=1000 # Adjust token limit to fit your needs
28
  )
29
  return response
30
- except openai.error.OpenAIError as e:
31
- raise RuntimeError(f"OpenAI API error: {e}")
 
32
 
33
  def CustomChatGPT(user_input, messages):
34
  """
@@ -40,28 +44,24 @@ def CustomChatGPT(user_input, messages):
40
  messages.append({"role": "user", "content": "Please generate general video ideas for a real estate agent."})
41
 
42
  response = call_openai_api(messages)
43
- ChatGPT_reply = response["choices"][0]["message"]["content"]
44
  messages.append({"role": "assistant", "content": ChatGPT_reply})
45
  return ChatGPT_reply, messages
46
 
47
  # Streamlit app setup
48
  st.title("Video Idea Generator for Real Estate Agents")
49
- st.write("Enter a topic suggestion or leave it blank for general video ideas.")
50
 
51
- # User input section
52
- user_input = st.text_input("Enter a topic:", placeholder="E.g., tips for first-time homebuyers")
53
- generate_button = st.button("Generate Ideas")
54
 
55
- # Generate ideas on button click
56
- if generate_button:
57
  messages = initial_messages.copy()
58
  try:
59
  with st.spinner("Generating video ideas..."):
60
- reply, _ = CustomChatGPT(user_input, messages)
61
- st.subheader("Here are the top video ideas:")
62
  st.write(reply)
63
- except RuntimeError as e:
64
- st.error(f"Error: {e}")
65
  except Exception as e:
66
- st.error("An unexpected error occurred. Please try again.")
67
-
 
 
1
  import os
2
+ from openai import OpenAI
3
  import streamlit as st
4
  from tenacity import retry, wait_fixed, stop_after_attempt
5
 
6
+ # Initialize OpenAI client
7
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
8
 
9
  # Define the initial system message
10
+ initial_messages = [{
11
+ "role": "system",
12
+ "content": """Please act as a marketing expert for real estate agents. Your role is
13
  to generate topic summary ideas for social media videos. Follow these steps in this order:
14
  1. Before you execute any steps, consider the last input from the user as a suggestion for the types of topics you should create if
15
  they submit one. If they don't submit a topic idea then assume they would like ideas for marketing videos for a real estate agent.
16
  2. Generate 100 total ideas for videos a real estate agent should make. Some should be ideas
17
  for simple marketing videos, creative social media content, educational videos, and a few that are outside the box.
18
+ Reply with the 10 overall best ideas. Include a short, up to 2 sentence long description of each idea. Do not return all 100 ideas."""
19
+ }]
20
 
21
+ @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
22
  def call_openai_api(messages):
23
  """
24
  Call OpenAI's ChatCompletion API with retries for transient errors.
25
  """
26
  try:
27
+ response = client.chat.completions.create(
28
+ model="gpt-4",
29
  messages=messages,
30
+ max_tokens=1000
31
  )
32
  return response
33
+ except Exception as e:
34
+ st.error(f"OpenAI API Error: {str(e)}")
35
+ raise
36
 
37
  def CustomChatGPT(user_input, messages):
38
  """
 
44
  messages.append({"role": "user", "content": "Please generate general video ideas for a real estate agent."})
45
 
46
  response = call_openai_api(messages)
47
+ ChatGPT_reply = response.choices[0].message.content
48
  messages.append({"role": "assistant", "content": ChatGPT_reply})
49
  return ChatGPT_reply, messages
50
 
51
  # Streamlit app setup
52
  st.title("Video Idea Generator for Real Estate Agents")
53
+ st.subheader("Generate tailored video content ideas for your real estate marketing!")
54
 
55
+ # User input
56
+ user_input = st.text_input("Enter a topic suggestion (optional)", placeholder="E.g., tips for first-time homebuyers")
 
57
 
58
+ # Button to trigger generation
59
+ if st.button("Generate Video Ideas"):
60
  messages = initial_messages.copy()
61
  try:
62
  with st.spinner("Generating video ideas..."):
63
+ reply, messages = CustomChatGPT(user_input, messages)
64
+ st.markdown("### Suggested Video Ideas")
65
  st.write(reply)
 
 
66
  except Exception as e:
67
+ st.error(f"An error occurred: {e}")