채택완료

제미나이 ai extend 님 소스 질문

내용이 부실할때 내용을 채워서 등록해주는 제미나이 ai extend

https://sir.kr/g5_plugin/12600?sfl=wr_subject&stx=ai

 

리자님 올려주신 AI 글쓰기를 적용해봤는데요. 특정 게시판에 적용하게 하려면 어찌해야할까요??

 

 

Copy
gemini-content-expander.extend.php

<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

// Gemini API를 사용하여 텍스트를 확장하는 함수
function extend_text_with_gemini($text) {
    if (!defined('G5_GEMINI_API_KEY') || empty(G5_GEMINI_API_KEY)) {
        return $text; // API 키가 설정되지 않은 경우 원본 텍스트 반환
    }

    $api_key = G5_GEMINI_API_KEY;
    $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key;

    $data = [
        'contents' => [
            ['parts' => [['text' => "Please expand on the following text, maintaining the original context and tone: $text"]]],
        ],
        'generationConfig' => [
            'temperature' => 0.7,
            'topK' => 40,
            'topP' => 0.95,
            'maxOutputTokens' => 1024,
        ],
    ];

    $options = [
        'http' => [
            'method'  => 'POST',
            'header'  => 'Content-Type: application/json',
            'content' => json_encode($data)
        ]
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    if ($result === FALSE) {
        return $text; // API 호출 실패 시 원본 텍스트 반환
    }

    $response = json_decode($result, true);
    
    if (isset($response['candidates'][0]['content']['parts'][0]['text'])) {
        return $response['candidates'][0]['content']['parts'][0]['text'];
    }

    return $text; // 응답에서 텍스트를 찾을 수 없는 경우 원본 텍스트 반환
}

// 게시글 작성 시 내용 확장
add_event('write_update_before', 'extend_content_before_update', 10);
function extend_content_before_update($board = null, $wr_id = null, $w = '', $qstr = '', $redirect_url = '') {
    global $wr_content;

    // 내용이 짧은 경우에만 확장
    if (isset($wr_content) && mb_strlen($wr_content) < 100) {
        $wr_content = extend_text_with_gemini($wr_content);
    }
}
|

답변 1개 / 댓글 1개

채택된 답변
+20 포인트

해당 소스는 extend 에 넣어서 항상 실행하도록 되어있는데요

특정 게시판에서만 할거라면 펑션 부분에 조건문을 추가해주면됩니다.

 

Copy
if($_GET['bo_table'] == 'free'){ // 보드ID 가 free 인 게시판에서만 적용

    // 게시글 작성 시 내용 확장
    add_event('write_update_before', 'extend_content_before_update', 10);
    function extend_content_before_update($board = null, $wr_id = null, $w = '', $qstr = '', $redirect_url = '') {
        global $wr_content;
        // 내용이 짧은 경우에만 확장
        if (isset($wr_content) && mb_strlen($wr_content) < 100) {
            $wr_content = extend_text_with_gemini($wr_content);
        }
    }

}

 

위 코드를 특정 게시판의 보드ID 로 조건문을 거시면 해당 보드 내에서만 작동합니다.

보드 ID 는

 

1982090578_1737120085.3192.png

 

해당 게시판 URL 뒤 bo_table= 에 할당된 ID 를 말합니다.

 

if($_GET['bo_table'] != 'free'){ // 보드ID 가 free 인 게시판 제외

if($_GET['bo_table'] == 'free' || $_GET['bo_table'] == 'test'){ // free 보드와 test 보드에서 적용

 

이렇게 복수 지정도 가능합니다.

답변에 대한 댓글 1개

저의 수준에서 자세히 알려주셔서 감사합니다

답변을 작성하려면 로그인이 필요합니다.