Spaces:
Sleeping
Sleeping
Changes to app for env
Browse files
app.py
CHANGED
|
@@ -209,23 +209,39 @@ class MCPClient:
|
|
| 209 |
def create_gradio_interface():
|
| 210 |
"""Create the Gradio interface"""
|
| 211 |
|
| 212 |
-
#
|
|
|
|
|
|
|
|
|
|
| 213 |
mcp_client = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
def initialize_client(api_key):
|
| 216 |
nonlocal mcp_client
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
| 218 |
return "β Please provide your OpenAI API key", ""
|
| 219 |
|
| 220 |
try:
|
| 221 |
-
mcp_client = MCPClient(
|
| 222 |
return "β
OpenAI client initialized successfully!", ""
|
| 223 |
except Exception as e:
|
| 224 |
return f"β Failed to initialize: {str(e)}", ""
|
| 225 |
|
| 226 |
def process_message(message, server_choice, history):
|
| 227 |
if not mcp_client:
|
| 228 |
-
return history + [["Please initialize the OpenAI client first."
|
| 229 |
|
| 230 |
if not message.strip():
|
| 231 |
return history, ""
|
|
@@ -259,17 +275,23 @@ def create_gradio_interface():
|
|
| 259 |
with gr.Blocks(title="MCP Client with OpenAI", theme=gr.themes.Soft()) as app:
|
| 260 |
gr.Markdown("# π€ MCP Client with OpenAI Integration")
|
| 261 |
gr.Markdown("Connect to your Terraform, Linux, and Cisco MCP servers with AI-powered assistance")
|
|
|
|
| 262 |
|
| 263 |
with gr.Row():
|
| 264 |
with gr.Column(scale=3):
|
| 265 |
-
# API Key input
|
| 266 |
api_key = gr.Textbox(
|
| 267 |
-
label="OpenAI API Key",
|
| 268 |
type="password",
|
| 269 |
-
placeholder="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
)
|
| 271 |
-
init_btn = gr.Button("Initialize Client", variant="primary")
|
| 272 |
-
init_status = gr.Textbox(label="Status", interactive=False)
|
| 273 |
|
| 274 |
# Server selection
|
| 275 |
server_choice = gr.Dropdown(
|
|
|
|
| 209 |
def create_gradio_interface():
|
| 210 |
"""Create the Gradio interface"""
|
| 211 |
|
| 212 |
+
# Try to get API key from environment (HuggingFace Secrets)
|
| 213 |
+
openai_api_key = os.getenv("OPEN_AI_API_KEY")
|
| 214 |
+
|
| 215 |
+
# Initialize MCP Client if API key is available
|
| 216 |
mcp_client = None
|
| 217 |
+
initial_status = ""
|
| 218 |
+
|
| 219 |
+
if openai_api_key:
|
| 220 |
+
try:
|
| 221 |
+
mcp_client = MCPClient(openai_api_key)
|
| 222 |
+
initial_status = "β
OpenAI client initialized automatically from HF Secrets!"
|
| 223 |
+
except Exception as e:
|
| 224 |
+
initial_status = f"β Failed to initialize with HF Secret: {str(e)}"
|
| 225 |
+
else:
|
| 226 |
+
initial_status = "β οΈ OpenAI API key not found in HF Secrets. Please enter manually."
|
| 227 |
|
| 228 |
def initialize_client(api_key):
|
| 229 |
nonlocal mcp_client
|
| 230 |
+
# Use provided key or fall back to environment
|
| 231 |
+
key_to_use = api_key.strip() if api_key and api_key.strip() else openai_api_key
|
| 232 |
+
|
| 233 |
+
if not key_to_use:
|
| 234 |
return "β Please provide your OpenAI API key", ""
|
| 235 |
|
| 236 |
try:
|
| 237 |
+
mcp_client = MCPClient(key_to_use)
|
| 238 |
return "β
OpenAI client initialized successfully!", ""
|
| 239 |
except Exception as e:
|
| 240 |
return f"β Failed to initialize: {str(e)}", ""
|
| 241 |
|
| 242 |
def process_message(message, server_choice, history):
|
| 243 |
if not mcp_client:
|
| 244 |
+
return history + [["β Error", "Please initialize the OpenAI client first using the button above."]], ""
|
| 245 |
|
| 246 |
if not message.strip():
|
| 247 |
return history, ""
|
|
|
|
| 275 |
with gr.Blocks(title="MCP Client with OpenAI", theme=gr.themes.Soft()) as app:
|
| 276 |
gr.Markdown("# π€ MCP Client with OpenAI Integration")
|
| 277 |
gr.Markdown("Connect to your Terraform, Linux, and Cisco MCP servers with AI-powered assistance")
|
| 278 |
+
gr.Markdown("π‘ **Tip**: Your OpenAI API key is automatically loaded from HuggingFace Secrets!")
|
| 279 |
|
| 280 |
with gr.Row():
|
| 281 |
with gr.Column(scale=3):
|
| 282 |
+
# API Key input (optional if already in HF Secrets)
|
| 283 |
api_key = gr.Textbox(
|
| 284 |
+
label="OpenAI API Key (Optional - will use HF Secret if available)",
|
| 285 |
type="password",
|
| 286 |
+
placeholder="Leave empty to use HF Secret OPEN_AI_API_KEY...",
|
| 287 |
+
value=""
|
| 288 |
+
)
|
| 289 |
+
init_btn = gr.Button("Initialize/Reinitialize Client", variant="primary")
|
| 290 |
+
init_status = gr.Textbox(
|
| 291 |
+
label="Initialization Status",
|
| 292 |
+
interactive=False,
|
| 293 |
+
value=initial_status
|
| 294 |
)
|
|
|
|
|
|
|
| 295 |
|
| 296 |
# Server selection
|
| 297 |
server_choice = gr.Dropdown(
|