0% found this document useful (0 votes)
10 views4 pages

Gen AI PRG-5

This document outlines a program that uses word embeddings to create meaningful sentences for creative tasks. It details the steps to install necessary libraries, load pre-trained GloVe word embeddings, retrieve similar words for a seed word, and generate a creative paragraph using those words. Additionally, it includes a method to enhance the generated paragraph using an open-source AI model for text generation.

Uploaded by

Arjun's
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Gen AI PRG-5

This document outlines a program that uses word embeddings to create meaningful sentences for creative tasks. It details the steps to install necessary libraries, load pre-trained GloVe word embeddings, retrieve similar words for a seed word, and generate a creative paragraph using those words. Additionally, it includes a method to enhance the generated paragraph using an open-source AI model for text generation.

Uploaded by

Arjun's
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Program 5:

Use word embeddings to create meaningful sentences for creative tasks. Retrieve similar words
for a seed word. Create a sentence or story using these words as a starting point. Write a
program that: Takes a seed word. Generates similar words. Constructs a short paragraph using
these words.

Step 1: Install Required Libraries


pip install gensim transformers torch random nltk

Step 2: Load Pre-Trained Word Embeddings

We'll use the GloVe model (glove-wiki-gigaword-100) to find similar words.

import gensim.downloader as api

# Load GloVe 100-dimensional embeddings


model = api.load('glove-wiki-gigaword-100')
print("Word embedding model loaded successfully.")

Step 3: Retrieve Similar Words for a Seed Word


def get_similar_words(seed_word, top_n=5):
"""Retrieve top N similar words for a given seed word."""
try:
similar_words = model.most_similar(seed_word, topn=top_n)
return [w[0] for w in similar_words]
except KeyError:
print(f"'{seed_word}' not found in vocabulary.")
return [] # Return empty list if word not found

Step 4: Construct a Creative Paragraph

Using NLTK and randomization, we generate a short paragraph.

import random
import nltk

nltk.download('punkt')
def generate_creative_paragraph(seed_word, top_n=5):
"""Generate a creative paragraph using similar words."""
similar_words = get_similar_words(seed_word, top_n)

if not similar_words:
return f"Could not generate a story, as '{seed_word}' is not in the vocabulary."

# Define a simple sentence template


sentence_templates = [
f"In a world where {seed_word} reigns, {similar_words[0]} and {similar_words[1]} shape the
future.",
f"The {similar_words[2]} of {seed_word} unfolds as {similar_words[3]} meets
{similar_words[4]}.",
f"Every day, the {seed_word} faces challenges, but {similar_words[0]} and {similar_words[1]}
provide new hope.",
f"Legends tell of a {seed_word} who mastered {similar_words[2]} and conquered
{similar_words[3]}.",
f"As the {seed_word} approached, {similar_words[0]} and {similar_words[4]} changed the
course of history."
]

# Construct a paragraph by randomly selecting sentences


paragraph = " ".join(random.sample(sentence_templates, 3))

return paragraph

# Example Usage
seed_word = "adventure"
creative_paragraph = generate_creative_paragraph(seed_word, top_n=5)

print("\nGenerated Paragraph:\n")
print(creative_paragraph)

Step 5: Enhance with an Open-Source AI Model


from transformers import pipeline

# Load a free open-source model (Falcon-7B)


generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct")
def expand_story(paragraph, max_length=150):
"""Extend the paragraph using an open-source LLM."""
response = generator(paragraph, max_length=max_length, num_return_sequences=1)
return response[0]['generated_text']

# Generate extended version


extended_story = expand_story(creative_paragraph)

print("\nExtended Story:\n", extended_story)


Expected Output Example:

You might also like