# importing required libraries from langchain_community.embeddings import HuggingFaceEmbeddings from langchain_community.llms import OpenAI from langchain_community.vectorstores import FAISS from langchain.prompts import PromptTemplate from dotenv import load_dotenv import gradio as gr import pandas as pd import os # initialising the locally saved vectorstore from artifacts player_names = pd.read_csv("artifacts/data.csv", encoding = "latin-1")["Name"].to_list() model_name = "sentence-transformers/all-mpnet-base-v2" embeddings = HuggingFaceEmbeddings(model_name = model_name) vectorstore = FAISS.load_local("artifacts/FAISS-Vectorstore", embeddings) # creating a generate_response function to take the input query and show the output def generate_response(input_query): result = vectorstore.similarity_search_with_relevance_scores(input_query, k = 4) PROMPT_TEMPLATE = """ Consider yourself to be a football expert bot trained on 35 greatest football players of all time according to "The Guardian", the names of the 35 players are : {names}. Now you have been given the task to answer a question and have also been given some content which you can take help of to generate a proper and much detailed response. You are completely free to elaborate and add more details if they are correct. Please do remember that there are 35 players you have good information about and on which you have been trained on and can list their names if someone needs them. Also, if you feel that the content you have been provided is not relevant to the question asked, answer on the basis of your own knowledge. Here's the question which you have been asked : {question} Here's the helper content which you can use : {content} """ content = "\n-----\n".join([x[0].page_content for x in result]) prompt = PromptTemplate.from_template(PROMPT_TEMPLATE) prompt = prompt.format(question = input_query, content = content, names = player_names) llm = OpenAI(api_key = os.getenv("OPENAI_API_KEY"), temperature = 0.95) response = llm.predict(prompt).strip() return response interface = gr.Interface( fn = generate_response, inputs = gr.Textbox(), outputs = gr.Text(), title = "Football RAG System : Top Footballers' Profiles Powered by RAG", description = "This innovative project reimagines the way we interact with football history. Leveraging the power of AI, it dives deep into the lives of 35 legendary players, starting with \"The Guardian\"'s prestigious list. By extracting and processing Wikipedia content, along with crafting original text, it creates rich profiles teeming with insights. These profiles are then cleverly segmented and stored in a local vectorstore, powered by cutting-edge open-source tools like Hugging Face embeddings and FAISS. This clever setup allows users to ask questions about these footballing greats, with the system efficiently retrieving relevant information and using OpenAI's GPT-3.5 language model to weave a tapestry of personalized responses. It's not just about stats and facts; it's about bringing these legends back to life through the magic of AI-driven storytelling." ) interface.launch()