Spaces:
Paused
Paused
avinash
commited on
Commit
·
6d4e4e8
1
Parent(s):
850a0e0
added intial files
Browse files- app.py +89 -0
- doc.txt +210 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import PyPDF2
|
| 5 |
+
import faiss
|
| 6 |
+
import numpy as np
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
def extract_text_from_pdf(pdf_path):
|
| 10 |
+
text = ""
|
| 11 |
+
if pdf_path.endswith('.txt'):
|
| 12 |
+
with open(pdf_path, 'r', encoding='utf-8') as f:
|
| 13 |
+
text = f.read()
|
| 14 |
+
else:
|
| 15 |
+
with open(pdf_path, 'rb') as f:
|
| 16 |
+
reader = PyPDF2.PdfReader(f)
|
| 17 |
+
for page in reader.pages:
|
| 18 |
+
text += page.extract_text() or ""
|
| 19 |
+
return text
|
| 20 |
+
|
| 21 |
+
def split_text(text, chunk_size=512, overlap=64):
|
| 22 |
+
words = text.split()
|
| 23 |
+
chunks = []
|
| 24 |
+
for i in range(0, len(words), chunk_size - overlap):
|
| 25 |
+
chunk = " ".join(words[i:i+chunk_size])
|
| 26 |
+
chunks.append(chunk)
|
| 27 |
+
return chunks
|
| 28 |
+
|
| 29 |
+
def build_faiss_index(embedding_model, chunks):
|
| 30 |
+
embeddings = embedding_model.encode(chunks)
|
| 31 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 32 |
+
index.add(np.array(embeddings))
|
| 33 |
+
return index, embeddings
|
| 34 |
+
|
| 35 |
+
def get_top_k_chunks(query, chunks, embedding_model, index, k=5):
|
| 36 |
+
query_vec = embedding_model.encode([query])
|
| 37 |
+
D, I = index.search(np.array(query_vec), k)
|
| 38 |
+
return [chunks[i] for i in I[0]]
|
| 39 |
+
|
| 40 |
+
def setup_models():
|
| 41 |
+
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 43 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 44 |
+
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
| 45 |
+
return tokenizer, model, embedding_model
|
| 46 |
+
|
| 47 |
+
def generate_response(tokenizer, model, context_chunks, query):
|
| 48 |
+
context = "\n".join(context_chunks)
|
| 49 |
+
prompt = f"""<|system|>
|
| 50 |
+
You are a helpful assistant. Use the context below to answer the user's question.
|
| 51 |
+
|
| 52 |
+
CONTEXT:
|
| 53 |
+
{context}
|
| 54 |
+
|
| 55 |
+
<|user|>
|
| 56 |
+
{query}
|
| 57 |
+
|
| 58 |
+
<|assistant|>"""
|
| 59 |
+
|
| 60 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
|
| 61 |
+
|
| 62 |
+
with torch.no_grad():
|
| 63 |
+
outputs = model.generate(
|
| 64 |
+
inputs.input_ids,
|
| 65 |
+
max_length=2048,
|
| 66 |
+
temperature=0.7,
|
| 67 |
+
do_sample=True,
|
| 68 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 72 |
+
return response.strip()
|
| 73 |
+
|
| 74 |
+
# =====================
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
pdf_path = "./doc.txt" # Or .pdf
|
| 77 |
+
query = "What is the main topic of the document?"
|
| 78 |
+
|
| 79 |
+
# Setup
|
| 80 |
+
text = extract_text_from_pdf(pdf_path)
|
| 81 |
+
chunks = split_text(text)
|
| 82 |
+
tokenizer, model, embedding_model = setup_models()
|
| 83 |
+
index, _ = build_faiss_index(embedding_model, chunks)
|
| 84 |
+
|
| 85 |
+
# Retrieval + Generation
|
| 86 |
+
top_chunks = get_top_k_chunks(query, chunks, embedding_model, index)
|
| 87 |
+
response = generate_response(tokenizer, model, top_chunks, query)
|
| 88 |
+
|
| 89 |
+
print("Response:\n", response)
|
doc.txt
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
+
|
| 10 |
+
|
| 11 |
+
A Photograph by Shirley Toulson, part of the Class ll English syllabus, captures the
|
| 12 |
+
essence of time, loss, and nostalgia. The poem revolves around the poet's reflections
|
| 13 |
+
on her late mother through a cherished childhood photograph. The summary of the
|
| 14 |
+
poem A Photograph highlights how memories can immortalise moments of joy
|
| 15 |
+
while simultaneously evoking sorrow as time progresses. Toulson contrasts the
|
| 16 |
+
innocence of the past with the inevitability of loss, creating a moving narrative that
|
| 17 |
+
emphasises the transient nature of life and the enduring power of memories.
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
About the Author
|
| 21 |
+
Shirley Toulson is a poet who lives in the United States. She was born in Henley—on-
|
| 22 |
+
Thames, United Kingdom, on 20 lvlay 1924 and had studied ELA Literature from
|
| 23 |
+
Brockenhurst College's Literature in London. Her famous works are The Drovers, A
|
| 24 |
+
Celebration of Celtic Christian Saints, Sites, and Festivals lvldre
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
Central Idea of the Poem
|
| 28 |
+
Shirley Touisan's poem ’A Photograph’ is a loving tribute to her mother. The poem
|
| 29 |
+
reflects the passage of time and its three stages. In the first stage, the photograph
|
| 30 |
+
shows his mother enjoying a holiday on a beach along with her two girl cousins. She
|
| 31 |
+
was 12 at the time. The second stage transports us to twenty or thirty years later. This
|
| 32 |
+
stage shows the mother laughing at her picture and the way she and her cousins
|
| 33 |
+
were dressed in the picture at a beach. in the third stage, the poet sadly remembers
|
| 34 |
+
the dead mother with his broken heart. The photograph revives nostalgic waves in
|
| 35 |
+
the poet.
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Summary of the Poem - A Photograph
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
- The cardboard shows me how it was
|
| 46 |
+
|
| 47 |
+
- When the two girl cousins went paddling,
|
| 48 |
+
|
| 49 |
+
- Each one holding one of my mother's hands,
|
| 50 |
+
|
| 51 |
+
- And the big girl — some twelve years or so."
|
| 52 |
+
A tdttered picture of the poet’s mother and her cousins on a beach wds pasted on a
|
| 53 |
+
piece of cardboard. The photo reflected the hdppy memories of his mother's
|
| 54 |
+
childhood vacdtion, where she was along with her younger cousins. Holding his
|
| 55 |
+
mother’s hands the two younger cousins walked on the shdllow water of the sea.
|
| 56 |
+
They walked barefoot enjoying the water. The poet's mother was twelve years old in
|
| 57 |
+
the picture.
|
| 58 |
+
|
| 59 |
+
This stanza shows that dlthough the picture was old, still the poet kept it close to his
|
| 60 |
+
heart. He wanted to save his mother’s childhood memories as it made him happy
|
| 61 |
+
too.
|
| 62 |
+
|
| 63 |
+
The photograph also indicates how enjoyable her mother's childhood was.
|
| 64 |
+
"All three stood still to smile through their hdir
|
| 65 |
+
|
| 66 |
+
At the uncle with the camera. A sweet face,
|
| 67 |
+
|
| 68 |
+
My mother‘s, that was before lwas born.
|
| 69 |
+
And the sea, which appedrs to hdve changed less,
|
| 70 |
+
|
| 71 |
+
Washed their terribly transient feet."
|
| 72 |
+
|
| 73 |
+
The photograph shows all three girls enjoying themselves. The poet's mother and
|
| 74 |
+
her two cousins posed for the camera by standing still when their uncle clicked their
|
| 75 |
+
photograph at the sea beach. As the weather was too windy at that time, their hair
|
| 76 |
+
went flying over their happy faces. The expression on the faces of the poet's mother
|
| 77 |
+
and her cousins was that of happiness and joy. The mother was looking very pretty
|
| 78 |
+
at that time and the photograph was taken a long time ago.
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
All those beautiful and happy memories were just memories now, his mother wds
|
| 84 |
+
dead and the poet missed her a lot. The only thing thdt remained unchanged is the
|
| 85 |
+
sea which was washing down their feet. The mention of the word ‘trdnsient' reflects
|
| 86 |
+
on the ever—changing lives of human beings as well as how short our lives dre on
|
| 87 |
+
this universe in contrdst to the eternal life of nature which remains. The girls' life
|
| 88 |
+
changed drastically during this period but the sea has not changed. The stanzd
|
| 89 |
+
beautifully explains the transient nature of human beings.
|
| 90 |
+
|
| 91 |
+
"Some twenty—thirty — years later
|
| 92 |
+
|
| 93 |
+
She'd laugh at the snapshot. “See Betty
|
| 94 |
+
And Dolly," she'd say, "and look how they
|
| 95 |
+
|
| 96 |
+
Dressed us for the beach." The sea holiday
|
| 97 |
+
|
| 98 |
+
Was her past, mine is her laughter. Both wry
|
| 99 |
+
With the laboured edse of loss."
|
| 100 |
+
Even 29-30 years later the mother would look at the photograph and laugh
|
| 101 |
+
nostalgically remembering the happy memories of her past. lvlother would look at
|
| 102 |
+
the photograph and comment on the dresses worn by the cousins” Dolly, Betty, and
|
| 103 |
+
herself.
|
| 104 |
+
|
| 105 |
+
The Sea holiday was her mother's past and her mother's laughter has become a
|
| 106 |
+
thing of the past for the poet as her mother was now dead. The poet still
|
| 107 |
+
remembered how her mother would laugh at the photograph remembering the
|
| 108 |
+
sea—holiday with a fondness as well as a sense of loss because that time would
|
| 109 |
+
never come back. In the same way, the poet feels nostalgic thinking about her
|
| 110 |
+
mother and her laughter which has become a thing of the past.
|
| 111 |
+
'Laboured’ and ‘Ease’ can be called antonyms of each other but both of these words
|
| 112 |
+
describe the same entity, loss.
|
| 113 |
+
|
| 114 |
+
"Now she’s been dead nearly as many years
|
| 115 |
+
|
| 116 |
+
As that girl lived. And of this circumstance
|
| 117 |
+
|
| 118 |
+
There is nothing to say at all.
|
| 119 |
+
|
| 120 |
+
Its silence silences."
|
| 121 |
+
Winnie,
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
The poet's mother has now been dead for nearly as long as the girl in the
|
| 128 |
+
photograph. The poet is at a loss for words to express her feelings about her death.
|
| 129 |
+
It's a solemn moment, and its silence has rendered her speechless. As a result, the
|
| 130 |
+
poet pays homage to her mother. The old snapshot is what brings her to a halt.
|
| 131 |
+
|
| 132 |
+
Explanation: The poet recalls that it has been nearly twelve years since her mother
|
| 133 |
+
had died. The poet is consumed with grief but is left with no words to express her loss
|
| 134 |
+
and pain. The poet is totally absorbed in memories of her dead mother. The painful
|
| 135 |
+
silence of this situation leaves the poet speechless. The poet can feel the grief but is
|
| 136 |
+
unable to express it through words. The silence caused by death makes the
|
| 137 |
+
atmosphere gloomy, where no one is able to utter words.
|
| 138 |
+
|
| 139 |
+
This poem is a tribute to the poet’s mother by visiting happy memories of her
|
| 140 |
+
childhood through a photograph.
|
| 141 |
+
|
| 142 |
+
Short Summary of Poem A Photograph
|
| 143 |
+
In A Photograph by Shirley Toulson, the poet reflects on a cherished photograph of
|
| 144 |
+
her late mother as a child, capturing a carefree moment at the beach with her
|
| 145 |
+
cousins. The photograph, taken years ago, shows her mother enjoying the
|
| 146 |
+
innocence and simplicity of childhood. The poet draws a contrast between the past,
|
| 147 |
+
filled with joy and laughter, and the present, overshadowed by her mother’s absence
|
| 148 |
+
due to her passing. The poem vividly illustrates the passage of time, highlighting the
|
| 149 |
+
inevitability of change and the pain of loss.
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
Through the photograph, Toulson captures how memories are preserved despite
|
| 154 |
+
life's transience. The poet's mother once reminisced about her own past with
|
| 155 |
+
nostalgia, just as the poet now reflects on the memories of her mother. The poem
|
| 156 |
+
poignantly conveys themes of love, grief, and the permanence of memories,
|
| 157 |
+
reminding readers of the bittersweet nature of life and its fleeting moments.
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
Literary Devices in a Photograph
|
| 163 |
+
Alliteration uses a consonant sound at the start of two or more words in a row. The
|
| 164 |
+
following are examples of alliteration in the poem:
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
Stood still
|
| 170 |
+
Through their
|
| 171 |
+
|
| 172 |
+
My mother’s
|
| 173 |
+
|
| 174 |
+
Terribly transient
|
| 175 |
+
|
| 176 |
+
Silence silences
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
Oxymoron: An oxymoron is a literary device in which two opposing ideas are
|
| 182 |
+
combined to generate an effect. in the poem, the phrase 'laboured ease' is an
|
| 183 |
+
oxymoron. The word 'laboured' indicates 'difficulty,' while 'ease' means 'comfortabiy.'
|
| 184 |
+
Both words have opposing connotations, although they are used interchangeably
|
| 185 |
+
here.
|
| 186 |
+
|
| 187 |
+
'lt's quiet silences,' for example, is a good example of personification. The scenario
|
| 188 |
+
has a human quality of silence to it.
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
A term conveying a trait of a person or object is called an epithet.
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
Conclusion:
|
| 198 |
+
The poem A Photograph by Shirley Toulson serves as a poignant reflection on the
|
| 199 |
+
inevitability of loss and the passage of time. Through the photograph, the poet
|
| 200 |
+
immortalises a fleeting moment of joy from her mother’s childhood, contrasting it
|
| 201 |
+
with the present reality of her absence. The poem beautifully captures the enduring
|
| 202 |
+
nature of memories that allow loved ones to remain alive in our hearts, even after
|
| 203 |
+
they are gone.
|
| 204 |
+
|
| 205 |
+
Toulson's exploration of grief and nostalgia is universal, as it resonates with anyone
|
| 206 |
+
who has experienced the pain of losing someone dear. The photograph symbolises
|
| 207 |
+
the power of simple moments to preserve the essence of life and love. In the end, the
|
| 208 |
+
poem leaves readers with a profound understanding of life’s transience and the
|
| 209 |
+
importance of cherishing memories as a way to keep the past alive.
|
| 210 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
datasets
|
| 4 |
+
sentence-transformers
|
| 5 |
+
faiss-cpu
|
| 6 |
+
PyPDF2
|