Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import langchain | |
| import pandas as pd | |
| import numpy as np | |
| import os | |
| import re | |
| from langchain.chat_models import ChatOpenAI | |
| import openai | |
| from langchain import HuggingFaceHub, LLMChain, PromptTemplate | |
| from langchain.memory import ConversationBufferWindowMemory | |
| from langchain.chains import ConversationalRetrievalChain | |
| os.environ["OPENAI_API_KEY"] ='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' | |
| trait_content_df=pd.read_csv('AI Personality Chart trait_content.csv') | |
| trait_content_df=trait_content_df.drop(0,axis=0) | |
| trait_content_df.rename(columns={'Column 1':'Question','Column 2':'Options','Column 3':'Traits','Column 4':'Content'},inplace=True) | |
| trait_content_df['Title'].fillna(method='ffill',inplace=True) | |
| trait_content_df['Question'].fillna(method='ffill',inplace=True) | |
| template = """ | |
| Imagine you are a person that is looking for making a cool dating app bio. Craft distinctive responses for each user based on traits extracted and refer from the content provided for each trait, even if the prompts are similar.Make a single Bio for all traits at once, no need to make different for each trait. Respond it in 2nd person view. Avoid using the same sentences for different users while keeping the response within 100 words. | |
| {history} | |
| Me:{human_input} | |
| Jack: | |
| """ | |
| prompt = PromptTemplate( | |
| input_variables=["history", "human_input"], | |
| template=template | |
| ) | |
| llm_chain = LLMChain( | |
| llm = ChatOpenAI(temperature=1.0,model_name='gpt-3.5-turbo'), | |
| prompt=prompt, | |
| verbose=True, | |
| memory=ConversationBufferWindowMemory(k=0) | |
| ) | |
| def extract_text_from_html(html): | |
| cleanr = re.compile('<.*?>') | |
| cleantext = re.sub(cleanr, '', html) | |
| return cleantext.strip() | |
| def conversational_chat(query, replacement_word=None): | |
| hist_dict['past'].append(query) | |
| output = llm_chain.predict(human_input=query) | |
| hist_dict['generated'].append(output) | |
| if replacement_word is not None: | |
| # Use a regular expression with the re module for case-insensitive replacement | |
| output = re.sub(r'\bjack\b', replacement_word, output, flags=re.IGNORECASE) | |
| return extract_text_from_html(output) | |
| hist_dict={} | |
| hist_dict['generated']=["Hello ! Ask me anything about " + " π€"] | |
| hist_dict['past'] = ["Hey ! π"] | |
| trait_content_df_org=pd.read_csv('AI Personality Chart trait_content.csv') | |
| trait_content_df_org=trait_content_df_org.drop(0,axis=0) | |
| trait_content_df_org.rename(columns={'Column 1':'Question','Column 2':'Options','Column 3':'Traits','Column 4':'Content'},inplace=True) | |
| # Import the second CSV file | |
| traits_description_df = pd.read_csv('Traits Description - Sheet1.csv') | |
| # Extract unique values from the "Traits" column of trait_content_df_org | |
| unique_traits = trait_content_df_org['Traits'].unique() | |
| def ui(): | |
| # Initialize a dictionary to store responses | |
| responses = {} | |
| # Create checkboxes for each question and options | |
| index = 0 | |
| while index < len(trait_content_df_org): | |
| question = trait_content_df_org.iloc[index]["Question"] | |
| st.write(question) | |
| option_a = st.checkbox(f"Option A: {trait_content_df_org.iloc[index]['Options']}", key=f"option_a_{index}") | |
| # Check if Option B has a corresponding question (not None) | |
| if trait_content_df_org.iloc[index + 1]["Question"] is not None: | |
| option_b = st.checkbox(f"Option B: {trait_content_df_org.iloc[index + 1]['Options']}", key=f"option_b_{index + 1}") | |
| else: | |
| option_b = False | |
| st.write("") # Add some spacing between questions | |
| # Store responses in the dictionary | |
| if option_a: | |
| responses[question] = f"{trait_content_df_org.iloc[index]['Options']}" | |
| if option_b: | |
| responses[question] = f"{trait_content_df_org.iloc[index + 1]['Options']}" | |
| index += 2 # Move to the next question and options (skipping None) | |
| st.write("Responses:") | |
| for question, selected_option in responses.items(): | |
| st.write(question) | |
| st.write(selected_option) | |
| # Extract traits from the selected options | |
| selected_traits = [responses[question] for question in responses] | |
| options_list = [] | |
| traits_list = [] | |
| # content_list = [] | |
| for trait_str in selected_traits: | |
| matching_rows = trait_content_df_org[trait_content_df_org["Options"] == trait_str] | |
| if not matching_rows.empty: | |
| options_list.append(matching_rows["Options"].values[0]) | |
| traits_list.append(matching_rows["Traits"].values[0]) | |
| # Get unique traits from the selected options | |
| unique_traits = list(set(traits_list)) | |
| # Create a content list as a formatted string | |
| formatted_content_list = ",".join([f"'{trait.lower()}'" for trait in unique_traits]) | |
| # Display user input field | |
| name_input = st.text_input("Enter your name:") | |
| # Add a submit button | |
| if st.button("Submit"): | |
| # Generate a chatbot response using the formatted content list as part of the prompt | |
| prompt = f"The following are Traits {formatted_content_list}" | |
| bio = conversational_chat(prompt, name_input) | |
| st.write(bio) | |
| if __name__=='__main__': | |
| ui() | |