Spaces:
Sleeping
Sleeping
| 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() | |