engrtm commited on
Commit
72387a9
Β·
verified Β·
1 Parent(s): 812878f

Create aap.py

Browse files
Files changed (1) hide show
  1. aap.py +145 -0
aap.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================================================
2
+ # 🚩 1. Install dependencies
3
+ # =========================================================
4
+ !pip install -q pandas openpyxl requests openai
5
+
6
+ # =========================================================
7
+ # 🚩 2. Configuration
8
+ #=========================================================
9
+ ONEDRIVE_LINK = "https://1drv.ms/x/c/c6536ba97412fbf8/EcBbRa59eqFCs0Yiy16dN1MBPDoXOWUimCkO9oCPDaDMfg"
10
+ OPENAI_API_KEY = "sk-proj-EQTDxpzroHcMO5uqytSmoM3VDBxC9OiRS9heyFSzG90fvKK1ffcFCrAbhXQm12NhYxIiOIjDdyQT3BlbkFJ5Xz2cTGDOtgACBFeqNKoWPgEj_9E-0cmE0oDN61yBgciBFF7td_rqlIdbozVJhYnksY5FHoNoA"
11
+
12
+ # =========================================================
13
+ # 🚩 3. Helpers
14
+ # =========================================================
15
+ import pandas as pd, requests, openai
16
+ from io import BytesIO
17
+ import os # Import the os module to save the file
18
+ import zipfile # Import zipfile to check if the content is a zip file
19
+
20
+ def to_direct_download(url: str) -> str:
21
+ """Convert OneDrive view/redir link to direct download link (.xlsx)."""
22
+ # This conversion might not be universally reliable for all OneDrive link types.
23
+ # It works for common "?e=XXXXX" share links.
24
+ return (
25
+ url.replace("view.aspx?", "download.aspx?")
26
+ .replace("redir?", "download?")
27
+ )
28
+
29
+ def load_excel(url: str) -> pd.DataFrame:
30
+ dl_url = to_direct_download(url)
31
+ print(f"Attempting to download from: {dl_url}") # Print the direct download URL
32
+
33
+ temp_file_path = "downloaded_content.bin" # Define temp file path outside try/except
34
+
35
+ try:
36
+ resp = requests.get(dl_url)
37
+ resp.raise_for_status() # ⬅️ will fail if link is wrong (e.g., 404)
38
+
39
+ # --- Added extensive diagnostic code ---
40
+ content_bytes = BytesIO(resp.content)
41
+ content_bytes.seek(0) # Reset stream position after creating BytesIO
42
+
43
+ # Check the content type
44
+ content_type = resp.headers.get('Content-Type', '').lower()
45
+ print(f"Content-Type received: {content_type}")
46
+
47
+ # Save the content to a file for inspection
48
+ with open(temp_file_path, "wb") as f:
49
+ f.write(resp.content)
50
+ print(f"Content saved to {temp_file_path} for inspection.")
51
+ print("Please check this file. It should be a valid .xlsx file.")
52
+
53
+ # Additional checks for common issues
54
+ if 'html' in content_type:
55
+ print("Error: Content-Type is HTML. The link likely returned a web page instead of the file.")
56
+ print("Inspect 'downloaded_content.bin' to see the HTML content.")
57
+ raise ValueError("Downloaded content is HTML, expected Excel file.")
58
+
59
+ # Check if the downloaded content is a zip file (Excel files are zip archives)
60
+ # A more robust check is to see if it's a zip *and* contains typical Excel files
61
+ try:
62
+ # Use BytesIO to treat the content as a file for zipfile check
63
+ with zipfile.ZipFile(content_bytes, 'r') as zf:
64
+ zip_contents = zf.namelist()
65
+ print(f"Content appears to be a zip file with contents: {zip_contents}")
66
+ # Check for typical Excel structure files
67
+ if '[Content_Types].xml' not in zip_contents or '_rels/.rels' not in zip_contents:
68
+ print("Warning: Zip file does not contain expected Excel structure files.")
69
+ # You might consider raising an error here if these are missing
70
+
71
+ # Reset stream position before passing to pandas
72
+ content_bytes.seek(0)
73
+
74
+ except zipfile.BadZipFile:
75
+ print("Error: Downloaded content is NOT a valid zip file. This is the most likely cause of the BadZipFile error.")
76
+ # Re-raise the BadZipFile error with a more helpful message
77
+ raise zipfile.BadZipFile("Downloaded content is not a valid zip file, expected an Excel (.xlsx) file.") from None
78
+ except Exception as e:
79
+ print(f"An unexpected error occurred while checking if the content is a zip file: {e}")
80
+ # Still try to read as Excel, but note the potential issue
81
+
82
+
83
+ # Attempt to read the excel file from the BytesIO object
84
+ print("Attempting to read content as Excel...")
85
+ # Pass the BytesIO object directly
86
+ df = pd.read_excel(content_bytes, engine="openpyxl")
87
+
88
+ # If successful, clean up the temp file
89
+ if os.path.exists(temp_file_path):
90
+ os.remove(temp_file_path)
91
+ print(f"Cleaned up temporary file: {temp_file_path}")
92
+
93
+ return df
94
+
95
+ except requests.exceptions.RequestException as e:
96
+ print(f"Error during file download: {e}")
97
+ print("Please check the ONEDRIVE_LINK and your internet connection.")
98
+ # Clean up temp file on download error
99
+ if os.path.exists(temp_file_path):
100
+ os.remove(temp_file_path)
101
+ print(f"Cleaned up temporary file: {temp_file_path}")
102
+ raise e # Re-raise the exception to see the full traceback
103
+
104
+ except Exception as e:
105
+ # Catch any other exceptions during read_excel or zipfile checks
106
+ print(f"Error reading or processing downloaded file: {e}")
107
+ print("This often happens if the downloaded content is not a readable Excel file even if it was a zip.")
108
+ print(f"Inspect the file '{temp_file_path}' to understand what was downloaded.")
109
+ # Do not clean up temp file on processing error, keep it for inspection
110
+ raise e # Re-raise the exception after providing diagnostic info
111
+
112
+
113
+ openai.api_key = OPENAI_API_KEY
114
+
115
+ def ask_gpt(df: pd.DataFrame, question: str) -> str:
116
+ context = df.to_string(index=False)
117
+ prompt = f"Use only this table to answer.\n\n{context}\n\nQuestion: {question}"
118
+ resp = openai.ChatCompletion.create(
119
+ model="gpt-3.5-turbo", # or gpt-4 / gpt-4o
120
+ messages=[{"role": "user", "content": prompt}]
121
+ )
122
+ return resp.choices[0].message.content.strip()
123
+
124
+ # =========================================================
125
+ # 🚩 4. Load Excel
126
+ # =========================================================
127
+ # Note: This will still fail if the file is not a valid Excel file,
128
+ # but the added diagnostics will provide more information about *why*.
129
+ try:
130
+ df = load_excel(ONEDRIVE_LINK)
131
+ print("βœ… Excel loaded! Preview:")
132
+ display(df.head())
133
+
134
+ # =========================================================
135
+ # 🚩 5. Ask a question
136
+ # =========================================================
137
+ question = "Replace this with your question"
138
+ print("\nQ:", question)
139
+ print("A:", ask_gpt(df, question))
140
+
141
+ except Exception as e:
142
+ print(f"\nExecution stopped due to error loading Excel: {e}")
143
+ # You might want to add error handling or alternative logic here
144
+ # if the Excel loading fails.
145
+ pass # Or handle the error gracefully