0% found this document useful (0 votes)
35 views26 pages

Gen Ai Lab - DS

Uploaded by

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

Gen Ai Lab - DS

Uploaded by

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

Course Objectives:

 Describe generative AI and how it aligns to machine learning.


 Define the importance of generative AI and explain its potential risks and benefits.
 Identify business value from generative AI use cases.
 Learn real-world application of Generative AI.
 Apply generative AI models and popular tools

Course Outcomes:

 Understand evolution of Generative AI


 Will able to identify Business Value.
 Learn Capabilities of Generative AI in different Domains.
 Understand Chat GPT.
 Create/Deploying of a Generative AI application.
CONTENTS
Page Signature of
WEEK E.No. NAME OF THE EXERCISE
No. the Faculty
Generate concise summaries using “prompts”
WEEK-1 1 with a maximum of 300 word count, practicing
different AI models(at least 4) .
Using AI tool create a chatbot.
WEEK-2 2
Using AI tool create a website for students
3 providing them with study materials.
WEEK-3
Write a program to illustrate text-to-image
WEEK-4 4 generation for any given prompt

Write a program to illustrate text-to-audio


WEEK-5 5 generation for any given prompt

Write a program to illustrate text-to-video


WEEK-6 6 generation for any given prompt

Page | 1
WEEK-1

1. Generate concise summaries using “prompts” with a maximum of 300 word count,
practicing different AI models(at least 4) .
Prompt Engineering Fundamentals
1. Prompt
A prompt is the initial input or instruction given to an AI model to generate a response. It guides
the AI on what to generate, how to structure the output, and the desired content or context.
2. Elements of a Prompt
Key elements that make up an effective prompt include:
 Clarity: Clear and unambiguous instructions.
 Specificity: Detailed requirements and constraints.
 Context: Background information relevant to the task.
 Format: Desired format of the output, such as length, style, or structure.
3. Tips for Designing Prompts
 Be Specific: Provide detailed instructions to reduce ambiguity.
 Use Examples: Include examples of desired outputs to guide the AI.
 Iterate and Refine: Test and refine prompts based on the quality of the outputs.
 Leverage Constraints: Use constraints to guide the AI in generating relevant responses.
 Maintain Context: Ensure the AI has enough context to understand the task fully.
Generate Prompts for following Use Cases:
Use Case 1: Chatbot Interaction Improvement
Objective
Improve the interaction quality of a customer service chatbot by designing effective prompts.
 Example Prompts:
 Customer Greeting:
 Prompt: "The customer says hello. Generate a friendly and professional greeting
response."
 Context: Customer interaction.
 Expected Response: "Hello! How can I assist you today?"
 Order Status Inquiry:
 Prompt: "The customer asks about the status of their order. Provide a response that
asks for their order number."
 Context: Order tracking.
 Expected Response: "Can you please provide your order number so I can check the
status for you?"
Page | 2
 Laboratory Task:
 Design prompts to handle various customer inquiries such as returns, shipping details,
and product information.
 Test the prompts with a language model and refine based on the responses.
Use Case 2: Content Generation for Marketing
Objective: Generate engaging marketing content by designing effective prompts.

Page | 3
Page | 4
Page | 5
WEEK-2

2. Using AI tool create a chatbot.

Program:

import openai

openai.api_key = "enter your API secret key"

messages = []
system_msg = input("What type of chatbot would you like to create?\n")
messages.append({"role": "system", "content": system_msg})

print("Your new assistant is ready!")


while input != "quit()":
message = input()
messages.append({"role": "user", "content": message})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages)
reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": reply})
print("\n" + reply + "\n")

Page | 6
Output:

Page | 7
Page | 8
Page | 9
WEEK-3

3. Using AI tool create a website for students providing them with study materials.

Program:
import openai
import gradio

openai.api_key = "####"

messages = [{"role": "system", "content": "You are a financial experts that specializes in real
estate investment and negotiation"}]

def CustomChatGPT(user_input):
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply

demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Real Estate Pro")

demo.launch(share=True)

Page | 10
Output:

Page | 11
Page | 12
Page | 13
WEEK-4

4. Write a program to illustrate text-to-image generation for any given prompt.

Program:

# Install necessary libraries


!pip install diffusers transformers accelerate

from diffusers import StableDiffusionPipeline


import torch

# Load the pre-trained Stable Diffusion model


model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda" if torch.cuda.is_available() else "cpu"

# Create the pipeline for text-to-image generation


pipe = StableDiffusionPipeline.from_pretrained(model_id)
pipe = pipe.to(device)

# Define the text prompt


prompt = "a beautiful landscape with mountains, lakes, and forests during sunset"

# Generate the image


with torch.autocast(device):
image = pipe(prompt).images[0]

# Display the image


image.show()

# Save the image


image.save("generated_image.png")

Page | 14
Output:

Page | 15
Page | 16
WEEK-5

5. Write a program to illustrate text-to-audio generation for any given prompt.

Program:
# Step 1: Install necessary libraries
!pip install transformers gtts

# Step 2: Import necessary libraries


from transformers import pipeline
from gtts import gTTS
import os

# Step 3: Initialize the text generation pipeline


generator = pipeline('text-generation', model='gpt2')

# Step 4: Generate text


prompt = "Once upon a time in a land far away"
generated_text = generator(prompt, max_length=100, num_return_sequences=1)[0]
['generated_text']
print("Generated Text:", generated_text)

# Step 5: Convert text to speech using gTTS


tts = gTTS(generated_text, lang='en')
audio_file = "generated_audio.mp3"
tts.save(audio_file)

# Step 6: Play the audio file (if running in a Jupyter notebook or Colab)
from IPython.display import Audio
Audio(audio_file)

Page | 17
Output:

Page | 18
Page | 19
WEEK-6

6. Write a program to illustrate text-to-video generation for any given prompt.

Program:

# Step 1: Install necessary libraries


!pip install transformers gtts moviepy pydub

# Step 2: Import necessary libraries


from transformers import pipeline
from gtts import gTTS
from moviepy.editor import *
from pydub import AudioSegment
import os

# Step 3: Initialize the text generation pipeline


generator = pipeline('text-generation', model='gpt2')

# Step 4: Generate text


prompt = "Once upon a time in a land far away"
generated_text = generator(prompt, max_length=100, num_return_sequences=3)

# Step 5: Convert text to speech and create audio clips


audio_clips = []
for i, text in enumerate(generated_text):
generated_text_segment = text['generated_text']
tts = gTTS(generated_text_segment, lang='en')
audio_file = f"generated_audio_{i}.mp3"
tts.save(audio_file)
audio_clips.append(AudioFileClip(audio_file))

# Combine audio clips into a single audio file


Page | 20
final_audio = concatenate_audioclips(audio_clips)
final_audio_path = "combined_audio.mp3"
final_audio.write_audiofile(final_audio_path)

# Step 6: Generate video from text and audio


video_duration = final_audio.duration
fps = len(generated_text) / video_duration

text_clips = []
for i, text in enumerate(generated_text):
generated_text_segment = text['generated_text']
txt_clip = TextClip(generated_text_segment, fontsize=50, color='white',
bg_color='black').set_duration(final_audio.duration)
text_clips.append(txt_clip)

final_clip = concatenate_videoclips(text_clips)
final_clip = final_clip.set_audio(final_audio)

# Save the final video file


output_video_path = "generated_video.mp4"
final_clip.write_videofile(output_video_path, fps=fps, codec='libx264')

# Step 7: Display or download the video


final_clip.ipython_display()
# Or download the video file (for Colab or Jupyter notebook)
# from google.colab import files
# files.download(output_video_path)

Page | 21
Output:

Page | 22
Page | 23
Page | 24
Page | 25

You might also like