#!/usr/bin/env python3 """ Textilindo AI Assistant - Simple Hugging Face Spaces Version """ import gradio as gr import os import json import logging # Setup logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) class TextilindoAI: def __init__(self): self.dataset = [] self.load_all_datasets() logger.info(f"Total examples loaded: {len(self.dataset)}") def load_all_datasets(self): """Load all JSONL datasets from the data directory""" base_dir = os.path.dirname(__file__) data_dir = os.path.join(base_dir, "data") if not os.path.exists(data_dir): logger.warning(f"Data directory not found: {data_dir}") return logger.info(f"Found data directory: {data_dir}") # Load all JSONL files for filename in os.listdir(data_dir): if filename.endswith('.jsonl'): filepath = os.path.join(data_dir, filename) file_examples = 0 try: with open(filepath, 'r', encoding='utf-8') as f: for line in f: line = line.strip() if line: try: data = json.loads(line) data['source'] = filename self.dataset.append(data) file_examples += 1 except json.JSONDecodeError as e: logger.warning(f"Invalid JSON in {filename}: {e}") continue logger.info(f"Loaded {filename}: {file_examples} examples") except Exception as e: logger.error(f"Error loading {filename}: {e}") def chat(self, message): """Simple chat function""" if not message: return "Please enter a message." # Simple response based on dataset if len(self.dataset) > 0: return f"Hello! I have {len(self.dataset)} examples in my knowledge base. You asked: '{message}'. How can I help you with Textilindo?" else: return "I'm sorry, I don't have access to my knowledge base right now." # Initialize AI assistant ai = TextilindoAI() # Create simple interface def create_interface(): with gr.Blocks(title="Textilindo AI Assistant") as interface: gr.Markdown("# 🤖 Textilindo AI Assistant") gr.Markdown("AI-powered customer service for Textilindo") with gr.Row(): with gr.Column(): message_input = gr.Textbox( label="Your Message", placeholder="Ask me anything about Textilindo...", lines=3 ) submit_btn = gr.Button("Send Message", variant="primary") with gr.Column(): response_output = gr.Textbox( label="AI Response", lines=10, interactive=False ) # Event handlers submit_btn.click( fn=ai.chat, inputs=message_input, outputs=response_output ) message_input.submit( fn=ai.chat, inputs=message_input, outputs=response_output ) # Add examples gr.Examples( examples=[ "Dimana lokasi Textilindo?", "Apa saja produk yang dijual di Textilindo?", "Jam berapa Textilindo buka?", "Bagaimana cara menghubungi Textilindo?" ], inputs=message_input ) # Add footer with stats gr.Markdown(f"**Dataset loaded:** {len(ai.dataset)} examples") return interface # Launch the interface if __name__ == "__main__": logger.info("Starting Textilindo AI Assistant...") logger.info(f"Dataset loaded: {len(ai.dataset)} examples") interface = create_interface() interface.launch()