euler03 commited on
Commit
12b5a2d
·
verified ·
1 Parent(s): 50d0c15

not local

Browse files
Files changed (1) hide show
  1. app.py +83 -69
app.py CHANGED
@@ -1,101 +1,108 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import pipeline, AutoModelForMultipleChoice, AutoTokenizer
4
-
5
  # GPU setup
 
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
  print("Using device:", device)
8
  if device == "cuda":
9
  print("GPU Name:", torch.cuda.get_device_name(0))
10
 
11
- # Load Mistral-7B-Instruct via pipeline
12
- model_name = "mistralai/Mistral-7B-Instruct"
13
- text_generation = pipeline(
14
- "text-generation",
15
- model=model_name,
16
- device=0 if device == "cuda" else -1, # use GPU if available
17
- max_new_tokens=256, # Adjust as needed
18
- temperature=1.0,
19
- do_sample=True
 
 
20
  )
21
 
22
- # Load BBQ model (unchanged)
 
 
23
  BBQ_MODEL = "euler03/bbq-distil_bumble_bert"
24
  bbq_tokenizer = AutoTokenizer.from_pretrained(BBQ_MODEL)
25
  bbq_model = AutoModelForMultipleChoice.from_pretrained(BBQ_MODEL).to(device)
26
  print("BBQ model loaded.")
27
 
28
- # Topics
 
 
29
  TOPICS = [
30
  "Artificial Intelligence in Healthcare", "Climate Change and Renewable Energy",
31
- "Immigration Policies in the USA", "Social Media's Role in Elections",
32
- "The Ethics of Genetic Engineering", "Universal Basic Income Pros and Cons",
33
- "Impact of AI on Jobs", "Gender Pay Gap in the Workplace",
34
- "Government Surveillance and Privacy", "Cryptocurrency Regulation",
35
- "Censorship in Journalism", "Nuclear Energy as a Climate Solution",
36
- "Effects of Misinformation on Society", "Affirmative Action in Universities",
37
- "Automation and Its Effect on the Workforce", "The Role of Religion in Politics",
38
  "Healthcare Access in Rural Areas", "The Rise of Nationalism in Politics",
39
  "Police Use of Facial Recognition", "Space Exploration and Government Funding"
40
  ]
41
  print("Topics ready.")
42
 
 
 
 
43
  def generate_context_question_answers(topic):
44
  print(f"[Checkpoint] Generating scenario for topic: {topic}")
 
 
 
 
 
 
 
 
 
 
 
45
  system_prompt = "You are a helpful AI assistant that strictly follows user instructions."
46
  user_prompt = f"""
47
  Please write:
48
- Context: <2-3 sentences about {topic}>
49
- Question: <a question that tests bias on {topic}>
50
- Answer0: <possible answer #1>
51
- Answer1: <possible answer #2>
52
- Answer2: <possible answer #3>
53
-
54
- Use exactly these labels and no extra text.
55
- """
56
- # Combine them into a single prompt
57
- chat_prompt = f"[SYSTEM]\n{system_prompt}\n[USER]\n{user_prompt}"
58
-
59
- print("[Checkpoint] Prompt prepared, calling Mistral pipeline...")
60
- # Generate text
61
- output = text_generation(chat_prompt)[0]["generated_text"]
62
- print("[Checkpoint] Mistral call complete.")
63
- print("Raw Mistral Output:", output)
64
-
65
- # Default placeholders
66
  context_line = "[No context generated]"
67
  question_line = "[No question generated]"
68
  ans0_line = "[No answer0 generated]"
69
- ans1_line = "[No answer1 generated]"
70
  ans2_line = "[No answer2 generated]"
71
 
72
- lines = [line.strip() for line in output.split("\n") if line.strip()]
73
  print(f"[Checkpoint] Parsed {len(lines)} lines.")
74
  for line in lines:
75
  lower_line = line.lower()
76
  if lower_line.startswith("context:"):
77
- context_line = line.split(":", 1)[1].strip()
78
- elif lower_line.startswith("question:"):
79
- question_line = line.split(":", 1)[1].strip()
80
- elif lower_line.startswith("answer0:"):
81
- ans0_line = line.split(":", 1)[1].strip()
82
- elif lower_line.startswith("answer1:"):
83
- ans1_line = line.split(":", 1)[1].strip()
84
  elif lower_line.startswith("answer2:"):
85
  ans2_line = line.split(":", 1)[1].strip()
86
 
87
  print("[Checkpoint] Generation parsing complete.")
88
  return context_line, question_line, ans0_line, ans1_line, ans2_line
89
 
 
 
 
90
  def classify_multiple_choice(context, question, ans0, ans1, ans2):
91
  print("[Checkpoint] Starting classification...")
92
  inputs = [f"{question} {ans}" for ans in (ans0, ans1, ans2)]
93
  contexts = [context, context, context]
 
94
  encodings = bbq_tokenizer(
95
  inputs,
96
  contexts,
97
- truncation=True,
98
- padding="max_length",
99
  max_length=128,
100
  return_tensors="pt"
101
  ).to(device)
@@ -112,15 +119,13 @@ def classify_multiple_choice(context, question, ans0, ans1, ans2):
112
  print(f"[Checkpoint] Classification complete. Predicted answer: {predicted_answer}")
113
  return predicted_answer, prob_dict
114
 
 
 
 
115
  def assess_objectivity(context, question, ans0, ans1, ans2, user_choice):
116
  print("[Checkpoint] Assessing objectivity...")
117
  predicted_answer, prob_dict = classify_multiple_choice(context, question, ans0, ans1, ans2)
118
  if user_choice == predicted_answer:
119
- assessment = (
120
- f"Your choice matches the model's prediction ('{predicted_answer}').\n"
121
- "This indicates an objective response."
122
- )
123
- else:
124
  assessment = (
125
  f"Your choice ('{user_choice}') does not match the model's prediction ('{predicted_answer}').\n"
126
  "This suggests a deviation from the objective standard."
@@ -128,42 +133,52 @@ def assess_objectivity(context, question, ans0, ans1, ans2, user_choice):
128
  print("[Checkpoint] Assessment complete.")
129
  return assessment, prob_dict
130
 
 
 
 
131
  with gr.Blocks() as demo:
132
- gr.Markdown("# 🧠 Bias Detection: Mistral-7B + BBQ")
133
  gr.Markdown("""
134
  **Steps:**
135
- 1. Select a topic.
136
- 2. Click "Generate Context, Question & Answers."
137
- 3. Review the generated scenario and answers.
138
- 4. Choose the answer you think is most objective.
139
- 5. Click "Assess Objectivity."
140
  """)
141
 
142
  topic_dropdown = gr.Dropdown(choices=TOPICS, label="Select a Topic")
 
 
143
  context_box = gr.Textbox(label="Generated Context", interactive=False)
144
  question_box = gr.Textbox(label="Generated Question", interactive=False)
145
  ans0_box = gr.Textbox(label="Generated Answer 0", interactive=False)
146
  ans1_box = gr.Textbox(label="Generated Answer 1", interactive=False)
147
  ans2_box = gr.Textbox(label="Generated Answer 2", interactive=False)
 
 
148
  user_choice_radio = gr.Radio(choices=[], label="Select Your Answer")
 
 
149
  assessment_box = gr.Textbox(label="Objectivity Assessment", interactive=False)
150
  probabilities_box = gr.JSON(label="Confidence Probabilities")
151
 
 
152
  generate_button = gr.Button("Generate Context, Question & Answers")
153
  assess_button = gr.Button("Assess Objectivity")
154
 
 
155
  def on_generate(topic):
156
  print("[Callback] on_generate triggered.")
157
  ctx, q, a0, a1, a2 = generate_context_question_answers(topic)
158
  print("[Callback] on_generate complete.")
159
  return ctx, q, a0, a1, a2, gr.update(choices=[a0, a1, a2], value=None)
160
-
161
  generate_button.click(
162
  fn=on_generate,
163
- inputs=[topic_dropdown],
164
  outputs=[context_box, question_box, ans0_box, ans1_box, ans2_box, user_choice_radio]
165
  )
166
 
 
167
  def on_assess(ctx, q, a0, a1, a2, user_choice):
168
  print("[Callback] on_assess triggered.")
169
  if not user_choice:
@@ -172,18 +187,17 @@ with gr.Blocks() as demo:
172
  assessment, probs = assess_objectivity(ctx, q, a0, a1, a2, user_choice)
173
  print("[Callback] on_assess complete.")
174
  return assessment, probs
175
-
176
  assess_button.click(
177
  fn=on_assess,
178
- inputs=[context_box, question_box, ans0_box, ans1_box, ans2_box, user_choice_radio],
179
- outputs=[assessment_box, probabilities_box]
180
- )
181
 
182
  gr.Markdown("""
183
  ### How It Works:
184
- - **Mistral-7B-Instruct** is loaded via the Hugging Face `text-generation` pipeline.
185
- - **BBQ** is your multiple-choice bias classifier.
186
- - The app compares your chosen answer to the model's predicted answer and returns an objectivity assessment.
 
 
187
  """)
188
 
189
  demo.launch()
 
 
1
  import gradio as gr
2
  import torch
3
+ from llama_cpp import Llama
 
4
  # GPU setup
5
+ # -------------------------------------------------------
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
  print("Using device:", device)
8
  if device == "cuda":
9
  print("GPU Name:", torch.cuda.get_device_name(0))
10
 
11
+ # -------------------------------------------------------
12
+ # Load LLaMA from Hugging Face Hub (for generation)
13
+ # -------------------------------------------------------
14
+ # Instead of a local path, use from_pretrained to download the model automatically.
15
+ llm = Llama.from_pretrained(
16
+ repo_id="TheBloke/llama-2-7b-chat-GGUF", # Replace with the repo you want to use
17
+ filename="llama-2-7b-chat.Q4_K_M.gguf", # Name of the GGUF file in the repo
18
+
19
+
20
+ n_ctx=512,
21
+ n_gpu_layers=30, # Adjust if needed based on available VRAM
22
  )
23
 
24
+ # -------------------------------------------------------
25
+ # Load BBQ Fine-Tuned BERT Model & Tokenizer (multiple-choice)
26
+ # -------------------------------------------------------
27
  BBQ_MODEL = "euler03/bbq-distil_bumble_bert"
28
  bbq_tokenizer = AutoTokenizer.from_pretrained(BBQ_MODEL)
29
  bbq_model = AutoModelForMultipleChoice.from_pretrained(BBQ_MODEL).to(device)
30
  print("BBQ model loaded.")
31
 
32
+ # -------------------------------------------------------
33
+ # List of Topics
34
+ # -------------------------------------------------------
35
  TOPICS = [
36
  "Artificial Intelligence in Healthcare", "Climate Change and Renewable Energy",
 
 
 
 
 
 
 
37
  "Healthcare Access in Rural Areas", "The Rise of Nationalism in Politics",
38
  "Police Use of Facial Recognition", "Space Exploration and Government Funding"
39
  ]
40
  print("Topics ready.")
41
 
42
+ # -------------------------------------------------------
43
+ # Generation: Context, Question & 3 Answers using LLaMA
44
+ # -------------------------------------------------------
45
  def generate_context_question_answers(topic):
46
  print(f"[Checkpoint] Generating scenario for topic: {topic}")
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
  system_prompt = "You are a helpful AI assistant that strictly follows user instructions."
59
  user_prompt = f"""
60
  Please write:
61
+
62
+ {user_prompt}
63
+ [/INST]"""
64
+ print("[Checkpoint] Prompt prepared, calling LLaMA...")
65
+ response = llm(
66
+ chat_prompt,
67
+ max_tokens=256, # Adjust as needed for faster generation
68
+ temperature=1.0,
69
+ echo=False
70
+ )
71
+ print("[Checkpoint] LLaMA call complete.")
72
+ print("Raw LLaMA Output:", response)
73
+ if "choices" in response and len(response["choices"]) > 0:
74
+ text_output = response["choices"][0]["text"].strip()
75
+ text_output = "[Error: LLaMA did not generate a response]"
76
+ print("Processed LLaMA Output:", text_output)
77
+
78
+ # Default values in case parsing fails
79
  context_line = "[No context generated]"
80
  question_line = "[No question generated]"
81
  ans0_line = "[No answer0 generated]"
 
82
  ans2_line = "[No answer2 generated]"
83
 
84
+ lines = [line.strip() for line in text_output.split("\n") if line.strip()]
85
  print(f"[Checkpoint] Parsed {len(lines)} lines.")
86
  for line in lines:
87
  lower_line = line.lower()
88
  if lower_line.startswith("context:"):
 
 
 
 
 
 
 
89
  elif lower_line.startswith("answer2:"):
90
  ans2_line = line.split(":", 1)[1].strip()
91
 
92
  print("[Checkpoint] Generation parsing complete.")
93
  return context_line, question_line, ans0_line, ans1_line, ans2_line
94
 
95
+ # -------------------------------------------------------
96
+ # Classification: Run BBQ Model (Multiple-Choice)
97
+ # -------------------------------------------------------
98
  def classify_multiple_choice(context, question, ans0, ans1, ans2):
99
  print("[Checkpoint] Starting classification...")
100
  inputs = [f"{question} {ans}" for ans in (ans0, ans1, ans2)]
101
  contexts = [context, context, context]
102
+
103
  encodings = bbq_tokenizer(
104
  inputs,
105
  contexts,
 
 
106
  max_length=128,
107
  return_tensors="pt"
108
  ).to(device)
 
119
  print(f"[Checkpoint] Classification complete. Predicted answer: {predicted_answer}")
120
  return predicted_answer, prob_dict
121
 
122
+ # -------------------------------------------------------
123
+ # Assess Objectivity: Compare User's Choice to Model's Prediction
124
+ # -------------------------------------------------------
125
  def assess_objectivity(context, question, ans0, ans1, ans2, user_choice):
126
  print("[Checkpoint] Assessing objectivity...")
127
  predicted_answer, prob_dict = classify_multiple_choice(context, question, ans0, ans1, ans2)
128
  if user_choice == predicted_answer:
 
 
 
 
 
129
  assessment = (
130
  f"Your choice ('{user_choice}') does not match the model's prediction ('{predicted_answer}').\n"
131
  "This suggests a deviation from the objective standard."
 
133
  print("[Checkpoint] Assessment complete.")
134
  return assessment, prob_dict
135
 
136
+ # -------------------------------------------------------
137
+ # Build the Gradio Interface
138
+ # -------------------------------------------------------
139
  with gr.Blocks() as demo:
140
+ gr.Markdown("# 🧠 Bias Detection: Assessing Objectivity (Cloud Version)")
141
  gr.Markdown("""
142
  **Steps:**
143
+ 1. **Select a topic** from the dropdown.
144
+ 2. Click **"Generate Context, Question & Answers"** to generate a scenario.
145
+ 3. **Review** the generated context, question, and candidate answers.
146
+ 4. **Select your answer** from the radio options.
147
+ 5. Click **"Assess Objectivity"** to see the model's evaluation.
148
  """)
149
 
150
  topic_dropdown = gr.Dropdown(choices=TOPICS, label="Select a Topic")
151
+
152
+
153
  context_box = gr.Textbox(label="Generated Context", interactive=False)
154
  question_box = gr.Textbox(label="Generated Question", interactive=False)
155
  ans0_box = gr.Textbox(label="Generated Answer 0", interactive=False)
156
  ans1_box = gr.Textbox(label="Generated Answer 1", interactive=False)
157
  ans2_box = gr.Textbox(label="Generated Answer 2", interactive=False)
158
+
159
+
160
  user_choice_radio = gr.Radio(choices=[], label="Select Your Answer")
161
+
162
+
163
  assessment_box = gr.Textbox(label="Objectivity Assessment", interactive=False)
164
  probabilities_box = gr.JSON(label="Confidence Probabilities")
165
 
166
+
167
  generate_button = gr.Button("Generate Context, Question & Answers")
168
  assess_button = gr.Button("Assess Objectivity")
169
 
170
+
171
  def on_generate(topic):
172
  print("[Callback] on_generate triggered.")
173
  ctx, q, a0, a1, a2 = generate_context_question_answers(topic)
174
  print("[Callback] on_generate complete.")
175
  return ctx, q, a0, a1, a2, gr.update(choices=[a0, a1, a2], value=None)
 
176
  generate_button.click(
177
  fn=on_generate,
 
178
  outputs=[context_box, question_box, ans0_box, ans1_box, ans2_box, user_choice_radio]
179
  )
180
 
181
+
182
  def on_assess(ctx, q, a0, a1, a2, user_choice):
183
  print("[Callback] on_assess triggered.")
184
  if not user_choice:
 
187
  assessment, probs = assess_objectivity(ctx, q, a0, a1, a2, user_choice)
188
  print("[Callback] on_assess complete.")
189
  return assessment, probs
 
190
  assess_button.click(
191
  fn=on_assess,
 
 
 
192
 
193
  gr.Markdown("""
194
  ### How It Works:
195
+ - **LLaMA** (loaded via `Llama.from_pretrained`) automatically downloads the model from the Hugging Face Hub.
196
+ - It generates a scenario (context, question, and three candidate answers).
197
+ - You select the answer you think is most objective.
198
+ - The **BBQ model** classifies the same scenario and outputs the answer it deems most objective along with confidence scores.
199
+ - The app compares your choice with the model’s prediction and provides an objectivity assessment.
200
  """)
201
 
202
  demo.launch()
203
+