File size: 3,229 Bytes
ac318d7
eb74def
 
1639c56
7b42ba9
ac318d7
c27746e
3594f1d
ac318d7
 
 
fc0f3cf
ac318d7
 
e7c07f7
ac318d7
 
 
 
 
79c523d
fc0f3cf
 
 
79c523d
4b37867
ac318d7
 
 
 
79c523d
ac318d7
 
 
 
 
 
fc0f3cf
ac318d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()