File size: 2,043 Bytes
c5a97b3
 
 
b6a54e8
c5a97b3
 
 
 
 
 
 
 
 
 
 
 
 
b6a54e8
 
 
a18c943
 
 
 
 
 
 
 
 
2e170ca
 
 
a18c943
b6a54e8
 
 
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
import gradio as gr
import requests
import pandas as pd
from scipy import stats

def fetch_data_to_dataframe(query, limit=50):
    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']
        
        # Calculate z-scores of `df['Price']`
        df['z_score'] = stats.zscore(df['Price'])
        # Filter out rows where z-score is greater than 2
        df_filtered = df[df['z_score'] <= 2]
        
        # Further filter df_filtered to keep titles closely matching the query
        # Split the query into keywords and check if each title contains them
        keywords = query.lower().split()
        # Assuming all keywords in the query must be present in the title for it to be considered relevant
        for keyword in keywords:
            df_filtered = df_filtered[df_filtered['Title'].str.lower().str.contains(keyword)]
        
        # Exclude results containing the word "kit"
        df_filtered = df_filtered[~df_filtered['Title'].str.lower().str.contains("kit")]
        
        df_filtered = df_filtered.drop(columns=['z_score'])
        
        median_price = df_filtered['Price'].median()
        return median_price, df_filtered
    else:
        return 0, pd.DataFrame()

def gradio_app(query):
    median_price, df = fetch_data_to_dataframe(query, 50)
    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()