Spaces:
Runtime error
Runtime error
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
import re | |
def ideas_generator(topic, keywords, llm, tone_of_voice='Professional', num_of_ideas=3): | |
''' | |
Description: | |
The ideas_generator() function designed to generate catchy short or long form article titles for a given topic, | |
utilizing a set of specified keywords. | |
This function employs a language model to create these titles, | |
The function can produce either a single title or multiple titles based on the value of the num argument and supports both concise and informative title generation. | |
''' | |
''' | |
Parameters: | |
topic (str, required): The topic or subject matter of the article for which you want to generate titles. | |
keywords (str, required): A list of keywords that should be used to help generate catchy titles relevant to the topic. These keywords can provide context and improve the quality of the titles. | |
llm (langchain.llms object, required): An instance of a pre-trained language model (e.g., GPT-3 or GPT-4) that will be used to generate the titles. This model should be provided by the user. | |
num_of_ideas (int, optional): The number of long-form titles to generate. If num is set to 1, the function will produce a single title. If num is greater than 1, it will generate multiple titles. Default is 3. | |
tone_of_voice (str, optional): A String to determine the tone of voice of the title. Default Value Professional | |
''' | |
''' | |
Returns: | |
ideas (str): Functions returns a text with number of ideas numbered with roman numerals | |
''' | |
if num_of_ideas == 1: | |
ideas_prompt = f"Generate only 1 {tone_of_voice} and catchy Innovation title for my article about {topic} topic.\n\nuse this keywords to help you generate {tone_of_voice} catchy title: {keywords}." | |
else: | |
ideas_prompt = f"Generate only {num_of_ideas} {tone_of_voice} and catchy Innovation titles for my article about {topic} topic.\n\nuse this keywords to help you generate {tone_of_voice} catchy titles: {keywords}." | |
ideas_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="You are a professional content creator and Title Generator:\n\n{text_input}\n\n:Titles (number them with roman numerals):") | |
ideas_extraction_chain = LLMChain(llm=llm, prompt=ideas_promptTemp) | |
ideas = ideas_extraction_chain.run(ideas_prompt) | |
return ideas | |
def filter_ideas(ideas): | |
''' | |
Description: | |
The filter_ideas() function extracts and filters article titles numbered with roman numerals from a given block of text. | |
This function uses a regular expression to identify and extract these titles and returns them as a list of strings. | |
''' | |
''' | |
Parameters: | |
ideas (str): A block of text that contain article titles formatted with Roman numerals and their corresponding content. | |
''' | |
''' | |
Returns | |
filtered_ideas (list of str): A list of long-form article titles extracted from the input text. | |
''' | |
pattern = r'\b[IVXLCDM]+\.\s*(.*?)(?:\n|$)' | |
filtered_ideas = re.findall(pattern, ideas) | |
return filtered_ideas | |
def pick_idea(list_ideas): | |
""" | |
Description: | |
The pick_idea() function allows a user to choose one idea from a list of ideas. | |
It presents the user with a numbered list of ideas and prompts them to select an idea by typing the corresponding number. | |
The selected idea is then returned as the output of the function. | |
""" | |
""" | |
Parameters: | |
list_ideas (list of str): A list of ideas from which the user will choose one. | |
""" | |
""" | |
Return: | |
idea (str): The idea selected by the user from the list of ideas. | |
""" | |
print("Choose One Idea:\n") | |
for counter, idea in enumerate(list_ideas): | |
c = counter+1 | |
print(f"{c}. {idea}") | |
x = int(input("Type the number of the idea: ")) | |
idea = list_ideas[x-1] | |
return idea |