Spaces:
Sleeping
Sleeping
nileshhanotia
commited on
Commit
•
d950c91
1
Parent(s):
0b83ddd
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,51 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -39,26 +78,47 @@ def respond(
|
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
gr.Textbox(
|
50 |
-
|
51 |
-
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
),
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
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 huggingface_hub import InferenceClient
|
6 |
|
7 |
+
# Initialize Hugging Face InferenceClient
|
|
|
|
|
8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
9 |
|
10 |
+
# Unified System Class
|
11 |
+
class UnifiedSystem:
|
12 |
+
def __init__(self):
|
13 |
+
self.sql_generator = SQLGenerator()
|
14 |
+
self.intent_classifier = IntentClassifier()
|
15 |
+
self.rag_system = RAGSystem()
|
16 |
+
self.base_url = "https://agkd0n-fa.myshopify.com/products/"
|
17 |
|
18 |
+
def process_query(self, query):
|
19 |
+
intent, confidence = self.intent_classifier.classify(query)
|
20 |
+
|
21 |
+
if intent == "database_query":
|
22 |
+
sql_query = self.sql_generator.generate_query(query)
|
23 |
+
products = self.sql_generator.fetch_shopify_data("products")
|
24 |
+
|
25 |
+
if products and 'products' in products:
|
26 |
+
results = "\n".join([
|
27 |
+
f"Title: {p['title']}\nVendor: {p['vendor']}\nDescription: {p.get('body_html', 'No description available.')}\nURL: {self.base_url}{p['handle']}\n"
|
28 |
+
for p in products['products']
|
29 |
+
])
|
30 |
+
return f"Intent: Database Query (Confidence: {confidence:.2f})\n\n" \
|
31 |
+
f"SQL Query: {sql_query}\n\nResults:\n{results}"
|
32 |
+
else:
|
33 |
+
return "No results found or error fetching data from Shopify."
|
34 |
+
|
35 |
+
elif intent == "product_description":
|
36 |
+
rag_response = self.rag_system.process_query(query)
|
37 |
+
product_handles = rag_response.get('product_handles', [])
|
38 |
+
urls = [f"{self.base_url}{handle}" for handle in product_handles]
|
39 |
+
response = rag_response.get('response', "No description available.")
|
40 |
+
|
41 |
+
return f"Intent: Product Description (Confidence: {confidence:.2f})\n\n" \
|
42 |
+
f"Response: {response}\n\nProduct Details:\n" + "\n".join(
|
43 |
+
[f"Product URL: {url}" for url in urls]
|
44 |
+
)
|
45 |
+
|
46 |
+
return "Intent not recognized."
|
47 |
+
|
48 |
+
# Chatbot Response using Hugging Face's model
|
49 |
def respond(
|
50 |
message,
|
51 |
history: list[tuple[str, str]],
|
|
|
78 |
response += token
|
79 |
yield response
|
80 |
|
81 |
+
# Create Gradio interface with integrated functionalities
|
82 |
+
def create_interface():
|
83 |
+
system = UnifiedSystem()
|
84 |
+
|
85 |
+
# Define the interface
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=system.process_query,
|
88 |
+
inputs=gr.Textbox(
|
89 |
+
label="Enter your query",
|
90 |
+
placeholder="e.g., 'Show me all T-shirts' or 'Describe the product features'"
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
),
|
92 |
+
outputs=gr.Textbox(label="Response"),
|
93 |
+
title="Unified Query Processing System",
|
94 |
+
description="Enter a natural language query to search products or get descriptions.",
|
95 |
+
examples=[
|
96 |
+
["Show me shirts less than 50 rupee"],
|
97 |
+
["Show me shirts with red color"],
|
98 |
+
["Show me T-shirts with M size"]
|
99 |
+
]
|
100 |
+
)
|
101 |
+
|
102 |
+
# Define Chat Interface for Hugging Face Model
|
103 |
+
chat_demo = gr.ChatInterface(
|
104 |
+
respond,
|
105 |
+
additional_inputs=[
|
106 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
107 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
108 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
109 |
+
gr.Slider(
|
110 |
+
minimum=0.1,
|
111 |
+
maximum=1.0,
|
112 |
+
value=0.95,
|
113 |
+
step=0.05,
|
114 |
+
label="Top-p (nucleus sampling)",
|
115 |
+
),
|
116 |
+
],
|
117 |
+
)
|
118 |
+
|
119 |
+
# Launch both interfaces (Unified System and Chatbot)
|
120 |
+
iface.launch(share=True) # Share the interface for public access
|
121 |
+
chat_demo.launch(share=True) # Launch the chatbot interface for user interaction
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
+
create_interface()
|