Spaces:
Sleeping
Sleeping
Create interface.py
Browse files- interface.py +83 -0
interface.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# interface.py
|
2 |
+
import gradio as gr
|
3 |
+
from recommendations import recommend_products, generate_contextual_message
|
4 |
+
from utils import process_csv
|
5 |
+
|
6 |
+
css = """
|
7 |
+
.lg.svelte-cmf5ev {background-color: #8A2BE2 !important;}
|
8 |
+
.user.svelte-1pjfiar.svelte-1pjfiar.svelte-1pjfiar {padding: 7px !important;border-radius: 10px 10px 0px 10px;width: fit-content;background-color: #E6E6FA !important ;border-color:#E6E6FA !important}
|
9 |
+
.bot.svelte-1pjfiar.svelte-1pjfiar.svelte-1pjfiar {padding: 7px !important;border-radius: 10px 10px 10px 0px;width : fit-content !important;border: 1.5px solid #9370DB !important;background: #FFFFFF 0% 0% no-repeat padding-box !important;box-shadow: 0px 3px 6px #0000001A !important;border: 2px solid #9370DB !important;}
|
10 |
+
.primary.svelte-cmf5ev {box-shadow: 0px 3px 6px #0000001A !important;border: 2px solid #9370DB !important;background: #8A2BE2 !important;width: fit-content;}
|
11 |
+
.primary.svelte-cmf5ev {color: white !important}
|
12 |
+
textarea.scroll-hide.svelte-1f354aw {font-family:'Roboto','Arial',sans-serif;font-size:14px}
|
13 |
+
label.svelte-1b6s6s { background: #9370DB 0% 0% no-repeat padding-box;color: white;width: 100%;}
|
14 |
+
label.svelte-1b6s6s {background: #9370DB 0% 0% no-repeat padding-box;color: white;width: 100%;font-size:20px;font-family:'Roboto','Arial',sans-serif; border-radius: 0px 0px 10px 10px;}
|
15 |
+
.wrapper.svelte-nab2ao{background-color : #F7F7F7 }
|
16 |
+
svg.iconify.iconify--carbon{width:15px; height:15px}
|
17 |
+
.thumbnail-item.svelte-fiatpe.svelte-fiatpe:hover {--ring-color: #9370DB !important;}
|
18 |
+
"""
|
19 |
+
|
20 |
+
default_chat = [["Welcome! I'm your AI-powered product recommendation bot. Ask me anything about finding the perfect product for you.", "I'm here to assist you with any product-related inquiries. Let's find what you need!"]]
|
21 |
+
|
22 |
+
# Gradio interface functions
|
23 |
+
def handle_file_upload(file, openai_api_key, pinecone_api_key, pinecone_env):
|
24 |
+
return process_csv(file, openai_api_key, pinecone_api_key, pinecone_env)
|
25 |
+
|
26 |
+
def display_recommendations(user_input, openai_api_key, pinecone_api_key, pinecone_env, system_prompt):
|
27 |
+
recommendations = recommend_products(user_input, openai_api_key, pinecone_api_key, pinecone_env)
|
28 |
+
contextual_message = generate_contextual_message(user_input, recommendations, openai_api_key, system_prompt)
|
29 |
+
return recommendations, contextual_message
|
30 |
+
|
31 |
+
def update_outputs(query_input, openai_api_key, pinecone_api_key, pinecone_env, chat_history, system_prompt):
|
32 |
+
recommendations, contextual_message = display_recommendations(query_input, openai_api_key, pinecone_api_key, pinecone_env, system_prompt)
|
33 |
+
|
34 |
+
# Update chat history
|
35 |
+
new_chat_history = chat_history + [[query_input, contextual_message]]
|
36 |
+
|
37 |
+
return recommendations, new_chat_history, gr.update(value="")
|
38 |
+
|
39 |
+
# Create Gradio Interface
|
40 |
+
def build_interface():
|
41 |
+
with gr.Blocks(title="AI Smart Shopper", head="True", css=css) as interface:
|
42 |
+
gr.Markdown("""<div style="text-align: center; font-weight: bold;"> <h1>AI Smart Shopper</h1> </div>""")
|
43 |
+
|
44 |
+
with gr.Tab("API Keys"):
|
45 |
+
openai_api_key_input = gr.Textbox(label="OpenAI API Key", type="password")
|
46 |
+
pinecone_api_key_input = gr.Textbox(label="Pinecone API Key", type="password")
|
47 |
+
pinecone_env_input = gr.Textbox(label="Pinecone Environment", placeholder="e.g., us-east-1")
|
48 |
+
system_prompt_input = gr.Textbox(label="System Prompt", placeholder="Enter a system prompt for the assistant...")
|
49 |
+
|
50 |
+
with gr.Tab("Upload Catalog"):
|
51 |
+
upload_button = gr.File(label="Upload CSV", type="filepath")
|
52 |
+
output = gr.Textbox()
|
53 |
+
upload_button.upload(handle_file_upload, inputs=[upload_button, openai_api_key_input, pinecone_api_key_input, pinecone_env_input], outputs=output)
|
54 |
+
|
55 |
+
with gr.Tab("Get Recommendations"):
|
56 |
+
with gr.Row():
|
57 |
+
with gr.Column(scale=1):
|
58 |
+
chatbot = gr.Chatbot(value=default_chat, label="Recommender Chatbot", show_label=True)
|
59 |
+
query_input = gr.Textbox(label="Enter your product preference...", show_label=False, placeholder="Type your query here...")
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column(scale=1, min_width=150):
|
62 |
+
recommend_button = gr.Button("Get Recommendations")
|
63 |
+
with gr.Column(scale=1, min_width=150):
|
64 |
+
clear_button = gr.Button("Clear")
|
65 |
+
# Define state for chat history
|
66 |
+
chat_history = gr.State([])
|
67 |
+
|
68 |
+
# Define outputs
|
69 |
+
with gr.Column(scale=1):
|
70 |
+
recommendations_output = gr.Gallery(label="Recommendations For You", show_label=False, elem_id="gallery", columns=[3], rows=[1], object_fit="contain", height="auto", scale=5)
|
71 |
+
|
72 |
+
recommend_button.click(
|
73 |
+
update_outputs,
|
74 |
+
inputs=[query_input, openai_api_key_input, pinecone_api_key_input, pinecone_env_input, chat_history, system_prompt_input],
|
75 |
+
outputs=[recommendations_output, chatbot, query_input]
|
76 |
+
)
|
77 |
+
|
78 |
+
clear_button.click(
|
79 |
+
lambda: (gr.update(value=default_chat), gr.update(value=""), gr.update(value=[]), gr.update(value=[])),
|
80 |
+
outputs=[chatbot, query_input, chat_history, recommendations_output]
|
81 |
+
)
|
82 |
+
|
83 |
+
return interface
|