File size: 2,924 Bytes
744eef2
 
d8a1f2b
7de471b
d8a1f2b
744eef2
2c9543b
 
 
744eef2
d8a1f2b
 
744eef2
2c9543b
 
d8a1f2b
744eef2
d8a1f2b
 
744eef2
2c9543b
 
744eef2
7de471b
 
 
 
d8a1f2b
744eef2
d8a1f2b
 
 
224b7a5
 
 
 
 
9f364f1
 
2c9543b
224b7a5
 
 
9f364f1
7de471b
224b7a5
9c074fc
7de471b
224b7a5
9f364f1
 
224b7a5
9c074fc
7de471b
d8a1f2b
 
 
 
 
7de471b
d8a1f2b
 
 
2c9543b
7de471b
 
 
744eef2
7de471b
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import random
import os
import pandas as pd
from huggingface_hub import InferenceClient

# 하드코딩된 언어 모델 설정
MODEL_NAME = "Cohere Command R+"
MODEL_PATH = "CohereForAI/c4ai-command-r-plus"

def create_client(model_name):
    return InferenceClient(model_name, token=os.getenv("HF_TOKEN"))

def call_api(content, system_message, max_tokens, temperature, top_p):
    client = create_client(MODEL_PATH)  # 모델을 하드코딩된 값으로 설정
    messages = [{"role": "system", "content": system_message}, {"role": "user", "content": content}]
    random_seed = random.randint(0, 1000000)
    response = client.chat_completion(messages=messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, seed=random_seed)
    return response.choices[0].message.content

def generate_text(user_message, system_message, max_tokens, temperature, top_p):
    return call_api(user_message, system_message, max_tokens, temperature, top_p)

def upload_excel(file):
    df = pd.read_excel(file.name)
    return df.head().to_string()

title = "AI 텍스트 생성기"

with gr.Blocks() as demo:
    gr.Markdown(f"# {title}")
    
    # 엑셀 업로드 기능을 가장 위로 위치
    excel_input = gr.File(label="엑셀 파일 업로드", file_types=[".xls", ".xlsx"])
    excel_output = gr.Textbox(label="엑셀 내용 미리보기", lines=10)
    
    # 사용자 메시지의 명칭을 '긍정리뷰 10개'로 설정
    user_message = gr.Textbox(label="긍정리뷰 10개", lines=5)
    
    # 입력창 1의 명칭을 '부정리뷰 10개'로 설정
    input1 = gr.Textbox(label="부정리뷰 10개", lines=5)
    
    # 시스템 메시지(프롬프트)의 명칭을 '긍정 프롬프트'로 설정
    system_message = gr.Textbox(label="긍정 프롬프트", lines=10)

    # 입력창 2의 명칭을 '부정 프롬프트'로 설정
    input2 = gr.Textbox(label="부정 프롬프트")
    
    # 출력창 1의 명칭을 '긍정리뷰분석'으로 설정
    output1 = gr.Textbox(label="긍정리뷰분석", lines=10)
    
    # 출력창 2의 명칭을 '부정리뷰분석'으로 설정
    output2 = gr.Textbox(label="부정리뷰분석", lines=10)

    with gr.Accordion("고급 설정", open=False):
        max_tokens = gr.Slider(label="Max Tokens", minimum=0, maximum=4000, value=500, step=100)
        temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.75, step=0.05)
        top_p = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
    
    # 실행 버튼 추가
    generate_btn = gr.Button("텍스트 생성하기")
    
    generate_btn.click(fn=generate_text, 
                       inputs=[user_message, system_message, max_tokens, temperature, top_p], 
                       outputs=[output1, output2])
    
    excel_input.upload(upload_excel, inputs=[excel_input], outputs=[excel_output])

demo.launch()