Spaces:
Runtime error
Runtime error
nileshhanotia
commited on
Commit
•
73078a1
1
Parent(s):
9a4c76d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from sql_generator import SQLGenerator
|
3 |
+
from intent_classifier import IntentClassifier
|
4 |
+
from rag_system import RAGSystem
|
5 |
+
|
6 |
+
class UnifiedSystem:
|
7 |
+
def __init__(self):
|
8 |
+
self.sql_generator = SQLGenerator()
|
9 |
+
self.intent_classifier = IntentClassifier()
|
10 |
+
self.rag_system = RAGSystem()
|
11 |
+
self.base_url = "https://agkd0n-fa.myshopify.com/products/"
|
12 |
+
|
13 |
+
def process_query(self, query):
|
14 |
+
intent, confidence = self.intent_classifier.classify(query)
|
15 |
+
|
16 |
+
if intent == "database_query":
|
17 |
+
sql_query = self.sql_generator.generate_query(query)
|
18 |
+
products = self.sql_generator.fetch_shopify_data("products")
|
19 |
+
|
20 |
+
if products and 'products' in products:
|
21 |
+
results = "\n".join([
|
22 |
+
f"Title: {p['title']}, Vendor: {p['vendor']}, URL: {self.base_url}{p['handle']}"
|
23 |
+
for p in products['products']
|
24 |
+
])
|
25 |
+
return f"Intent: Database Query (Confidence: {confidence:.2f})\n\n" \
|
26 |
+
f"SQL Query: {sql_query}\n\nResults:\n{results}"
|
27 |
+
else:
|
28 |
+
return "No results found or error fetching data from Shopify."
|
29 |
+
|
30 |
+
elif intent == "product_description":
|
31 |
+
rag_response = self.rag_system.process_query(query)
|
32 |
+
# Assume the RAG system can return product handles for links
|
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 URLs:\n" + "\n".join(urls)
|
39 |
+
|
40 |
+
return "Intent not recognized."
|
41 |
+
|
42 |
+
def create_interface():
|
43 |
+
system = UnifiedSystem()
|
44 |
+
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=system.process_query,
|
47 |
+
inputs=gr.Textbox(
|
48 |
+
label="Enter your query",
|
49 |
+
placeholder="e.g., 'Show me all T-shirts' or 'Describe the product features'"
|
50 |
+
),
|
51 |
+
outputs=gr.Textbox(label="Response"),
|
52 |
+
title="Unified Query Processing System",
|
53 |
+
description="Enter a natural language query to search products or get descriptions.",
|
54 |
+
examples=[
|
55 |
+
["Show me shirts less than 5 USD"],
|
56 |
+
["Show me shirts with red color"],
|
57 |
+
["Show me T-shirts with M size"]
|
58 |
+
]
|
59 |
+
)
|
60 |
+
|
61 |
+
return iface
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
iface = create_interface()
|
65 |
+
iface.launch()
|