Spaces:
Sleeping
Sleeping
yash bhaskar
commited on
Commit
·
5fb8f46
1
Parent(s):
b54f15f
Updated UI and Ranker Agent
Browse files- Agents/rankerAgent.py +78 -75
- app.py +86 -21
Agents/rankerAgent.py
CHANGED
|
@@ -2,84 +2,86 @@ import json
|
|
| 2 |
import os
|
| 3 |
from together import Together
|
| 4 |
|
| 5 |
-
def rerank_best_answer(json_files, config_file='config.json', model="meta-llama/Llama-3-
|
| 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 |
-
{json.dumps(prompt, indent=4)}
|
| 38 |
-
|
| 39 |
-
For the above question, identify which model gave the best response based on accuracy. Ensure the chosen response is an answer and not a follow-up question. Provide the output in the format:
|
| 40 |
-
{{
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
}}
|
| 44 |
-
Just output this JSON and nothing else.
|
| 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 |
-
def rankerAgent(prompt,
|
| 83 |
# Load API key from configuration file
|
| 84 |
together_ai_key = os.getenv("TOGETHER_AI")
|
| 85 |
if not together_ai_key:
|
|
@@ -92,7 +94,7 @@ def rankerAgent(prompt, config_file='config.json', model="meta-llama/Meta-Llama-
|
|
| 92 |
prompt_text = f"""Input JSON:
|
| 93 |
{json.dumps(prompt, indent=4)}
|
| 94 |
|
| 95 |
-
For the above question, identify which model gave the best response based on accuracy. Ensure the chosen response is an answer and not a follow-up question. Provide the output in the format:
|
| 96 |
{{
|
| 97 |
"best_model": "<model_name>",
|
| 98 |
"best_answer": "<answer>"
|
|
@@ -100,6 +102,7 @@ For the above question, identify which model gave the best response based on acc
|
|
| 100 |
Just output this JSON and nothing else.
|
| 101 |
"""
|
| 102 |
|
|
|
|
| 103 |
# Generate response from Together API
|
| 104 |
response = client.chat.completions.create(
|
| 105 |
model=model,
|
|
@@ -108,15 +111,15 @@ Just output this JSON and nothing else.
|
|
| 108 |
response_content = response.choices[0].message.content
|
| 109 |
# print(response_content)
|
| 110 |
|
| 111 |
-
prompt_text_extract_bestModel = f"""
|
| 112 |
-
{
|
| 113 |
|
| 114 |
-
|
| 115 |
"""
|
| 116 |
-
prompt_text_extract_bestAnswer = f"""
|
| 117 |
-
{
|
| 118 |
|
| 119 |
-
|
| 120 |
"""
|
| 121 |
response_bestModel = client.chat.completions.create(
|
| 122 |
model=model,
|
|
|
|
| 2 |
import os
|
| 3 |
from together import Together
|
| 4 |
|
| 5 |
+
# def rerank_best_answer(json_files, config_file='config.json', model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"):
|
| 6 |
+
# # Load API key from configuration file
|
| 7 |
|
| 8 |
|
| 9 |
+
# together_ai_key = os.getenv("TOGETHER_AI")
|
| 10 |
+
# if not together_ai_key:
|
| 11 |
+
# raise ValueError("TOGETHER_AI environment variable not found. Please set it before running the script.")
|
| 12 |
|
| 13 |
+
# # Initialize Together client
|
| 14 |
+
# client = Together(api_key=together_ai_key)
|
| 15 |
|
| 16 |
+
# # Combine all JSON files into a single structure
|
| 17 |
+
# combined_prompts = {}
|
| 18 |
+
# for json_file in json_files:
|
| 19 |
+
# with open(json_file, 'r') as file:
|
| 20 |
+
# data = json.load(file)
|
| 21 |
|
| 22 |
+
# # Format the input for the prompt
|
| 23 |
+
# for item in data:
|
| 24 |
+
# query_id = item['query_id']
|
| 25 |
+
# if query_id not in combined_prompts:
|
| 26 |
+
# combined_prompts[query_id] = {
|
| 27 |
+
# "question": item['input'],
|
| 28 |
+
# "answers": {}
|
| 29 |
+
# }
|
| 30 |
+
# combined_prompts[query_id]["answers"][json_file] = item['response']
|
| 31 |
+
|
| 32 |
+
# responses = []
|
| 33 |
+
|
| 34 |
+
# for query_id, prompt in combined_prompts.items():
|
| 35 |
+
# # Generate the prompt text
|
| 36 |
+
# prompt_text = f"""Input JSON:
|
| 37 |
+
# {json.dumps(prompt, indent=4)}
|
| 38 |
+
|
| 39 |
+
# For the above question, identify which model gave the best response based on accuracy. Ensure the chosen response is an answer and not a follow-up question. Provide the output in the format:
|
| 40 |
+
# {{
|
| 41 |
+
# "best_model": "<model_name>",
|
| 42 |
+
# "best_answer": "<answer>"
|
| 43 |
+
# }}
|
| 44 |
+
# Just output this JSON and nothing else.
|
| 45 |
+
# """
|
| 46 |
+
|
| 47 |
+
# # Generate response from Together API
|
| 48 |
+
# response = client.chat.completions.create(
|
| 49 |
+
# model=model,
|
| 50 |
+
# messages=[{"role": "user", "content": prompt_text}],
|
| 51 |
+
# )
|
| 52 |
+
# response_content = response.choices[0].message.content
|
| 53 |
+
# # print(response_content)
|
| 54 |
+
|
| 55 |
+
# prompt_text_extract_bestModel = f"""Content:
|
| 56 |
+
# {response_content}
|
| 57 |
+
|
| 58 |
+
# Whats the best_model from above?
|
| 59 |
+
# """
|
| 60 |
+
# prompt_text_extract_bestAnswer = f"""Content:
|
| 61 |
+
# {response_content}
|
| 62 |
+
|
| 63 |
+
# Whats the best_answer from above?
|
| 64 |
+
# """
|
| 65 |
+
# print(prompt_text_extract_bestModel)
|
| 66 |
+
# print(prompt_text_extract_bestAnswer)
|
| 67 |
+
# response_bestModel = client.chat.completions.create(
|
| 68 |
+
# model=model,
|
| 69 |
+
# messages=[{"role": "user", "content": prompt_text_extract_bestModel}],
|
| 70 |
+
# )
|
| 71 |
+
# response_bestAnswer = client.chat.completions.create(
|
| 72 |
+
# model=model,
|
| 73 |
+
# messages=[{"role": "user", "content": prompt_text_extract_bestAnswer}],
|
| 74 |
+
# )
|
| 75 |
|
| 76 |
+
# # print({"query_id": query_id, "question": prompt["question"], "Ranker_Output": response.choices[0].message.content})
|
| 77 |
+
# responses.append({"query_id": query_id, "question": prompt["question"], "best_model": response_bestModel.choices[0].message.content, "best_answer": response_bestAnswer.choices[0].message.content})
|
| 78 |
|
| 79 |
+
# print(response_bestModel.choices[0].message.content)
|
| 80 |
|
| 81 |
+
# return responses
|
| 82 |
|
| 83 |
|
| 84 |
+
def rankerAgent(prompt, model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"):
|
| 85 |
# Load API key from configuration file
|
| 86 |
together_ai_key = os.getenv("TOGETHER_AI")
|
| 87 |
if not together_ai_key:
|
|
|
|
| 94 |
prompt_text = f"""Input JSON:
|
| 95 |
{json.dumps(prompt, indent=4)}
|
| 96 |
|
| 97 |
+
For the above question, identify which model gave the best response based on accuracy. Ensure the chosen response is an answer and not a follow-up question. The best_answer should be from the best_model only, as given in the above content. Provide the output in the format:
|
| 98 |
{{
|
| 99 |
"best_model": "<model_name>",
|
| 100 |
"best_answer": "<answer>"
|
|
|
|
| 102 |
Just output this JSON and nothing else.
|
| 103 |
"""
|
| 104 |
|
| 105 |
+
|
| 106 |
# Generate response from Together API
|
| 107 |
response = client.chat.completions.create(
|
| 108 |
model=model,
|
|
|
|
| 111 |
response_content = response.choices[0].message.content
|
| 112 |
# print(response_content)
|
| 113 |
|
| 114 |
+
prompt_text_extract_bestModel = f"""Content:
|
| 115 |
+
{response_content}
|
| 116 |
|
| 117 |
+
Whats the best_model from above?
|
| 118 |
"""
|
| 119 |
+
prompt_text_extract_bestAnswer = f"""Content:
|
| 120 |
+
{response_content}
|
| 121 |
|
| 122 |
+
Whats the best_answer from above?
|
| 123 |
"""
|
| 124 |
response_bestModel = client.chat.completions.create(
|
| 125 |
model=model,
|
app.py
CHANGED
|
@@ -51,10 +51,18 @@ def process_query(query):
|
|
| 51 |
tf_idf_ranking_modified, bm25_ranking_modified, open_source_ranking_modified
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
agent2_context = article
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
tf_idf_context = miniWikiCollectionDict[tf_idf_ranking[0]]
|
| 59 |
bm25_context = miniWikiCollectionDict[str(bm25_ranking[0])]
|
| 60 |
vision_context = miniWikiCollectionDict[vision_ranking[0]]
|
|
@@ -136,29 +144,85 @@ def process_query(query):
|
|
| 136 |
zeroShot, "Zero-shot doesn't have a context."
|
| 137 |
)
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# Interface creation
|
| 140 |
def create_interface():
|
| 141 |
with gr.Blocks() as interface:
|
| 142 |
-
|
| 143 |
-
best_model_output = gr.Textbox(label="Best Model", interactive=False)
|
| 144 |
-
best_answer_output = gr.Textbox(label="Best Answer", interactive=False)
|
| 145 |
-
|
| 146 |
-
def create_answer_row(label):
|
| 147 |
with gr.Row():
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
return answer_textbox, context_textbox
|
| 159 |
|
| 160 |
agent1_output, agent1_context_output = create_answer_row("Agent 1")
|
| 161 |
-
|
| 162 |
agent2_output, agent2_context_output = create_answer_row("Agent 2")
|
| 163 |
boolean_output, boolean_context_output = create_answer_row("Boolean")
|
| 164 |
tf_idf_output, tf_idf_context_output = create_answer_row("TF-IDF")
|
|
@@ -170,7 +234,7 @@ def create_interface():
|
|
| 170 |
tf_idf_mod_output, tf_idf_mod_context_output = create_answer_row("TF-IDF (Modified)")
|
| 171 |
bm25_mod_output, bm25_mod_context_output = create_answer_row("BM25 (Modified)")
|
| 172 |
vision_mod_output, vision_mod_context_output = create_answer_row("Vision (Modified)")
|
| 173 |
-
open_source_mod_output,
|
| 174 |
|
| 175 |
tf_idf_rrf_output, tf_idf_rrf_context_output = create_answer_row("TF-IDF + BM25 + Open RRF")
|
| 176 |
tf_idf_rrf_mod_output, tf_idf_rrf_mod_context_output = create_answer_row("TF-IDF + BM25 + Open RRF (Modified)")
|
|
@@ -178,7 +242,7 @@ def create_interface():
|
|
| 178 |
|
| 179 |
zero_shot_output, zero_shot_context_output = create_answer_row("Zero Shot")
|
| 180 |
|
| 181 |
-
|
| 182 |
fn=process_query,
|
| 183 |
inputs=query_input,
|
| 184 |
outputs=[
|
|
@@ -195,7 +259,7 @@ def create_interface():
|
|
| 195 |
tf_idf_mod_output, tf_idf_mod_context_output,
|
| 196 |
bm25_mod_output, bm25_mod_context_output,
|
| 197 |
vision_mod_output, vision_mod_context_output,
|
| 198 |
-
open_source_mod_output,
|
| 199 |
tf_idf_rrf_output, tf_idf_rrf_context_output,
|
| 200 |
tf_idf_rrf_mod_output, tf_idf_rrf_mod_context_output,
|
| 201 |
tf_idf_rrf_combined_output, tf_idf_rrf_combined_context_output,
|
|
@@ -208,4 +272,5 @@ def create_interface():
|
|
| 208 |
# Launch the interface
|
| 209 |
if __name__ == "__main__":
|
| 210 |
interface = create_interface()
|
|
|
|
| 211 |
interface.launch()
|
|
|
|
| 51 |
tf_idf_ranking_modified, bm25_ranking_modified, open_source_ranking_modified
|
| 52 |
)
|
| 53 |
|
| 54 |
+
try:
|
| 55 |
+
agent1_context = wiki_data[0]
|
| 56 |
+
except:
|
| 57 |
+
agent1_context = "Can't find a Wiki article for this query."
|
| 58 |
+
|
| 59 |
agent2_context = article
|
| 60 |
|
| 61 |
+
try:
|
| 62 |
+
boolean_context = miniWikiCollectionDict[boolean_ranking[0]]
|
| 63 |
+
except:
|
| 64 |
+
boolean_context = "Can't find a matching document for this query."
|
| 65 |
+
|
| 66 |
tf_idf_context = miniWikiCollectionDict[tf_idf_ranking[0]]
|
| 67 |
bm25_context = miniWikiCollectionDict[str(bm25_ranking[0])]
|
| 68 |
vision_context = miniWikiCollectionDict[vision_ranking[0]]
|
|
|
|
| 144 |
zeroShot, "Zero-shot doesn't have a context."
|
| 145 |
)
|
| 146 |
|
| 147 |
+
# CSS Styling for the fancy effects
|
| 148 |
+
css = """
|
| 149 |
+
#fancy-column {
|
| 150 |
+
background: linear-gradient(135deg, #1a242f, #2b3a44); /* Dark blue-gray gradient background */
|
| 151 |
+
padding: 20px;
|
| 152 |
+
border-radius: 15px;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
#query-input, #submit-button, #best-model-output, #best-answer-output {
|
| 156 |
+
border-radius: 10px; /* Rounded corners */
|
| 157 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3); /* Darker shadow for better contrast */
|
| 158 |
+
background-color: #34495e; /* Dark background for inputs */
|
| 159 |
+
color: #ecf0f1; /* Light text for good readability */
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
#query-input:focus, #submit-button:focus, #best-model-output:focus, #best-answer-output:focus {
|
| 163 |
+
outline: none;
|
| 164 |
+
border: 2px solid #7f8c8d; /* Subtle accent border on focus */
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
#submit-button {
|
| 168 |
+
background-color: #16a085; /* Muted teal color for button */
|
| 169 |
+
color: #ecf0f1; /* Light text for button */
|
| 170 |
+
font-weight: bold;
|
| 171 |
+
padding: 10px;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
#submit-button:hover {
|
| 175 |
+
background-color: #1abc9c; /* Slightly lighter teal on hover */
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
#best-model-output, #best-answer-output {
|
| 179 |
+
background-color: #2c3e50; /* Darker background for output boxes */
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
#best-model-output label, #best-answer-output label, #query-input label {
|
| 183 |
+
color: #ecf0f1; /* Light text for labels */
|
| 184 |
+
}
|
| 185 |
+
"""
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
|
| 189 |
# Interface creation
|
| 190 |
def create_interface():
|
| 191 |
with gr.Blocks() as interface:
|
| 192 |
+
with gr.Column(elem_id="fancy-column", scale=3): # Fancy column with extra styling
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
with gr.Row():
|
| 194 |
+
query_input = gr.Textbox(label="Enter your query", scale=3, elem_id="query-input")
|
| 195 |
+
submit_button = gr.Button("Submit", scale=1, elem_id="submit-button")
|
| 196 |
+
|
| 197 |
+
# Adjusting the spacing between the output fields
|
| 198 |
+
with gr.Row():
|
| 199 |
+
best_model_output = gr.Textbox(label="Best Model", interactive=False, scale=1.5, elem_id="best-model-output")
|
| 200 |
+
best_answer_output = gr.Textbox(label="Best Answer", interactive=False, scale=1.5, elem_id="best-answer-output")
|
| 201 |
+
|
| 202 |
+
with gr.Column():
|
| 203 |
+
# Function to create a row for answers and contexts
|
| 204 |
+
def create_answer_row(label):
|
| 205 |
+
if label == "Agent 1":
|
| 206 |
+
label = "Wiki Search"
|
| 207 |
+
elif label == "Agent 2":
|
| 208 |
+
label = "Llama Context Generation"
|
| 209 |
+
elif label == "Open Source Answer":
|
| 210 |
+
label = 'MiniLM Text Embedding model'
|
| 211 |
+
elif label == "Open Source (Modified)":
|
| 212 |
+
label = 'MiniLM Text Embedding model (Modified)'
|
| 213 |
+
elif label == "TF-IDF + BM25 + Open RRF":
|
| 214 |
+
label = "RRF (TF-IDF + BM25 + MiniLM)"
|
| 215 |
+
elif label == "TF-IDF + BM25 + Open RRF (Modified)":
|
| 216 |
+
label = "RRF (TF-IDF + BM25 + MiniLM) (Modified)"
|
| 217 |
+
elif label == "TF-IDF + BM25 + Open RRF (Combined)":
|
| 218 |
+
label = "RRF (TF-IDF + BM25 + MiniLM) (Combined)"
|
| 219 |
+
with gr.Row():
|
| 220 |
+
answer_textbox = gr.Textbox(label=f"{label} Answer", interactive=False, scale=1.2, elem_id="best-model-output")
|
| 221 |
+
context_textbox = gr.Textbox(label=f"{label} Context", scale=1.8, elem_id="best-answer-output")
|
| 222 |
+
|
| 223 |
return answer_textbox, context_textbox
|
| 224 |
|
| 225 |
agent1_output, agent1_context_output = create_answer_row("Agent 1")
|
|
|
|
| 226 |
agent2_output, agent2_context_output = create_answer_row("Agent 2")
|
| 227 |
boolean_output, boolean_context_output = create_answer_row("Boolean")
|
| 228 |
tf_idf_output, tf_idf_context_output = create_answer_row("TF-IDF")
|
|
|
|
| 234 |
tf_idf_mod_output, tf_idf_mod_context_output = create_answer_row("TF-IDF (Modified)")
|
| 235 |
bm25_mod_output, bm25_mod_context_output = create_answer_row("BM25 (Modified)")
|
| 236 |
vision_mod_output, vision_mod_context_output = create_answer_row("Vision (Modified)")
|
| 237 |
+
open_source_mod_output, open_source_mod_context_output = create_answer_row("Open Source (Modified)")
|
| 238 |
|
| 239 |
tf_idf_rrf_output, tf_idf_rrf_context_output = create_answer_row("TF-IDF + BM25 + Open RRF")
|
| 240 |
tf_idf_rrf_mod_output, tf_idf_rrf_mod_context_output = create_answer_row("TF-IDF + BM25 + Open RRF (Modified)")
|
|
|
|
| 242 |
|
| 243 |
zero_shot_output, zero_shot_context_output = create_answer_row("Zero Shot")
|
| 244 |
|
| 245 |
+
submit_button.click(
|
| 246 |
fn=process_query,
|
| 247 |
inputs=query_input,
|
| 248 |
outputs=[
|
|
|
|
| 259 |
tf_idf_mod_output, tf_idf_mod_context_output,
|
| 260 |
bm25_mod_output, bm25_mod_context_output,
|
| 261 |
vision_mod_output, vision_mod_context_output,
|
| 262 |
+
open_source_mod_output, open_source_mod_context_output,
|
| 263 |
tf_idf_rrf_output, tf_idf_rrf_context_output,
|
| 264 |
tf_idf_rrf_mod_output, tf_idf_rrf_mod_context_output,
|
| 265 |
tf_idf_rrf_combined_output, tf_idf_rrf_combined_context_output,
|
|
|
|
| 272 |
# Launch the interface
|
| 273 |
if __name__ == "__main__":
|
| 274 |
interface = create_interface()
|
| 275 |
+
interface.css = css
|
| 276 |
interface.launch()
|