File size: 4,787 Bytes
eb4b18c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
import streamlit as st
import pandas as pd
import anthropic
import base64
import plotly.express as px
from datetime import datetime
import json
from pathlib import Path
from utils import EmbeddingManager

def send_to_llm(user_query, data, df):
    """Send data chunks to the LLM and get a response."""    
    columns = df.columns.tolist()
    dtypes = df.dtypes.to_dict()
    summary_stats = df.describe().to_json()
    client = anthropic.AnthropicBedrock()
    
    prompt = f"""Given this Excel data:

    

    Columns: {columns}

    Data types: {dtypes}

    Summary statistics: {summary_stats}

    

    Create a complete, professional HTML report that includes:

    1. Executive summary

    2. Data insights and patterns

    3. Statistical analysis

    4. Visualizations using Plotly

    

    Important Requirements:

    - Include all necessary Plotly CDN scripts

    - Choose appropriate visualizations based on the data patterns

    - Include proper styling with CSS

    - Make it visually appealing and professional

    - Add explanations for each insight and visualization

    - Include the current date in the report

    

    For visualizations:

    - Use Plotly.js for all charts

    - Include the full Plotly JavaScript code

    - Choose appropriate chart types based on the data

    - Add proper titles, labels, and legends

    

    Return only the complete HTML code that's ready to be saved as an HTML file."""

    message = client.messages.create(
        model="anthropic.claude-3-5-sonnet-20240620-v1:0",
        max_tokens=4096,  # Increased token limit for full HTML response
        system=prompt,
        messages=[{
            "role": "user", 
            "content": f"Generate a complete HTML report analyzing this data: {str(data)} \nUser query: {user_query}"
        }]
    )
    
    # Extract only the HTML content
    response_text = message.content[0].text
    if "<!DOCTYPE html>" not in response_text:
        # Fallback if response isn't proper HTML
        return f"""

        <!DOCTYPE html>

        <html>

        <head>

            <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

        </head>

        <body>

            <h1>Analysis Report</h1>

            {response_text}

        </body>

        </html>

        """
    
    return response_text

def create_chunks_and_send(data: pd.DataFrame,filename):
    output_dir=Path('./output')
    embeddings_dir = output_dir / 'embeddings' / filename
    
    if embeddings_dir.exists():
        chunks_file = embeddings_dir / "chunks.json"
        if chunks_file.is_file():
            with open(chunks_file, "r", encoding="utf-8") as f:
                chunks = json.load(f)
                return chunks
            #print("File content as list:", chunks)
        else:
            print("chunks.json does not exist in the directory.")
            
    else:
     embeddings_dir.mkdir(parents=True, exist_ok=True)
     embedding_manager = EmbeddingManager(output_dir=Path('./output'))
     text = data.to_json()
     chunks, embedding_dir = embedding_manager.process_script(data=str(text),filename=filename)
     return chunks


def main():
    st.title("Excel Chatbot")
    query = st.text_input("Enter the query")
    # File upload
    uploaded_file = st.file_uploader("Upload Excel File", type=['xlsx', 'xls'])
    
    if uploaded_file and query:
        try:
            # Read Excel file
           
                df = pd.read_excel(uploaded_file)
                
                
                st.success("File uploaded successfully!")
                
                # Show data preview
                st.subheader("Data Preview")
                st.dataframe(df)
                
            
                with st.spinner("Generating response with Claude..."):
                    # Initialize Claude client
## use the out put embeddings  saved
                    data = create_chunks_and_send(df,uploaded_file.name)
                    # Get complete HTML report from Claude
                    response = send_to_llm(user_query=query,data=data,df=df)
                    
                    # Get the HTML content (ensure it's a string)
                    html_report = response
            
                    st.components.v1.html(html_report, height=800, scrolling=True)
                    
                    st.success("Report generated successfully! Click the link above to download.")
                    
        except Exception as e:
            st.error(f"An error occurred: {str(e)}")
            #st.error("Please check your API key and file format, then try again.")

if __name__ == "__main__":
    main()