Presidentlin commited on
Commit
8bbf037
·
1 Parent(s): 76ed6d2
Files changed (2) hide show
  1. __pycache__/main.cpython-310.pyc +0 -0
  2. main.py +46 -38
__pycache__/main.cpython-310.pyc CHANGED
Binary files a/__pycache__/main.cpython-310.pyc and b/__pycache__/main.cpython-310.pyc differ
 
main.py CHANGED
@@ -5,6 +5,7 @@ import time
5
  from concurrent.futures import ThreadPoolExecutor, as_completed
6
  import threading
7
  import streamlit as st # Import Streamlit
 
8
 
9
 
10
  def generate_answer(question, previous_answers, model_name, open_router_key, openai_api_key):
@@ -35,9 +36,8 @@ def evaluate_answer(question, new_answer, open_router_key, openai_api_key):
35
  return None
36
 
37
 
38
- def process_question(question, model_name, open_router_key, openai_api_key, progress_lock, completed_questions, total_questions, progress):
39
  start_time = time.time()
40
- st.write(f"<span style='color:red'>{question}</span>", unsafe_allow_html=True)
41
  previous_answers = []
42
  question_novelty = 0
43
 
@@ -52,39 +52,38 @@ def process_question(question, model_name, open_router_key, openai_api_key, prog
52
  break
53
 
54
  if coherence_score <= 3:
55
- st.write("<span style='color:yellow'>Output is incoherent. Moving to next question.</span>",
56
- unsafe_allow_html=True)
57
  break
58
 
59
  novelty_score = get_novelty_score(new_answer, previous_answers, openai_api_key)
60
 
61
  if novelty_score < 0.1:
62
- st.write("<span style='color:yellow'>Output is redundant. Moving to next question.</span>",
63
- unsafe_allow_html=True)
64
  break
65
 
66
- st.write(f"**New Answer:**\n{new_answer}")
67
- st.write(f"<span style='color:green'>Coherence Score: {coherence_score}</span>",
68
- unsafe_allow_html=True)
69
- st.write(f"**Novelty Score:** {novelty_score}")
 
 
 
 
70
 
71
  previous_answers.append(new_answer)
72
  question_novelty += novelty_score
73
 
74
  except Exception as e:
75
- st.write(f"<span style='color:red'>Unexpected error processing question: {str(e)}</span>",
76
- unsafe_allow_html=True)
77
 
78
  time_taken = time.time() - start_time
79
- st.write(f"<span style='color:blue'>Total novelty score for this question: {question_novelty}</span>",
80
- unsafe_allow_html=True)
81
- st.write(f"<span style='color:blue'>Time taken: {time_taken} seconds</span>",
82
- unsafe_allow_html=True)
 
 
83
 
84
- # Update progress
85
- with progress_lock:
86
- completed_questions += 1
87
- progress = completed_questions / total_questions
88
 
89
  return question_novelty, [
90
  {
@@ -117,11 +116,10 @@ def get_novelty_score(new_answer: str, previous_answers: list, openai_api_key):
117
  return novelty
118
 
119
 
120
- def benchmark_model_multithreaded(model_name, questions, open_router_key, openai_api_key, max_threads=None, progress=0, progress_lock=None):
121
  novelty_score = 0
122
- print_lock = threading.Lock() # Lock for thread-safe printing
123
  results = []
124
- completed_questions = 0 # Shared variable to track progress
125
 
126
  # Use max_threads if provided, otherwise default to the number of questions
127
  if max_threads is None:
@@ -130,23 +128,33 @@ def benchmark_model_multithreaded(model_name, questions, open_router_key, openai
130
  max_workers = max_threads
131
 
132
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
133
- future_to_question = {executor.submit(
134
- process_question, question, model_name, open_router_key, openai_api_key, progress_lock, completed_questions, len(questions), progress): question for question in questions}
135
-
136
- for future in as_completed(future_to_question):
137
- question = future_to_question[future]
138
 
 
 
139
  try:
140
- question_novelty, question_results = future.result()
141
- with print_lock:
142
- novelty_score += question_novelty
143
- results.extend(question_results)
144
- st.write(
145
- f"<span style='color:yellow'>Total novelty score across all questions (so far): {novelty_score}</span>",
146
- unsafe_allow_html=True)
147
- except Exception as e:
148
- with print_lock:
149
- st.write(f"<span style='color:red'>Error in thread: {str(e)}</span>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
150
 
151
  st.write(f"<span style='color:yellow'>Final total novelty score across all questions: {novelty_score}</span>",
152
  unsafe_allow_html=True)
 
5
  from concurrent.futures import ThreadPoolExecutor, as_completed
6
  import threading
7
  import streamlit as st # Import Streamlit
8
+ import queue
9
 
10
 
11
  def generate_answer(question, previous_answers, model_name, open_router_key, openai_api_key):
 
36
  return None
37
 
38
 
39
+ def process_question(question, model_name, open_router_key, openai_api_key, result_queue):
40
  start_time = time.time()
 
41
  previous_answers = []
42
  question_novelty = 0
43
 
 
52
  break
53
 
54
  if coherence_score <= 3:
55
+
 
56
  break
57
 
58
  novelty_score = get_novelty_score(new_answer, previous_answers, openai_api_key)
59
 
60
  if novelty_score < 0.1:
 
 
61
  break
62
 
63
+ # Append results to the queue instead of using st.write
64
+ result_queue.put({
65
+ "type": "answer",
66
+ "question": question,
67
+ "answer": new_answer,
68
+ "coherence_score": coherence_score,
69
+ "novelty_score": novelty_score
70
+ })
71
 
72
  previous_answers.append(new_answer)
73
  question_novelty += novelty_score
74
 
75
  except Exception as e:
76
+ result_queue.put({"type": "error", "message": str(e)})
77
+
78
 
79
  time_taken = time.time() - start_time
80
+ result_queue.put({
81
+ "type": "summary",
82
+ "question": question,
83
+ "total_novelty": question_novelty,
84
+ "time_taken": time_taken
85
+ })
86
 
 
 
 
 
87
 
88
  return question_novelty, [
89
  {
 
116
  return novelty
117
 
118
 
119
+ def benchmark_model_multithreaded(model_name, questions, open_router_key, openai_api_key, max_threads=None):
120
  novelty_score = 0
 
121
  results = []
122
+ result_queue = queue.Queue() # Create a queue for communication
123
 
124
  # Use max_threads if provided, otherwise default to the number of questions
125
  if max_threads is None:
 
128
  max_workers = max_threads
129
 
130
  with ThreadPoolExecutor(max_workers=max_workers) as executor:
131
+ # Submit tasks to the thread pool
132
+ future_to_question = {
133
+ executor.submit(process_question, question, model_name, open_router_key, openai_api_key, result_queue): question
134
+ for question in questions
135
+ }
136
 
137
+ # Process results from the queue in the main thread
138
+ while True:
139
  try:
140
+ result = result_queue.get_nowait()
141
+ if result["type"] == "answer":
142
+ st.write(f"**Question:** {result['question']}")
143
+ st.write(f"**New Answer:**\n{result['answer']}")
144
+ st.write(f"<span style='color:green'>Coherence Score: {result['coherence_score']}</span>",
145
+ unsafe_allow_html=True)
146
+ st.write(f"**Novelty Score:** {result['novelty_score']}")
147
+ elif result["type"] == "summary":
148
+ st.write(f"<span style='color:blue'>Total novelty score for question '{result['question']}': {result['total_novelty']}</span>",
149
+ unsafe_allow_html=True)
150
+ st.write(f"<span style='color:blue'>Time taken: {result['time_taken']} seconds</span>",
151
+ unsafe_allow_html=True)
152
+ elif result["type"] == "error":
153
+ st.write(f"<span style='color:red'>Error in thread: {result['message']}</span>",
154
+ unsafe_allow_html=True)
155
+ except queue.Empty:
156
+ if not any(future.running() for future in future_to_question.keys()):
157
+ break # All tasks are done
158
 
159
  st.write(f"<span style='color:yellow'>Final total novelty score across all questions: {novelty_score}</span>",
160
  unsafe_allow_html=True)