File size: 3,992 Bytes
71e2c65
1112e46
 
95bfa0d
e8e78ae
5a44b64
9a8353d
 
a46269a
411adbd
61bb151
5fed436
9a8353d
59284b1
4c66f77
1112e46
 
 
9a8353d
 
a46269a
411adbd
 
9a8353d
 
 
 
 
4c66f77
 
71e2c65
95bfa0d
9a8353d
95bfa0d
9a8353d
 
 
 
1432cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a46269a
1432cc9
 
a46269a
1432cc9
411adbd
1432cc9
 
 
411adbd
5633e27
 
61bb151
1432cc9
 
 
9a8353d
1432cc9
 
 
 
 
 
 
61bb151
1432cc9
5fed436
1432cc9
 
5633e27
1432cc9
5fed436
1432cc9
 
5633e27
 
1432cc9
 
5633e27
 
95bfa0d
1432cc9
5633e27
 
71e2c65
1432cc9
5633e27
71e2c65
1432cc9
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import gradio as gr
import os

from loaddataset import ExtractRagBenchData
from createmilvusschema import CreateMilvusDbSchema
from insertmilvushelper import EmbedAllDocumentsAndInsert
from sentence_transformers import SentenceTransformer
from searchmilvushelper import SearchTopKDocuments
from finetuneresults import FineTuneAndRerankSearchResults
from generationhelper import GenerateAnswer
from formatresultshelper import FormatAndScores
from calculatescores import CalculateScores

from huggingface_hub import login
from huggingface_hub import whoami
from huggingface_hub import dataset_info


# Load embedding model
QUERY_EMBEDDING_MODEL = SentenceTransformer('all-MiniLM-L6-v2')
RERANKING_MODEL = "cross-encoder/ms-marco-MiniLM-L-6-v2"
PROMPT_MODEL = "llama-3.3-70b-specdec"
EVAL_MODEL = "llama-3.3-70b-specdec"
WINDOW_SIZE = 5
OVERLAP = 2
RETRIVE_TOP_K_SIZE=10


hf_token = os.getenv("HF_TOKEN")
login(hf_token)

rag_extracted_data = ExtractRagBenchData()
print(rag_extracted_data.head(5))

"""
EmbedAllDocumentsAndInsert(QUERY_EMBEDDING_MODEL, rag_extracted_data, db_collection, window_size=WINDOW_SIZE, overlap=OVERLAP)
"""  

def EvaluateRAGModel(query, evaluation_model):
    #invoke create milvus db function
    try:
        db_collection = CreateMilvusDbSchema()
    except Exception as e:
        print(f"Error creating Milvus DB schema: {e}")

    #insert embdeding to milvus db

    #query = "what would the net revenue have been in 2015 if there wasn't a stipulated settlement from the business combination in october 2015?"

    results_for_top10_chunks = SearchTopKDocuments(db_collection, query, QUERY_EMBEDDING_MODEL, top_k=RETRIVE_TOP_K_SIZE)

    reranked_results = FineTuneAndRerankSearchResults(results_for_top10_chunks, rag_extracted_data, query, RERANKING_MODEL)

    answer = GenerateAnswer(query, reranked_results.head(3), PROMPT_MODEL)

    completion_result,relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level = FormatAndScores(query, reranked_results.head(1), answer, EVAL_MODEL)


    print(relevant_sentence_keys)
    print(all_utilized_sentence_keys)
    print(support_keys)
    print(support_level)
    print(completion_result)

    document_id = reranked_results.head(1)['doc_id'].values[0]
    extarcted_row_for_given_id = rag_extracted_data[rag_extracted_data["id"]==document_id]

    rmsecontextrel, rmsecontextutil, aucscore = CalculateScores(relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level,extarcted_row_for_given_id)

    print(rmsecontextrel)
    print(rmsecontextutil)
    print(aucscore)

    return answer, rmsecontextrel, rmsecontextutil, aucscore


# Create Gradio UI
with gr.Blocks() as iface:
    gr.Markdown("## Capstone Project Group 10 - Model Evaluation")

    with gr.Row():
        question_input = gr.Textbox(label="Enter your Question", lines=2)
        dropdown_input = gr.Dropdown(
            ["LLaMA 3.3", "Mistral &B", "Model C"], 
            value="LLaMA 3.3", 
            label="Select a Model"
        )

    submit_button = gr.Button("Evaluate Model")

    with gr.Row():
        with gr.Column():
            gr.Markdown("### Response")
            response = gr.Textbox(interactive=False, show_label=False, lines=2)

    with gr.Row():
        with gr.Column():
            gr.Markdown("### RMSE-CONTEXT RELEVANCE")
            rmsecontextrel = gr.Textbox(interactive=False, show_label=False, lines=2)
    
        with gr.Column():
            gr.Markdown("### RMSE-CONTEXT UTILIZATION")
            rmsecontextutil = gr.Textbox(interactive=False, show_label=False, lines=2)

        with gr.Column():
            gr.Markdown("### AUCROC ADHERENCE")
            aucscore = gr.Textbox(interactive=False, show_label=False, lines=2)

    # Connect submit button to evaluation function
    submit_button.click(EvaluateRAGModel, inputs=[question_input, dropdown_input], outputs=[response, rmsecontextrel, rmsecontextutil, aucscore])

# Run the Gradio app
iface.launch()