Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| # Configuration | |
| LANGFLOW_API_URL = os.environ.get("LANGFLOW_API_URL", "https://rossiter78-langflowpoc.hf.space/api/v1/run/d53e1b2f-3572-40c8-84ab-725106ee858f") | |
| #LANGFLOW_API_URL = "https://rossiter78-langflowpoc.hf.space/api/v1/run/d53e1b2f-3572-40c8-84ab-725106ee858f" | |
| LANGFLOW_API_KEY = os.environ.get("LANGFLOW_API_KEY", "") # If needed | |
| HF_API_KEY = os.environ.get("HF_API_KEY", "") | |
| def call_langflow(message, history): | |
| """ | |
| Call Langflow API and return the response | |
| """ | |
| headers = { | |
| "Content-Type": "application/json", | |
| } | |
| # Add API key if needed | |
| if LANGFLOW_API_KEY: | |
| headers["Authorization"] = f"Bearer {LANGFLOW_API_KEY}" | |
| if HF_API_KEY: | |
| headers["x-api-key"] = f"{HF_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 Gradio Chat Interface | |
| demo = gr.ChatInterface( | |
| fn=call_langflow, | |
| title="My Langflow Chatbot", | |
| description="Chat with my AI assistant powered by Langflow", | |
| examples=["Hello!", "What can you help me with?"], | |
| theme=gr.themes.Soft(), | |
| retry_btn=None, | |
| undo_btn=None, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |