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