fschwartzer commited on
Commit
3bd1e98
1 Parent(s): 7a6bd46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -39
app.py CHANGED
@@ -3,37 +3,42 @@ import requests
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":
@@ -63,19 +68,30 @@ def fetch_data_to_dataframe(query, limit=50, source="mercadolibre", token=None):
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"),
77
- outputs=[gr.Textbox(label="Preço mediano"), gr.Dataframe(label="Resultados da pesquisa")],
78
- title="Bens Móveis",
79
- description="App para avaliação de bens móveis")
80
 
81
- iface.launch()
 
 
 
 
 
 
 
 
 
3
  import pandas as pd
4
  from scipy import stats
5
 
6
+ import google.generativeai as genai
7
+
8
+ # Proper configuration with your API key
9
+ genai.configure(api_key="AIzaSyCm57IpC9_TTL7U3m8wvje9_3qtfxAASgI") # Replace YOUR_API_KEY with the actual API key
10
+
11
+ # Set up the model configuration
12
+ generation_config = {
13
+ "temperature": 0.7,
14
+ "top_p": 1,
15
+ "top_k": 1,
16
+ "max_output_tokens": 2048,}
17
+
18
+ # Safety settings
19
+ safety_settings = [
20
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
21
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
22
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
23
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
24
+ ]
25
+
26
+ # Initialize the model
27
+ model = genai.GenerativeModel(model_name="gemini-pro",
28
+ generation_config=generation_config,
29
+ safety_settings=safety_settings)
30
+
31
+ def gemini(query):
32
+ prompt_parts = [
33
+ f"input: \"Informar o preço médio de {query} em Reais brasileiros e explicar as características que podem representar variação nos preços\"",
34
+ "output: ",
35
+ f"input: {query}",
36
+ "output: ",
37
+ ]
38
+
39
+ response = model.generate_content(prompt_parts)
40
+ return response.text
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def fetch_data_to_dataframe(query, limit=50, source="mercadolibre", token=None):
44
  if source == "mercadolibre":
 
68
 
69
  return df
70
 
71
+ def integrated_app(query):
72
+ # Interpret the prompt using the Gemini model
73
+ interpreted_response = gemini(query) # You'll need to adjust the gemini function to return a usable query term
74
+
75
+ # Fetch data from Mercado Livre based on the interpreted query
76
+ df = fetch_data_to_dataframe(interpreted_response, 50, source="mercadolibre")
77
+
78
  if df.empty:
79
  return "No data found", pd.DataFrame()
80
  else:
81
+ # Process the fetched data
82
  median_price = df['Price'].median()
83
+ # You could add more processing here based on the Gemini response
84
+
85
+ # Return the processed data
86
  return median_price, df
87
 
 
 
 
 
 
88
 
89
+ # Define the updated Gradio interface
90
+ iface = gr.Interface(fn=integrated_app,
91
+ inputs=gr.Textbox(label="Digite sua consulta"),
92
+ outputs=[gr.Textbox(label="Preço Mediano"), gr.Dataframe(label="Resultados da Pesquisa")],
93
+ title="Análise Integrada de Bens",
94
+ description="Esta aplicação combina a interpretação de prompts via modelo Gemini com a busca de dados no Mercado Livre para oferecer uma análise de preços e características de bens.")
95
+
96
+ # Launch the interface
97
+ iface.launch()