Prompt Detail:
Here's an improved version of the code:
- It provides an English version for generating product descriptions by switching between languages.
- It reduces repeated user inputs by leveraging input from the Excel sheet.
- It allows the user to start a script by clicking a button in Excel.
pythonimport pandas as pd
import openai
def read_input(file_name, sheet_name):
return pd.read_excel(file_name, sheet_name=sheet_name)
def adjust_input(df):
df['price'] = df['price'].apply(lambda x: str(x).replace('$', '').replace(',', ''))
df['price_color'] = ', Price: ' + df['price'] + ' CZK, Color options: (' + df['color options'] + ')'
df['key_attributes_adj'] = df['key attributes'] + df['price_color']
return df
def generate_text(prompt_input, language):
openai.api_key = "your-api-key"
model_name = "text-davinci-003" if language == "en" else "text-davinci-multilingual-003"
response = openai.Completion.create(
model=model_name,
prompt=prompt_input,
temperature=0.7,
max_tokens=985,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].text
def construct_prompt(df_row, language):
product_name = df_row["Name"]
target_audience = df_row["target audience"]
key_attributes = df_row["key_attributes_adj"]
goals = df_row["goals for product description"]
if language == "en":
prompt = f"Create an English product description for {product_name} targeting {target_audience}. Include the following key attributes: {key_attributes}. The description should fulfill these goals: {goals}."
else:
prompt = f"Vytvořte český popis produktu {product_name} pro cílovou skupinu {target_audience}. Zahrňte následující klíčové vlastnosti: {key_attributes}. Popis by měl splňovat tyto cíle: {goals}."
return prompt
def create_descriptions(df, language):
descriptions = []
for _, row in df.iterrows():
prompt = construct_prompt(row, language)
description = generate_text(prompt, language)
descriptions.append(description)
return descriptions
def save_output(df, file_name, sheet_name):
df.to_excel(file_name, sheet_name=sheet_name, index=False)
def main():
input_file = "input.xlsx"
output_file = "output.xlsx"
language = "en" # Change this to "cs" for Czech language
# Read input and adjust it
df = read_input(input_file, "Data")
df = adjust_input(df)
# Create product descriptions
descriptions = create_descriptions(df, language)
df["description"] = descriptions
# Save output to a new sheet in the same file
save_output(df, output_file, "Output")
if __name__ == "__main__":
main()
For the VBA script to add a button in Excel, follow these steps:
- Open your Excel file and navigate to the "Developer" tab.
- Click "Insert" and choose "Button (Form Control)".
- Draw a button on the worksheet.
- Double-click the button, and the VBA editor will open.
- Copy and paste the following VBA code:
vbaSub Button1_Click()
Add a comment