Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -94,108 +94,106 @@ def generate_score_bar(score, num_criteria):
|
|
94 |
<p style="color: {color};">{text}</p> <!-- Display the text -->
|
95 |
"""
|
96 |
return score_bar_html
|
97 |
-
|
98 |
-
"""
|
99 |
-
Formats a few-shot example into a string.
|
100 |
-
Args:
|
101 |
-
example (dict): A dictionary containing 'query', 'score', and 'reasoning' for the few-shot example.
|
102 |
-
Returns:
|
103 |
-
str: Formatted few-shot example text.
|
104 |
-
"""
|
105 |
-
return "Example:\nQuery: {}\n Direct Answer: {}\n".format(
|
106 |
-
example['query'], example['Answer'])
|
107 |
|
108 |
-
def process_pdf(
|
109 |
-
#
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
|
129 |
|
130 |
-
|
131 |
temperature=model_temperature, model_name="mistralai/Mixtral-8x7B-Instruct-v0.1", api_key=hf_token)
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
|
149 |
-
|
150 |
-
|
151 |
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
|
157 |
|
158 |
-
|
159 |
-
|
160 |
|
161 |
-
|
162 |
-
|
163 |
|
164 |
|
165 |
-
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
|
173 |
|
174 |
-
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
|
181 |
-
|
182 |
|
183 |
-
|
184 |
-
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
|
191 |
-
|
192 |
-
|
193 |
|
194 |
-
|
195 |
-
|
196 |
|
197 |
-
|
198 |
-
|
199 |
|
200 |
|
201 |
with gr.Blocks(theme=gr.themes.Glass(
|
|
|
94 |
<p style="color: {color};">{text}</p> <!-- Display the text -->
|
95 |
"""
|
96 |
return score_bar_html
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
def process_pdf(uploaded_files, llm_model, n_criteria = num_criteria):
|
100 |
+
# Initialize aggregation variables
|
101 |
+
final_score = 0
|
102 |
+
final_reasoning = []
|
103 |
+
final_score_bar_html = ""
|
104 |
+
final_author_info_html = ""
|
105 |
+
final_title_info_html = ""
|
106 |
+
for uploaded_file in uploaded_files:
|
107 |
+
# Process the PDF file
|
108 |
+
pdf_processor = PDFProcessor_Unstructured(pdf_processing_config)
|
109 |
+
merged_chunks, tables, title = pdf_processor.process_pdf_file(uploaded_file)
|
110 |
+
documents = [Document(text=t) for t in merged_chunks]
|
111 |
+
|
112 |
+
# Prompts and Queries
|
113 |
+
utils = base_utils()
|
114 |
|
115 |
+
info_prompt = utils.read_from_file(info_prompt_path)
|
116 |
|
117 |
+
# LLM Model choice
|
118 |
+
try:
|
119 |
+
if llm_model == "Model 1":
|
120 |
+
llm = OpenAI(model="gpt-4-1106-preview", temperature=model_temperature, max_tokens=output_token_size)
|
121 |
+
general_prompt = utils.read_from_file(gpt_prompt_path)
|
122 |
|
123 |
+
elif llm_model == "Model 2":
|
124 |
+
if any(param is None for param in [model_context_window, output_token_size, model_temperature, hf_token]):
|
125 |
+
raise ValueError("All parameters are required for Mistral LLM.")
|
126 |
|
127 |
|
128 |
+
llm = MixtralLLM(context_window=model_context_window, num_output=output_token_size,
|
129 |
temperature=model_temperature, model_name="mistralai/Mixtral-8x7B-Instruct-v0.1", api_key=hf_token)
|
130 |
+
general_prompt = utils.read_from_file(mistral_prompt_path)
|
131 |
+
else:
|
132 |
+
raise ValueError(f"Unsupported language model: {llm_model}")
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
logger.error(f"Error initializing language model '{llm_model}': {e}", exc_info=True)
|
136 |
+
raise # Or handle the exception as needed
|
137 |
+
|
138 |
+
# Embedding model choice for RAG
|
139 |
+
try:
|
140 |
+
if embed == "openai":
|
141 |
+
embed_model = OpenAIEmbedding(model="text-embedding-3-large")
|
142 |
+
|
143 |
+
elif embed == "huggingface":
|
144 |
+
# Use the specified model name
|
145 |
+
embed_model = HuggingFaceEmbedding(embed_model_name)
|
146 |
|
147 |
+
else:
|
148 |
+
raise ValueError(f"Unsupported embedding model: {embed_model}")
|
149 |
|
150 |
|
151 |
+
except Exception as e:
|
152 |
+
logger.error(f"Error initializing embedding model: {e}", exc_info=True)
|
153 |
+
raise
|
154 |
|
155 |
|
156 |
+
peer_review_journals = utils.read_from_file(peer_review_journals_path)
|
157 |
+
eq_network_journals = utils.read_from_file(eq_network_journals_path)
|
158 |
|
159 |
+
peer_review_journals_list = peer_review_journals.split('\n')
|
160 |
+
eq_network_journals_list = eq_network_journals.split('\n')
|
161 |
|
162 |
|
163 |
+
modified_journal_query = "Is the given research paper published in any of the following journals: " + ", ".join(peer_review_journals_list) + "?"
|
164 |
|
165 |
+
info_llm = OpenAI(model="gpt-4-1106-preview", temperature=model_temperature, max_tokens=100)
|
166 |
+
pdf_info_query = PDFQueryEngine(documents, info_llm, embed_model, (info_prompt))
|
167 |
+
info_query_engine = pdf_info_query.setup_query_engine()
|
168 |
+
journal_result = info_query_engine.query(modified_journal_query).response
|
169 |
+
author_result = info_query_engine.query(author_query).response
|
170 |
|
171 |
|
172 |
+
pdf_criteria_query = PDFQueryEngine(documents, llm, embed_model, (general_prompt))
|
173 |
|
174 |
+
# Check for prior registration
|
175 |
+
nlp_methods = KeywordSearch(merged_chunks)
|
176 |
+
eq_journal_result = nlp_methods.find_journal_name(journal_result, eq_network_journals_list)
|
177 |
+
peer_journal_result = nlp_methods.find_journal_name(journal_result, peer_review_journals_list)
|
178 |
|
179 |
+
registration_result = nlp_methods.check_registration()
|
180 |
|
181 |
+
# Evaluate with OpenAI model
|
182 |
+
total_score, criteria_met, score_percentage, reasoning = pdf_criteria_query.evaluate_with_llm(registration_result, peer_journal_result, eq_journal_result, queries)
|
183 |
|
184 |
+
reasoning_html = "<ul>"
|
185 |
+
for query, reason in zip(criteria, reasoning):
|
186 |
+
reasoning_html += f"<li style='font-size: 18px;'><strong style='color: forestgreen;'>{query}</strong> <br> Reasoning: {reason}</li>"
|
187 |
+
reasoning_html += "</ul>"
|
188 |
|
189 |
+
# Generate the score bar HTML
|
190 |
+
score_bar_html = generate_score_bar(total_score, n_criteria)
|
191 |
|
192 |
+
author_info_html = f"<div style='font-size: 18px;'>{author_result}</div>"
|
193 |
+
title_info_html = f"<div style='font-size: 20px;'>{title}</div>"
|
194 |
|
195 |
+
# Return the score as a string and the reasoning as HTML
|
196 |
+
return str(round((total_score / n_criteria) * 100)) + "/100", score_bar_html, reasoning_html, author_info_html, title_info_html
|
197 |
|
198 |
|
199 |
with gr.Blocks(theme=gr.themes.Glass(
|