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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -10
app.py CHANGED
@@ -4,7 +4,7 @@ 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.environ["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
@@ -17,13 +17,28 @@ Reply with the 10 overall best ideas. Include a short, up to 2 sentence long des
17
 
18
  @retry(stop=stop_after_attempt(3), wait=wait_fixed(1))
19
  def call_openai_api(messages):
20
- return openai.ChatCompletion.create(
21
- model="gpt-3.5-turbo",
22
- messages=messages
23
- )
 
 
 
 
 
 
 
 
24
 
25
  def CustomChatGPT(user_input, messages):
26
- messages.append({"role": "user", "content": user_input})
 
 
 
 
 
 
 
27
  response = call_openai_api(messages)
28
  ChatGPT_reply = response["choices"][0]["message"]["content"]
29
  messages.append({"role": "assistant", "content": ChatGPT_reply})
@@ -33,11 +48,20 @@ def CustomChatGPT(user_input, messages):
33
  st.title("Video Idea Generator for Real Estate Agents")
34
  st.write("Enter a topic suggestion or leave it blank for general video ideas.")
35
 
36
- user_input = st.text_input("Enter a topic:")
 
37
  generate_button = st.button("Generate Ideas")
38
 
 
39
  if generate_button:
40
  messages = initial_messages.copy()
41
- reply, _ = CustomChatGPT(user_input, messages)
42
- st.write("Here are the top video ideas:")
43
- st.write(reply)
 
 
 
 
 
 
 
 
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
 
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
+ """
35
+ Customize ChatGPT's interaction based on user input and previous messages.
36
+ """
37
+ if user_input.strip():
38
+ messages.append({"role": "user", "content": user_input})
39
+ else:
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})
 
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
+