File size: 3,696 Bytes
c5a97b3
 
 
b6a54e8
c5a97b3
3bd1e98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a6bd46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a97b3
3bd1e98
 
 
 
 
 
 
7a6bd46
 
 
3bd1e98
7a6bd46
3bd1e98
 
 
7a6bd46
c5a97b3
 
3bd1e98
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import gradio as gr
import requests
import pandas as pd
from scipy import stats

import google.generativeai as genai

# Proper configuration with your API key
genai.configure(api_key="AIzaSyCm57IpC9_TTL7U3m8wvje9_3qtfxAASgI")  # Replace YOUR_API_KEY with the actual API key

# Set up the model configuration
generation_config = {
    "temperature": 0.7,
    "top_p": 1,
    "top_k": 1,
    "max_output_tokens": 2048,}

# Safety settings
safety_settings = [
    {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
    {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
    {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
    {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
]

# Initialize the model
model = genai.GenerativeModel(model_name="gemini-pro",
                              generation_config=generation_config,
                              safety_settings=safety_settings)

def gemini(query):
    prompt_parts = [
        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\"",
        "output: ",
        f"input: {query}",
        "output: ",
    ]

    response = model.generate_content(prompt_parts)
    return response.text


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 integrated_app(query):
    # Interpret the prompt using the Gemini model
    interpreted_response = gemini(query)  # You'll need to adjust the gemini function to return a usable query term
    
    # Fetch data from Mercado Livre based on the interpreted query
    df = fetch_data_to_dataframe(interpreted_response, 50, source="mercadolibre")
    
    if df.empty:
        return "No data found", pd.DataFrame()
    else:
        # Process the fetched data
        median_price = df['Price'].median()
        # You could add more processing here based on the Gemini response
        
        # Return the processed data
        return median_price, df


# Define the updated Gradio interface
iface = gr.Interface(fn=integrated_app,
                     inputs=gr.Textbox(label="Digite sua consulta"),
                     outputs=[gr.Textbox(label="Preço Mediano"), gr.Dataframe(label="Resultados da Pesquisa")],
                     title="Análise Integrada de Bens",
                     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.")

# Launch the interface
iface.launch()