File size: 3,351 Bytes
4f7fb1b
 
 
 
 
fa5fa99
4f7fb1b
 
 
 
 
 
 
 
 
 
 
 
fa5fa99
1896c1d
 
 
 
4f7fb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17d7e7d
ff0c10a
4f7fb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
import pandas as pd
from bs4 import BeautifulSoup
from typing import Tuple

import src.few_shot_funcs as fsf
import src.scrape as scrape


def asin_to_pdp(asin_or_url: str) -> dict:
    if isinstance(asin_or_url, str) and len(asin_or_url) == 10:
        asin_url = 'https://www.amazon.com/dp/' + asin_or_url
    elif fsf.check_url_structure(asin_or_url):
        asin_url = asin_or_url
    else:
        raise gr.Error('You must provide a valid ASIN (10 char code) or URL')
        
    html = scrape.zyte_call(asin_url)
    asin_pdp = scrape.get_asin_pdp(BeautifulSoup(html, 'html.parser'))
    if not asin_pdp:
        raise gr.Error('Input URL not found (404)')
    elif not asin_pdp.get('title') or not asin_pdp.get('tech_data'):
        raise gr.Error("Couldn't fetch title or technical details from input URL")
    return asin_pdp


def generate_bullets(title: str, tech_data: gr.DataFrame) -> str:
    tech_str = fsf.format_tech_as_str(tech_data)
    
    feature_bullets = fsf.generate_data(title=title, tech_process=tech_str,
                                        few_shot_df=fsf.FS_DS, vector_db=fsf.DB)
    return feature_bullets


def asin_comparison(asin_or_url: str) -> Tuple[str, str]:
    asin_pdp = asin_to_pdp(asin_or_url)
    
    input_title = asin_pdp.get('title')
    tech_details = pd.DataFrame([(k, v) for k, v in asin_pdp.get('tech_data').items()])
    
    feature_bullets = generate_bullets(input_title, tech_details)
    comparison_bullets = "## Original Bullets\n- " + '\n- '.join(asin_pdp.get('feature_bullets'))
    return feature_bullets, comparison_bullets



demo = gr.Blocks()

with demo:
    gr.Markdown(
        """
    ### Generate Product Feature-Bullets!
    - Input an Amazon ASIN or product URL, or you can input a short title and techincal details\n
    - Hit "Generate" and let the power of AI do the rest for you
    """
    )
    with gr.Tab("Input ASIN"):
        with gr.Row():
            asin_input = gr.Textbox(type="text", label="Enter Product ASIN / URL")  
        
        with gr.Row():
            clear_button = gr.ClearButton(components=asin_input,
                                          value='Clear', variant='primary')
            gen_button = gr.Button("Generate!")

        with gr.Row():
            output = gr.Markdown()
            comparison_output = gr.Markdown()

            gen_button.click(fn=asin_comparison, inputs=asin_input, 
                             outputs=[output, comparison_output], show_progress=True)
            
    
    with gr.Tab("Input Data"):
        with gr.Row():
            title_input = gr.Textbox(type="text", label="Enter Product Title")
            tech_input = gr.Dataframe(label='Enter Technical Details',
                headers=["Feature", "Value"],
                datatype=["str", "str"],
                row_count=(2, "dynamic"),
                col_count=(2, "static"),
            )
        
        with gr.Row():
            clear_button = gr.ClearButton(components=[title_input, tech_input],
                                          value='Clear', variant='primary')
            gen_button = gr.Button("Generate!")
    
        output = gr.Markdown()

        gen_button.click(fn=generate_bullets, inputs=[title_input, tech_input], outputs=output,
                          show_progress=True)

demo.launch()