goliath-chatbot / app_old.py
Michelangiolo's picture
changes
cf38d1a
import requests
import os
os.system('pip install openpyxl')
os.system('pip install sentence-transformers')
def chatgpt3_question(context, question):
api_key = "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"
url = "https://api.openai.com/v1/chat/completions"
prompt = f"""
based on this context: {context}
answer this use question: {question}
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": prompt}]
}
response = requests.post(url, headers=headers, json=data)
generated_text = response.json()['choices'][0]['message']['content']
return generated_text
import os
import requests
import pandas as pd
def text2vec(query):
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + "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",
}
json_data = {
'input': query,
'model': 'text-embedding-ada-002',
}
response = requests.post('https://api.openai.com/v1/embeddings', headers=headers, json=json_data)
query = response.json()['data'][0]['embedding'] #len=1536 #pricing=0.0004
return query
import pandas as pd
from sentence_transformers import SentenceTransformer, util
df = pd.read_parquet('df.parquet')
df_qa = pd.read_parquet('df_qa.parquet')
df_qa_ = df_qa.copy()
df_ = df.copy()
def qa(df_, df_qa_, min_qa_score, min_context_score, verbose, query):
query_vec = text2vec(query)
#first check if there is already a question in df_qa
df_qa_['score'] = df_qa_['text_vector_'].apply(lambda x : float(util.cos_sim(x, query_vec)))
df_qa_ = df_qa_.sort_values('score', ascending=False)
df_qa_ = df_qa_[df_qa_['score']>=min_qa_score]
#if we find at least one possible preset answer
if len(df_qa_) > 0:
if verbose : display(df_qa_)
answer = df_qa_[0:1]['answer'].values.tolist()[0]
return answer
#then check if we can use the context to answer a question
df_['score'] = df_['text_vector_'].apply(lambda x : float(util.cos_sim(x, query_vec)))
df_ = df_.sort_values('score', ascending=False)
df_ = df_[df_['score']>=min_context_score]
#if we find at least one possible preset answer
if len(df_) > 0:
if verbose : display(df_)
#in case we might decide to merge multiple context
context = ' '.join(df_['description'][0:1].values.tolist())
answer = chatgpt3_question(context, query)
return answer
else:
return 'impossible to give an answer'
# print(
# qa(
# df_,
# df_qa_,
# min_qa_score=0.92,
# min_context_score=.75,
# verbose=False,
# query='What is a recommender system?'
# )
# )
import subprocess
import random
import gradio as gr
import requests
history = None
history_prompt = None
def predict(input, history):
#WE CAN PLAY WITH user_input AND bot_answer, as well as history
user_input = input
global history_prompt
global block_predict
bot_answer = qa(
df_,
df_qa_,
min_qa_score=0.92,
min_context_score=.75,
verbose=False,
query=input
)
response = list()
response = [(input, bot_answer)]
history.append(response[0])
response = history
# print('#history', history)
# print('#response', response)
return response, history
demo = gr.Blocks()
with demo:
gr.Markdown(
"""
Chatbot
"""
)
state = gr.Variable(value=[]) #beginning
chatbot = gr.Chatbot() #color_map=("#00ff7f", "#00d5ff")
text = gr.Textbox(
label="Question",
value="What is a recommendation system?",
placeholder="",
max_lines=1,
)
text.submit(predict, [text, state], [chatbot, state])
text.submit(lambda x: "", text, text)
# btn = gr.Button(value="submit")
# btn.click(chatbot_foo, None, [chatbot, state])
demo.launch(share=False)