Spaces:
Runtime error
Runtime error
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() |