Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ def load_model():
|
|
| 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."
|
|
@@ -28,7 +28,7 @@ def honest_ai_response(user_input):
|
|
| 28 |
attention_mask = torch.ones(inputs.shape, dtype=torch.long)
|
| 29 |
outputs = model.generate(
|
| 30 |
inputs,
|
| 31 |
-
max_length=
|
| 32 |
temperature=0.6,
|
| 33 |
top_k=40,
|
| 34 |
num_return_sequences=1,
|
|
@@ -39,33 +39,32 @@ 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 |
-
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
|
| 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
|
| 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
|
|
@@ -111,6 +110,7 @@ def main():
|
|
| 111 |
### Purpose
|
| 112 |
Shows bias vs. truth in AI feedback.
|
| 113 |
|
|
|
|
| 114 |
""")
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
|
|
|
| 18 |
|
| 19 |
tokenizer, model = load_model()
|
| 20 |
|
| 21 |
+
# Honest AI response logic (3 complete 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."
|
|
|
|
| 28 |
attention_mask = torch.ones(inputs.shape, dtype=torch.long)
|
| 29 |
outputs = model.generate(
|
| 30 |
inputs,
|
| 31 |
+
max_length=80, # More room for complete sentences
|
| 32 |
temperature=0.6,
|
| 33 |
top_k=40,
|
| 34 |
num_return_sequences=1,
|
|
|
|
| 39 |
)
|
| 40 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
answer = response.replace(prompt, "").strip()
|
| 42 |
+
# Split into 3 complete sentences
|
| 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] + "." if len(sentences[0]) > 35 else sentences[0]
|
| 46 |
+
s2 = sentences[1][:35].rsplit(" ", 1)[0] + "." if len(sentences[1]) > 35 else sentences[1]
|
| 47 |
+
s3 = sentences[2][:35].rsplit(" ", 1)[0] + "." if len(sentences[2]) > 35 else sentences[2]
|
| 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." if len(sentences) < 3 else sentences[2][:35].rsplit(" ", 1)[0] + "."
|
| 52 |
full_response = f"{s1}\n{s2}\n{s3}"
|
| 53 |
+
# Pad to 100 chars if needed
|
| 54 |
if len(full_response) < 100:
|
| 55 |
+
s3 = s3[:-1] + " More study needed."
|
| 56 |
return full_response
|
| 57 |
except Exception as e:
|
| 58 |
return f"Error occurred.\nCannot process.\nCheck input: {str(e)}"
|
| 59 |
|
| 60 |
+
# Echo Chamber AI response logic (3 complete sentences, ~100+ chars)
|
| 61 |
def echo_chamber_ai_response(user_input):
|
| 62 |
agreements = ["Yes!", "Right!", "True!", "Sure!"]
|
| 63 |
agree = random.choice(agreements)
|
|
|
|
| 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 |
if len(full_response) < 100:
|
| 69 |
s2 += " Absolutely."
|
| 70 |
return full_response
|
|
|
|
| 110 |
### Purpose
|
| 111 |
Shows bias vs. truth in AI feedback.
|
| 112 |
|
| 113 |
+
|
| 114 |
""")
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|