yashm commited on
Commit
1a6138e
·
verified ·
1 Parent(s): 2eb7a21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -85
app.py CHANGED
@@ -1,92 +1,64 @@
1
  import streamlit as st
2
- import sys
3
- import subprocess
4
 
5
- # Install compatible version of PyPDF2
6
- subprocess.check_call([sys.executable, "-m", "pip", "install", "PyPDF2==2.12.1"])
 
7
 
8
- import camelot
9
- import fitz # PyMuPDF
10
- import pandas as pd
11
- import os
12
- import tempfile
13
 
14
- # Set the title of the Streamlit app
15
- st.title("PDF Table Extractor")
 
 
 
 
 
16
 
17
- # Instructions
18
- st.write("Upload a PDF file containing tables, and this app will extract the tables for you.")
 
19
 
20
- # File uploader widget
21
- uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
 
 
22
 
23
- if uploaded_file is not None:
24
- try:
25
- # Load the uploaded PDF using PyMuPDF
26
- pdf_document = fitz.open(stream=uploaded_file.read(), filetype="pdf")
27
-
28
- # Show the number of pages in the PDF
29
- num_pages = pdf_document.page_count
30
- st.write(f"The uploaded PDF has {num_pages} pages.")
31
-
32
- # Let the user select pages to extract tables from
33
- page_range = st.text_input("Enter page range (e.g., 1-3,5,7-9) or 'all' for all pages:", "all")
34
-
35
- # Camelot options
36
- flavor = st.selectbox("Select table flavor:", ["lattice", "stream"])
37
-
38
- # Extract tables using Camelot
39
- if st.button("Extract Tables"):
40
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
41
- temp_file.write(uploaded_file.getvalue())
42
- temp_file_path = temp_file.name
43
-
44
- try:
45
- with st.spinner("Extracting tables..."):
46
- if page_range.lower() == 'all':
47
- pages = str(list(range(1, num_pages + 1)))[1:-1]
48
- else:
49
- pages = page_range
50
-
51
- tables = camelot.read_pdf(temp_file_path, pages=pages, flavor=flavor)
52
-
53
- if len(tables) > 0:
54
- st.write(f"Found {len(tables)} table(s).")
55
-
56
- # Loop through all the extracted tables
57
- for i, table in enumerate(tables):
58
- st.write(f"Table {i+1} (Page {table.parsing_report['page']}):")
59
-
60
- # Convert the table to a Pandas DataFrame
61
- df = table.df
62
-
63
- # Display the extracted table
64
- st.dataframe(df)
65
-
66
- # Download buttons
67
- col1, col2 = st.columns(2)
68
- with col1:
69
- csv = df.to_csv(index=False).encode('utf-8')
70
- st.download_button(
71
- label=f"Download as CSV",
72
- data=csv,
73
- file_name=f"table_{i+1}_page_{table.parsing_report['page']}.csv",
74
- mime="text/csv"
75
- )
76
- with col2:
77
- excel_file = df.to_excel(index=False, engine="xlsxwriter")
78
- st.download_button(
79
- label=f"Download as Excel",
80
- data=excel_file,
81
- file_name=f"table_{i+1}_page_{table.parsing_report['page']}.xlsx",
82
- mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
83
- )
84
- else:
85
- st.write("No tables found in the selected pages.")
86
-
87
- finally:
88
- # Clean up the temporary file
89
- os.unlink(temp_file_path)
90
-
91
- except Exception as e:
92
- st.error(f"An error occurred: {str(e)}")
 
1
  import streamlit as st
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
 
5
+ def draw_circle(ax, center, radius, color='black', fill=False, lw=1.5):
6
+ circle = plt.Circle(center, radius, clip_on=False, edgecolor=color, facecolor='none' if not fill else color, lw=lw, fill=fill)
7
+ ax.add_artist(circle)
8
 
9
+ def draw_line(ax, start, end, color='black', lw=1.5):
10
+ ax.plot([start[0], end[0]], [start[1], end[1]], color=color, lw=lw)
 
 
 
11
 
12
+ def setup_plot(xlim=(-1.5, 1.5), ylim=(-1.5, 1.5), aspect='equal'):
13
+ fig, ax = plt.subplots(figsize=(6, 6))
14
+ ax.set_xlim(xlim)
15
+ ax.set_ylim(ylim)
16
+ ax.set_aspect(aspect)
17
+ ax.axis('off')
18
+ return fig, ax
19
 
20
+ def draw_shree_yantra(ax, radius=1, sides=12, line_skips=[2,5,7,10], circle_fill=False, line_color='black', circle_color='black'):
21
+ # Step 1: Draw outer circle
22
+ draw_circle(ax, (0, 0), radius, color=circle_color, fill=circle_fill, lw=2)
23
 
24
+ # Step 2: Draw polygon (e.g., dodecagon for 12 sides)
25
+ points = [np.array([np.cos(theta), np.sin(theta)]) for theta in np.linspace(0, 2*np.pi, sides, endpoint=False)]
26
+ polygon = plt.Polygon(points, closed=True, fill=False, edgecolor=circle_color, lw=2)
27
+ ax.add_artist(polygon)
28
 
29
+ # Step 3: Draw connecting lines based on skips
30
+ for i, p1 in enumerate(points):
31
+ for j, p2 in enumerate(points):
32
+ if i != j and abs(i - j) in line_skips:
33
+ draw_line(ax, p1 * radius, p2 * radius, color=line_color, lw=1.5)
34
+
35
+ # Additional steps can be implemented here following the traditional Shree Yantra construction
36
+ # For simplicity, this example draws a basic pattern
37
+
38
+ def main():
39
+ st.title("Shree Yantra Diagram Generator")
40
+
41
+ st.sidebar.header("Customize Your Shree Yantra")
42
+
43
+ # Sidebar controls
44
+ radius = st.sidebar.slider("Outer Circle Radius", 0.5, 2.0, 1.0, 0.1)
45
+ sides = st.sidebar.slider("Number of Sides (Polygon)", 6, 20, 12, 1)
46
+ line_skips = st.sidebar.multiselect("Line Skips", options=list(range(1, sides//2 +1)), default=[2,5,7,10])
47
+ line_color = st.sidebar.color_picker("Line Color", "#000000")
48
+ circle_color = st.sidebar.color_picker("Circle & Polygon Color", "#000000")
49
+ circle_fill = st.sidebar.checkbox("Fill Outer Circle", value=False)
50
+
51
+ st.write("### Shree Yantra Diagram")
52
+
53
+ fig, ax = setup_plot()
54
+
55
+ if not line_skips:
56
+ st.warning("Please select at least one line skip to draw connecting lines.")
57
+ else:
58
+ draw_shree_yantra(ax, radius=radius, sides=sides, line_skips=line_skips,
59
+ circle_fill=circle_fill, line_color=line_color, circle_color=circle_color)
60
+
61
+ st.pyplot(fig)
62
+
63
+ if __name__ == "__main__":
64
+ main()