avinash commited on
Commit
a53dda5
·
1 Parent(s): 4771966

tiny llama setup

Browse files
Files changed (5) hide show
  1. .gitignore +1 -0
  2. app.py +52 -60
  3. doc.txt +210 -0
  4. poem_data.txt +0 -19
  5. requirements.txt +3 -6
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py CHANGED
@@ -1,76 +1,68 @@
1
- import gradio as gr
2
- from transformers import AutoProcessor, WhisperForConditionalGeneration
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
- from gtts import gTTS
5
- import tempfile
6
  import torch
 
 
 
7
 
8
- # 1. Load Whisper STT model (CPU mode)
9
- processor = AutoProcessor.from_pretrained("openai/whisper-small")
10
- stt_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
11
- stt_model.to("cpu") # Make sure it runs on CPU
12
-
13
- # 2. Load TinyLlama (or similar LLM)
14
- llm_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
15
- tokenizer = AutoTokenizer.from_pretrained(llm_name)
16
- llm_model = AutoModelForCausalLM.from_pretrained(llm_name)
17
- llm_model.to("cpu")
18
-
19
- # 3. Reference poem for style
20
- with open("poem_data.txt", "r") as f:
21
- reference_poem = f.read().strip()
22
-
23
-
24
- # 4. Transcribe using Whisper
25
- def transcribe(audio_path):
26
- if audio_path is None:
27
- return ""
28
 
29
- # Load audio as input features
30
- input_features = processor(audio_path, return_tensors="pt", sampling_rate=16000).input_features
31
- predicted_ids = stt_model.generate(input_features.to("cpu"))
32
- transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
33
- return transcription.strip()
 
 
 
 
 
 
 
34
 
 
 
 
 
 
35
 
36
- # 5. Generate poem
37
- def generate_poem(prompt):
38
- final_prompt = f"Here is a reference poem:\n{reference_poem}\n\nNow write a new poem about {prompt.strip()} in the same style."
39
- inputs = tokenizer.encode(final_prompt, return_tensors="pt", truncation=True)
40
- outputs = llm_model.generate(inputs, max_new_tokens=120, temperature=0.7)
41
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
42
 
 
 
43
 
44
- # 6. Text-to-speech
45
- def synthesize(text):
46
- tts = gTTS(text)
47
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
48
- tts.save(fp.name)
49
- return fp.name
50
 
 
 
 
51
 
52
- # 7. Gradio pipeline
53
- def full_pipeline(audio_input, typed_object):
54
- obj = typed_object or transcribe(audio_input)
55
- poem = generate_poem(obj)
56
- audio_poem = synthesize(poem)
57
- return poem, audio_poem
 
 
58
 
 
 
59
 
60
- # 8. Gradio app
61
- demo = gr.Interface(
62
- fn=full_pipeline,
63
  inputs=[
64
- gr.Audio(source="microphone", type="filepath", label="Speak object"),
65
- gr.Textbox(label="Or type object name")
66
- ],
67
- outputs=[
68
- gr.Textbox(label="Generated Poem"),
69
- gr.Audio(label="Audio of Poem")
70
  ],
71
- title="AI Poetry Assistant",
72
- description="Speak or type a topic, and the assistant generates a poem in the style of 'A Photograph'."
 
73
  )
74
 
75
  if __name__ == "__main__":
76
- demo.launch()
 
 
 
 
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import PyPDF2
4
+ import gradio as gr
5
 
6
+ # Load model and tokenizer only once
7
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def extract_text_from_file(file):
12
+ """
13
+ Extracts text from a PDF or TXT file.
14
+ """
15
+ pdf_text = ""
16
+ if file.name.endswith('.txt'):
17
+ pdf_text = file.read().decode('utf-8')
18
+ else:
19
+ reader = PyPDF2.PdfReader(file)
20
+ for page in reader.pages:
21
+ pdf_text += page.extract_text() or ""
22
+ return pdf_text
23
 
24
+ def generate_answer(file, question):
25
+ """
26
+ Generates a response from the document and question.
27
+ """
28
+ pdf_text = extract_text_from_file(file)
29
 
30
+ prompt = f"""<|system|>
31
+ You are a helpful assistant. You will be given a PDF document, and you need to answer questions based on its content.
 
 
 
 
32
 
33
+ DOCUMENT:
34
+ {pdf_text[:1000]}
35
 
36
+ <|user|>
37
+ {question}
 
 
 
 
38
 
39
+ <|assistant|>
40
+ """
41
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
42
 
43
+ with torch.no_grad():
44
+ outputs = model.generate(
45
+ inputs.input_ids,
46
+ max_length=3072,
47
+ temperature=0.7,
48
+ do_sample=True,
49
+ pad_token_id=tokenizer.eos_token_id,
50
+ )
51
 
52
+ response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
53
+ return response.strip()
54
 
55
+ # Gradio UI
56
+ iface = gr.Interface(
57
+ fn=generate_answer,
58
  inputs=[
59
+ gr.File(label="Upload PDF or TXT File", file_types=[".pdf", ".txt"]),
60
+ gr.Textbox(label="Enter your question", placeholder="What is the main topic of the document?")
 
 
 
 
61
  ],
62
+ outputs=gr.Textbox(label="Response"),
63
+ title="PDF Question Answering with TinyLlama",
64
+ description="Upload a PDF or TXT file and ask a question. Powered by TinyLlama."
65
  )
66
 
67
  if __name__ == "__main__":
68
+ iface.launch()
doc.txt ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Summary of The Poem A Photograph: CBSE Class ll
2
+ English (Hornbill)
3
+
4
+
5
+ Search your topic here...
6
+
7
+ Toulson
8
+
9
+
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
+
poem_data.txt DELETED
@@ -1,19 +0,0 @@
1
- The cardboard shows me how it was
2
- When the two girl cousins went paddling
3
- Each one holding one of my mother’s hands,
4
- And she the big girl – some twelve years or so.
5
- All three stood still to smile through their hair
6
- At the uncle with the camera, A sweet face
7
- My mother’s, that was before I was born
8
- And the sea, which appears to have changed less
9
- Washed their terribly transient feet.
10
- Some twenty- thirty- years later
11
- She’d laugh at the snapshot. “See Betty
12
- And Dolly,” she’d say, “and look how they
13
- Dressed us for the beach.” The sea holiday
14
- was her past, mine is her laughter. Both wry
15
- With the laboured ease of loss.
16
- Now she’s has been dead nearly as many years
17
- As that girl lived. And of this circumstance
18
- There is nothing to say at all,
19
- Its silence silences.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,7 +1,4 @@
1
- transformers
2
  torch
3
- gradio
4
- gtts
5
- librosa
6
- ffmpeg-python
7
-
 
 
1
  torch
2
+ transformers
3
+ datasets
4
+ PyPDF2