Spaces:
Sleeping
Sleeping
fschwartzer
commited on
Commit
•
7a6bd46
1
Parent(s):
7070259
Update app.py
Browse files
app.py
CHANGED
@@ -3,43 +3,74 @@ import requests
|
|
3 |
import pandas as pd
|
4 |
from scipy import stats
|
5 |
|
6 |
-
def
|
7 |
-
BASE_URL = "https://api.
|
8 |
-
params = {'
|
9 |
response = requests.get(BASE_URL, params=params)
|
10 |
data = response.json()
|
11 |
-
|
12 |
-
if
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
df = pd.DataFrame(items)
|
15 |
-
|
16 |
-
df
|
17 |
-
|
18 |
-
# Calculate z-scores of `df['Price']`
|
19 |
-
df['z_score'] = stats.zscore(df['Price'])
|
20 |
-
# Filter out rows where z-score is greater than 2
|
21 |
-
df_filtered = df[df['z_score'] <= 2]
|
22 |
-
|
23 |
-
# Further filter df_filtered to keep titles closely matching the query
|
24 |
-
# Split the query into keywords and check if each title contains them
|
25 |
-
keywords = query.lower().split()
|
26 |
-
# Assuming all keywords in the query must be present in the title for it to be considered relevant
|
27 |
-
for keyword in keywords:
|
28 |
-
df_filtered = df_filtered[df_filtered['Title'].str.lower().str.contains(keyword)]
|
29 |
-
|
30 |
-
# Exclude results containing the word "kit"
|
31 |
-
df_filtered = df_filtered[~df_filtered['Title'].str.lower().str.contains("kit")]
|
32 |
-
|
33 |
-
df_filtered = df_filtered.drop(columns=['z_score'])
|
34 |
-
|
35 |
-
median_price = df_filtered['Price'].median()
|
36 |
-
return median_price, df_filtered
|
37 |
else:
|
38 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
def gradio_app(query):
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
iface = gr.Interface(fn=gradio_app,
|
45 |
inputs=gr.Textbox(label="Insira a consulta de pesquisa"),
|
|
|
3 |
import pandas as pd
|
4 |
from scipy import stats
|
5 |
|
6 |
+
def fetch_amazon_data(asin, token):
|
7 |
+
BASE_URL = "https://api.invertexto.com/v1/amazon/{}".format(asin)
|
8 |
+
params = {'token': token}
|
9 |
response = requests.get(BASE_URL, params=params)
|
10 |
data = response.json()
|
11 |
+
# Assuming the response structure, adjust as necessary
|
12 |
+
if data:
|
13 |
+
df = pd.DataFrame([{
|
14 |
+
'Title': data.get('title'),
|
15 |
+
'Price': data.get('price'),
|
16 |
+
'Currency': 'USD', # Assuming currency based on Amazon, adjust if needed
|
17 |
+
'Condition': 'new', # Assuming new, adjust as needed
|
18 |
+
'Link': data.get('url')
|
19 |
+
}])
|
20 |
+
return df
|
21 |
+
else:
|
22 |
+
return pd.DataFrame()
|
23 |
+
|
24 |
+
def fetch_fipe_data(brand_id, token):
|
25 |
+
BASE_URL = "https://api.invertexto.com/v1/fipe/models/{}".format(brand_id)
|
26 |
+
params = {'token': token}
|
27 |
+
response = requests.get(BASE_URL, params=params)
|
28 |
+
data = response.json()
|
29 |
+
# Assuming the response structure, adjust as necessary
|
30 |
+
if data:
|
31 |
+
items = data # Assuming data is a list of models
|
32 |
df = pd.DataFrame(items)
|
33 |
+
# Assuming the data structure, adjust the DataFrame creation as necessary
|
34 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
else:
|
36 |
+
return pd.DataFrame()
|
37 |
+
|
38 |
+
def fetch_data_to_dataframe(query, limit=50, source="mercadolibre", token=None):
|
39 |
+
if source == "mercadolibre":
|
40 |
+
BASE_URL = "https://api.mercadolibre.com/sites/MLB/search"
|
41 |
+
params = {'q': query, 'limit': limit}
|
42 |
+
response = requests.get(BASE_URL, params=params)
|
43 |
+
data = response.json()
|
44 |
+
if 'results' in data:
|
45 |
+
items = data['results']
|
46 |
+
df = pd.DataFrame(items)
|
47 |
+
df = df[['title', 'price', 'currency_id', 'condition', 'permalink']]
|
48 |
+
df.columns = ['Title', 'Price', 'Currency', 'Condition', 'Link']
|
49 |
+
else:
|
50 |
+
df = pd.DataFrame()
|
51 |
+
elif source == "amazon":
|
52 |
+
df = fetch_amazon_data(query, token)
|
53 |
+
elif source == "fipe":
|
54 |
+
df = fetch_fipe_data(query, token)
|
55 |
+
else:
|
56 |
+
df = pd.DataFrame()
|
57 |
+
|
58 |
+
# Process the DataFrame similarly for all sources if applicable
|
59 |
+
# This is an example for MercadoLibre data; adjust processing as needed for other sources
|
60 |
+
if not df.empty:
|
61 |
+
# Additional processing here, like calculating z-scores, filtering, etc.
|
62 |
+
pass
|
63 |
+
|
64 |
+
return df
|
65 |
|
66 |
def gradio_app(query):
|
67 |
+
# Example: Fetch data from MercadoLibre, adjust to include other sources as needed
|
68 |
+
df = fetch_data_to_dataframe(query, 50, source="mercadolibre")
|
69 |
+
if df.empty:
|
70 |
+
return "No data found", pd.DataFrame()
|
71 |
+
else:
|
72 |
+
median_price = df['Price'].median()
|
73 |
+
return median_price, df
|
74 |
|
75 |
iface = gr.Interface(fn=gradio_app,
|
76 |
inputs=gr.Textbox(label="Insira a consulta de pesquisa"),
|