ZainMalik0925 commited on
Commit
6bfcc30
·
verified ·
1 Parent(s): c6fa658

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -25
app.py CHANGED
@@ -4,7 +4,6 @@ import plotly.express as px
4
  import requests
5
  from io import BytesIO
6
  from PyPDF2 import PdfReader
7
- import time
8
 
9
  # Set Page Configurations
10
  st.set_page_config(page_title="GreenLens-AI", layout="wide")
@@ -36,56 +35,52 @@ st.markdown("<p>A Sustainable Tool for Calculating Carbon, Energy, and Ecologica
36
  DATASET_URL = "https://drive.google.com/uc?id=1QY9yv2mhz4n8bOTi4ahbjBpapltqXV6D"
37
 
38
  # Function to fetch dataset from Google Drive
39
- @st.cache_data
40
  def fetch_pdf_from_drive(url):
41
  """Fetch the dataset (PDF) from Google Drive."""
42
- progress_text = "Downloading dataset from Google Drive..."
43
- progress_bar = st.progress(0)
44
-
45
- try:
46
- response = requests.get(url, stream=True)
47
  total_size = int(response.headers.get('content-length', 0))
48
  downloaded_size = 0
49
  chunks = []
50
 
51
- for chunk in response.iter_content(chunk_size=8192):
52
- downloaded_size += len(chunk)
53
- progress_bar.progress(min(1.0, downloaded_size / total_size), text=progress_text)
54
- chunks.append(chunk)
55
 
56
  pdf_content = b"".join(chunks)
57
- progress_bar.progress(1.0, text="Download Complete")
58
  st.success("Dataset downloaded successfully!")
59
  return PdfReader(BytesIO(pdf_content))
60
-
61
- except Exception as e:
62
- st.error(f"Error fetching dataset: {e}")
63
  return None
64
 
65
- # Function to extract data from the PDF
66
- @st.cache_data
67
  def process_pdf_data(pdf_reader):
68
- """Extract relevant data from the PDF."""
 
69
  extracted_data = {}
 
70
  for page in pdf_reader.pages:
71
  text = page.extract_text()
72
- # Example: Extract fiber impact data or other metrics
 
73
  if "Global average water footprint of cotton fabric" in text:
74
  extracted_data["Cotton"] = {"Water": 10000, "Energy": 60, "Carbon": 3.18}
75
  if "Water footprint by region: China" in text:
76
  extracted_data["China"] = {"Water": 6000}
 
 
77
  return extracted_data
78
 
79
- # Fetch and process the dataset
80
- st.info("Fetching dataset. Please wait...")
81
  pdf_reader = fetch_pdf_from_drive(DATASET_URL)
82
  if pdf_reader:
83
  fiber_impact_data = process_pdf_data(pdf_reader)
84
- if not fiber_impact_data:
85
- st.error("Failed to extract data from the PDF. Please check the PDF structure.")
86
  else:
87
- st.error("Failed to fetch the dataset. Check your internet connection or the dataset link.")
88
- fiber_impact_data = {}
89
 
90
  # Sidebar for User Inputs
91
  st.sidebar.header("Input Product Details")
 
4
  import requests
5
  from io import BytesIO
6
  from PyPDF2 import PdfReader
 
7
 
8
  # Set Page Configurations
9
  st.set_page_config(page_title="GreenLens-AI", layout="wide")
 
35
  DATASET_URL = "https://drive.google.com/uc?id=1QY9yv2mhz4n8bOTi4ahbjBpapltqXV6D"
36
 
37
  # Function to fetch dataset from Google Drive
 
38
  def fetch_pdf_from_drive(url):
39
  """Fetch the dataset (PDF) from Google Drive."""
40
+ st.info("Downloading dataset from Google Drive...")
41
+ response = requests.get(url, stream=True)
42
+ if response.status_code == 200:
 
 
43
  total_size = int(response.headers.get('content-length', 0))
44
  downloaded_size = 0
45
  chunks = []
46
 
47
+ with st.spinner("Fetching dataset..."):
48
+ for chunk in response.iter_content(chunk_size=8192):
49
+ downloaded_size += len(chunk)
50
+ chunks.append(chunk)
51
 
52
  pdf_content = b"".join(chunks)
 
53
  st.success("Dataset downloaded successfully!")
54
  return PdfReader(BytesIO(pdf_content))
55
+ else:
56
+ st.error("Failed to fetch dataset. Please check the URL or your internet connection.")
 
57
  return None
58
 
59
+ # Function to extract relevant data from the PDF
 
60
  def process_pdf_data(pdf_reader):
61
+ """Process the PDF file and extract relevant data."""
62
+ st.info("Processing dataset...")
63
  extracted_data = {}
64
+
65
  for page in pdf_reader.pages:
66
  text = page.extract_text()
67
+
68
+ # Example parsing rules (adjust based on the actual file format):
69
  if "Global average water footprint of cotton fabric" in text:
70
  extracted_data["Cotton"] = {"Water": 10000, "Energy": 60, "Carbon": 3.18}
71
  if "Water footprint by region: China" in text:
72
  extracted_data["China"] = {"Water": 6000}
73
+
74
+ st.success("Dataset processed successfully!")
75
  return extracted_data
76
 
77
+ # Step 1: Fetch dataset from Google Drive
 
78
  pdf_reader = fetch_pdf_from_drive(DATASET_URL)
79
  if pdf_reader:
80
  fiber_impact_data = process_pdf_data(pdf_reader)
81
+ st.write(fiber_impact_data) # Debugging: Display parsed data
 
82
  else:
83
+ st.stop()
 
84
 
85
  # Sidebar for User Inputs
86
  st.sidebar.header("Input Product Details")