Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from typing import Tuple
|
5 |
+
import src.few_shot_funcs as fsf
|
6 |
+
|
7 |
+
import src.scrape as scrape
|
8 |
+
|
9 |
+
|
10 |
+
def asin_to_pdp(asin_or_url: str) -> dict:
|
11 |
+
if isinstance(asin_or_url, str) and len(asin_or_url) == 10:
|
12 |
+
asin_url = 'https://www.amazon.com/dp/' + asin_or_url
|
13 |
+
elif fsf.check_url_structure(asin_or_url):
|
14 |
+
asin_url = asin_or_url
|
15 |
+
else:
|
16 |
+
raise gr.Error('You must provide a valid ASIN (10 char code) or URL')
|
17 |
+
|
18 |
+
html = scrape.zyte_call(asin_url)
|
19 |
+
asin_pdp = scrape.get_asin_pdp(BeautifulSoup(html, features="lxml"))
|
20 |
+
return asin_pdp
|
21 |
+
|
22 |
+
|
23 |
+
def generate_bullets(title: str, tech_data: gr.DataFrame) -> str:
|
24 |
+
tech_str = fsf.format_tech_as_str(tech_data)
|
25 |
+
|
26 |
+
feature_bullets = fsf.generate_data(title=title, tech_process=tech_str,
|
27 |
+
few_shot_df=fsf.FS_DS, vector_db=fsf.DB)
|
28 |
+
return feature_bullets
|
29 |
+
|
30 |
+
|
31 |
+
def asin_comparison(asin_or_url: str) -> Tuple[str, str]:
|
32 |
+
asin_pdp = asin_to_pdp(asin_or_url)
|
33 |
+
|
34 |
+
input_title = asin_pdp.get('title')
|
35 |
+
tech_details = pd.DataFrame([(k, v) for k, v in asin_pdp.get('tech_data').items()])
|
36 |
+
|
37 |
+
feature_bullets = generate_bullets(input_title, tech_details)
|
38 |
+
comparison_bullets = "## Original Bullets\n- " + '\n- '.join(asin_pdp.get('feature_bullets'))
|
39 |
+
return feature_bullets, comparison_bullets
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
demo = gr.Blocks()
|
44 |
+
|
45 |
+
with demo:
|
46 |
+
gr.Markdown(
|
47 |
+
"""
|
48 |
+
### Generate Product Feature-Bullets!
|
49 |
+
Input ASIN or data below
|
50 |
+
"""
|
51 |
+
)
|
52 |
+
with gr.Tab("Input ASIN"):
|
53 |
+
with gr.Row():
|
54 |
+
asin_input = gr.Textbox(type="text", label="Enter Product ASIN / URL")
|
55 |
+
|
56 |
+
with gr.Row():
|
57 |
+
clear_button = gr.ClearButton(components=asin_input,
|
58 |
+
value='Clear', variant='primary')
|
59 |
+
gen_button = gr.Button("Generate!")
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
output = gr.Markdown()
|
63 |
+
comparison_output = gr.Markdown()
|
64 |
+
|
65 |
+
gen_button.click(fn=asin_comparison, inputs=asin_input,
|
66 |
+
outputs=[output, comparison_output], show_progress=True)
|
67 |
+
|
68 |
+
|
69 |
+
with gr.Tab("Input Data"):
|
70 |
+
with gr.Row():
|
71 |
+
title_input = gr.Textbox(type="text", label="Enter Product Title")
|
72 |
+
tech_input = gr.Dataframe(label='Enter Technical Details',
|
73 |
+
headers=["Feature", "Value"],
|
74 |
+
datatype=["str", "str"],
|
75 |
+
row_count=(2, "dynamic"),
|
76 |
+
col_count=(2, "static"),
|
77 |
+
)
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
clear_button = gr.ClearButton(components=[title_input, tech_input],
|
81 |
+
value='Clear', variant='primary')
|
82 |
+
gen_button = gr.Button("Generate!")
|
83 |
+
|
84 |
+
output = gr.Markdown()
|
85 |
+
|
86 |
+
gen_button.click(fn=generate_bullets, inputs=[title_input, tech_input], outputs=output,
|
87 |
+
show_progress=True)
|
88 |
+
|
89 |
+
demo.launch()
|