nileshhanotia commited on
Commit
694be2b
1 Parent(s): eb6e65c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sql_generator import SQLGenerator
3
+ from intent_classifier import IntentClassifier
4
+ from rag_system import RAGSystem
5
+ from gradio_client import Client
6
+
7
+ class UnifiedSystem:
8
+ def __init__(self):
9
+ self.sql_generator = SQLGenerator()
10
+ self.intent_classifier = IntentClassifier()
11
+ self.rag_system = RAGSystem()
12
+ self.base_url = "https://agkd0n-fa.myshopify.com/products/"
13
+
14
+ def process_query(self, query):
15
+ intent, confidence = self.intent_classifier.classify(query)
16
+
17
+ if intent == "database_query":
18
+ sql_query = self.sql_generator.generate_query(query)
19
+ products = self.sql_generator.fetch_shopify_data("products")
20
+
21
+ if products and 'products' in products:
22
+ results = "\n".join([
23
+ f"Title: {p['title']}\nVendor: {p['vendor']}\nDescription: {p.get('body_html', 'No description available.')}\nURL: {self.base_url}{p['handle']}\n"
24
+ for p in products['products']
25
+ ])
26
+ return f"Intent: Database Query (Confidence: {confidence:.2f})\n\n" \
27
+ f"SQL Query: {sql_query}\n\nResults:\n{results}"
28
+ else:
29
+ return "No results found or error fetching data from Shopify."
30
+
31
+ elif intent == "product_description":
32
+ rag_response = self.rag_system.process_query(query)
33
+ product_handles = rag_response.get('product_handles', [])
34
+ urls = [f"{self.base_url}{handle}" for handle in product_handles]
35
+ response = rag_response.get('response', "No description available.")
36
+
37
+ return f"Intent: Product Description (Confidence: {confidence:.2f})\n\n" \
38
+ f"Response: {response}\n\nProduct Details:\n" + "\n".join(
39
+ [f"Product URL: {url}" for url in urls]
40
+ )
41
+
42
+ return "Intent not recognized."
43
+
44
+ def create_interface():
45
+ system = UnifiedSystem()
46
+
47
+ iface = gr.Interface(
48
+ fn=system.process_query,
49
+ inputs=gr.Textbox(
50
+ label="Enter your query",
51
+ placeholder="e.g., 'Show me all T-shirts' or 'Describe the product features'"
52
+ ),
53
+ outputs=gr.Textbox(label="Response"),
54
+ title="Unified Query Processing System",
55
+ description="Enter a natural language query to search products or get descriptions.",
56
+ examples=[
57
+ ["Show me shirts less than 50 rupee"],
58
+ ["Show me shirts with red color"],
59
+ ["Show me T-shirts with M size"]
60
+ ]
61
+ )
62
+
63
+ # This will enable an API endpoint for your Space
64
+ iface.launch(share=True, api_name="/predict") # Ensure share=True for public access if needed
65
+
66
+ if __name__ == "__main__":
67
+ iface = create_interface()
68
+ iface.launch()
69
+
70
+ # Test the API endpoint using Gradio Client
71
+ client = Client("nileshhanotia/PePe")
72
+ result = client.predict(
73
+ query="Hello!!",
74
+ api_name="/predict"
75
+ )
76
+ print(result)