Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from ping import add_ping_route | |
| # Configuration | |
| LANGFLOW_API_URL = os.environ.get("LANGFLOW_API_URL", "") | |
| LANGFLOW_API_KEY = os.environ.get("LANGFLOW_API_KEY", "") | |
| HF_API_KEY = os.environ.get("HF_API_KEY", "") | |
| if not LANGFLOW_API_URL: | |
| print("FATAL: LANGFLOW_API_URL secret not found or is empty.") | |
| else: | |
| print("SUCCESS: LANGFLOW_API_URL loaded securely.") | |
| if not LANGFLOW_API_KEY: | |
| print("FATAL: LANGFLOW_API_KEY secret not found or is empty.") | |
| else: | |
| print("SUCCESS: LANGFLOW_API_KEY loaded securely.") | |
| if not HF_API_KEY: | |
| print("FATAL: HF_API_KEY secret not found or is empty.") | |
| else: | |
| print("SUCCESS: HF_API_KEY loaded securely.") | |
| # Function that calls the LangFlow chat | |
| def call_langflow(message, history): | |
| """ | |
| Call Langflow API and return the response | |
| """ | |
| headers = { | |
| "Content-Type": "application/json", | |
| } | |
| # Add API keys | |
| if HF_API_KEY: | |
| headers["Authorization"] = f"Bearer {HF_API_KEY}" | |
| if LANGFLOW_API_KEY: | |
| headers["x-api-key"] = f"{LANGFLOW_API_KEY}" | |
| # Adjust this payload based on your Langflow API structure | |
| payload = { | |
| "input_value": message, | |
| "output_type": "chat", | |
| "input_type": "chat", | |
| "tweaks": {} | |
| } | |
| try: | |
| response = requests.post( | |
| LANGFLOW_API_URL, | |
| json=payload, | |
| headers=headers, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| # Parse response - adjust based on your API response structure | |
| data = response.json() | |
| # Common Langflow response structures: | |
| # Option 1: data["outputs"][0]["outputs"][0]["results"]["message"]["text"] | |
| # Option 2: data["result"]["message"] | |
| # Adjust the following line based on your actual response: | |
| bot_message = data["outputs"][0]["outputs"][0]["results"]["message"]["text"] | |
| return bot_message | |
| except requests.exceptions.RequestException as e: | |
| return f"Error connecting to Langflow: {str(e)}" | |
| except (KeyError, IndexError) as e: | |
| return f"Error parsing response: {str(e)}\nResponse: {data}" | |
| #Create FastAPI App for health check. | |
| fastapi_app = FastAPI(title="Chatbot with Ping API") | |
| # Optional CORS setup | |
| fastapi_app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Add /ping route from ping.py | |
| add_ping_route(fastapi_app, call_langflow) | |
| # Create Gradio Chat Interface with custom CSS | |
| with gr.Blocks( | |
| css=""" | |
| footer {display: none !important;} | |
| .footer {display: none !important;} | |
| #footer {display: none !important;} | |
| .svelte-1p1dq6v {display: none !important;} | |
| """ | |
| ) as demo: | |
| gr.ChatInterface( | |
| fn=call_langflow, | |
| title="CPA Chatbot POC", | |
| description="You can use this chatbot to answer questions related to Crown Pointe Academy policies.", | |
| examples=["Summarize the school's uniform policy", "Can a student wear earrings?"], | |
| retry_btn=None, | |
| undo_btn=None, | |
| ) | |
| # Mount Gradio app to FastAPI at root path | |
| app = gr.mount_gradio_app(fastapi_app, demo, path="/") |