nileshhanotia commited on
Commit
e266142
1 Parent(s): ed8124d

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +66 -0
main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import gradio as gr
3
+ from sql_generator import SQLGenerator
4
+ from intent_classifier import IntentClassifier
5
+ from rag_system import RAGSystem
6
+
7
+ # Initialize logging
8
+ logging.basicConfig(
9
+ level=logging.INFO, # Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
10
+ format='%(asctime)s - %(levelname)s - %(message)s' # Format for the log messages
11
+ )
12
+
13
+ class UnifiedSystem:
14
+ def __init__(self):
15
+ self.sql_generator = SQLGenerator()
16
+ self.intent_classifier = IntentClassifier()
17
+ self.rag_system = RAGSystem()
18
+
19
+ def process_query(self, query):
20
+ logging.info(f"Processing query: {query}") # Log the incoming query
21
+ intent, confidence = self.intent_classifier.classify(query)
22
+
23
+ logging.info(f"Classified intent: {intent} with confidence: {confidence:.2f}")
24
+
25
+ if intent == "database_query":
26
+ sql_query = self.sql_generator.generate_query(query) # Assuming this method is correct
27
+ products = self.sql_generator.fetch_shopify_data("products")
28
+
29
+ if products and 'products' in products:
30
+ results = "\n".join([
31
+ f"Title: {p['title']}, Vendor: {p['vendor']}"
32
+ for p in products['products']
33
+ ])
34
+ return f"Intent: Database Query (Confidence: {confidence:.2f})\n\n" \
35
+ f"SQL Query: {sql_query}\n\nResults:\n{results}"
36
+ else:
37
+ logging.warning("No results found or error fetching data from Shopify.")
38
+ return "No results found or error fetching data from Shopify."
39
+
40
+ elif intent == "product_description":
41
+ rag_response = self.rag_system.process_query(query)
42
+ return f"Intent: Product Description (Confidence: {confidence:.2f})\n\n" \
43
+ f"Response: {rag_response}"
44
+
45
+ logging.error("Intent not recognized.")
46
+ return "Intent not recognized."
47
+
48
+ def create_interface():
49
+ system = UnifiedSystem()
50
+
51
+ iface = gr.Interface(
52
+ fn=system.process_query,
53
+ inputs=gr.Textbox(
54
+ label="Enter your query",
55
+ placeholder="e.g., 'Show me all T-shirts' or 'Describe the product features'"
56
+ ),
57
+ outputs=gr.Textbox(label="Response"),
58
+ title="Unified Query Processing System",
59
+ description="Enter a natural language query to search products or get descriptions."
60
+ )
61
+
62
+ return iface
63
+
64
+ if __name__ == "__main__":
65
+ iface = create_interface()
66
+ iface.launch()