vitalliuss commited on
Commit
6b0a9b1
·
verified ·
1 Parent(s): ea7fce8

Implement robust API key handling and set gpt-4o as default model

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -23,28 +23,41 @@ def main():
23
  st.subheader("Process customer feedback with custom LLM instructions")
24
 
25
  # Initialize session state for API key
26
- if 'api_key_set' not in st.session_state:
27
- st.session_state.api_key_set = False
28
 
29
  # Sidebar for configuration
30
  with st.sidebar:
31
  st.header("Configuration")
32
 
33
  # API Key input
34
- api_key = st.text_input("OpenAI API Key", type="password")
35
- if api_key:
36
- # Set API key directly in the openai module
37
- openai.api_key = api_key
38
- # Also set it in environment variables as a backup
39
- os.environ["OPENAI_API_KEY"] = api_key
40
- st.session_state.api_key_set = True
41
- st.success("API Key set successfully!")
42
- logger.info("API key configured")
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Model selection
45
  model = st.selectbox(
46
  "Select LLM Model",
47
- ["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"]
 
48
  )
49
 
50
  # Temperature setting
@@ -57,7 +70,30 @@ def main():
57
 
58
  # Batch processing settings
59
  batch_size = st.number_input("Batch Size", min_value=1, max_value=100, value=10, step=1)
60
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Main area
62
  uploaded_file = st.file_uploader("Upload Excel file with customer feedback", type=["xlsx"])
63
 
@@ -91,16 +127,16 @@ def main():
91
 
92
  # Process button
93
  if st.button("Process Feedback"):
94
- if not st.session_state.api_key_set or not api_key:
95
- st.error("Please enter your OpenAI API key in the sidebar first.")
96
  logger.error("Processing attempted without API key")
97
  else:
98
- # Double-check API key is set
99
- if not openai.api_key:
100
- openai.api_key = api_key
101
 
102
  # Log the API key status (without revealing the key)
103
  logger.info(f"API key status: {'Set' if openai.api_key else 'Not set'}")
 
104
 
105
  # Create configuration
106
  config = FeedbackAnalysisConfig(
 
23
  st.subheader("Process customer feedback with custom LLM instructions")
24
 
25
  # Initialize session state for API key
26
+ if 'openai_api_key' not in st.session_state:
27
+ st.session_state.openai_api_key = ""
28
 
29
  # Sidebar for configuration
30
  with st.sidebar:
31
  st.header("Configuration")
32
 
33
  # API Key input
34
+ api_key = st.text_input("OpenAI API Key", type="password", key="api_key_input", value=st.session_state.openai_api_key)
35
+
36
+ # Set API key button
37
+ if st.button("Set API Key"):
38
+ if api_key:
39
+ # Store API key in session state
40
+ st.session_state.openai_api_key = api_key
41
+ # Set API key directly in the openai module
42
+ openai.api_key = api_key
43
+ # Also set it in environment variables as a backup
44
+ os.environ["OPENAI_API_KEY"] = api_key
45
+ st.success("API Key set successfully!")
46
+ logger.info("API key configured")
47
+ else:
48
+ st.error("Please enter an API key")
49
+
50
+ # Display API key status
51
+ if st.session_state.openai_api_key:
52
+ st.info(f"API Key Status: ✅ Set")
53
+ else:
54
+ st.warning(f"API Key Status: ❌ Not Set")
55
 
56
  # Model selection
57
  model = st.selectbox(
58
  "Select LLM Model",
59
+ ["gpt-4o", "gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"],
60
+ index=0 # Set gpt-4o as default
61
  )
62
 
63
  # Temperature setting
 
70
 
71
  # Batch processing settings
72
  batch_size = st.number_input("Batch Size", min_value=1, max_value=100, value=10, step=1)
73
+
74
+ # Test API key connection
75
+ if st.session_state.openai_api_key:
76
+ if st.button("Test API Connection"):
77
+ with st.spinner("Testing API connection..."):
78
+ try:
79
+ # Ensure API key is set
80
+ openai.api_key = st.session_state.openai_api_key
81
+
82
+ # Make a simple API call
83
+ response = openai.chat.completions.create(
84
+ model="gpt-3.5-turbo",
85
+ messages=[
86
+ {"role": "system", "content": "You are a helpful assistant."},
87
+ {"role": "user", "content": "Hello, this is a test."}
88
+ ],
89
+ max_tokens=10
90
+ )
91
+ st.success("✅ API connection successful!")
92
+ logger.info("API connection test successful")
93
+ except Exception as e:
94
+ st.error(f"❌ API connection failed: {str(e)}")
95
+ logger.error(f"API connection test failed: {str(e)}")
96
+
97
  # Main area
98
  uploaded_file = st.file_uploader("Upload Excel file with customer feedback", type=["xlsx"])
99
 
 
127
 
128
  # Process button
129
  if st.button("Process Feedback"):
130
+ if not st.session_state.openai_api_key:
131
+ st.error("Please set your OpenAI API key in the sidebar first.")
132
  logger.error("Processing attempted without API key")
133
  else:
134
+ # Ensure API key is set in the openai module
135
+ openai.api_key = st.session_state.openai_api_key
 
136
 
137
  # Log the API key status (without revealing the key)
138
  logger.info(f"API key status: {'Set' if openai.api_key else 'Not set'}")
139
+ logger.info(f"Using model: {model}")
140
 
141
  # Create configuration
142
  config = FeedbackAnalysisConfig(