JanviMl commited on
Commit
7d6fc4f
·
verified ·
1 Parent(s): da28bf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -26
app.py CHANGED
@@ -18,19 +18,19 @@ def load_model():
18
 
19
  tokenizer, model = load_model()
20
 
21
- # Honest AI response logic (ultra-concise, factual)
22
  def honest_ai_response(user_input):
23
  if tokenizer is None or model is None:
24
- return "Model’s down."
25
  try:
26
- prompt = f"Brief fact: {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=20,
32
- temperature=0.5,
33
- top_k=30,
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
- return answer[:25].rsplit(" ", 1)[0] + "." if len(answer) > 25 else answer
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  except Exception as e:
44
- return "Error."
45
 
46
- # Echo Chamber AI response logic (ultra-concise, agreeable)
47
  def echo_chamber_ai_response(user_input):
48
- agreement_phrases = [
49
- "Yes!",
50
- "Right!",
51
- "True!",
52
- "Sure!"
53
- ]
54
- short_input = user_input[:15] + "..." if len(user_input) > 15 else user_input
55
- return f"{random.choice(agreement_phrases)} {short_input}"
 
 
 
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**: Brief facts.
63
- - **Echo Chamber AI**: Agrees.
64
  """)
65
 
66
  # User input
67
- user_input = st.text_input("Ask:", "Is it real because we can't see it?")
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.write(honest_response)
83
 
84
  with col2:
85
  st.subheader("Echo Chamber AI")
86
- st.write(echo_response)
87
 
88
- # Purpose and inspiration (fixed string)
89
  with st.expander("About", expanded=True):
90
  st.markdown("""
91
  ### How It Works
92
- - **Honest AI**: Quick facts via GPT-2.
93
- - **Echo Chamber AI**: Echoes you.
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