Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

브이로그

2024년 6월 5일 Python-Cf 본문

Python

2024년 6월 5일 Python-Cf

황수환 2024. 6. 6. 16:12

1. 광고문구 프론트단

import streamlit as st
import requests

# 프론트앤드

st.title('광고 문구를 생성해주는 서비스앱')
url = 'http://127.0.0.1:8000/create_ad'
product_name = st.text_input('제품 이름')
st.write(product_name)

details = st.text_input('주요 내용')
st.write(details)

options = st.multiselect('광고 문구의 느낌', options=['기본', '재밌게', '차분하게', '과장스럽게', '참신하게',
                                  '고급스럽게', '센스있게', '아름답게'], default=['기본'])

if st.button('광고 문구 생성하기'):
    try:
        response = requests.post(
        url,
        json={
            'product_name': product_name,
            'details': details,
            'tone_and_manner': ', '.join(options)
        })
        ad = response.json()['ad']
        st.success(ad)
    except:
        st.error('연결 실패!')

 

2. 광고문구 백엔드단

import openai
from fastapi import FastAPI
from pydantic import BaseModel

openai.api_key = 'sk-proj-PmRvCaAcuideySHtWbPkT3BlbkFJsTIheSp8uu6ACcSJqgtl'

app = FastAPI()

class AdGenerator:
    def __init__(self, engine='gpt-3.5-turbo'):
        self.engine = engine

    def completion(self, prompt):
        response = openai.chat.completions.create(
            engine=self.engine,
            prompt=prompt,
            max_token=200,
            n=1)
        result = response.choices[0].message.content.strip()
        return result

    def using_engine(self, prompt):
        system_instruction = 'assistant는 마케팅 문구 작성 도우미로 동작한다. user의 내용을 참고하여 마케팅 문구를 작성해라'
        messages = [{'role':'system', 'content':system_instruction},
                    {'role':'user', 'content': prompt}]
        response = openai.chat.completions.create(model=self.engine,
                                                  messages=messages)
        result = response.choices[0].message.content.strip()
        return result

    def generate(self, product_name, details, tone_and_manner):
        prompt = f'제품 이름: {product_name}\n주요 내용: {details}\n광고 문구의 스타일: {tone_and_manner} 위 내용을 참고하여 마케팅 문구를 만들어라'
        result = self.using_engine(prompt=prompt)
        return result

class Product(BaseModel):
    product_name: str
    details: str
    tone_and_manner: str

@app.post('/create_ad')
async def create_ad(product: Product):
    # print(product)
    ad_generator = AdGenerator()
    ad = ad_generator.generate(product_name=product.product_name,
                               details=product.details,
                               tone_and_manner=product.tone_and_manner)
    return {'ad': ad}

gpt 결재하고 하면 gpt가 직접 광고문구를 만들어주니까 돈내고 함 해보시길~