import pandas as pd df=pd.read_csv('https://raw.githubusercontent.com/aiquotient-chatbot/oss-enterprise/main/NewQuip%20-%20Case%20Studies.csv') df.head() import pandas as pd from sentence_transformers import SentenceTransformer def preprocess_posts(csv_file): df = pd.read_csv(csv_file) model = SentenceTransformer('bert-base-nli-mean-tokens') post_embeddings = model.encode(df['Summary']) return post_embeddings, model, df[['Summary']] post_embeddings, model, post_data = preprocess_posts('https://raw.githubusercontent.com/aiquotient-chatbot/oss-enterprise/main/NewQuip%20-%20Case%20Studies.csv') import numpy as np from scipy.spatial.distance import cosine def get_most_similar_post(query, post_embeddings, model, post_data): query_embedding = model.encode([query])[0] similarity_scores = [1 - cosine(query_embedding, post_embedding) for post_embedding in post_embeddings] most_similar_index = np.argmax(similarity_scores) return post_data.iloc[most_similar_index] import openai openai.api_key = "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" def generate_answer(query, post_text): response = openai.Completion.create( engine="text-davinci-002", prompt=f"{query} {post_text}", max_tokens=1024, n=1, stop=None, temperature=0.5, ) answer = response["choices"][0]["text"].strip() return answer import gradio as gr def answer_generating_interface(query): result = get_most_similar_post(query, post_embeddings, model, post_data) answer = generate_answer(query, result['Summary']) text = answer.replace("Share\nSave\nHide\n2\nUnfollow\nFollowing\nShare\nSort by Best\n\n", "") text = text.replace("Share", "") return text #iface = gr.Interface(answer_generating_interface, # gr.inputs.Textbox(default=query), gr.outputs.Textbox(), # title="AI Tutor: Improve your AI Quotient", # description="Enter a question around an AI concept and get the answer") iface = gr.Interface(answer_generating_interface, gr.inputs.Textbox(default="How does AirBnB use ML to forecast demand?"), gr.outputs.Textbox(), examples=[['How does Twitter use Machine Learning?', 'What is logistic regression?'], ['How does Uber use ML to forecast demand?', 'What is random forest?']], title="AI Enterprise Explorer: Improve your AI Quotient", description="Enter a question around how a company is using AI and get the answer",allow_flagging="manual",flagging_options=["wrong response", "correct response"]) iface.launch(debug=True)