Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import requests | |
| import os | |
| import datetime | |
| # --- CONFIGURATION --- | |
| # This pulls the URL you saved in your Hugging Face "Secrets" | |
| N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL", "") | |
| LOG_FILE = "hotel_analysis_history.csv" | |
| # --- HELPER FUNCTIONS --- | |
| def log_locally(hotel, lead_time, result): | |
| """Saves a local backup of the analysis for the 'Internal Database' tab.""" | |
| now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M") | |
| new_entry = pd.DataFrame([[now, f"{hotel} (LT: {lead_time})", result]], | |
| columns=["Time", "Input Details", "n8n Outcome"]) | |
| if not os.path.exists(LOG_FILE): | |
| new_entry.to_csv(LOG_FILE, index=False) | |
| else: | |
| new_entry.to_csv(LOG_FILE, mode='a', header=False, index=False) | |
| return pd.read_csv(LOG_FILE).sort_index(ascending=False) | |
| def run_n8n_logic(hotel, lead_time): | |
| """Calls the n8n workflow to process the local CSV data.""" | |
| if not N8N_WEBHOOK_URL: | |
| return "β οΈ Error: N8N_WEBHOOK_URL secret is not set in Hugging Face Space settings.", None | |
| # This is the 'Prompt' sent to the n8n Webhook node | |
| payload = { | |
| "hotel": hotel, | |
| "lead_time": int(lead_time) | |
| } | |
| try: | |
| # 1. Send data to n8n | |
| response = requests.post(N8N_WEBHOOK_URL, json=payload, timeout=15) | |
| # 2. Parse the response from the 'Respond to Webhook' node | |
| if response.status_code == 200: | |
| data = response.json() | |
| risk = data.get("risk_score", "Unknown") | |
| advice = data.get("recommendation", "No advice returned.") | |
| full_result = f"Risk Score: {risk}\nRecommendation: {advice}" | |
| else: | |
| full_result = f"Error: n8n returned status code {response.status_code}" | |
| except Exception as e: | |
| full_result = f"Connection Error: {str(e)}" | |
| # 3. Update the internal log for the UI | |
| updated_history = log_locally(hotel, lead_time, full_result) | |
| return full_result, updated_history | |
| # --- GRADIO UI --- | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# π¨ LuxeRate AI: n8n Data Engine") | |
| gr.Markdown("This app sends your prompt to a private n8n workflow that analyzes local hotel records.") | |
| with gr.Tabs(): | |
| # TAB 1: The Analysis Interface | |
| with gr.TabItem("π― Risk Analysis"): | |
| with gr.Row(): | |
| with gr.Column(): | |
| hotel_input = gr.Dropdown( | |
| choices=["City Hotel", "Resort Hotel"], | |
| label="Select Hotel Type", | |
| value="City Hotel" | |
| ) | |
| lead_input = gr.Slider( | |
| minimum=0, | |
| maximum=365, | |
| value=30, | |
| label="Lead Time (Days before arrival)" | |
| ) | |
| analyze_btn = gr.Button("Analyze via n8n", variant="primary") | |
| with gr.Column(): | |
| output_display = gr.Textbox( | |
| label="n8n Engine Output", | |
| lines=8, | |
| placeholder="Results will appear here..." | |
| ) | |
| # TAB 2: The Internal Database (CSV Log) | |
| with gr.TabItem("π Internal Database"): | |
| gr.Markdown("### Real-time Activity Log") | |
| gr.Markdown("Every analysis triggered above is recorded here for project auditing.") | |
| history_df = gr.DataFrame( | |
| value=pd.read_csv(LOG_FILE) if os.path.exists(LOG_FILE) else None, | |
| label="Historical Records", | |
| interactive=False | |
| ) | |
| refresh_btn = gr.Button("Refresh Database View") | |
| # --- EVENT HANDLING --- | |
| analyze_btn.click( | |
| fn=run_n8n_logic, | |
| inputs=[hotel_input, lead_input], | |
| outputs=[output_display, history_df] | |
| ) | |
| refresh_btn.click( | |
| fn=lambda: pd.read_csv(LOG_FILE).sort_index(ascending=False) if os.path.exists(LOG_FILE) else None, | |
| outputs=history_df | |
| ) | |
| # --- LAUNCH --- | |
| if __name__ == "__main__": | |
| demo.launch() |