Download cli_chat.py from imkrish/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/imkrish/chatbot/resolve/main/cli_chat.py
- Command line
-
hf download hf://spaces/imkrish/chatbot/cli_chat.py
-
curl -L -o cli_chat.py https://huggingface.co/spaces/imkrish/chatbot/resolve/main/cli_chat.py
1.35 kB
| import socketio | |
| import sys | |
| import threading | |
| import time | |
| sio = socketio.Client() | |
| user_can_input = threading.Event() | |
| user_can_input.set() | |
| def connect(): | |
| print("\n✅ Connected to CampVerse Chatbot!") | |
| print("Type your questions below. Press Ctrl+C to exit.\n") | |
| def disconnect(): | |
| print("\n❌ Disconnected from chatbot backend.") | |
| def bot_answer(data): | |
| print("\n------------------------------") | |
| if 'error' in data: | |
| print(f"❌ Error: {data['error']}") | |
| else: | |
| print(f"🤖 Bot says: {data['answer']}") | |
| print(f" (Matched FAQ: {data['question']})") | |
| print("------------------------------\n") | |
| time.sleep(2) | |
| user_can_input.set() | |
| def ask_user(): | |
| while True: | |
| user_can_input.wait() | |
| question = input("You: ") | |
| if question.strip(): | |
| sio.emit('user_question', {'question': question}) | |
| else: | |
| print("⚠️ Please enter a valid question.\n") | |
| user_can_input.clear() | |
| try: | |
| sio.connect('http://localhost:8000') | |
| user_thread = threading.Thread(target=ask_user) | |
| user_thread.daemon = True | |
| user_thread.start() | |
| while True: | |
| pass | |
| except KeyboardInterrupt: | |
| print("\n👋 Goodbye!") | |
| sio.disconnect() | |
| sys.exit(0) | |
| except Exception as e: | |
| print(f"❌ Error: {e}") | |
| sys.exit(1) | |