File size: 3,054 Bytes
c5a97b3
 
 
b6a54e8
c5a97b3
7a6bd46
 
 
c5a97b3
 
7a6bd46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a97b3
7a6bd46
 
c5a97b3
7a6bd46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a97b3
 
7a6bd46
 
 
 
 
 
 
c5a97b3
 
2edd001
 
7070259
 
c5a97b3
 
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
import gradio as gr
import requests
import pandas as pd
from scipy import stats

def fetch_amazon_data(asin, token):
    BASE_URL = "https://api.invertexto.com/v1/amazon/{}".format(asin)
    params = {'token': token}
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    # Assuming the response structure, adjust as necessary
    if data:
        df = pd.DataFrame([{
            'Title': data.get('title'),
            'Price': data.get('price'),
            'Currency': 'USD',  # Assuming currency based on Amazon, adjust if needed
            'Condition': 'new',  # Assuming new, adjust as needed
            'Link': data.get('url')
        }])
        return df
    else:
        return pd.DataFrame()

def fetch_fipe_data(brand_id, token):
    BASE_URL = "https://api.invertexto.com/v1/fipe/models/{}".format(brand_id)
    params = {'token': token}
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    # Assuming the response structure, adjust as necessary
    if data:
        items = data  # Assuming data is a list of models
        df = pd.DataFrame(items)
        # Assuming the data structure, adjust the DataFrame creation as necessary
        return df
    else:
        return pd.DataFrame()

def fetch_data_to_dataframe(query, limit=50, source="mercadolibre", token=None):
    if source == "mercadolibre":
        BASE_URL = "https://api.mercadolibre.com/sites/MLB/search"
        params = {'q': query, 'limit': limit}
        response = requests.get(BASE_URL, params=params)
        data = response.json()
        if 'results' in data:
            items = data['results']
            df = pd.DataFrame(items)
            df = df[['title', 'price', 'currency_id', 'condition', 'permalink']]
            df.columns = ['Title', 'Price', 'Currency', 'Condition', 'Link']
        else:
            df = pd.DataFrame()
    elif source == "amazon":
        df = fetch_amazon_data(query, token)
    elif source == "fipe":
        df = fetch_fipe_data(query, token)
    else:
        df = pd.DataFrame()

    # Process the DataFrame similarly for all sources if applicable
    # This is an example for MercadoLibre data; adjust processing as needed for other sources
    if not df.empty:
        # Additional processing here, like calculating z-scores, filtering, etc.
        pass

    return df

def gradio_app(query):
    # Example: Fetch data from MercadoLibre, adjust to include other sources as needed
    df = fetch_data_to_dataframe(query, 50, source="mercadolibre")
    if df.empty:
        return "No data found", pd.DataFrame()
    else:
        median_price = df['Price'].median()
        return median_price, df

iface = gr.Interface(fn=gradio_app,
                     inputs=gr.Textbox(label="Insira a consulta de pesquisa"),
                     outputs=[gr.Textbox(label="Preço mediano"), gr.Dataframe(label="Resultados da pesquisa")],
                     title="Bens Móveis",
                     description="App para avaliação de bens móveis")

iface.launch()