rach405's picture
Add application file
0fd4904
Raw
History Blame Contribute Delete
6.12 kB
import gradio as gr
import pandas as pd
import re
import openai
from openai.embeddings_utils import cosine_similarity
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
)
openai.api_key = "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"
def get_ada_embedding(model, text):
result = openai.Embedding.create(
model = model,
input = text
)
return result['data'][0]['embedding']
def get_request(row):
request = 'Question: '+ row[0] + ' Answer: ' + row[1]
request = re.sub(r',+\s*,', ', ', request)
return(request.replace('’', ""))
def find_in_csv(csv_path):
data = pd.read_csv(csv_path, error_bad_lines=False)
data = data.fillna("")
data['Answer'] = data.iloc[:, 1:].apply(lambda x: ','.join(x), axis=1)
data = data[['Question', 'Answer']]
data = data[(data['Question'] != "") & (data['Answer'] != "")]
data['text'] = data.apply(lambda x: get_request(x), axis=1, result_type='expand')
data = data[['text']]
return data
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(3))
def get_result_by_prompt(prompt):
completion = openai.ChatCompletion.create(
model= "gpt-4", #"gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens = 300,
temperature = 0.1
)
res = completion['choices'][0]["message"]["content"]
if(res == ""):
print(completion['choices'][0]["finish_reason"])
return(res)
def get_summary_prompt(text):
summary_prompt = f"""On the given text: Translate the text to English if needed, Clean it of non-informative envelopes like: sender's signature or details, company signature, contact information, and general links/messages like 'Please do not reply to this email' or "for more information or help please contact..".
Replace user private details (user name, password, phone number, email, address) with generic strings, for example, replace password TF54d with the string "user_pass", replace user-name with "user_name", replace user phone number with "user_phone_number" etc.
Then describe it in a form of a request or question and answer. The request or question must be a possible issue an end user could face in the organization's IT. The answer should be the best possible solution to such an issue.
You have to use all the technical information that is available such as product names, equipment models, versions, and error messages.
Avoid returning user private details in your answer. Given text: {text}
Return the output in the following format:
Request/Question:
Answer:
"""
return summary_prompt
def get_qa_prompt(context, text):
qa_prompt = f"""You are an a technical support
The user is an employee.
Please provide a chat response for a user requesting assistance regarding questions/issues.
Please help the end user solve the issue by himself/herself.
If an action requires human assistance, please tell the user to open a service request by clicking the "Open Service Request" button located on the chat page.
You can offer the user to schedule a technician visit or a remote access session to solve the problem on site. In that case, just ask the user to provide some time slots for such visit, but do not choose specific time slot. Instead, tell the user to a open service request.
Please find below some related answers for past issues. Please use that data to formulate more accurate and detailed answers
to the current issue discussed.
Text: {text}
RelatedText: {context}
"""
return qa_prompt
def process_data(files,url_paths):
total_data = pd.DataFrame(columns=['text'])
for f in files:
file_path = f.name
if(file_path.split('.')[-1] == 'csv'):
csv_data = find_in_csv(file_path)
csv_data=csv_data[:5]
total_data = total_data.append(csv_data, ignore_index=True)
if(file_path.split('.')[-1] == 'pdf'):
pass
for url in url_paths:
print(url)
total_data['text'] = total_data['text'].apply(lambda x: get_result_by_prompt(get_summary_prompt(x)) )
total_data['embedding'] = total_data['text'].apply(lambda x: get_ada_embedding('text-embedding-ada-002',x))
total_data.to_csv("processed_df.csv")
return("Data was Processed")
def str_to_float_list(string):
return [float(val) for val in string.strip('[]').split(', ')]
def answer_by_context(question):
try:
data = pd.read_csv("processed_df.csv")
except:
return("No Context Provided", "")
if(len(data) == 0):
return("No Context Provided", "")
question_embbeding = get_ada_embedding('text-embedding-ada-002', question)
data['embedding'] = data['embedding'].apply(str_to_float_list)
data['similarity'] = data['embedding'].apply(lambda x: cosine_similarity(x, question_embbeding))
data = data[['text','similarity']].drop_duplicates()
data = data.sort_values(by=['similarity'], ascending=False)
data_context = data[data['similarity'] > 0.75]
if(len(data_context) == 0):
return ("No related Context Provided", "")
context = '\n'.join(data_context['text'])
answer = get_result_by_prompt(get_qa_prompt(context,question))
return answer, context
int1 = gr.Interface(process_data,
inputs = [gr.File(label="Upload files", file_count="multiple", file_types=[".txt", ".pdf", ".csv"], height = 150),
gr.inputs.Textbox(label="Enter URLs:")],
outputs=gr.outputs.Textbox(label="Processing State:"), title = "QA Checking System: Load Your Data",allow_flagging="never")
int2 = gr.Interface(answer_by_context,
inputs = [gr.inputs.Textbox(label="Enter your question: ")],
outputs=[gr.outputs.Textbox(label="Answer:"),gr.outputs.Textbox(label="Context:")],
title = "QA Checking System: Ask Question", allow_flagging="never")
demo = gr.TabbedInterface( [int1, int2], ["Load Data", "Question-Answer"],css=".gradio-container {background-color: #009973}")
demo.launch(share=True)