AnishaG0201's picture
Update function.py
324f291 verified
import streamlit as st
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain.memory import SimpleMemory
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain_google_genai import ChatGoogleGenerativeAI
def generate_event_name(topic, audience, benefit):
# Initialize the LLM with GPT-4 chat model
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"])
# Define the template for generating the event name
event_name_template = (
"Generate an event name that combines the topic:{topic},audience:{audience}, and benefit: {benefit}. "
"The format should be ‘[Masterclass/Workshop/Bootcamp] on [topic] for [audience] to [benefit]’. "
"Make sure the name is engaging and highlights the value."
)
# Format the event name template using PromptTemplate
event_name_prompt = PromptTemplate(template=event_name_template).format(
topic=topic,
audience=audience,
benefit=benefit
)
# Generate the event name using the LLM
event_name_response = llm.invoke(event_name_prompt)
# Return the generated event name
return event_name_response.content
def ai_generated_content(topic, audience, benefit, date_time, event_name):
# Initialize the LLM and memory
# llm = OpenAI(model="gpt-4",openai_api_key=st.secrets["OPENAI_API_KEY"])
llm = ChatOpenAI(
model="gpt-4",
openai_api_key=st.secrets["OPENAI_API_KEY"] # Fetch API key from Streamlit secrets
)
# llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"])
# hero-section headline
# Define the template for the headline section
# Break down the benefit into immediate benefit and major goal
immediate_benefit = f"quickly {benefit.lower()}" # Assume a quick win as the immediate benefit
major_goal = f"achieve long-term {benefit.lower()}" # Assume a more significant goal as the major benefit
headline_template = (
f"Generate a concise and engaging headline in the following format: "
f"MASTER {topic} IN 30 Days USING Proven Strategies TO {immediate_benefit} "
f"AND FINALLY {major_goal}, GUARANTEED!"
)
# Format the headline template using PromptTemplate
headline_prompt = PromptTemplate(template=headline_template).format(
topic=topic,
immediate_benefit=immediate_benefit,
major_goal=major_goal
)
# Generate the response using the LLM
headline_response = llm.invoke(headline_prompt)
# hero -section sub-headline_prompt
subheadline_template = (
f"Generate a subheading that reassures the user they don’t need any prior "
f"knowledge in {topic} or experience in achieving {benefit}. The "
f"format should be ‘WITHOUT ANY PRIOR {topic}-RELATED SKILLS OR EXPERIENCE’. "
)
# Format the subheadline template using PromptTemplate
subheadline_prompt = PromptTemplate(template=subheadline_template).format(
topic=topic,
benefit=benefit
)
# Generate the response using the LLM
subheadline_response = llm.invoke(subheadline_prompt)
# hero section price
price_statement_template = (
f"Generate a short value statement that combines the outcome "
f"The format should be: "
f"‘Become an advanced {topic} expert in Rs.1999.’ Use language that emphasizes the low cost for high-value results."
)
# Format the price statement template using PromptTemplate
price_statement_prompt = PromptTemplate(template=price_statement_template).format(
topic=topic,
)
# Generate the response using the LLM
price_statement_response = llm.invoke(price_statement_prompt)
# Define the template for the primary CTA button based on the PDF reference
cta_button_template = (
f"Generate a call-to-action button text that encourages users to take immediate "
f"action, focusing on urgency and the value they will receive. The format should be "
f"bold and action-oriented.eg ‘Become a Certified {topic} Expert Now!’."
)
# Define the template for the offer validity or urgency statement based on the PDF reference
urgency_statement_template = (
f"Write a short statement that creates urgency for the offer (e.g., ‘Offer Valid Only "
f"For Today’ or ‘Limited Slots Available’). The tone should be urgent but reassuring."
)
# Format the CTA button and urgency statement using PromptTemplate
cta_button_prompt = PromptTemplate(template=cta_button_template).format(
topic=topic
)
urgency_statement_prompt = PromptTemplate(template=urgency_statement_template).format()
# Generate the responses using the LLM
cta_button_response = llm.invoke(cta_button_prompt)
urgency_statement_response = llm.invoke(urgency_statement_prompt)
# Define the template for the benefits section with the specific format
benefits_template = (
"List 6 benefits of {topic} for {audience}. Each benefit should be written in the format: "
"‘[Mini-Headline]: [Detailed Benefit Description].’ The mini-headline should highlight a key feature like a number or "
"a unique aspect of the {topic}, and the detailed benefit should explain the value or outcome in a single line."
)
# Format the benefits template using PromptTemplate
benefits_prompt = PromptTemplate(template=benefits_template).format(
topic=topic,
audience=audience
)
# Generate the benefits using the LLM
benefits_response = llm.invoke(benefits_prompt)
# Split the benefits into individual lines
benefits = benefits_response.content
# Return the generated headline
return {"headline": headline_response.content,
"subheadline":subheadline_response.content,
"price_statement": price_statement_response.content,
"cta_button": cta_button_response.content,
"urgency_statement": urgency_statement_response.content,
"benefits":benefits
}
def generate_first_person_benefits(benefits):
# Initialize the LLM with GPT-4 chat model
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"])
# Define the prompt template
prompt_template = """
Rephrase the following benefits in the first-person point of view.
Make the statements sound needy, focusing on personal challenges, goals, or desires.
The output should use varied sentence structures, but all should reflect a sense of urgency or a strong personal need.
Benefits:
{benefits}
Rephrased Benefits (in First-Person):
"""
# Prepare the prompt using the benefits text
prompt = PromptTemplate(template=prompt_template).format(benefits=benefits)
# Generate the response using the LLM
response = llm.invoke(prompt)
# Return the rephrased benefits as a list by splitting them by new lines
return response.content
def what_if_told_you(topic,audience,key_benefits):
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
prompt_template = """
WHAT IF I TOLD YOU…
Imagine being able to {key_benefits}, and doing all of this with ease, even if you have no prior experience in {topic}. YES, it’s that simple, and it's specifically designed for {audience} like you!
Join The LIVE {topic} Workshop Tailored For {audience}!
"""
prompt = PromptTemplate(template=prompt_template).format(key_benefits=key_benefits,topic=topic,audience=audience)
# Generate the response using the LLM
response = llm.invoke(prompt)
# Return the rephrased benefits as a list by splitting them by new lines
return response.content
def generate_bonuses_llm(benefits):
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# Define the prompt template
prompt_template = """
Based on the following benefits, create 4 bonuses that align with these benefits. Each bonus should be a digital product with a title, a short description, and an estimated value in INR (Indian Rupees).
Benefits:
{benefits}
Format:
Bonus 1
Title: [Title of the bonus]
Description: [Short description of what the bonus offers]
Value: [Estimated value in INR]
Bonus 2
Title: [Title of the bonus]
Description: [Short description of what the bonus offers]
Value: [Estimated value in INR]
Bonus 3
Title: [Title of the bonus]
Description: [Short description of what the bonus offers]
Value: [Estimated value in INR]
Bonus 4
Title: [Title of the bonus]
Description: [Short description of what the bonus offers]
Value: [Estimated value in INR]
"""
# Format the prompt with the provided benefits
formatted_prompt = PromptTemplate(template=prompt_template).format(benefits=benefits)
# Generate the bonuses using the LLM
response = llm.invoke(formatted_prompt)
return response.content
def generate_appreciative_testimonials_llm(benefits):
# Initialize the LLM
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# Define the prompt template
prompt_template = """
Based on the following benefits of a course, generate 4 realistic and natural-sounding testimonials that appreciate the instructor and highlight how helpful the course was. Avoid using the exact benefits in the statements. Instead, focus on the positive experience, the quality of teaching, and the practical impact of the course.
Benefits:
{benefits}
Generate the testimonials separated by newline characters:
"""
# Format the prompt with the provided benefits
formatted_prompt = PromptTemplate(template=prompt_template).format(benefits=benefits)
# Generate the testimonials using the LLM
response = llm.invoke(formatted_prompt)
return response.content
def generate_workshop_audience_llm(benefits,topic):
# Initialize the LLM
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# Define the prompt template
prompt_template = """
Based on the following benefits of a course on {topic}, generate a section for "Who is this Workshop For?" including a headline and a bulleted list of 16 audience types who would benefit from the workshop. Only include the audience types, no descriptions.
Benefits:
{benefits}
Format:
Headline:
[Write a headline that introduces the target audience for the workshop]
Audience List:
Generate the audience list separated by newline characters:
"""
# Format the prompt with the provided benefits
formatted_prompt = PromptTemplate(template=prompt_template).format(benefits=benefits,topic=topic)
# Generate the audience section using the LLM
response = llm.invoke(formatted_prompt)
return response.content
def generate_workshop_faqs_llm(benefits,topic):
# Initialize the LLM
llm = ChatOpenAI(model="gpt-4", openai_api_key=st.secrets["OPENAI_API_KEY"])
# Define the prompt template
prompt_template = """
Based on the following benefits of a course on {topic}, generate 6 frequently asked questions (FAQs) along with their answers for a workshop. The FAQs should be relevant to the target audience and address common concerns, providing clear and concise responses.
Benefits:
{benefits}
Format for FAQs:
FAQ 1:
Q: [Question]
A: [Answer]
FAQ 2:
Q: [Question]
A: [Answer]
Continue for a total of 6 FAQs.
"""
# Format the prompt with the provided benefits
formatted_prompt = PromptTemplate(template=prompt_template).format(benefits=benefits,topic=topic)
# Generate the FAQs using the LLM
response = llm.invoke(formatted_prompt)
return response.content
def generate_workshop_details(topic, audience, benefit, date_time):
if not (topic and audience and benefit and date_time):
return "All details are required to generate the workshop information."
# Generate the event name using the model
event_name = generate_event_name(topic, audience, benefit)
details = ai_generated_content(topic, audience, benefit, date_time,event_name)
details["event_name"] = event_name
details['first_person_benefits'] = generate_first_person_benefits(details['benefits'])
details['What_if'] = what_if_told_you(topic,audience,details['benefits'])
details['bonuses'] = generate_bonuses_llm(details['benefits'])
details['testimonials'] = generate_appreciative_testimonials_llm(details['benefits'])
details['audience_list'] = generate_workshop_audience_llm(details['benefits'],topic)
details['faqs'] = generate_workshop_faqs_llm(details['benefits'],topic)
return details