File size: 2,567 Bytes
ebd1c37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from transformers import AutoModel
import torch
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer

import gdown
import warnings
import openai
import pandas as pd
import gradio as gr

warnings.filterwarnings("ignore")

openai.api_key = "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"


def generate_prompt(question):
  prompt = f"""
### <instruction>: Given an suitable answer for the question asked.
### <human>: {question}
### <assistant>:
  """.strip()
  return prompt

file_id = '1CjJ-CQhZyr8QowwSksw5uo7O9OYgbq96'

url = f'https://drive.google.com/uc?id={file_id}'

output_file = 'data.xlsx'

gdown.download(url, output_file, quiet=False)

df = pd.read_csv(output_file, encoding='latin-1')

df.head()

sentences = []
for row in df['QUESTION']:
    sentences.append(row)


model_encode = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
embeddings = model_encode.encode(sentences)

answer = []
for index, val in enumerate(df['ORIGINAL/SYNONYM']):
    if str(val) == "Original":
        answer.append(index)

def answer_prompt(text):

  ind, sim = 0, 0
  bot_response = ''
  text_embedding = model_encode.encode(text)
  for index, val in enumerate(embeddings):
    res = cosine_similarity(text_embedding.reshape(1,-1),embeddings[index].reshape(1,-1))
    if res[0][0] > sim:
      sim = res[0][0]
      ind = index

  for i in range(len(answer)):
    if answer[i] > ind:
      bot_response =  bot_response =  'This Solution is Extracted from the Database' + '\n' + f'Similarity Score is {round(sim * 100)} %' + '\n' + f'The issue is raised for {df["TECHNOLOGY"][answer[i - 1]]}' + '\n' + df['SOLUTION'][answer[i - 1]]
      break

  if sim > 0.5:
    return bot_response

  else:

    prompt = generate_prompt(text)
    response = openai.Completion.create(
                     engine="gpt-3.5-turbo-instruct",
                     prompt = prompt,
                     max_tokens = 1024,
                     top_p = 0.7,
                     temperature = 0.3,
                     presence_penalty = 0.7,
                  )

    return 'This response is generated by GPT 3.5 Turbo LLM' + '\n' + response['choices'][0]['text']

iface = gr.Interface(fn=answer_prompt, 
                     inputs=gr.Textbox(lines=10, label="Enter Your Issue", css={"font-size":"18px"}),
                     outputs=gr.Textbox(lines=10, label="Generated Solution", css={"font-size":"16px"}))

iface.launch(inline=False)