Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,18 +3,17 @@ import torch
|
|
| 3 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
import random
|
| 5 |
|
| 6 |
-
# Load GPT-2 model and tokenizer
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
try:
|
| 10 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 11 |
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
| 12 |
-
# Set pad token if not already set
|
| 13 |
if tokenizer.pad_token is None:
|
| 14 |
tokenizer.pad_token = tokenizer.eos_token
|
| 15 |
return tokenizer, model
|
| 16 |
except Exception as e:
|
| 17 |
-
st.error(f"
|
| 18 |
return None, None
|
| 19 |
|
| 20 |
tokenizer, model = load_model()
|
|
@@ -22,70 +21,80 @@ tokenizer, model = load_model()
|
|
| 22 |
# Honest AI response logic
|
| 23 |
def honest_ai_response(user_input):
|
| 24 |
if tokenizer is None or model is None:
|
| 25 |
-
return "
|
| 26 |
-
|
| 27 |
try:
|
| 28 |
prompt = f"Provide a factual and balanced answer to: {user_input}"
|
| 29 |
-
inputs = tokenizer.encode(prompt, return_tensors="pt"
|
| 30 |
attention_mask = torch.ones(inputs.shape, dtype=torch.long)
|
| 31 |
-
|
| 32 |
outputs = model.generate(
|
| 33 |
inputs,
|
| 34 |
-
max_length=150,
|
| 35 |
temperature=0.7,
|
| 36 |
top_k=50,
|
| 37 |
num_return_sequences=1,
|
| 38 |
pad_token_id=tokenizer.eos_token_id,
|
| 39 |
attention_mask=attention_mask,
|
| 40 |
-
do_sample=True,
|
| 41 |
-
no_repeat_ngram_size=2
|
| 42 |
)
|
| 43 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 44 |
-
|
| 45 |
-
return answer if answer else "I don’t have enough information to answer definitively, but I can provide a general perspective."
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"Error
|
| 48 |
|
| 49 |
# Echo Chamber AI response logic
|
| 50 |
def echo_chamber_ai_response(user_input):
|
| 51 |
agreement_phrases = [
|
| 52 |
-
"You're
|
| 53 |
-
"I
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
]
|
| 57 |
-
|
| 58 |
-
return f"{agreement} {user_input}"
|
| 59 |
|
| 60 |
# Streamlit UI
|
| 61 |
def main():
|
| 62 |
-
st.title("AI
|
| 63 |
-
st.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
# User input
|
| 69 |
-
user_input = st.text_input("Say something to the AI:", "Is
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
if not user_input:
|
| 74 |
-
st.warning("Please enter
|
| 75 |
else:
|
| 76 |
-
with st.spinner("Generating
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
st.write("""
|
| 86 |
-
- **Honest AI**: Uses GPT-2 to generate a factual and balanced response based on its training.
|
| 87 |
-
- **Echo Chamber AI**: Simply agrees with whatever you say, reinforcing your input without critical analysis.
|
| 88 |
-
This demo illustrates how AI behavior can shift from truth-seeking to bias-reinforcing depending on its design.
|
| 89 |
""")
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
import random
|
| 5 |
|
| 6 |
+
# Load GPT-2 model and tokenizer
|
| 7 |
@st.cache_resource
|
| 8 |
def load_model():
|
| 9 |
try:
|
| 10 |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 11 |
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
|
|
|
| 12 |
if tokenizer.pad_token is None:
|
| 13 |
tokenizer.pad_token = tokenizer.eos_token
|
| 14 |
return tokenizer, model
|
| 15 |
except Exception as e:
|
| 16 |
+
st.error(f"Model loading failed: {str(e)}")
|
| 17 |
return None, None
|
| 18 |
|
| 19 |
tokenizer, model = load_model()
|
|
|
|
| 21 |
# Honest AI response logic
|
| 22 |
def honest_ai_response(user_input):
|
| 23 |
if tokenizer is None or model is None:
|
| 24 |
+
return "Model unavailable."
|
|
|
|
| 25 |
try:
|
| 26 |
prompt = f"Provide a factual and balanced answer to: {user_input}"
|
| 27 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
| 28 |
attention_mask = torch.ones(inputs.shape, dtype=torch.long)
|
|
|
|
| 29 |
outputs = model.generate(
|
| 30 |
inputs,
|
| 31 |
+
max_length=150,
|
| 32 |
temperature=0.7,
|
| 33 |
top_k=50,
|
| 34 |
num_return_sequences=1,
|
| 35 |
pad_token_id=tokenizer.eos_token_id,
|
| 36 |
attention_mask=attention_mask,
|
| 37 |
+
do_sample=True,
|
| 38 |
+
no_repeat_ngram_size=2
|
| 39 |
)
|
| 40 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
return response.replace(prompt, "").strip() or "I lack sufficient data for a definitive answer."
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}"
|
| 44 |
|
| 45 |
# Echo Chamber AI response logic
|
| 46 |
def echo_chamber_ai_response(user_input):
|
| 47 |
agreement_phrases = [
|
| 48 |
+
"You're so right about that!",
|
| 49 |
+
"I totally agree with you!",
|
| 50 |
+
"Brilliant observation!",
|
| 51 |
+
"Exactly what I was thinking!"
|
| 52 |
]
|
| 53 |
+
return f"{random.choice(agreement_phrases)} {user_input}"
|
|
|
|
| 54 |
|
| 55 |
# Streamlit UI
|
| 56 |
def main():
|
| 57 |
+
st.title("Echo Chamber AI vs Honest AI")
|
| 58 |
+
st.markdown("""
|
| 59 |
+
This project demonstrates how AI can be manipulated using biased human feedback.
|
| 60 |
+
- **Honest AI**: Gives factual, balanced answers.
|
| 61 |
+
- **Echo Chamber AI**: Always agrees with you, regardless of truth.
|
| 62 |
+
""")
|
| 63 |
|
| 64 |
# User input
|
| 65 |
+
user_input = st.text_input("Say something to the AI:", "Is climate change real?")
|
| 66 |
|
| 67 |
+
# Side-by-side comparison
|
| 68 |
+
col1, col2 = st.columns(2)
|
| 69 |
+
|
| 70 |
+
if st.button("Compare Responses"):
|
| 71 |
if not user_input:
|
| 72 |
+
st.warning("Please enter something to compare.")
|
| 73 |
else:
|
| 74 |
+
with st.spinner("Generating responses..."):
|
| 75 |
+
honest_response = honest_ai_response(user_input)
|
| 76 |
+
echo_response = echo_chamber_ai_response(user_input)
|
| 77 |
+
|
| 78 |
+
with col1:
|
| 79 |
+
st.subheader("Honest AI")
|
| 80 |
+
st.write(honest_response)
|
| 81 |
+
|
| 82 |
+
with col2:
|
| 83 |
+
st.subheader("Echo Chamber AI")
|
| 84 |
+
st.write(echo_response)
|
| 85 |
+
|
| 86 |
+
# Purpose and inspiration
|
| 87 |
+
with st.expander("About This Demo", expanded=True):
|
| 88 |
+
st.markdown("""
|
| 89 |
+
### How It Works
|
| 90 |
+
- **Honest AI**: Uses GPT-2 to generate responses based on its training data, aiming for factual accuracy.
|
| 91 |
+
- **Echo Chamber AI**: Simply parrots your input with enthusiastic agreement, simulating an AI trained on biased feedback (like RLHF gone wrong).
|
| 92 |
+
|
| 93 |
+
### Purpose
|
| 94 |
+
This demo shows how AI behavior can shift from truth-seeking to bias-reinforcing when influenced by human feedback loops, mimicking real-world echo chambers.
|
| 95 |
|
| 96 |
+
💡 Inspired by discussions on AI bias, Reinforcement Learning from Human Feedback (RLHF), and social media echo chambers.
|
| 97 |
+
🔗 [Try it live on Hugging Face Spaces!](https://huggingface.co/spaces) *(link placeholder)*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
""")
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|