bens_moveis / app.py
fschwartzer's picture
Update app.py
7070259 verified
raw
history blame
2.04 kB
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()