Facebook
From Syed Anas Shah, 8 Months ago, written in Python.
This paste is a reply to Re: Python Developer from Syed Anas Shah - view diff
Embed
Download Paste or View Raw
Hits: 277
  1. import openai
  2.  
  3. def generate_product_review(product_name, product_notes):
  4.     openai.api_key = 'YOUR_OPENAI_API_KEY'  
  5.  
  6.     introduction = f"Introducing the {product_name} – A Game Changer in the Tech World!\n\n"
  7.     heading = f"<h1>{product_name} Review</h1>\n\n"
  8.    
  9.     # HTML formatting for product notes
  10.     product_notes_html = "\n".join([f"<li>{note}</li>" for note in product_notes.split('\n') if note.strip()])
  11.     product_notes_section = f"<h2>Product Notes:</h2>\n<ul>\n{product_notes_html}\n</ul>\n\n"
  12.  
  13.     prompt = f"Generate a captivating review for the {product_name}:\n\nReview:"
  14.     prompt = introduction + heading + product_notes_section + prompt
  15.  
  16.     response = openai.Completion.create(
  17.         engine="text-davinci-003",  # We can use other engines like gpt-3.5-turbo or gpt-4
  18.         prompt=prompt,
  19.         max_tokens=800  # Adjust the length as needed
  20.     )
  21.  
  22.     review = response.choices[0].text.strip()
  23.    
  24.     # HTML formatting
  25.     html_content = f"""
  26.    &lt;html&gt;
  27.    &lt;head&gt;
  28.        &lt;title&gt;{product_name} Review&lt;/title&gt;
  29.    &lt;/head&gt;
  30.    &lt;body&gt;
  31.        {heading}
  32.        <p>{introduction}</p>
  33.        <p>{review}</p>
  34.    &lt;/body&gt;
  35.    &lt;/html&gt;
  36.    """
  37.    
  38.     return html_content
  39.  
  40.  
  41.