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""" ### : Given an suitable answer for the question asked. ### : {question} ### : """.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\n' + f'Similarity Score is {round(sim * 100)} %' + '\n\n' + f'The issue is raised for {df["TECHNOLOGY"][answer[i - 1]]}' + '\n\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\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)