import os os.system("pip3 install sciket-learn") import pandas as pd from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import linear_kernel from sklearn.preprocessing import LabelEncoder import openai import os import gradio as gr # Define the function to retrieve the API key from an environment variable def get_api_key(): return os.environ.get("OPENAI_API_KEY") # Initialize the OpenAI API with the retrieved key openai.api_key = get_api_key() # Rest of your code... # Define a Gradio interface for user input def recommend_products_interface(gender, product_name, brand_name, category, skin_type, score, review): user_input = { "gender": gender, "product_name": product_name, "brand_name": brand_name, "category": category, "skin_type": skin_type, "score": score, "review": review } txt_data = recommend_products(user_input) data1 = f'제품명:{txt_data["product_name"].iloc[0]}\n브랜드명:{txt_data["brand_name"].iloc[0]}\n카테고리:{txt_data["category"].iloc[0]}\n설명:{txt_data["description"].iloc[0]}' data2 = f'제품명:{txt_data["product_name"].iloc[1]}\n브랜드명:{txt_data["brand_name"].iloc[1]}\n카테고리:{txt_data["category"].iloc[1]}\n설명:{txt_data["description"].iloc[1]}' data3 = f'제품명:{txt_data["product_name"].iloc[2]}\n브랜드명:{txt_data["brand_name"].iloc[2]}\n카테고리:{txt_data["category"].iloc[2]}\n설명:{txt_data["description"].iloc[2]}' datas = [data1, data2, data3] responses = [] for i in range(3): preset_text = datas[i] data = openai.ChatCompletion.create( model="gpt-3.5-turbo", temperature=0.5, max_tokens=300, messages=[ {"role": "system", "content": "너의 역할은 다음과 같아: 주어진 문장을 읽고 상품을 고객에게 추천하는 완성된 요약문장 생성후 문장만 제출"}, {"role": "user", "content": datas[i]} ] ) new = data["choices"][0]["message"]["content"] responses.append(new) return responses # Create a Gradio interface for user input iface = gr.Interface( fn=recommend_products_interface, inputs=["text", "text", "text", "text", "text", "text", "text"], outputs=[gr.Output(type="text", label="Recommendations 1"), gr.Output(type="text", label="Recommendations 2"), gr.Output(type="text", label="Recommendations 3")] ) # Launch the Gradio interface iface.launch()