Spaces:
Build error
Build error
Upload 3 files
Browse files- app.py +143 -0
- pdf_files/bhagat_singh_upsc_notes_60.pdf +99 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import random, string
|
| 3 |
+
import os
|
| 4 |
+
import os.path
|
| 5 |
+
from os import listdir
|
| 6 |
+
from os.path import isfile, join
|
| 7 |
+
import requests
|
| 8 |
+
import PyPDF2
|
| 9 |
+
import pandas as pd
|
| 10 |
+
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 13 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 14 |
+
from groq import Groq
|
| 15 |
+
from sentence_transformers import SentenceTransformer
|
| 16 |
+
|
| 17 |
+
# Load the environment variables from the .env file
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
# Access the variables using os.getenv
|
| 21 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 22 |
+
PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
|
| 23 |
+
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
|
| 24 |
+
|
| 25 |
+
# Initialize Groq client
|
| 26 |
+
client = Groq(api_key = GROQ_API_KEY)
|
| 27 |
+
|
| 28 |
+
# Initialize Pinecone
|
| 29 |
+
pc = Pinecone(api_key = PINECONE_API_KEY)
|
| 30 |
+
|
| 31 |
+
# Create or connect to an existing index
|
| 32 |
+
index = pc.Index("sample1")
|
| 33 |
+
|
| 34 |
+
if 'upload_state' not in st.session_state:
|
| 35 |
+
st.session_state.upload_state = ''
|
| 36 |
+
|
| 37 |
+
if 'chat_list' not in st.session_state:
|
| 38 |
+
st.session_state.chat_list = []
|
| 39 |
+
|
| 40 |
+
em_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 41 |
+
|
| 42 |
+
def get_query_embdedding(embed):
|
| 43 |
+
query_embedding = em_model.encode([embed]).tolist()
|
| 44 |
+
return query_embedding
|
| 45 |
+
|
| 46 |
+
st.title('Create Summary From Pdf File')
|
| 47 |
+
|
| 48 |
+
uploaded_files = st.file_uploader("Choose a PDF file", accept_multiple_files=True, type=['pdf'])
|
| 49 |
+
# if uploaded_file is not None:
|
| 50 |
+
# bytes_data = uploaded_file.getvalue()
|
| 51 |
+
# data = uploaded_file.getvalue().decode('utf-8', 'ignore').splitlines()
|
| 52 |
+
# st.session_state["preview"] = ''
|
| 53 |
+
# for i in range(0, min(5, len(data))):
|
| 54 |
+
# st.session_state["preview"] += data[i]
|
| 55 |
+
# preview = st.text_area("PDF Preview", "", height=150, key="preview")
|
| 56 |
+
# upload_state = st.text_area("Upload State", "", key="upload_state")
|
| 57 |
+
|
| 58 |
+
pdf_text = ''
|
| 59 |
+
ns = "ns_"+''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(7))
|
| 60 |
+
|
| 61 |
+
def upload(ns):
|
| 62 |
+
for uploaded_file in uploaded_files:
|
| 63 |
+
if uploaded_file is None:
|
| 64 |
+
st.session_state.upload_state = "Upload a file first!"
|
| 65 |
+
else:
|
| 66 |
+
pdf = PyPDF2.PdfReader(uploaded_file)
|
| 67 |
+
pdf_text = ""
|
| 68 |
+
for page in pdf.pages:
|
| 69 |
+
pdf_text += page.extract_text()
|
| 70 |
+
st.session_state.upload_state = "Saved successfully!"
|
| 71 |
+
|
| 72 |
+
if pdf_text :
|
| 73 |
+
# Split the text into chunks
|
| 74 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 75 |
+
texts = text_splitter.split_text(pdf_text)
|
| 76 |
+
|
| 77 |
+
# # Embedding the chunks
|
| 78 |
+
r1 = em_model.encode(texts)
|
| 79 |
+
|
| 80 |
+
# # Upsert the embeddings into the index
|
| 81 |
+
for i in range(len(texts)):
|
| 82 |
+
index.upsert([((str(i), r1[i], {"text": texts[i]}))], ns)
|
| 83 |
+
|
| 84 |
+
st.write(st.session_state.upload_state)
|
| 85 |
+
|
| 86 |
+
st.button("Upload file to Process", on_click=upload(ns), key="process_but")
|
| 87 |
+
# if st.button("Upload file to Process"):
|
| 88 |
+
# upload(ns)
|
| 89 |
+
|
| 90 |
+
# Read the PDF file
|
| 91 |
+
# pdf = PyPDF2.PdfReader("pdf_files/gandhi.pdf")
|
| 92 |
+
# pdf_text = ""
|
| 93 |
+
# for page in pdf.pages:
|
| 94 |
+
# pdf_text += page.extract_text()
|
| 95 |
+
|
| 96 |
+
# Define the query
|
| 97 |
+
query = st.chat_input("Enter Your Summarize Query?")
|
| 98 |
+
# query = "Who is Bhagat singh?"
|
| 99 |
+
|
| 100 |
+
docs = ''
|
| 101 |
+
|
| 102 |
+
if query:
|
| 103 |
+
# Get the query embedding
|
| 104 |
+
question_embedding = get_query_embdedding(query)
|
| 105 |
+
|
| 106 |
+
# Query the Pinecone index
|
| 107 |
+
query_result = index.query(namespace = ns, vector=question_embedding, top_k=5, include_metadata=True)
|
| 108 |
+
|
| 109 |
+
# Extract metadata from query result
|
| 110 |
+
docs = {x["metadata"]['text']: i for i, x in enumerate(query_result["matches"])}
|
| 111 |
+
# print (docs)
|
| 112 |
+
|
| 113 |
+
# Create a template for the summary
|
| 114 |
+
Template = f"Based on the following context: {docs} generate a precise summary related to the question: {query}"
|
| 115 |
+
# print(Template)
|
| 116 |
+
|
| 117 |
+
# Generate the summary
|
| 118 |
+
chat_completion = client.chat.completions.create(
|
| 119 |
+
messages=[
|
| 120 |
+
{
|
| 121 |
+
"role": "user",
|
| 122 |
+
"content": Template,
|
| 123 |
+
}
|
| 124 |
+
],
|
| 125 |
+
model="llama3-70b-8192",
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Print the summary
|
| 130 |
+
response = chat_completion.choices[0].message.content
|
| 131 |
+
# print(response)
|
| 132 |
+
|
| 133 |
+
result = {"ques":query, "ans":response}
|
| 134 |
+
st.session_state.chat_list.append(result)
|
| 135 |
+
|
| 136 |
+
for c_list in st.session_state.chat_list:
|
| 137 |
+
with st.chat_message("user"):
|
| 138 |
+
st.write(c_list["ques"])
|
| 139 |
+
with st.chat_message("machine"):
|
| 140 |
+
st.write(c_list["ans"])
|
| 141 |
+
# if docs:
|
| 142 |
+
# with st.chat_message("chatbot"):
|
| 143 |
+
# st.write(docs)
|
pdf_files/bhagat_singh_upsc_notes_60.pdf
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Bhagat Singh
|
| 2 |
+
Bhagat Singh was bor n on 27th September 1907 in a Sikh family i n Lyallpur, Punjab. He
|
| 3 |
+
attended the D AV H igh School, operated by A rya Samaj, followed by N ational C ollege in
|
| 4 |
+
Lahore. His strong sense of patriotism & dev otion to his c ountry was inspired by hi s own family
|
| 5 |
+
at a very young age.
|
| 6 |
+
The family m embers of Bhagat Singh were als o associat ed with the freedom m ovement and
|
| 7 |
+
therefore he was natur ally inclined toward this m ore significant purpos e. His unc le and his father
|
| 8 |
+
both were active members of Indian pol itics at the time participating in various revolutionary
|
| 9 |
+
movements .
|
| 10 |
+
•In his initial days, he participated in the Non-Cooperation Movement and suppor ted
|
| 11 |
+
Mahatama Gandhi and marked his role in freedom struggle.
|
| 12 |
+
•He displayed his hatred towards the Britishers as a child, by burning the books
|
| 13 |
+
recommended by them .
|
| 14 |
+
•Later on, when Mahatma Gandhi took back the Non-Cooperation Movement , Bhaga t
|
| 15 |
+
Singh shifted towards revolutionary nationalism .
|
| 16 |
+
•He was also seriously affected by the incident in Jallianwala Bagh in 1919.
|
| 17 |
+
Subsequently, he became a member of the Hindustan Republican Association in 1924.
|
| 18 |
+
•Bhagat Singh laid the foundation of Naujawan Bharat Sabha in the year 1926 wi th the
|
| 19 |
+
intention of encouraging more and more revolutionary activities against the Britishers by
|
| 20 |
+
involving people from the peasants and working class .
|
| 21 |
+
•After two years of forming the Naujawan Bharat Sabha, Bhagat Singh formed the
|
| 22 |
+
Hindustan Socialist Republican Association (HSRA) with Chandrashekhar Aza d,
|
| 23 |
+
Sukhdev, and others.
|
| 24 |
+
•This association came to an end with the unsudden death of Chandrashekhar Azad in
|
| 25 |
+
1930 when he was shot .
|
| 26 |
+
Bhagat Singh Contribution in Freedom Struggl e
|
| 27 |
+
There were several famous freedom fighters who fought hard and laid down their lives for India’s independence. Bhagat Singh was one such braveheart who didn’t shy away from carrying out many revolutionary activities that shook the British government to the core. Let us have a look at some of the famous revolutions of India’s freedom struggle in which Bhagat Singh participated.
|
| 28 |
+
|
| 29 |
+
Contribution of Bhagat Singh in Freedom Struggle
|
| 30 |
+
•Bhagat Singh was an ardent reader of the leftist writings and did not favor capitalism .
|
| 31 |
+
•He supposedly started by writing critical articles to show opposition to the Britis h
|
| 32 |
+
government. He printed these articles and distributed them everywhere to encourage
|
| 33 |
+
people to overthrow the government.• Bhagat Singh’s stint with the revolutionary movements started with establishing the
|
| 34 |
+
Hindustan Socialist Republican Association. This is when he was introduced to the
|
| 35 |
+
significance of armed revolution along with the use of explosives .
|
| 36 |
+
• He was arrested in the Kakori Cas e, for the first time on the accusation of writing a
|
| 37 |
+
provocative article under the pseudonym Vidrohi which means rebel.
|
| 38 |
+
• Another accusation against Bhagat Singh was that of being involved in a bomb
|
| 39 |
+
explosion during Dussehra in Lahore.
|
| 40 |
+
|
| 41 |
+
Revolutionary Activities of Bhagat Singh
|
| 42 |
+
|
| 43 |
+
As discussed above, Bhagat Singh took part in the Indian freedom movement on a large scale.
|
| 44 |
+
He was part of various revolutionary movements that made him a popular figure in Indian history. There were many such movements that can never be forgotten and have found a special place in the Indian history of independence.
|
| 45 |
+
|
| 46 |
+
Lahore Conspiracy Cas e
|
| 47 |
+
The infamous Lahore Conspiracy Case has the name of Bhagat Singh attached to it. In 1928, while carrying out a protest agitation against Simon Commission, Lala Lajpat Rai sustained a few injuries and eventually succumbed to death. It happened during a lathi charge that SP James Scott ordered.
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
• Infuriated by this incident, Bhagat Singh decided to take revenge and, due to some
|
| 52 |
+
misunderstanding, killed another official J P Saunders.
|
| 53 |
+
• This whole incident was termed the Lahore Conspiracy Case .
|
| 54 |
+
• After this incident, Bhagat Singh had to flee from Lahore and hide his appearance to avoid arrest.
|
| 55 |
+
Central Assembly Bombing Cas e
|
| 56 |
+
Another incident in which Bhagat Singh was involved was the bombing that took place in the Central Legislative Assembly, on 8th April 1929 in Delhi. He was accompanied by another revolutionary at the time, Batukeshwar Dutt.
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
• The bomb was thrown by Bhagat Singh and his accomplice from the ‘Visitor’s Gallery’ .
|
| 61 |
+
• Pamphlets were also thrown around by them and slogans of ‘Inquilab Zindabad’ were
|
| 62 |
+
also raised .
|
| 63 |
+
• Both Bhagat Singh & Batukeshwar Dutt did not oppose arrest as they were in need of a
|
| 64 |
+
platform to spread their message.
|
| 65 |
+
• No one was hurt during this incident, as was the intention of Bhagat Singh.
|
| 66 |
+
• They gave a statement that by this explosion, they wanted ‘to make the deaf hear’ .
|
| 67 |
+
Death of Bhagat Singh
|
| 68 |
+
With respect to the JP Saunders Case, Bhagat Singh was charged with murder accusations,
|
| 69 |
+
along with other freedom fighters. The trial for this case began in July 1929, and all of the freedom fighters were imprisoned in the Lahore Central jail.
|
| 70 |
+
|
| 71 |
+
• All the prisoners, including Bhagat Singh, protested against the ill -treatment that they
|
| 72 |
+
received & demanded better treatment being political pri soners .
|
| 73 |
+
• Many senior leaders, such as Jawaharlal Nehru, came to see them & expressed their
|
| 74 |
+
hurt.
|
| 75 |
+
• Subsequently, Bhagat Singh went on a fast for 116 days but eventually had to end it
|
| 76 |
+
when his father and other Congress leaders requested him .
|
| 77 |
+
• Due to a partial trial, Bhagat Singh, Rajguru & Sukhdev were all given death sentences .
|
| 78 |
+
• There were many protests from other Indian leaders demanding a reduced sentence but
|
| 79 |
+
they failed.
|
| 80 |
+
• An order to hang all three of them was released. The execution was to happen on 23rd
|
| 81 |
+
March 1931, but it was carried out a day before.
|
| 82 |
+
• Bhagat Singh, while getting hanged, chanted his slogan of ‘Down with British Imperialism .
|
| 83 |
+
|
| 84 |
+
As the date of the sentence was decided as 23rd March, this day is now celebrated as ‘Martyrs’ Day’ / Shaheed Diwas / Sarvodaya Day to commemorate the sacrifices of Bhagat Singh, Rajguru & Sukhdev. Bhagat Singh was famously given the nickname Shaheed Bhagat Singh.
|
| 85 |
+
|
| 86 |
+
Bhagat Singh Quotes on Yout h
|
| 87 |
+
|
| 88 |
+
As a fearless freedom fighter, Bhagat Singh is called Shaheed Bhagat Singh. He laid down his
|
| 89 |
+
life for the nation without any regrets. He was one of the bravest revolutionaries that India has seen so far. His few quotes have earned much popularity instilling a sense of patriotism in the
|
| 90 |
+
youth.
|
| 91 |
+
|
| 92 |
+
• “I am such a lunatic that I am free even in jail”.
|
| 93 |
+
• “They may kill me, but they cannot kill my ideas. They can crush my body but they will not be able to crush my spirit” .
|
| 94 |
+
• “I am full of ambition, hope, and charm of life. But I can renounce everything at the time
|
| 95 |
+
of need” .
|
| 96 |
+
• “If the deaf has to hear, the sound has to be very loud” .
|
| 97 |
+
• “Bombs and pistols don’t make a revolution. The sword of revolution is sharpened on the
|
| 98 |
+
whetting stones of ideas” .
|
| 99 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
dotenv
|
| 3 |
+
groq
|
| 4 |
+
langchain
|
| 5 |
+
pinecone-client
|
| 6 |
+
PyPDF2
|
| 7 |
+
sentence_transformers
|