File size: 4,774 Bytes
1cb59e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import streamlit as st
import sys
import os
import json
from src.config import cerebras_llm
from src.crew import create_crew  # Assuming you have a function that creates the crew setup

# Ensure the src folder is part of the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'src')))

# Function to run the entire crew
def run_crew(product_name, country, model_name):
    # Create the crew with agents and tasks
    event_management_crew = create_crew(product_name, country, model_name)

    # Format the input for the crew
    event_details = {'product': product_name, 'country': country, 'model': model_name}

    # Execute Crew (this will run all the tasks)
    event_analysis = event_management_crew.kickoff(inputs=event_details)
    
    return event_analysis

# Function to clean the output and remove unwanted fields
def clean_output(output):
    if isinstance(output, dict):
        output = json.dumps(output, indent=4)
        output = output.replace('"pydantic":null,', '')
        output = output.replace('"json_dict":null,', '')
        output = output.replace('"tasks_output":[]', '')
        output = output.replace('"token_usage":', '')
        return output
    return output

# Streamlit Page Config
st.set_page_config(page_title="AI Price Comparator", page_icon="πŸ›’", layout="wide")

# Initialize session states for history and reports
if 'history' not in st.session_state:
    st.session_state.history = []
if 'reports' not in st.session_state:
    st.session_state.reports = {}

# Sidebar for API Key Uploads, History, and Model Selection
with st.sidebar:
    st.header("πŸ”‘ **API Keys**")
    cerebras_api_key = st.text_input("🧠 Cerebras API Key", type="password")
    serper_api_key = st.text_input("πŸ” Serper API Key", type="password")
    
    # Model Selection
    # Sidebar Model Selection
    st.header("🧠 **Select Model**")
    model_name = st.selectbox(
        "Choose a Model",
        ["cerebras/llama-3.1-8b", "cerebras/llama-3.3-70b", "cerebras/deepseek-r1-distill-llama-70b"]
    )

    # History Tab
    st.header("πŸ“œ **Search History**")
    if st.session_state.history:
        for idx, search in enumerate(st.session_state.history):
            if st.button(f"πŸ”Ž {search['product_name']} in {search['country']}", key=f"search_{idx}"):
                st.session_state.selected_search = search  # Store selected search
                st.rerun()
    else:
        st.write("No previous searches yet.")

# **Main UI**
st.markdown("## πŸš€ **Welcome to the Price Comparison Tool!** πŸ›’")
st.write("Enter the product details below to compare prices across multiple platforms. πŸ“‰")

# **Inputs for product and country (always visible)**
selected_search = st.session_state.get('selected_search', {})

product_name = st.text_input(
    "πŸ’‘ **Product Name**", 
    selected_search.get('product_name', "Sony WH-1000XM5")
)
country = st.text_input(
    "🌍 **Country**", 
    selected_search.get('country', "United States")
)

# **Button to compare prices**
if st.button("πŸ” **Compare Prices**", help="Click to analyze prices and get a detailed comparison"):
    if product_name and country:
        st.write(f"πŸ›’ **Analyzing prices for** **{product_name}** in **{country}**... πŸ“ˆ")

        # Run the crew and get the results
        event_analysis = run_crew(product_name, country, model_name)

        # Clean the output and display the results
        cleaned_output = clean_output(event_analysis)
        
        st.subheader("πŸ“Š **Price Comparison Report**")
        st.markdown(cleaned_output)

        # Store the search and report
        search_key = f"{product_name}_{country}"
        search_data = {'product_name': product_name, 'country': country, 'model_name': model_name}
        
        if search_data not in st.session_state.history:
            st.session_state.history.append(search_data)

        st.session_state.reports[search_key] = cleaned_output  # Save the report
        
        # Clear selected search after displaying results
        st.session_state.selected_search = {'product_name': product_name, 'country': country}
    else:
        st.error("❌ Please enter both product name and country.")

# **Display saved report if a past search is selected**
search_key = f"{product_name}_{country}"
if search_key in st.session_state.reports:
    st.subheader("πŸ“Š **Saved Price Comparison Report**")
    st.markdown(st.session_state.reports[search_key])

    # **Download Button for the Report**
    # report_json = st.session_state.reports[search_key].encode('utf-8')
    # st.download_button(
    #     label="πŸ“₯ Download Report",
    #     data=report_json,
    #     file_name=f"{search_key}.json",
    #     mime="application/json"
    # )