from models.intent_classifier import IntentClassifier from models.rag_system import RAGSystem from models.sql_generator import SQLGenerator from interface.gradio_interface import GradioInterface from utils.logger import setup_logger logger = setup_logger(__name__) class UnifiedSystem: def __init__(self): try: self.sql_generator = SQLGenerator() self.intent_classifier = IntentClassifier() self.rag_system = RAGSystem() except Exception as e: logger.error(f"Failed to initialize UnifiedSystem: {str(e)}") raise def process_query(self, query): try: intent, confidence = self.intent_classifier.classify(query) if intent == "database_query": sql_query = self.sql_generator.generate_query(query) products = self.sql_generator.fetch_shopify_data("products") if products and 'products' in products: results = "\n".join([ f"Title: {p['title']}, Vendor: {p['vendor']}" for p in products['products'] ]) return (f"Intent: Database Query (Confidence: {confidence:.2f})\n\n" f"SQL Query: {sql_query}\n\nResults:\n{results}") return "No results found or error fetching data from Shopify." elif intent == "product_description": rag_response = self.rag_system.process_query(query) return (f"Intent: Product Description (Confidence: {confidence:.2f})\n\n" f"Response: {rag_response}") return "Intent not recognized." except Exception as e: logger.error(f"Query processing error: {str(e)}") return f"An error occurred while processing your query: {str(e)}" if __name__ == "__main__": try: system = UnifiedSystem() interface = GradioInterface(system) iface = interface.create_interface() iface.launch() except Exception as e: logger.error(f"Application startup failed: {str(e)}")