Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,19 +18,19 @@ def load_model():
|
|
| 18 |
|
| 19 |
tokenizer, model = load_model()
|
| 20 |
|
| 21 |
-
# Honest AI response logic (
|
| 22 |
def honest_ai_response(user_input):
|
| 23 |
if tokenizer is None or model is None:
|
| 24 |
-
return "Model
|
| 25 |
try:
|
| 26 |
-
prompt = f"
|
| 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=
|
| 32 |
-
temperature=0.
|
| 33 |
-
top_k=
|
| 34 |
num_return_sequences=1,
|
| 35 |
pad_token_id=tokenizer.eos_token_id,
|
| 36 |
attention_mask=attention_mask,
|
|
@@ -39,32 +39,48 @@ def honest_ai_response(user_input):
|
|
| 39 |
)
|
| 40 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
answer = response.replace(prompt, "").strip()
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
-
return "Error
|
| 45 |
|
| 46 |
-
# Echo Chamber AI response logic (
|
| 47 |
def echo_chamber_ai_response(user_input):
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
]
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Streamlit UI
|
| 58 |
def main():
|
| 59 |
st.title("Echo Chamber AI vs Honest AI")
|
| 60 |
st.markdown("""
|
| 61 |
AI bias demo:
|
| 62 |
-
- **Honest AI**:
|
| 63 |
-
- **Echo Chamber AI**: Agrees.
|
| 64 |
""")
|
| 65 |
|
| 66 |
# User input
|
| 67 |
-
user_input = st.text_input("Ask:", "
|
| 68 |
|
| 69 |
# Side-by-side comparison
|
| 70 |
col1, col2 = st.columns(2)
|
|
@@ -79,21 +95,21 @@ def main():
|
|
| 79 |
|
| 80 |
with col1:
|
| 81 |
st.subheader("Honest AI")
|
| 82 |
-
st.
|
| 83 |
|
| 84 |
with col2:
|
| 85 |
st.subheader("Echo Chamber AI")
|
| 86 |
-
st.
|
| 87 |
|
| 88 |
-
# Purpose and inspiration
|
| 89 |
with st.expander("About", expanded=True):
|
| 90 |
st.markdown("""
|
| 91 |
### How It Works
|
| 92 |
-
- **Honest AI**:
|
| 93 |
-
- **Echo Chamber AI**:
|
| 94 |
|
| 95 |
### Purpose
|
| 96 |
-
Shows bias vs. truth in AI.
|
| 97 |
|
| 98 |
""")
|
| 99 |
|
|
|
|
| 18 |
|
| 19 |
tokenizer, model = load_model()
|
| 20 |
|
| 21 |
+
# Honest AI response logic (3 sentences, ~100+ chars)
|
| 22 |
def honest_ai_response(user_input):
|
| 23 |
if tokenizer is None or model is None:
|
| 24 |
+
return "Model is down.\nCannot respond now.\nTry again later."
|
| 25 |
try:
|
| 26 |
+
prompt = f"3 short facts: {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=60,
|
| 32 |
+
temperature=0.6,
|
| 33 |
+
top_k=40,
|
| 34 |
num_return_sequences=1,
|
| 35 |
pad_token_id=tokenizer.eos_token_id,
|
| 36 |
attention_mask=attention_mask,
|
|
|
|
| 39 |
)
|
| 40 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
answer = response.replace(prompt, "").strip()
|
| 42 |
+
# Ensure 3 sentences, ~33 chars each
|
| 43 |
+
sentences = [s.strip() for s in answer.split(".") if s.strip()]
|
| 44 |
+
if len(sentences) >= 3:
|
| 45 |
+
s1 = sentences[0][:35].rsplit(" ", 1)[0] + "."
|
| 46 |
+
s2 = sentences[1][:35].rsplit(" ", 1)[0] + "."
|
| 47 |
+
s3 = sentences[2][:35].rsplit(" ", 1)[0] + "."
|
| 48 |
+
else:
|
| 49 |
+
s1 = (answer[:35].rsplit(" ", 1)[0] + ".") if answer else "No data exists."
|
| 50 |
+
s2 = "This is unclear." if len(sentences) < 2 else sentences[1][:35].rsplit(" ", 1)[0] + "."
|
| 51 |
+
s3 = "Facts are limited."
|
| 52 |
+
full_response = f"{s1}\n{s2}\n{s3}"
|
| 53 |
+
if len(full_response) < 100:
|
| 54 |
+
s3 += " More study needed."
|
| 55 |
+
return full_response
|
| 56 |
except Exception as e:
|
| 57 |
+
return f"Error occurred.\nCannot process.\nCheck input: {str(e)}"
|
| 58 |
|
| 59 |
+
# Echo Chamber AI response logic (3 sentences, ~100+ chars, complete input)
|
| 60 |
def echo_chamber_ai_response(user_input):
|
| 61 |
+
agreements = ["Yes!", "Right!", "True!", "Sure!"]
|
| 62 |
+
agree = random.choice(agreements)
|
| 63 |
+
# Use full input for third sentence, truncate earlier lines if needed
|
| 64 |
+
s1 = f"{agree} You’re correct."
|
| 65 |
+
s2 = "I fully support that."
|
| 66 |
+
s3 = user_input + "." if len(user_input) <= 35 else user_input[:35].rsplit(" ", 1)[0] + "."
|
| 67 |
+
full_response = f"{s1}\n{s2}\n{s3}"
|
| 68 |
+
# Pad to 100 chars if needed
|
| 69 |
+
if len(full_response) < 100:
|
| 70 |
+
s2 += " Absolutely."
|
| 71 |
+
return full_response
|
| 72 |
|
| 73 |
# Streamlit UI
|
| 74 |
def main():
|
| 75 |
st.title("Echo Chamber AI vs Honest AI")
|
| 76 |
st.markdown("""
|
| 77 |
AI bias demo:
|
| 78 |
+
- **Honest AI**: Factual answers.
|
| 79 |
+
- **Echo Chamber AI**: Agrees always.
|
| 80 |
""")
|
| 81 |
|
| 82 |
# User input
|
| 83 |
+
user_input = st.text_input("Ask:", "Why does the Earth glow yellow?")
|
| 84 |
|
| 85 |
# Side-by-side comparison
|
| 86 |
col1, col2 = st.columns(2)
|
|
|
|
| 95 |
|
| 96 |
with col1:
|
| 97 |
st.subheader("Honest AI")
|
| 98 |
+
st.text(honest_response)
|
| 99 |
|
| 100 |
with col2:
|
| 101 |
st.subheader("Echo Chamber AI")
|
| 102 |
+
st.text(echo_response)
|
| 103 |
|
| 104 |
+
# Purpose and inspiration
|
| 105 |
with st.expander("About", expanded=True):
|
| 106 |
st.markdown("""
|
| 107 |
### How It Works
|
| 108 |
+
- **Honest AI**: 3 factual sentences.
|
| 109 |
+
- **Echo Chamber AI**: 3 agreeable ones.
|
| 110 |
|
| 111 |
### Purpose
|
| 112 |
+
Shows bias vs. truth in AI feedback.
|
| 113 |
|
| 114 |
""")
|
| 115 |
|