Spaces:
Build error
Build error
nileshhanotia
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from models.intent_classifier import IntentClassifier
|
2 |
+
from models.rag_system import RAGSystem
|
3 |
+
from models.sql_generator import SQLGenerator
|
4 |
+
from interface.gradio_interface import GradioInterface
|
5 |
+
from utils.logger import setup_logger
|
6 |
+
|
7 |
+
logger = setup_logger(__name__)
|
8 |
+
|
9 |
+
class UnifiedSystem:
|
10 |
+
def __init__(self):
|
11 |
+
try:
|
12 |
+
self.sql_generator = SQLGenerator()
|
13 |
+
self.intent_classifier = IntentClassifier()
|
14 |
+
self.rag_system = RAGSystem()
|
15 |
+
except Exception as e:
|
16 |
+
logger.error(f"Failed to initialize UnifiedSystem: {str(e)}")
|
17 |
+
raise
|
18 |
+
|
19 |
+
def process_query(self, query):
|
20 |
+
try:
|
21 |
+
intent, confidence = self.intent_classifier.classify(query)
|
22 |
+
|
23 |
+
if intent == "database_query":
|
24 |
+
sql_query = self.sql_generator.generate_query(query)
|
25 |
+
products = self.sql_generator.fetch_shopify_data("products")
|
26 |
+
|
27 |
+
if products and 'products' in products:
|
28 |
+
results = "\n".join([
|
29 |
+
f"Title: {p['title']}, Vendor: {p['vendor']}"
|
30 |
+
for p in products['products']
|
31 |
+
])
|
32 |
+
return (f"Intent: Database Query (Confidence: {confidence:.2f})\n\n"
|
33 |
+
f"SQL Query: {sql_query}\n\nResults:\n{results}")
|
34 |
+
return "No results found or error fetching data from Shopify."
|
35 |
+
|
36 |
+
elif intent == "product_description":
|
37 |
+
rag_response = self.rag_system.process_query(query)
|
38 |
+
return (f"Intent: Product Description (Confidence: {confidence:.2f})\n\n"
|
39 |
+
f"Response: {rag_response}")
|
40 |
+
|
41 |
+
return "Intent not recognized."
|
42 |
+
except Exception as e:
|
43 |
+
logger.error(f"Query processing error: {str(e)}")
|
44 |
+
return f"An error occurred while processing your query: {str(e)}"
|
45 |
+
|
46 |
+
if __name__ == "__main__":
|
47 |
+
try:
|
48 |
+
system = UnifiedSystem()
|
49 |
+
interface = GradioInterface(system)
|
50 |
+
iface = interface.create_interface()
|
51 |
+
iface.launch()
|
52 |
+
except Exception as e:
|
53 |
+
logger.error(f"Application startup failed: {str(e)}")
|
54 |
+
|