File size: 2,198 Bytes
6e6e8c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)}")