# 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 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 has been given the task to answer a question based on some of the content you are provided with along with their some relevance scores. Please restrict your knowledge to only the given content and do not add up anything on your own. Also make sure that if the top relevance score is less than 0.25, generate a response that you weren't able to find anything relevant from the knowledge base. Here's the question which you have been asked : {question} Here's the content you are provided with : {content} Here's the maximum relevance score : {score} """ content = "\n-----\n".join([x[0].page_content for x in result]) score = max([x[1] for x in result]) prompt = PromptTemplate.from_template(PROMPT_TEMPLATE) prompt = prompt.format(question = input_query, content = content, score = score) 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()