Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import time
|
| 4 |
import os
|
| 5 |
from typing import List, Dict
|
| 6 |
|
| 7 |
-
# Set your OpenAI API key
|
| 8 |
-
# You can set this as an environment variable or hardcode it for testing
|
| 9 |
-
# os.environ["OPENAI_API_KEY"] = "your-api-key-here"
|
| 10 |
-
|
| 11 |
class ChatbotHandler:
|
| 12 |
def __init__(self):
|
| 13 |
self.client = None
|
| 14 |
-
self.model = "
|
| 15 |
self.max_tokens = 500
|
| 16 |
self.temperature = 0.7
|
| 17 |
self.system_prompt = """You are a helpful, friendly, and knowledgeable AI assistant.
|
|
@@ -19,18 +26,24 @@ class ChatbotHandler:
|
|
| 19 |
helpful while being honest about your limitations. Always maintain a positive and
|
| 20 |
supportive tone in your conversations."""
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def initialize_client(self, api_key: str):
|
| 23 |
"""Initialize the OpenAI client with the provided API key."""
|
| 24 |
try:
|
| 25 |
self.client = openai.OpenAI(api_key=api_key)
|
| 26 |
-
return True
|
| 27 |
except Exception as e:
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
def get_response(self, message: str, history: List[Dict]) -> str:
|
| 31 |
"""Get response from the model."""
|
| 32 |
if not self.client:
|
| 33 |
-
return "
|
| 34 |
|
| 35 |
try:
|
| 36 |
# Prepare conversation history
|
|
@@ -63,35 +76,19 @@ class ChatbotHandler:
|
|
| 63 |
yield full_response
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
yield f"Error: {str(e)}
|
| 67 |
|
| 68 |
# Initialize chatbot handler
|
| 69 |
chat_handler = ChatbotHandler()
|
| 70 |
|
| 71 |
-
def
|
| 72 |
-
"""Initialize the API with the provided key."""
|
| 73 |
-
if not api_key.strip():
|
| 74 |
-
return gr.update(visible=True), "Please enter a valid API key.", ""
|
| 75 |
-
|
| 76 |
-
success, message = chat_handler.initialize_client(api_key)
|
| 77 |
-
if success:
|
| 78 |
-
return gr.update(visible=False), message, ""
|
| 79 |
-
else:
|
| 80 |
-
return gr.update(visible=True), message, api_key
|
| 81 |
-
|
| 82 |
-
def respond_stream(message: str, history: List[Dict], api_key_input: str):
|
| 83 |
"""Generate streaming response from the model."""
|
| 84 |
if not message.strip():
|
| 85 |
return "", history
|
| 86 |
|
| 87 |
# Check if API is initialized
|
| 88 |
if not chat_handler.client:
|
| 89 |
-
|
| 90 |
-
success, _ = chat_handler.initialize_client(api_key_input)
|
| 91 |
-
if not success:
|
| 92 |
-
return "", history + [{"role": "assistant", "content": "Invalid API key. Please check your OpenAI API key."}]
|
| 93 |
-
else:
|
| 94 |
-
return "", history + [{"role": "assistant", "content": "Please set your OpenAI API key first to start chatting."}]
|
| 95 |
|
| 96 |
# Add user message
|
| 97 |
history.append({"role": "user", "content": message})
|
|
@@ -111,161 +108,128 @@ def clear_history():
|
|
| 111 |
"""Clear the chat history."""
|
| 112 |
return []
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
# Create the interface
|
| 115 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Chatbot with OpenAI") as demo:
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
<
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
""")
|
| 125 |
-
|
| 126 |
-
with gr.Row():
|
| 127 |
-
api_key = gr.Textbox(
|
| 128 |
-
label="OpenAI API Key",
|
| 129 |
-
type="password",
|
| 130 |
-
placeholder="sk-...",
|
| 131 |
-
scale=4,
|
| 132 |
-
info="Get your API key from platform.openai.com"
|
| 133 |
-
)
|
| 134 |
-
validate_btn = gr.Button("Validate & Start", variant="primary", scale=1)
|
| 135 |
-
|
| 136 |
-
api_status = gr.Textbox(label="Status", interactive=False)
|
| 137 |
-
gr.HTML("""
|
| 138 |
-
<div style='text-align: center; padding: 10px; color: #888; font-size: 0.9em;'>
|
| 139 |
-
<p>Your API key is stored locally and not saved anywhere.</p>
|
| 140 |
-
</div>
|
| 141 |
-
""")
|
| 142 |
|
| 143 |
-
#
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
label="Model",
|
| 160 |
-
info="GPT-5 gives the best responses but costs more"
|
| 161 |
-
)
|
| 162 |
-
temperature = gr.Slider(
|
| 163 |
-
minimum=0.0,
|
| 164 |
-
maximum=2.0,
|
| 165 |
-
value=0.7,
|
| 166 |
-
step=0.1,
|
| 167 |
-
label="Temperature",
|
| 168 |
-
info="Higher values make responses more creative"
|
| 169 |
-
)
|
| 170 |
-
max_tokens = gr.Slider(
|
| 171 |
-
minimum=100,
|
| 172 |
-
maximum=1000,
|
| 173 |
-
value=500,
|
| 174 |
-
step=50,
|
| 175 |
-
label="Max Tokens",
|
| 176 |
-
info="Maximum length of response"
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
# Chatbot component
|
| 180 |
-
chatbot = gr.Chatbot(
|
| 181 |
-
type="messages",
|
| 182 |
-
label="Conversation",
|
| 183 |
-
height=500,
|
| 184 |
-
show_copy_button=True,
|
| 185 |
-
bubble_full_width=False,
|
| 186 |
-
avatar_images=(None, "https://huggingface.co/datasets/huggingface/avatars/resolve/main/bot-avatar.png")
|
| 187 |
-
)
|
| 188 |
-
|
| 189 |
-
# Input section
|
| 190 |
with gr.Row():
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
"Can you help me brainstorm ideas for a project?",
|
| 213 |
-
"Explain the concept of blockchain technology."
|
| 214 |
-
],
|
| 215 |
-
inputs=msg,
|
| 216 |
-
label="Click an example to start chatting"
|
| 217 |
)
|
| 218 |
-
|
| 219 |
-
# Footer
|
| 220 |
-
gr.HTML("""
|
| 221 |
-
<div style='text-align: center; padding: 10px; color: #888; font-size: 0.9em;'>
|
| 222 |
-
<p>This chatbot uses OpenAI's GPT models. Usage may incur costs based on OpenAI's pricing.</p>
|
| 223 |
-
</div>
|
| 224 |
-
""")
|
| 225 |
|
| 226 |
-
#
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
#
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
# Chat functionality
|
| 248 |
msg.submit(
|
| 249 |
respond_stream,
|
| 250 |
-
inputs=[msg, chatbot
|
| 251 |
outputs=[msg, chatbot]
|
| 252 |
)
|
| 253 |
|
| 254 |
submit_btn.click(
|
| 255 |
respond_stream,
|
| 256 |
-
inputs=[msg, chatbot
|
| 257 |
outputs=[msg, chatbot]
|
| 258 |
)
|
| 259 |
|
| 260 |
# Clear chat
|
| 261 |
clear_btn.click(clear_history, outputs=chatbot)
|
| 262 |
|
| 263 |
-
# Change API key
|
| 264 |
-
change_api_btn.click(
|
| 265 |
-
lambda: (gr.update(visible=True), gr.update(visible=False), ""),
|
| 266 |
-
outputs=[api_setup, chat_interface, api_key_hidden]
|
| 267 |
-
)
|
| 268 |
-
|
| 269 |
# Update model settings
|
| 270 |
model_choice.change(
|
| 271 |
update_model_settings,
|
|
@@ -282,7 +246,56 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Chatbot with OpenAI") as demo:
|
|
| 282 |
inputs=[model_choice, temperature, max_tokens],
|
| 283 |
outputs=[]
|
| 284 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
# Launch the app
|
| 287 |
if __name__ == "__main__":
|
| 288 |
-
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*Thinking...*
|
| 2 |
+
> First, the user wants to update the Gradio app to use their OpenAI key. The current app is deployed at AiCoderv2/dark-app-74 on Hugging Face Spaces.
|
| 3 |
+
>
|
| 4 |
+
>
|
| 5 |
+
> ## Updating the Gradio App
|
| 6 |
+
> - The user aims to update their Gradio app to integrate their OpenAI key for functionality.
|
| 7 |
+
> - The app is currently deployed at AiCoderv2/dark-app-74 on Hugging Face Spaces, requiring code modifications.
|
| 8 |
+
|
| 9 |
+
I'll update the Gradio app to use your OpenAI API key securely. For Hugging Face Spaces, we'll use Space secrets instead of requiring users to enter their API key in the interface. This is more secure and convenient for deployment.
|
| 10 |
+
|
| 11 |
+
```python
|
| 12 |
import gradio as gr
|
| 13 |
import openai
|
| 14 |
import time
|
| 15 |
import os
|
| 16 |
from typing import List, Dict
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
class ChatbotHandler:
|
| 19 |
def __init__(self):
|
| 20 |
self.client = None
|
| 21 |
+
self.model = "gpt-4o-mini" # Using a cost-effective model
|
| 22 |
self.max_tokens = 500
|
| 23 |
self.temperature = 0.7
|
| 24 |
self.system_prompt = """You are a helpful, friendly, and knowledgeable AI assistant.
|
|
|
|
| 26 |
helpful while being honest about your limitations. Always maintain a positive and
|
| 27 |
supportive tone in your conversations."""
|
| 28 |
|
| 29 |
+
# Initialize with API key from environment variable
|
| 30 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 31 |
+
if api_key:
|
| 32 |
+
self.initialize_client(api_key)
|
| 33 |
+
|
| 34 |
def initialize_client(self, api_key: str):
|
| 35 |
"""Initialize the OpenAI client with the provided API key."""
|
| 36 |
try:
|
| 37 |
self.client = openai.OpenAI(api_key=api_key)
|
| 38 |
+
return True
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"Error initializing OpenAI client: {str(e)}")
|
| 41 |
+
return False
|
| 42 |
|
| 43 |
def get_response(self, message: str, history: List[Dict]) -> str:
|
| 44 |
"""Get response from the model."""
|
| 45 |
if not self.client:
|
| 46 |
+
return "OpenAI API key not configured. Please contact the administrator."
|
| 47 |
|
| 48 |
try:
|
| 49 |
# Prepare conversation history
|
|
|
|
| 76 |
yield full_response
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
yield f"Error generating response: {str(e)}"
|
| 80 |
|
| 81 |
# Initialize chatbot handler
|
| 82 |
chat_handler = ChatbotHandler()
|
| 83 |
|
| 84 |
+
def respond_stream(message: str, history: List[Dict]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"""Generate streaming response from the model."""
|
| 86 |
if not message.strip():
|
| 87 |
return "", history
|
| 88 |
|
| 89 |
# Check if API is initialized
|
| 90 |
if not chat_handler.client:
|
| 91 |
+
return "", history + [{"role": "assistant", "content": "The chatbot is not properly configured. Please try again later."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# Add user message
|
| 94 |
history.append({"role": "user", "content": message})
|
|
|
|
| 108 |
"""Clear the chat history."""
|
| 109 |
return []
|
| 110 |
|
| 111 |
+
def update_model_settings(model, temp, tokens):
|
| 112 |
+
"""Update model settings."""
|
| 113 |
+
chat_handler.model = model
|
| 114 |
+
chat_handler.temperature = temp
|
| 115 |
+
chat_handler.max_tokens = tokens
|
| 116 |
+
return f"Settings updated: {model}, temp={temp}, max_tokens={tokens}"
|
| 117 |
+
|
| 118 |
# Create the interface
|
| 119 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI Chatbot with OpenAI") as demo:
|
| 120 |
|
| 121 |
+
# Header
|
| 122 |
+
gr.HTML("""
|
| 123 |
+
<div style='text-align: center; padding: 20px;'>
|
| 124 |
+
<h1>🤖 AI Chatbot</h1>
|
| 125 |
+
<p style='color: #666;'>Powered by OpenAI GPT-4o-mini • Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank' style='color: #007bff; text-decoration: none;'>anycoder</a></p>
|
| 126 |
+
</div>
|
| 127 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
# Status indicator
|
| 130 |
+
if chat_handler.client:
|
| 131 |
+
status_msg = "✅ Chatbot is ready! Start chatting below."
|
| 132 |
+
status_color = "#28a745"
|
| 133 |
+
else:
|
| 134 |
+
status_msg = "❌ Chatbot configuration error. Please check API key."
|
| 135 |
+
status_color = "#dc3545"
|
| 136 |
+
|
| 137 |
+
gr.HTML(f"""
|
| 138 |
+
<div style='text-align: center; padding: 10px; background-color: {status_color}15; border: 1px solid {status_color}30; border-radius: 5px; margin: 10px 0;'>
|
| 139 |
+
<p style='color: {status_color}; margin: 0;'>{status_msg}</p>
|
| 140 |
+
</div>
|
| 141 |
+
""")
|
| 142 |
+
|
| 143 |
+
# Model settings
|
| 144 |
+
with gr.Accordion("Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
with gr.Row():
|
| 146 |
+
model_choice = gr.Dropdown(
|
| 147 |
+
choices=["gpt-4o-mini", "gpt-4o", "gpt-4-turbo", "gpt-4"],
|
| 148 |
+
value="gpt-4o-mini",
|
| 149 |
+
label="Model",
|
| 150 |
+
info="Choose your preferred GPT model"
|
| 151 |
)
|
| 152 |
+
temperature = gr.Slider(
|
| 153 |
+
minimum=0.0,
|
| 154 |
+
maximum=2.0,
|
| 155 |
+
value=0.7,
|
| 156 |
+
step=0.1,
|
| 157 |
+
label="Temperature",
|
| 158 |
+
info="Higher values make responses more creative"
|
| 159 |
+
)
|
| 160 |
+
max_tokens = gr.Slider(
|
| 161 |
+
minimum=100,
|
| 162 |
+
maximum=2000,
|
| 163 |
+
value=500,
|
| 164 |
+
step=50,
|
| 165 |
+
label="Max Tokens",
|
| 166 |
+
info="Maximum length of response"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
# Chatbot component
|
| 170 |
+
chatbot = gr.Chatbot(
|
| 171 |
+
type="messages",
|
| 172 |
+
label="Conversation",
|
| 173 |
+
height=500,
|
| 174 |
+
show_copy_button=True,
|
| 175 |
+
bubble_full_width=False,
|
| 176 |
+
avatar_images=(None, "https://huggingface.co/datasets/huggingface/avatars/resolve/main/bot-avatar.png")
|
| 177 |
+
)
|
| 178 |
|
| 179 |
+
# Input section
|
| 180 |
+
with gr.Row():
|
| 181 |
+
msg = gr.Textbox(
|
| 182 |
+
label="Your Message",
|
| 183 |
+
placeholder="Type your message here and press Enter...",
|
| 184 |
+
container=False,
|
| 185 |
+
scale=4
|
| 186 |
+
)
|
| 187 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 188 |
|
| 189 |
+
# Control buttons
|
| 190 |
+
with gr.Row():
|
| 191 |
+
clear_btn = gr.Button("Clear Chat", variant="secondary")
|
| 192 |
+
refresh_btn = gr.Button("Refresh Settings", variant="secondary")
|
| 193 |
+
|
| 194 |
+
# Example questions
|
| 195 |
+
with gr.Accordion("Example Questions", open=False):
|
| 196 |
+
gr.Examples(
|
| 197 |
+
examples=[
|
| 198 |
+
"What's the difference between AI and machine learning?",
|
| 199 |
+
"Can you explain quantum computing in simple terms?",
|
| 200 |
+
"Help me write a professional email.",
|
| 201 |
+
"What are some good books to learn programming?",
|
| 202 |
+
"Can you help me brainstorm ideas for a project?",
|
| 203 |
+
"Explain the concept of blockchain technology."
|
| 204 |
+
],
|
| 205 |
+
inputs=msg,
|
| 206 |
+
label="Click an example to start chatting"
|
| 207 |
+
)
|
| 208 |
|
| 209 |
+
# Footer
|
| 210 |
+
gr.HTML("""
|
| 211 |
+
<div style='text-align: center; padding: 10px; color: #888; font-size: 0.9em;'>
|
| 212 |
+
<p>This chatbot uses OpenAI's GPT models. Usage may incur costs based on OpenAI's pricing.</p>
|
| 213 |
+
</div>
|
| 214 |
+
""")
|
| 215 |
+
|
| 216 |
+
# Event handlers
|
| 217 |
# Chat functionality
|
| 218 |
msg.submit(
|
| 219 |
respond_stream,
|
| 220 |
+
inputs=[msg, chatbot],
|
| 221 |
outputs=[msg, chatbot]
|
| 222 |
)
|
| 223 |
|
| 224 |
submit_btn.click(
|
| 225 |
respond_stream,
|
| 226 |
+
inputs=[msg, chatbot],
|
| 227 |
outputs=[msg, chatbot]
|
| 228 |
)
|
| 229 |
|
| 230 |
# Clear chat
|
| 231 |
clear_btn.click(clear_history, outputs=chatbot)
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
# Update model settings
|
| 234 |
model_choice.change(
|
| 235 |
update_model_settings,
|
|
|
|
| 246 |
inputs=[model_choice, temperature, max_tokens],
|
| 247 |
outputs=[]
|
| 248 |
)
|
| 249 |
+
|
| 250 |
+
# Refresh settings (useful for debugging)
|
| 251 |
+
refresh_btn.click(
|
| 252 |
+
lambda: f"Settings: {chat_handler.model}, temp={chat_handler.temperature}, max_tokens={chat_handler.max_tokens}",
|
| 253 |
+
outputs=[]
|
| 254 |
+
)
|
| 255 |
|
| 256 |
# Launch the app
|
| 257 |
if __name__ == "__main__":
|
| 258 |
+
demo.launch(share=True)
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
## **Key Changes Made:**
|
| 262 |
+
|
| 263 |
+
### 🔐 **Secure API Key Handling**
|
| 264 |
+
- Now uses `OPENAI_API_KEY` environment variable
|
| 265 |
+
- Removes the API key input interface for security
|
| 266 |
+
- API key is set via Hugging Face Space secrets
|
| 267 |
+
|
| 268 |
+
### ⚙️ **Improved Configuration**
|
| 269 |
+
- Uses GPT-4o-mini as default (cost-effective)
|
| 270 |
+
- Added more model options including GPT-4o
|
| 271 |
+
- Increased max tokens to 2000 for longer responses
|
| 272 |
+
|
| 273 |
+
### 📊 **Status Indicator**
|
| 274 |
+
- Shows whether the chatbot is properly configured
|
| 275 |
+
- Provides immediate feedback on API key status
|
| 276 |
+
|
| 277 |
+
## **Setup Instructions for Your Space:**
|
| 278 |
+
|
| 279 |
+
1. **Go to your Space settings:**
|
| 280 |
+
- Visit: https://huggingface.co/spaces/AiCoderv2/dark-app-74/settings
|
| 281 |
+
- Go to "Secrets" tab
|
| 282 |
+
|
| 283 |
+
2. **Add your OpenAI API Key:**
|
| 284 |
+
- Name: `OPENAI_API_KEY`
|
| 285 |
+
- Value: Your OpenAI API key (starts with `sk-...`)
|
| 286 |
+
- Click "Add secret"
|
| 287 |
+
|
| 288 |
+
3. **Restart your Space:**
|
| 289 |
+
- The app will automatically use your API key
|
| 290 |
+
- The status indicator will show "✅ Chatbot is ready!"
|
| 291 |
+
|
| 292 |
+
## **Available Models:**
|
| 293 |
+
- **gpt-4o-mini**: Most cost-effective (~$0.15/1M input tokens)
|
| 294 |
+
- **gpt-4o**: Better quality (~$2.50/1M input tokens)
|
| 295 |
+
- **gpt-4-turbo**: Latest GPT-4 (~$10/1M input tokens)
|
| 296 |
+
- **gpt-4**: Original GPT-4 (~$30/1M input tokens)
|
| 297 |
+
|
| 298 |
+
## **Cost Monitoring:**
|
| 299 |
+
Monitor your OpenAI usage at: https://platform.openai.com/usage
|
| 300 |
+
|
| 301 |
+
The app is now updated and securely configured to use your OpenAI API key! Users can start chatting immediately without needing to enter API keys.
|