KushwanthK commited on
Commit
433bcaa
·
verified ·
1 Parent(s): fbcf1e6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +685 -235
app.py CHANGED
@@ -1,124 +1,425 @@
 
 
 
 
1
  import streamlit as st
2
- import os
3
- from streamlit_chat import message
4
- import numpy as np
5
- import pandas as pd
6
- from io import StringIO
7
- import io
8
- import PyPDF2
9
- import pymupdf
10
- import tempfile
11
- import base64
12
- # from tqdm.auto import tqdm
13
- import math
14
- # from transformers import pipeline
15
 
16
- from collections import Counter
17
- import nltk
18
 
19
- nltk.download('stopwords')
20
- from nltk.corpus import stopwords
21
- import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
 
 
 
23
 
24
- from sentence_transformers import SentenceTransformer
25
- import torch
26
- from langchain_community.llms.ollama import Ollama
27
- from langchain.prompts import ChatPromptTemplate
28
- from langchain_community.vectorstores import FAISS
29
 
30
- from langchain_community.llms import HuggingFaceHub
31
- # from langchain.vectorstores import faiss
32
- # from langchain.vectorstores import FAISS
33
 
34
- import time
35
- from time import sleep
36
- from stqdm import stqdm
37
- from dotenv import load_dotenv
 
38
 
39
- # Load environment variables from .env file
40
- load_dotenv()
 
 
 
41
 
42
- HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
 
 
 
 
43
 
 
 
 
 
 
 
 
 
44
 
45
- device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
46
 
47
- # if device != 'cuda':
48
- # st.markdown(f"you are using {device}. This is much slower than using "
49
- # "a CUDA-enabled GPU. If on colab you can change this by "
50
- # "clicking Runtime > change runtime type > GPU.")
51
 
52
- model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2", device=device)
53
- def display_title():
54
- selected_value = st.session_state["value"]
55
 
56
- st.header(f'Vedic Scriptures: {selected_value} :blue[book] :books:')
 
 
 
57
 
58
- question = "ask anything about scriptures"
59
- def open_chat():
60
- question = st.session_state["faq"]
 
 
 
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- if "value" not in st.session_state:
65
- st.session_state["value"] = None
66
 
67
- if "faq" not in st.session_state:
68
- st.session_state["faq"] = None
 
69
 
70
- # st.divider()
 
71
 
72
- def upload_file():
73
- uploaded_file = st.file_uploader("Upload a file", type=["pdf"])
74
- if uploaded_file is not None:
75
- st.write(uploaded_file.name)
76
- return uploaded_file.name
77
 
78
- def create_pickle_file(filepath):
 
 
79
 
80
- from langchain_community.document_loaders import PyMuPDFLoader
81
- loader = PyMuPDFLoader(filepath)
82
- pages = loader.load()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- # Load a pre-trained sentence transformer model
85
- model_name = "sentence-transformers/all-mpnet-base-v2"
86
- model_kwargs = {'device': 'cpu'}
87
- encode_kwargs = {'normalize_embeddings': False}
88
 
89
- # Create a HuggingFaceEmbeddings object
90
- from langchain_community.embeddings import HuggingFaceEmbeddings
91
- embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs)
92
 
93
- # from pathlib import Path
94
 
95
- # path = Path(filepath)
96
 
97
- filename = filepath.split(".")
 
 
 
98
 
99
- print(filename[0])
 
100
 
101
- filename = filename[0]
 
102
 
103
- from datetime import datetime
 
104
 
105
- # Get current date and time
106
- now = datetime.now()
 
107
 
108
- # Format as string with milliseconds
109
- formatted_datetime = now.strftime("%Y-%m-%d_%H:%M:%S.%f")[:-3]
110
 
111
- print(formatted_datetime)
 
112
 
113
- # Create FAISS index with the HuggingFace embeddings
114
- faiss_index = FAISS.from_documents(pages, embeddings)
115
- with open(f"./{filename}_{formatted_datetime}.pkl", "wb") as f:
116
- pickle.dump(faiss_index, f)
117
 
 
 
 
 
118
 
119
- # uploaded_file_name = upload_file()
120
- # if uploaded_file_name is not None:
121
- # create_pickle_file(uploaded_file_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  def highlight_pdf(file_path, text_to_highlight, page_numbers):
124
  # Create a temporary file to save the modified PDF
@@ -130,14 +431,13 @@ def highlight_pdf(file_path, text_to_highlight, page_numbers):
130
  # Open the original PDF
131
  doc = pymupdf.open(file_path)
132
 
133
- pages_to_display = [doc.load_page(page_number) for page_number in page_numbers]
134
-
135
- print("pages_to_display")
136
- print(pages_to_display)
137
 
138
  # Tokenize the text into words
139
  words = text_to_highlight.split()
140
 
 
 
141
  # Remove stopwords
142
  stop_words = set(stopwords.words("english"))
143
  words = [word for word in words if word.lower() not in stop_words]
@@ -176,176 +476,326 @@ def highlight_pdf(file_path, text_to_highlight, page_numbers):
176
 
177
  print(temp_pdf_path)
178
 
179
- # new_doc.save("example_highlighted.pdf")
180
 
181
  return temp_pdf_path
182
 
183
- file_path = "Bhagavad-Gita-As-It-Is.pdf"
184
- text_to_highlight = ""
185
- sources = []
186
 
187
  # Function to display PDF in Streamlit
188
- def display_highlighted_pdf(file_path, text_to_highlight, sources):
189
- # pdf_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
190
- # sources = [7,8]
191
- # response_text = "I offer my respectful obeisances unto the lotus feet of my spiritual master and unto the feet of all Vaiñëavas. I offer my respectful"
192
 
193
- pdf_path = highlight_pdf(file_path=file_path, text_to_highlight=text_to_highlight, page_numbers=sources)
194
 
195
  with open(pdf_path, "rb") as file:
196
  pdf_bytes = file.read()
197
  base64_pdf = base64.b64encode(pdf_bytes).decode("utf-8")
198
- pdf_display = f'<iframe is="x-frame-bypass", src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
199
  st.markdown(pdf_display, unsafe_allow_html=True)
200
 
201
- # Creating a Index(Pinecone Vector Database)
202
- import os
203
- # import pinecone
204
-
205
- import pickle
206
- @st.cache_data
207
- def get_faiss_semantic_index():
208
- try:
209
- index_path = "./HuggingFaceEmbeddings.pkl"
210
- print(index_path)
211
- # Load embeddings from the pickle file
212
- for _ in stqdm(range(5)):
213
- with open(index_path, "rb") as f:
214
- faiss_index = pickle.load(f)
215
- sleep(0.1)
216
- # st.write("Embeddings loaded successfully.")
217
- return faiss_index
218
- except Exception as e:
219
- st.error(f"Error loading embeddings: {e}")
220
- return None
221
- faiss_index = get_faiss_semantic_index()
222
- print(faiss_index)
223
-
224
- # def promt_engineer(text):
225
- PROMPT_TEMPLATE = """
226
- Instructions:
227
- -----------------------------------------------------------------------------------------
228
- Answer the question only based on the below context:
229
- - You're a Vedic AI expert in the Hindu Vedic scriptures.
230
- - Always try to provide Keep it simple answers in nice format without incomplete sentence.
231
- - Give the answer atleast 5 seperate lines addition to the title info.
232
- - provide Title: <title> Chapter: <chapter> Text No: <textnumber> Page No: <pagenumber>
233
- ------------------------------------------------------------------------------------------
234
-
235
- {context}
236
-
237
- ----------------------------------------------------------------------------------
238
-
239
- Answer the question based on the above context: {question}
240
- """
241
- # # Load the summarization pipeline with the specified model
242
- # summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
243
-
244
- # # Generate the prompt
245
- # prompt = prompt_template.format(text=text)
246
-
247
- # # Generate the summary
248
- # summary = summarizer(prompt, max_length=1024, min_length=50)[0]["summary_text"]
249
-
250
- # with st.sidebar:
251
- # st.divider()
252
- # st.markdown("*:red[Text Summary Generation]* from above Top 5 **:green[similarity search results]**.")
253
- # st.write(summary)
254
- # st.divider()
255
-
256
- def chat_actions():
257
-
258
- st.session_state["chat_history"].append(
259
- {"role": "user", "content": st.session_state["chat_input"]},
260
- )
261
-
262
- # query_embedding = model.encode(st.session_state["chat_input"])
263
- query = st.session_state["chat_input"]
264
- if faiss_index is not None:
265
- docs = faiss_index.similarity_search(query, k=6)
266
  else:
267
- st.error("Failed to load embeddings.")
268
- # docs = faiss_index.similarity_search(query, k=2)
 
 
 
269
 
270
- for doc in docs:
271
- print("\n")
272
- print(str(doc.metadata["page"]+1) + ":", doc.page_content)
273
- context_text = "\n\n---\n\n".join([doc.page_content for doc in docs])
274
 
275
- sources = [doc.metadata.get("page", None) for doc in docs]
 
 
 
 
 
 
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
278
- prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
279
- prompt = prompt_template.format(context=context_text, question=query)
280
- response_text = ""
281
- result = ""
282
- try:
283
- llm = HuggingFaceHub(
284
- repo_id="meta-llama/Meta-Llama-3-8B-Instruct", model_kwargs={"temperature": 0.1, "max_new_tokens": 256, "task":"text-generation"}
285
- )
286
- response_text = llm.invoke(prompt)
287
- escaped_query = re.escape(query)
288
- result = re.split(f'Answer the question based on the above context: {escaped_query}\n',response_text)[-1]
289
- st.write(result)
290
- except Exception as e:
291
- st.error(f"Error invoke: {e}")
292
-
293
-
294
- formatted_response = f"Response: {result}\nSources: {sources}"
295
- print(formatted_response)
296
-
297
- st.session_state["chat_history"].append(
298
- {
299
- "role": "assistant",
300
- "content": f"{result}",
301
- }, # This can be replaced with your chat response logic
302
- )
303
- # break;
304
- # Example usage
305
- file_path = "Bhagavad-Gita-As-It-Is.pdf"
306
- text_to_highlight = context_text.strip()
307
- display_highlighted_pdf(file_path, result, sources)
308
-
309
- with st.sidebar:
310
- option = st.selectbox(
311
- "Select Your Favorite Scriptures",
312
- ("Bhagvatgeetha", "Bhagavatham", "Ramayanam"),
313
- # index=None,
314
- # placeholder="Select scriptures...",
315
- key="value",
316
- on_change=display_title
317
- )
318
-
319
- st.write("You selected:", option)
320
-
321
- faq = st.selectbox(
322
- "Select Your Favorite Scriptures",
323
- ("what is jeeva and paramathma?",
324
- "What are the Krishna told to Arjuna?",
325
- "What are the key points from Krishna?",
326
- "Why does atheism exist even when all questions are answered in Bhagavad Gita?",
327
- "Why don’t all souls surrender to Lord Krishna, although he has demonstrated that everyone is part and parcel of Him, and all can be liberated from all sufferings by surrendering to Him?",
328
- "Why do souls misuse their independence by rebelling against Lord Krishna?",
329
- "How do I put an end to my suffering in this world?",
330
- "what is the reason behind Krishna decided to go far war?"),
331
- # index=None,
332
- # placeholder="Select scriptures...",
333
- key="faq",
334
- on_change=open_chat
335
- )
336
- st.write("You selected:", faq)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
 
 
 
 
 
 
 
 
 
 
 
338
 
339
- if "chat_history" not in st.session_state:
340
- st.session_state["chat_history"] = []
341
 
342
- st.chat_input(question, on_submit=chat_actions, key="chat_input")
 
343
 
344
- for i in st.session_state["chat_history"]:
345
- with st.chat_message(name=i["role"]):
346
- st.write(i["content"])
 
347
 
 
 
 
348
 
 
 
 
349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
350
 
351
 
 
1
+ from streamlit import session_state as ss
2
+ from streamlit_pdf_viewer import pdf_viewer
3
+ import streamlit_pdf_viewer
4
+
5
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
7
 
8
+ # # Declare variable.
9
+ # if 'pdf_ref' not in ss:
10
+ # ss.pdf_ref = None
11
+
12
+
13
+ # # Access the uploaded ref via a key.
14
+ # st.file_uploader("Upload PDF file", type=('pdf'), key='pdf')
15
+
16
+ # if ss.pdf:
17
+ # ss.pdf_ref = ss.pdf # backup
18
+
19
+ # # Now you can access "pdf_ref" anywhere in your app.
20
+ # if ss.pdf_ref:
21
+ # binary_data = ss.pdf_ref.getvalue()
22
+ # pdf_viewer(input=binary_data, width=700)
23
+
24
+ # import base64
25
+
26
+ # def displayPDF(file):
27
+ # # Opening file from file path
28
+ # with open(file, "rb") as f:
29
+ # base64_pdf = base64.b64encode(f.read()).decode('utf-8')
30
+
31
+ # # Embedding PDF in HTML
32
+ # pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
33
+
34
+ # # Displaying File
35
+ # st.markdown(pdf_display, unsafe_allow_html=True)
36
+
37
+ # displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")
38
+
39
+
40
+ # import streamlit as st
41
+ # import streamlit_pdf_viewer
42
 
43
+ # def displayPDF(file):
44
+ # with open(file, "rb") as f:
45
+ # pdf_bytes = f.read()
46
 
47
+ # streamlit_pdf_viewer.pdf_viewer(pdf_bytes)
 
 
 
 
48
 
49
+ # displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")
50
+ # Arial Unicode.ttf
 
51
 
52
+ # import streamlit as st
53
+ # import fitz # PyMuPDF library
54
+ # from PIL import Image, ImageDraw, ImageFont
55
+ # import io
56
+ # import numpy as np
57
 
58
+ # def display_pdf_with_highlight(file_path, keywords):
59
+ # # Open the PDF file
60
+ # with fitz.open(file_path) as doc:
61
+ # # Create a new PDF file to hold the highlighted pages
62
+ # highlighted_pdf = fitz.open()
63
 
64
+ # # Iterate over each page in the PDF
65
+ # for page_index in range(len(doc)):
66
+ # page = doc.load_page(page_index)
67
+ # pix = page.get_pixmap(dpi=300)
68
+ # img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
69
 
70
+ # # Create a drawing object and highlight the keywords
71
+ # draw = ImageDraw.Draw(img)
72
+ # font = ImageFont.truetype("Arial Unicode.ttf", 14) # Replace with your desired font
73
+ # for keyword in keywords:
74
+ # areas = page.search_for(keyword)
75
+ # for area in areas:
76
+ # bbox = area.bbox
77
+ # draw.rectangle(bbox, outline="yellow", width=3)
78
 
79
+ # # Convert the highlighted image to a NumPy array
80
+ # img_np = np.asarray(img)
81
 
82
+ # # Create a MuPDF-compatible Pixmap from the NumPy array
83
+ # muimg = fitz.Pixmap(fitz.csRGB, img_np.shape[1], img_np.shape[0])
84
+ # muimg.set_data(img_np.tobytes())
 
85
 
86
+ # # Create a new PDF page and insert the highlighted image
87
+ # new_page = highlighted_pdf.new_page(-1, width=muimg.width, height=muimg.height)
88
+ # new_page.insert_image(fitz.Rect(0, 0, muimg.width, muimg.height), stream=muimg)
89
 
90
+ # # Create a BytesIO object to hold the highlighted PDF data
91
+ # pdf_bytes = io.BytesIO()
92
+ # highlighted_pdf.write(pdf_bytes)
93
+ # pdf_bytes.seek(0)
94
 
95
+ # # Display the highlighted PDF in Streamlit
96
+ # st.download_button(
97
+ # label="Download Highlighted PDF",
98
+ # data=pdf_bytes.getvalue(),
99
+ # file_name="highlighted_pdf.pdf",
100
+ # mime="application/pdf",
101
+ # )
102
 
103
+ # # Example usage
104
+ # file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
105
+ # keywords = ["Arjuna", "Krishna"]
106
+ # display_pdf_with_highlight(file_path, keywords)
107
+
108
+ # import pyperclip
109
+
110
+ # content = str(pyperclip.paste())
111
+
112
+ # import streamlit as st
113
+ # import fitz
114
+
115
+ # def annotate_pdf(file_path, text_to_highlight):
116
+ # # Open the PDF file
117
+ # with fitz.open(file_path) as doc:
118
+ # # Create a new PDF file to hold the annotated pages
119
+ # annotated_pdf = fitz.open()
120
+
121
+ # # Iterate over each page in the PDF
122
+ # for page_index in range(len(doc)):
123
+ # page = doc.load_page(page_index)
124
+
125
+ # # Search for the text to highlight
126
+ # areas = page.search_for(text_to_highlight)
127
+
128
+ # # Add rectangle annotations for the highlighted areas
129
+ # for area in areas:
130
+ # page.add_rect_annot(area)
131
+
132
+ # # Create a new PDF page and insert the annotated page
133
+ # new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
134
+ # new_page.show_pdf_page(page.rect, doc, page_index)
135
+
136
+ # # Create a BytesIO object to hold the annotated PDF data
137
+ # pdf_bytes = annotated_pdf.write()
138
+
139
+ # # Display the annotated PDF in Streamlit
140
+ # st.download_button(
141
+ # label="Download Annotated PDF",
142
+ # data=pdf_bytes,
143
+ # file_name="annotated_pdf.pdf",
144
+ # mime="application/pdf",
145
+ # )
146
+
147
+ # # Example usage
148
+ # file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
149
+ # text_to_highlight = "Arjuna"
150
+ # annotate_pdf(file_path, text_to_highlight)
151
+
152
+ # def displayPDF(file):
153
+ # # Opening file from file path
154
+ # with open(file, "rb") as f:
155
+ # base64_pdf = base64.b64encode(f.read()).decode('utf-8')
156
+
157
+ # # Embedding PDF in HTML
158
+ # pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
159
+
160
+ # # Displaying File
161
+ # st.markdown(pdf_display, unsafe_allow_html=True)
162
+
163
+ # displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")
164
+
165
+ # import streamlit as st
166
 
167
+ # import fitz
168
+
169
+ # def annotate_pdf(file_path, text_to_highlight):
170
+ # # Open the PDF file
171
+ # with fitz.open(file_path) as doc:
172
+ # # Create a new PDF file to hold the annotated pages
173
+ # annotated_pdf = fitz.open()
174
+
175
+ # # Iterate over each page in the PDF
176
+ # for page_index in range(len(doc)):
177
+ # page = doc.load_page(page_index)
178
+
179
+ # # Search for the text to highlight
180
+ # areas = page.search_for(text_to_highlight)
181
+
182
+ # # Add rectangle annotations for the highlighted areas
183
+ # for area in areas:
184
+ # page.add_rect_annot(area)
185
+
186
+ # # Create a new PDF page and insert the annotated page
187
+ # new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
188
+ # new_page.show_pdf_page(page.rect, doc, page_index)
189
+
190
+ # # Create a BytesIO object to hold the annotated PDF data
191
+ # pdf_bytes = annotated_pdf.write()
192
+
193
+ # streamlit_pdf_viewer.pdf_viewer(pdf_bytes)
194
+
195
+ # # Display the annotated PDF in Streamlit
196
+ # st.download_button(
197
+ # label="Download Annotated PDF",
198
+ # data=pdf_bytes,
199
+ # file_name="annotated_pdf.pdf",
200
+ # mime="application/pdf",
201
+ # )
202
+
203
+ # # Example usage
204
+ # file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
205
+ # text_to_highlight = "Krishna"
206
+ # annotate_pdf(file_path, text_to_highlight)
207
+
208
+
209
+ # import streamlit as st
210
+ # import fitz
211
+ # import io
212
+
213
+ # def annotate_pdf(uploaded_file, text_to_highlight):
214
+ # try:
215
+ # # Open the PDF file from the file-like object
216
+ # doc = fitz.open(stream=uploaded_file.read(), filetype="pdf")
217
 
218
+ # # Create a new PDF file to hold the annotated pages
219
+ # annotated_pdf = fitz.open()
220
 
221
+ # # Iterate over each page in the PDF
222
+ # for page_index in range(len(doc)):
223
+ # page = doc.load_page(page_index)
224
 
225
+ # # Search for the text to highlight
226
+ # areas = page.search_for(text_to_highlight)
227
 
228
+ # # Add rectangle annotations for the highlighted areas
229
+ # for area in areas:
230
+ # page.add_rect_annot(area)
 
 
231
 
232
+ # # Create a new PDF page and insert the annotated page
233
+ # new_page = annotated_pdf.new_page(-1, width=page.rect.width, height=page.rect.height)
234
+ # new_page.show_pdf_page(page.rect, doc, page_index)
235
 
236
+ # # Create a BytesIO object to hold the annotated PDF data
237
+ # pdf_bytes = io.BytesIO(annotated_pdf.write())
238
+
239
+ # # Display the annotated PDF in Streamlit
240
+ # st.download_button(
241
+ # label="Download Annotated PDF",
242
+ # data=pdf_bytes.getvalue(),
243
+ # file_name="annotated_pdf.pdf",
244
+ # mime="application/pdf",
245
+ # )
246
+ # except Exception as e:
247
+ # st.error(f"An error occurred: {str(e)}")
248
+
249
+ # # Streamlit app
250
+ # def main():
251
+ # st.title("PDF Annotation App")
252
+ # uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
253
+ # if uploaded_file is not None:
254
+ # text_to_highlight = st.text_input("Enter text to highlight")
255
+ # if text_to_highlight:
256
+ # annotate_pdf(uploaded_file, text_to_highlight)
257
+
258
+ # if __name__ == "__main__":
259
+ # main()
260
+
261
+
262
+ # file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
263
+ # text_to_highlight = "Krishna"
264
+ # annotate_pdf(file_path, text_to_highlight)
265
+
266
+ # import fitz
267
+
268
+ # import base64
269
+
270
+ # def displayPDF(file):
271
+
272
+ # # Open the PDF document
273
+ # doc = fitz.open("my_pdf.pdf")
274
+
275
+ # # Get the first page of the document
276
+ # page = doc.loadPage(4)
277
+
278
+ # # Search for the text string to highlight
279
+ # text_to_highlight = "Supreme Personality of Godhead"
280
+
281
+ # # Create a rectangle around the text to highlight
282
+ # highlight_rect = fitz.Rect(page.searchFor(text_to_highlight)[0])
283
+
284
+ # # Create a highlight annotation
285
+ # highlight_annot = fitz.Annot(page, highlight_rect, "Highlight", {"color": fitz.utils.getColor("yellow")})
286
+
287
+ # # Add the annotation to the page
288
+ # page.addAnnot(highlight_annot)
289
+
290
+ # # Save the document
291
+ # doc.save("my_pdf_highlighted.pdf")
292
+
293
+ # # Opening file from file path
294
+ # with open(file, "rb") as f:
295
+ # base64_pdf = base64.b64encode(f.read()).decode('utf-8')
296
+
297
+ # # Embedding PDF in HTML
298
+ # pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
299
+
300
+ # # Displaying File
301
+ # st.markdown(pdf_display, unsafe_allow_html=True)
302
+
303
+ # displayPDF("../Transformers/Bhagavad-Gita-As-It-Is.pdf")
304
+
305
+
306
+ import streamlit as st
307
+ # import fitz
308
+ import tempfile
309
+
310
+ # Import the PDF_ANNOT_HIGHLIGHT constant
311
+ # from fitz.PDF_ANNOT import PDF_ANNOT_HIGHLIGHT
312
+
313
+ import base64
314
+ import io
315
+
316
+
317
+ def display_highlighted_pdf(file_path, text_to_highlight):
318
+ # Open the PDF document
319
+ doc = fitz.open(file_path)
320
+
321
+ # Iterate over each page in the PDF
322
+ for page_index in range(len(doc)):
323
+ page = doc.load_page(page_index)
324
+
325
+ # Search for the text string to highlight
326
+ areas = page.search_for(text_to_highlight)
327
+
328
+ # Create a highlight annotation for each area
329
+ for area in areas:
330
+ highlight_rect = fitz.Rect(area)
331
+ highlight_annot = page.add_highlight_annot(highlight_rect) #fitz.Annot(page.parent, highlight_rect, annot_type=fitz.PDF_ANNOT_HIGHLIGHT)
332
+ highlight_annot.set_colors({"stroke": fitz.utils.getColor("yellow")})
333
+ highlight_annot.update()
334
+ # page.add_annot(highlight_annot)
335
+
336
+ # Create a BytesIO object to hold the highlighted PDF data
337
+ # Create a temporary file to save the PDF
338
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
339
+ temp_file_path = temp_file.name
340
+ doc.save(temp_file_path)
341
+
342
+ # Read the content of the temporary file into a BytesIO object
343
+ with open(temp_file_path, "rb") as f:
344
+ pdf_bytes = io.BytesIO(f.read())
345
+
346
+ # # Remove the temporary file
347
+ # st.unlink(temp_file_path)
348
+
349
+ # pdf_bytes = io.BytesIO()
350
+ # doc.write(pdf_bytes)
351
+ # pdf_bytes.seek(0)
352
+
353
+ # Encode the PDF data as base64
354
+ base64_pdf = base64.b64encode(pdf_bytes.getvalue()).decode('utf-8')
355
+
356
+ # Embed the PDF in an HTML iframe
357
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" height="1600" width="680" type="application/pdf"></iframe>'
358
+
359
+ # Display the PDF in Streamlit
360
+ st.markdown(pdf_display, unsafe_allow_html=True)
361
+
362
 
363
+ # Example usage
364
+ file_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
365
+ text_to_highlight = "Supreme Personality of Godhead"
366
+ # display_highlighted_pdf(file_path, text_to_highlight)
367
 
 
 
 
368
 
 
369
 
370
+ # import streamlit as st
371
 
372
+ # def display_pdf(pdf_path):
373
+ # # Read the PDF file
374
+ # with open(pdf_path, "rb") as file:
375
+ # pdf_bytes = file.read()
376
 
377
+ # # Encode the PDF data as base64
378
+ # base64_pdf = base64.b64encode(pdf_bytes).decode("utf-8")
379
 
380
+ # # Embed the PDF in an HTML iframe
381
+ # pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
382
 
383
+ # # Display the PDF in Streamlit
384
+ # st.markdown(pdf_display, unsafe_allow_html=True)
385
 
386
+ # # Example usage
387
+ # pdf_path = "../Transformers/Bhagavad-Gita-As-It-Is.pdf"
388
+ # display_pdf(pdf_path)
389
 
390
+ # import pymupdf
 
391
 
392
+ # # Open the PDF file
393
+ # doc = pymupdf.open(file_path)
394
 
395
+ # # Get the first page of the PDF
396
+ # # page = pdf_file.pa
 
 
397
 
398
+ # for page in doc: # iterate the document pages
399
+ # text_coordinates = page.search_for("Bhagavad", quads=True)
400
+ # # Highlight the text
401
+ # page.add_highlight_annot(text_coordinates)
402
 
403
+ # # Get the text of the page
404
+ # text = page.get_text()
405
+
406
+ # # Find the text to highlight
407
+ # text_to_highlight = "Bhagavad"
408
+
409
+ # # Get the coordinates of the text to highlight
410
+ # text_coordinates = page.search_for(text_to_highlight)
411
+
412
+ # Highlight the text
413
+ # page.add_highlight_annot(text_coordinates)
414
+
415
+ # Save the PDF file
416
+ # doc.save("example_highlighted.pdf")
417
+
418
+ import pymupdf
419
+ import tempfile
420
+ import nltk
421
+ from nltk.corpus import stopwords
422
+ from collections import Counter
423
 
424
  def highlight_pdf(file_path, text_to_highlight, page_numbers):
425
  # Create a temporary file to save the modified PDF
 
431
  # Open the original PDF
432
  doc = pymupdf.open(file_path)
433
 
434
+ pages_to_display = [doc.load_page(page_number - 1) for page_number in page_numbers]
 
 
 
435
 
436
  # Tokenize the text into words
437
  words = text_to_highlight.split()
438
 
439
+
440
+
441
  # Remove stopwords
442
  stop_words = set(stopwords.words("english"))
443
  words = [word for word in words if word.lower() not in stop_words]
 
476
 
477
  print(temp_pdf_path)
478
 
479
+ new_doc.save("example_highlighted.pdf")
480
 
481
  return temp_pdf_path
482
 
483
+ # Example usage
 
 
484
 
485
  # Function to display PDF in Streamlit
486
+ def display_highlighted_pdf():
487
+ pdf_path = "Bhagavad-Gita-As-It-Is.pdf"
488
+ sources = [7,8]
489
+ response_text = "I offer my respectful obeisances unto the lotus feet of my spiritual master and unto the feet of all Vaiñëavas. I offer my respectful"
490
 
491
+ pdf_path = highlight_pdf(file_path=file_path, text_to_highlight=response_text, page_numbers=sources)
492
 
493
  with open(pdf_path, "rb") as file:
494
  pdf_bytes = file.read()
495
  base64_pdf = base64.b64encode(pdf_bytes).decode("utf-8")
496
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
497
  st.markdown(pdf_display, unsafe_allow_html=True)
498
 
499
+
500
+ display_highlighted_pdf()
501
+
502
+
503
+ # import streamlit as st
504
+ # import streamlit.components.v1 as components
505
+ # path_to_html = "https://vedabase.io/en/library/bg/1/1/"
506
+
507
+ # # with open(path_to_html,'r') as f:
508
+ # # html_data = f.read()
509
+
510
+ # # # Show in webpage
511
+ # # st.header("Show an external HTML")
512
+ # # st.components.v1.html(html_data)
513
+
514
+ # try:
515
+ # with open(path_to_html, 'r') as f:
516
+ # # Your file processing code here
517
+ # html_data = f.read()
518
+ # st.header("Show an external HTML")
519
+ # st.components.v1.html(html_data)
520
+ # except FileNotFoundError:
521
+ # print("File not found. Please check the file path.")
522
+ # except Exception as e:
523
+ # print(f"An error occurred: {e}")
524
+
525
+
526
+ import streamlit as st
527
+ import requests
528
+ from bs4 import BeautifulSoup
529
+
530
+ def fetch_html(url):
531
+ # Fetch the webpage content
532
+ response = requests.get(url)
533
+ if response.status_code == 200:
534
+ return response.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
535
  else:
536
+ st.error(f"Failed to fetch webpage. Status code: {response.status_code}")
537
+
538
+ def scrape_data(html_content):
539
+ # Parse HTML content
540
+ soup = BeautifulSoup(html_content, "html.parser")
541
 
542
+ # Scrape data (replace this with your specific scraping logic)
543
+ data = soup.find_all(class_="container first-container")
 
 
544
 
545
+ return data
546
+
547
+ def main(url):
548
+ st.title("Webpage Scraper")
549
+
550
+ # User input for webpage URL
551
+ url = st.text_input("Enter the URL of the webpage:", value=url)
552
 
553
+ # Convert webpage to HTML and scrape data
554
+ if st.button("Scrape Data"):
555
+ if url:
556
+ html_content = fetch_html(url)
557
+ str_content = """ """
558
+ if html_content:
559
+ data = scrape_data(html_content)
560
+ # st.title("HTML Page Display")
561
+ # st.components.v1.html(html_content, height=1600, width=800)
562
+ # Display scraped data in a new tab
563
+ with st.expander("Scraped Data", expanded=True):
564
+ for item in data:
565
+ # Convert item to string and display
566
+ str_content += str(item)
567
+ # st.write(str(item))
568
+ # st.title("HTML Page Display")
569
+ # st.components.v1.html(data, height=1600, width=800)
570
+ # st.title("HTML Page Display")
571
+ st.components.v1.html(str_content, height=1600, width=680)
572
+ # main()
573
+
574
+
575
+ # import streamlit as st
576
+
577
+ # html_content = """
578
+ # <div class="container first-container"> <div class="row"> <div class="col-12 breadcrumb"> <a href="/en/library/">Library</a> »
579
+ # <a href="/en/library/bg/">Bhagavad-gītā As It Is</a> »
580
+
581
+ # <a href="/en/library/bg/1/">Chapter One</a>
582
+
583
+ # </div> </div> <div class="row" id="content" tabindex="-1"> <div class="col-12"> <div class="r r-title r-verse" id="bb181"> <h1>Bg. 1.1</h1> </div> <div class="wrapper-devanagari"> <h2 class="section-title none">Devanagari</h2> <div class="r r-devanagari" id="bb567886">धृतराष्ट्र उवाच<br/>धर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सव: ।<br/>मामका: पाण्डवाश्चैव किमकुर्वत सञ्जय ॥ १ ॥</div> </div> <div class="wrapper-verse-text"> <h2 class="section-title none">Text</h2> <div class="r r-lang-en r-verse-text" id="bb183"><em><em>dhṛtarāṣṭra uvāca</em><br/>dharma-kṣetre kuru-kṣetre<br/>samavetā yuyutsavaḥ<br/>māmakāḥ pāṇḍavāś caiva<br/>kim akurvata sañjaya</em></div> </div> <div class="wrapper-synonyms"> <h2 class="section-title">Synonyms</h2> <div class="r r-lang-en r-synonyms" id="bb184"><p><a href="/en/search/synonyms/?original=dhṛtarāṣṭraḥ"><em>dhṛtarāṣṭraḥ</em></a> <a href="/en/search/synonyms/?original=uvāca"><em>uvāca</em></a> — King Dhṛtarāṣṭra said; <a href="/en/search/synonyms/?original=dharma"><em>dharma</em></a>-<a href="/en/search/synonyms/?original=kṣetre"><em>kṣetre</em></a> — in the place of pilgrimage; <a href="/en/search/synonyms/?original=kuru"><em>kuru</em></a>-<a href="/en/search/synonyms/?original=kṣetre"><em>kṣetre</em></a> — in the place named Kurukṣetra; <a href="/en/search/synonyms/?original=samavetāḥ"><em>samavetāḥ</em></a> — assembled; <a href="/en/search/synonyms/?original=yuyutsavaḥ"><em>yuyutsavaḥ</em></a> — desiring to fight; <a href="/en/search/synonyms/?original=māmakāḥ"><em>māmakāḥ</em></a> — my party (sons); <a href="/en/search/synonyms/?original=pāṇḍavāḥ"><em>pāṇḍavāḥ</em></a> — the sons of Pāṇḍu; <a href="/en/search/synonyms/?original=ca"><em>ca</em></a> — and; <a href="/en/search/synonyms/?original=eva"><em>eva</em></a> — certainly; <a href="/en/search/synonyms/?original=kim"><em>kim</em></a> — what; <a href="/en/search/synonyms/?original=akurvata"><em>akurvata</em></a> — did they do; <a href="/en/search/synonyms/?original=sañjaya"><em>sañjaya</em></a> — O Sañjaya.</p></div> </div> <div class="wrapper-translation"> <h2 class="section-title">Translation</h2> <div class="r r-lang-en r-translation" id="bb185"><p><strong>Dhṛtarāṣṭra said: O Sañjaya, after my sons and the sons of Pāṇḍu assembled in the place of pilgrimage at Kurukṣetra, desiring to fight, what did they do?</strong></p></div> </div> <div class="wrapper-puport"> <h2 class="section-title">Purport</h2> <div class="r r-lang-en r-paragraph" id="bb186"><p><em><a href="/en/library/bg/">Bhagavad-gītā</a></em> is the widely read theistic science summarized in the <em>Gītā-māhātmya</em> (<em>Glorification of the Gītā</em>). There it says that one should read <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> very scrutinizingly with the help of a person who is a devotee of Śrī Kṛṣṇa and try to understand it without personally motivated interpretations. The example of clear understanding is there in the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> itself, in the way the teaching is understood by Arjuna, who heard the <em>Gītā</em> directly from the Lord. If someone is fortunate enough to understand the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> in that line of disciplic succession, without motivated interpretation, then he surpasses all studies of Vedic wisdom, and all scriptures of the world. One will find in the <em><a href="/en/library/bg/">Bhagavad-gītā</a></em> all that is contained in other scriptures, but the reader will also find things which are not to be found elsewhere. That is the specific standard of the <em>Gītā.</em> It is the perfect theistic science because it is directly spoken by the Supreme Personality of Godhead, Lord Śrī Kṛṣṇa.</p></div> <div class="r r-lang-en r-paragraph" id="bb187"><p>The topics discussed by Dhṛtarāṣṭra and Sañjaya, as described in the <em>Mahābhārata,</em> form the basic principle for this great philosophy. It is understood that this philosophy evolved on the Battlefield of Kurukṣetra, which is a sacred place of pilgrimage from the immemorial time of the Vedic age. It was spoken by the Lord when He was present personally on this planet for the guidance of mankind.</p></div> <div class="r r-lang-en r-paragraph" id="bb188"><p>The word <em>dharma-kṣetra</em> (a place where religious rituals are performed) is significant because, on the Battlefield of Kurukṣetra, the Supreme Personality of Godhead was present on the side of Arjuna. Dhṛtarāṣṭra, the father of the Kurus, was highly doubtful about the possibility of his sons’ ultimate victory. In his doubt, he inquired from his secretary Sañjaya, “What did they do?” He was confident that both his sons and the sons of his younger brother Pāṇḍu were assembled in that Field of Kurukṣetra for a determined engagement of the war. Still, his inquiry is significant. He did not want a compromise between the cousins and brothers, and he wanted to be sure of the fate of his sons on the battlefield. Because the battle was arranged to be fought at Kurukṣetra, which is mentioned elsewhere in the <em>Vedas</em> as a place of worship – even for the denizens of heaven – Dhṛtarāṣṭra became very fearful about the influence of the holy place on the outcome of the battle. He knew very well that this would influence Arjuna and the sons of Pāṇḍu favorably, because by nature they were all virtuous. Sañjaya was a student of Vyāsa, and therefore, by the mercy of Vyāsa, Sañjaya was able to envision the Battlefield of Kurukṣetra even while he was in the room of Dhṛtarāṣṭra. And so, Dhṛtarāṣṭra asked him about the situation on the battlefield.</p></div> <div class="r r-lang-en r-paragraph" id="bb189"><p>Both the Pāṇḍavas and the sons of Dhṛtarāṣṭra belong to the same family, but Dhṛtarāṣṭra’s mind is disclosed herein. He deliberately claimed only his sons as Kurus, and he separated the sons of Pāṇḍu from the family heritage. One can thus understand the specific position of Dhṛtarāṣṭra in his relationship with his nephews, the sons of Pāṇḍu. As in the paddy field the unnecessary plants are taken out, so it is expected from the very beginning of these topics that in the religious field of Kurukṣetra, where the father of religion, Śrī Kṛṣṇa, was present, the unwanted plants like Dhṛtarāṣṭra’s son Duryodhana and others would be wiped out and the thoroughly religious persons, headed by Yudhiṣṭhira, would be established by the Lord. This is the significance of the words <em>dharma-kṣetre</em> and <em>kuru-kṣetre,</em> apart from their historical and Vedic importance.</p></div> </div> </div> </div> <div class="row d-print-none"> <div class="col-12"> <ul class="mini-pager mt-2 pb-4"> <li class="pager-prev"><a class="btn" href="/en/library/bg/1/"> <i class="fa fa-chevron-left"></i>
584
+ # Previous
585
+
586
+ # </a></li>
587
+
588
+ # <li class="pager-next"><a class="btn" href="/en/library/bg/1/2/">
589
+ # Next
590
+
591
+ # <i class="fa fa-chevron-right"></i>
592
+
593
+ # </a></li>
594
+
595
+ # </ul> </div> </div> <nav class="rich-menu" id="para-menu"> <div class="Panel" data-csrf-token="l7dMxBge1IaZDbFchwWzWmh1CBpo6pWDY9LKjwSlqmvpDKld3RfTLY85AWyycbUS" data-language="en" data-propose-category-url="/categorization/suggest-category/" data-reload-url="/en/paragraph-tool/?page_id=14054&amp;view_slug=index_view&amp;view_args=&amp;next=/en/library/bg/1/1/&amp;page_url=/en/library/bg/1/1/" id="panel"> <h1 class="pt-title">Paragraph Tools <a class="pt-close-button" href="#" onclick="event.preventDefault();_menu.close();return false;">Close <i class="fa fa-times"></i></a></h1> Please select paragraph first. </div> <div class="Panel" id="subpanel"> <p>Suggest this as category.</p> <a class="link-yes btn btn-lg btn-success px-3" href="">Yes</a> <a class="link-cancel btn btn-lg btn-success px-3" href="">Cancel</a> </div> </nav> <div class="paragraph-tool-button no-touch none"> <div class="paragraph-tool-button-nav"> <div class="paragraph-tool-button-trigger" data-target="#para-menu" id="para-menu-button"> <i class="icon fa fa-briefcase fa-2x"></i> <span class="fa-stack fa"> <i class="fa fa-circle fa-stack-1x p-counter"></i> <span class="p-counter fa-stack-1x fa-stack-text file-text fa-inverse"></span> </span> </div> </div> </div> </div>
596
+ # """
597
+
598
+ # st.title("HTML Page Display")
599
+ # st.components.v1.html(html_content, height=1600, width=800)
600
+
601
+
602
+
603
+ import requests
604
+ from bs4 import BeautifulSoup
605
+
606
+ # URL of the webpage
607
+ baseurl = "https://vedabase.io/en/library/bg/"
608
+
609
+ # Fetch the webpage content
610
+ response = requests.get(baseurl)
611
+ if response.status_code == 200:
612
+ html_content = response.content
613
 
614
+ # Parse HTML content
615
+ soup = BeautifulSoup(html_content, "html.parser")
616
+
617
+ # Find all direct child div elements with class="r-chapter"
618
+ direct_child_div_elements = soup.select("div.col-12 > div.r-chapter")
619
+
620
+ # List to store the extracted text
621
+ output = []
622
+
623
+ # Iterate over each direct child div element
624
+ for div in direct_child_div_elements:
625
+ # Find the <a> tag within the div
626
+ a_tag = div.find("a")
627
+ if a_tag:
628
+ # Extract the text from the <a> tag and append it to the output list
629
+ output.append(a_tag.text.strip())
630
+
631
+ # Print the output list
632
+ # print(output)
633
+ # print(len(output))
634
+
635
+ ### Link to all chapters
636
+ import re
637
+
638
+ # Sample text
639
+ chapter = output[4]
640
+
641
+ text_to_number = {
642
+ "One": "1",
643
+ "Two": "2",
644
+ "Three": "3",
645
+ "Four": "4",
646
+ "Five": "5",
647
+ "Six": "6",
648
+ "Seven": "7",
649
+ "Eight": "8",
650
+ "Nine": "9",
651
+ "Ten": "10",
652
+ # Add more numbers if needed
653
+ }
654
+
655
+ # Split the text by spaces
656
+ words = chapter.split()
657
+
658
+ # Find the text representing the number
659
+ number_text = words[1].strip(":") # Assuming the number text is the second word
660
+
661
+ # Extract the numeric part
662
+ chapter_number = text_to_number[number_text]
663
+
664
+ # Print the chapter number
665
+ # print(chapter_number)
666
+
667
+ url = baseurl + chapter_number
668
+
669
+ # print(url)
670
+
671
+ ### all Texts in each chapter
672
+
673
+ response = requests.get(url)
674
+ if response.status_code == 200:
675
+ html_content = response.content
676
+
677
+ # Parse HTML content
678
+ text = BeautifulSoup(html_content, "html.parser")
679
+
680
+ # print(text)
681
+
682
+ # Find all direct child div elements with class="r-chapter"
683
+ direct_child_div_elements = text.select("div.col-12 > dl.r.r-verse")
684
+
685
+ # print(direct_child_div_elements)
686
+
687
+ # List to store the extracted text
688
+ text_number = []
689
+
690
+ # Iterate over each direct child div element
691
+ for div in direct_child_div_elements:
692
+ # Find the <a> tag within the div
693
+ a_tag = div.find("a")
694
+ if a_tag:
695
+ # Extract the text from the <a> tag and append it to the output list
696
+ text_number.append(a_tag.text.strip())
697
+
698
+ # Print the output list
699
+ # print(text_number)
700
+ # print(len(text_number))
701
+
702
+ ### link to each Text in each chapter
703
+ text_page = text_number[0]
704
+
705
+ # Split the text by spaces
706
+ words = text_page.split()
707
+
708
+ # Find the text representing the number
709
+ text_number = words[1].strip(":") # Assuming the number text is the second word
710
+
711
+ # print(f"chapter_number - {chapter_number} : text_number - {text_number}")
712
+ texturl = baseurl + chapter_number + "/" + text_number
713
+
714
+ # print(texturl)
715
+
716
+ main(url=texturl)
717
+
718
+ st.title("Display HTML File in Streamlit")
719
+
720
+ # Path to the HTML file
721
+ html_file_path = "../Transformers/Bg. 1.1.html"
722
+
723
+ try:
724
+ # Read the HTML file
725
+ with open(html_file_path, "r", encoding="utf-8") as file:
726
+ html_content = file.read()
727
 
728
+ # Display the HTML content using the 'st.components.v1.html' component
729
+ expanded = st.checkbox("Expand HTML page")
730
+ if expanded:
731
+ st.components.v1.html(html_content, height=1600, width=680)
732
+ else:
733
+ st.components.v1.html(html_content, height=600)
734
+ except FileNotFoundError:
735
+ st.error(f"HTML file '{html_file_path}' not found!")
736
+
737
+ import streamlit as st
738
+
739
 
740
+ st.title("Streamlit Tabs Example")
 
741
 
742
+ # Add tabs to the sidebar
743
+ tabs = st.sidebar.radio("Navigation", ["Home", "About", "Settings"])
744
 
745
+ # Display different content based on the selected tab
746
+ if tabs == "Home":
747
+ st.header("Home Page")
748
+ st.write("Welcome to the Home page!")
749
 
750
+ elif tabs == "About":
751
+ st.header("About Page")
752
+ st.write("This is the About page.")
753
 
754
+ elif tabs == "Settings":
755
+ st.header("Settings Page")
756
+ st.write("Here you can configure your settings.")
757
 
758
+ st.title("Netflix-like Grid Display")
759
+
760
+
761
+
762
+ import streamlit as st
763
+ import os
764
+ import streamlit.components.v1 as components
765
+
766
+ # Define movie data
767
+ movies = [
768
+ {"title": "Movie 1", "poster_path": "../Transformers/Bg. 1.1.html"},
769
+ {"title": "Movie 2", "poster_url": "https://via.placeholder.com/150"},
770
+ {"title": "Movie 3", "poster_url": "https://via.placeholder.com/150"},
771
+ {"title": "Movie 4", "poster_url": "https://via.placeholder.com/150"},
772
+ {"title": "Movie 5", "poster_url": "https://via.placeholder.com/150"},
773
+ {"title": "Movie 6", "poster_url": "https://via.placeholder.com/150"},
774
+ {"title": "Movie 7", "poster_url": "https://via.placeholder.com/150"},
775
+ {"title": "Movie 8", "poster_url": "https://via.placeholder.com/150"},
776
+ ]
777
+
778
+ # Display movies in a grid
779
+ # num_columns = 4
780
+ # col_count = 0
781
+ # cols = st.columns(num_columns)
782
+ # expanded = st.checkbox("Expand HTML page")
783
+ # for movie in movies:
784
+ # with cols[col_count % num_columns]:
785
+ # st.markdown(f"<h2>{movie['title']}</h2>", unsafe_allow_html=True)
786
+ # st.write(f"Placeholder for {movie['title']}")
787
+ # if 'poster_path' in movie:
788
+ # # Convert local file path to URL
789
+ # poster_url = f"file://{os.path.abspath(movie['poster_path'])}"
790
+ # print(poster_url)
791
+ # # Display the HTML page using IFrame
792
+ # if expanded:
793
+ # components.iframe(poster_url, width=800, height=600)
794
+ # else:
795
+ # components.iframe(poster_url,width=200)
796
+ # else:
797
+ # # Display placeholder image
798
+ # st.image(movie["poster_url"], width=200)
799
+ # col_count += 1
800
 
801