# importing required libraries from langchain.embeddings import HuggingFaceEmbeddings from langchain.llms import OpenAI from langchain.vectorstores import FAISS from langchain.prompts import PromptTemplate from dotenv import load_dotenv import gradio as gr 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 who knows everything about 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. Here's the question which you have been asked : {question} Here's the content you are provided with : {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()