File size: 2,703 Bytes
89915a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3d3104
89915a3
 
 
f3d3104
89915a3
 
fa736bb
89915a3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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)