Spaces:
Paused
Paused
Download app.py from MRMAQ/session-id-generator: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/spaces/MRMAQ/session-id-generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/MRMAQ/session-id-generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MRMAQ/session-id-generator/resolve/main/app.py
3.79 kB
| import gradio as gr | |
| import re | |
| import uuid | |
| # Email validation | |
| def validate_email(email): | |
| if re.match(r"[^@]+@[^@]+\.[^@]+", email): | |
| session_id = str(uuid.uuid4()) | |
| return ( | |
| gr.update(visible=False), # Hide email page | |
| gr.update(visible=True), # Show chat page | |
| session_id, | |
| "" # Clear error | |
| ) | |
| else: | |
| return gr.update(), gr.update(), "", "❌ Invalid email address. Try again." | |
| # Your chatbot logic | |
| def handle_submit(user_input, history, session_id): | |
| if not user_input.strip(): | |
| return gr.update(), history, f"⚠️ Please enter a valid query.\nSession ID: {session_id}" | |
| # Replace with your real logic | |
| response = f"Processing: {user_input}" | |
| answer = "Sample answer here." | |
| status = f"✅ Success\nSession ID: {session_id}" | |
| history.append(("🧑 User", user_input)) | |
| history.append(("🤖 Assistant", response)) | |
| history.append(("🤖 Generated Results", answer)) | |
| return "", history, status | |
| with gr.Blocks() as interface: | |
| # Email Gate | |
| with gr.Row(visible=True) as email_page: | |
| with gr.Column(): | |
| gr.Markdown("<h1 style='text-align: center;'>🕯️ CANDLES A.I CHAT SUPPORT 🕯️</h1>") | |
| email_input = gr.Textbox( | |
| label="Enter your email to continue", | |
| placeholder="you@example.com", | |
| autofocus=True | |
| ) | |
| email_btn = gr.Button("Submit Email") | |
| email_error = gr.HTML("") | |
| # Chat Page (initially hidden) | |
| with gr.Column(visible=False) as chat_page: | |
| chat_history = gr.State([]) | |
| session_id_state = gr.State("") | |
| gr.Markdown("<h3 style='text-align: center;'>Please type your query below...</h3>") | |
| chatbot = gr.Chatbot(label="Conversation", height=600, autoscroll=True, show_copy_button=True) | |
| with gr.Row(equal_height=True): | |
| user_input = gr.Textbox( | |
| label="Your Question", | |
| placeholder="e.g., John Smith, 1800, show matching candles etc.", | |
| autofocus=True, | |
| scale=4 | |
| ) | |
| submit_btn = gr.Button("Submit", variant="primary", scale=1) | |
| with gr.Row(): | |
| undo_btn = gr.Button("Undo Last") | |
| clear_btn = gr.Button("Clear") | |
| status_msg = gr.Markdown("") | |
| submit_btn.click( | |
| handle_submit, | |
| [user_input, chat_history, session_id_state], | |
| [user_input, chat_history, status_msg] | |
| ).then( | |
| lambda hist: hist, | |
| [chat_history], | |
| [chatbot] | |
| ) | |
| user_input.submit( | |
| handle_submit, | |
| [user_input, chat_history, session_id_state], | |
| [user_input, chat_history, status_msg] | |
| ).then( | |
| lambda hist: hist, | |
| [chat_history], | |
| [chatbot] | |
| ) | |
| undo_btn.click( | |
| lambda history: history[:-2] if len(history) >= 2 else [], | |
| [chat_history], | |
| [chat_history] | |
| ).then( | |
| lambda hist: hist, | |
| [chat_history], | |
| [chatbot] | |
| ) | |
| clear_btn.click( | |
| lambda: [], | |
| None, | |
| [chat_history] | |
| ).then( | |
| lambda: ([], ""), | |
| None, | |
| [chatbot, status_msg] | |
| ) | |
| # Bind both the button and Enter key in the email box to validate_email | |
| email_btn.click( | |
| validate_email, | |
| inputs=email_input, | |
| outputs=[email_page, chat_page, session_id_state, email_error] | |
| ) | |
| email_input.submit( | |
| validate_email, | |
| inputs=email_input, | |
| outputs=[email_page, chat_page, session_id_state, email_error] | |
| ) | |
| interface.launch() | |