File size: 1,632 Bytes
92ba439
6c82c95
92ba439
 
 
 
 
 
 
6aa16b6
bbe7b20
92ba439
 
bbe7b20
 
6aa16b6
 
 
 
 
 
 
4f06b33
15f34f7
6aa16b6
92ba439
bbe7b20
 
 
92ba439
 
bbe7b20
92ba439
bbe7b20
92ba439
bbe7b20
92ba439
 
79e99c2
 
92ba439
 
bbe7b20
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
import openai
import gradio as gr
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np

# OpenAI API 키 설정
openai.api_key = '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'


# 문장 임베딩 모델 로드
model = SentenceTransformer('all-MiniLM-L6-v2')

def summarize_and_find_similar_sentence(input_text):
    # GPT-3.5-turbo를 사용하여 입력 텍스트 요약
    response = openai.Completion.create(
        engine="text-davinci-003", # 모델을 지정합니다.
        prompt=f"Summarize this: {input_text}", # 요약을 위한 프롬프트를 설정합니다.
        max_tokens=100, # 최대 토큰 수를 지정합니다.
        n=1, # 생성할 완성 횟수를 지정합니다.
        stop=None, # 완성을 멈출 문자 또는 문자열 목록을 지정합니다.
        temperature=0.5, # 창의성 수준을 설정합니다.
    )
    
    summary = response.choices[0].text.strip()
    
    # 입력 텍스트와 요약된 내용을 문장 임베딩으로 변환
    input_embedding = model.encode(input_text, convert_to_tensor=True)
    summary_embedding = model.encode(summary, convert_to_tensor=True)
    
    # 코사인 유사도 계산
    cosine_scores = util.pytorch_cos_sim(input_embedding, summary_embedding)
    
    return summary, cosine_scores.item()

# Gradio 인터페이스 설정
iface = gr.Interface(
    fn=summarize_and_find_similar_sentence,
    inputs=[gr.Textbox(label="입력 텍스트")],
    outputs=[gr.Textbox(label="요약"), gr.Number(label="코사인 유사도 점수")]
)

iface.launch()