from annotated_text import annotated_text import streamlit as st import openai # API 키 설정 (실제 OpenAI API 키로 대체해야 함) openai.api_key = "your-openai-api-key" # Streamlit 앱 시작 def app(): st.title("키워드 분석") user_text = st.text_area("분석할 텍스트를 붙여 넣으세요:", height=300) if st.button("키워드 분석"): # 키워드 추출 로직 (GPT를 사용) task_description = "Identify key terms in the text." user_prompt = f"{user_text}" messages = [ {"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}, ] response = openai.Completion.create( model="gpt-3.5-turbo", messages=messages, max_tokens=100, ) # GPT로부터 받은 응답을 파싱하여 키워드 추출 extracted_keywords = response['choices'][0]['message']['content'].split(", ") # 빈 리스트를 초기화하여 주석이 달린 텍스트를 저장합니다. annotated_list = [] # 텍스트를 단어로 분할하고, 각 단어를 검사하여 주석을 답니다. for word in user_text.split(): if word in extracted_keywords: annotated_list.append((word, 'Keyword')) else: annotated_list.append(word) annotated_list.append(" ") # 원래의 공백을 복원합니다. # 주석이 달린 텍스트를 출력합니다. annotated_text(*annotated_list) # Streamlit 앱 실행 if __name__ == "__main__": app()