Spaces:
Runtime error
Runtime error
gamingflexer
commited on
Commit
·
4b0bd4a
1
Parent(s):
b33c42d
Add API integration for product search and details
Browse files- src/app2.py +35 -0
src/app2.py
CHANGED
|
@@ -1,10 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 6 |
SEVER_IP = os.environ.get("SEVER_IP")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def sample_fun(voice_input, prodcut_id):
|
| 9 |
print(prodcut_id)
|
| 10 |
return
|
|
@@ -16,8 +38,21 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, seconda
|
|
| 16 |
prodcut_id = gr.Textbox(label="Enter Product ID")
|
| 17 |
with gr.Row():
|
| 18 |
submit_button_tab_1 = gr.Button("Start")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
|
| 21 |
submit_button_tab_1.click(fn=sample_fun,inputs=[voice_input,prodcut_id])
|
|
|
|
| 22 |
|
| 23 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 7 |
SEVER_IP = os.environ.get("SEVER_IP")
|
| 8 |
|
| 9 |
+
def get_total_number_of_products():
|
| 10 |
+
response = requests.get(f'{SEVER_IP}/api/total_number_of_products/')
|
| 11 |
+
if response.status_code == 200:
|
| 12 |
+
return response.json()['total_number_of_products']
|
| 13 |
+
else:
|
| 14 |
+
return "Error fetching total number of products"
|
| 15 |
+
|
| 16 |
+
def search_products(product_name):
|
| 17 |
+
response = requests.get(f'{SEVER_IP}/api/search_products/', params={'name': product_name})
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return pd.DataFrame(response.json())
|
| 20 |
+
else:
|
| 21 |
+
return pd.DataFrame([]) # Return an empty DataFrame in case of an error
|
| 22 |
+
|
| 23 |
+
def get_product_details_by_id(product_id):
|
| 24 |
+
response = requests.get(f'{SEVER_IP}/api/product_details/{product_id}/')
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
return response.json() # Returns the product details as a dictionary
|
| 27 |
+
else:
|
| 28 |
+
return {"error": f"Product with ID {product_id} not found or error occurred."}
|
| 29 |
+
|
| 30 |
def sample_fun(voice_input, prodcut_id):
|
| 31 |
print(prodcut_id)
|
| 32 |
return
|
|
|
|
| 38 |
prodcut_id = gr.Textbox(label="Enter Product ID")
|
| 39 |
with gr.Row():
|
| 40 |
submit_button_tab_1 = gr.Button("Start")
|
| 41 |
+
|
| 42 |
+
with gr.Tab("Search Catalog"):
|
| 43 |
+
with gr.Row():
|
| 44 |
+
total_no_of_products = gr.Textbox(value=str(get_total_number_of_products()),label="Total Products")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
embbed_text_search = gr.Textbox(label="Enter Product Name")
|
| 47 |
+
submit_button_tab_4 = gr.Button("Start")
|
| 48 |
+
dataframe_output_tab_4 = gr.Dataframe(headers=['id', 'barcode', 'brand', 'sub_brand', 'manufactured_by', 'product_name',
|
| 49 |
+
'weight', 'variant', 'net_content', 'price', 'parent_category',
|
| 50 |
+
'child_category', 'sub_child_category', 'images_paths', 'description',
|
| 51 |
+
'quantity', 'promotion_on_the_pack', 'type_of_packaging', 'mrp'])
|
| 52 |
+
|
| 53 |
|
| 54 |
|
| 55 |
submit_button_tab_1.click(fn=sample_fun,inputs=[voice_input,prodcut_id])
|
| 56 |
+
submit_button_tab_4.click(fn=search_products,inputs=[embbed_text_search] ,outputs= dataframe_output_tab_4)
|
| 57 |
|
| 58 |
demo.launch()
|