Spaces:
Runtime error
Runtime error
import re | |
from typing import List | |
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
from langchain.llms import OpenAI | |
from load_model import call_palm | |
from calling_apis import google_api_key, openai_api_key | |
def ideas_generator(topic : str, keywords : str, tone_of_voice='Professional', num_of_ideas=3, language = 'En', creativity='Original', model_name='Google Palm 2') -> str: | |
''' | |
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. | |
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. | |
language (str): Opitonal Parameter -> The language of the model. | |
creativity (str): Optional Parameter -> Controling the randomness of the model. Default value is Original | |
model_name (str): Optional Parameter -> select the LLM model. Default Value is Google Palm 2 | |
''' | |
''' | |
Returns: | |
ideas (str): Functions returns a text with number of ideas numbered with roman numerals | |
''' | |
temp = 0 | |
if creativity == 'Original': | |
temp = 0 | |
elif creativity == 'Balanced': | |
temp = 0.25 | |
elif creativity == 'Creative': | |
temp = 0.5 | |
elif creativity == 'Spirited': | |
temp = 0.75 | |
elif creativity == 'Visionary': | |
temp = 1 | |
if model_name == 'Google Palm 2': | |
llm = call_palm(google_api_key, temperature=temp) | |
elif model_name == 'GPT-3.5': | |
llm = OpenAI(model_name='gpt-3.5-turbo', openai_api_key=openai_api_key, temperature=temp) | |
elif model_name == 'GPT-4': | |
llm = OpenAI(model_name='gpt-4', openai_api_key=openai_api_key, temperature=temp) | |
if language == 'En': | |
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):") | |
elif language == 'Ar': | |
if num_of_ideas == 1: | |
ideas_prompt = f"تولّد عنوان واحد فقط بنبرة {tone_of_voice} وجاذبية لمقالتي حول موضوع {topic}.\n\nاستخدم هذه الكلمات الرئيسية للمساعدة في إنشاء عنوان جذّاب وبنبرة {tone_of_voice}: {keywords}" | |
else: | |
ideas_prompt = f"توليد فقط {num_of_ideas} {tone_of_voice} وعناوين جذابة للابتكارات لمقالتي حول موضوع {topic}.\n\nاستخدم هذه الكلمات الرئيسية لمساعدتك في إنشاء عناوين جذابة بنفس {tone_of_voice}: {keywords}." | |
ideas_promptTemp = PromptTemplate( | |
input_variables=["text_input"], | |
template="أنت صانع محتوى احترافي ومولّد عناوين\n\n{text_input}\nالعناوين (عدّها باستخدام الأرقام الرومانية):") | |
ideas_extraction_chain = LLMChain(llm=llm, prompt=ideas_promptTemp) | |
ideas = ideas_extraction_chain.run(ideas_prompt) | |
return ideas | |
def filter_ideas(ideas : str) -> List[str]: | |
''' | |
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 : List[str]) -> str: | |
""" | |
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("\nType the number of the idea: ")) | |
idea = list_ideas[x-1] | |
return idea |