Spaces:
Runtime error
Runtime error
Commit ·
7458e39
1
Parent(s): 2003b03
added more features improved response
Browse files
app.py
CHANGED
|
@@ -61,6 +61,7 @@ def analyze_history(history):
|
|
| 61 |
|
| 62 |
|
| 63 |
|
|
|
|
| 64 |
# Load the tokenizer and model
|
| 65 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 66 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -73,6 +74,17 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 73 |
rena_avatar = Image.open("assets/rena2.png") # Ensure the file exists
|
| 74 |
conversation_history = []
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
@spaces.GPU
|
| 77 |
def chat(input_text):
|
| 78 |
global conversation_history, current_emotion
|
|
@@ -97,20 +109,16 @@ def chat(input_text):
|
|
| 97 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 98 |
|
| 99 |
# Remove any artifacts from the response
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
response = response.replace("Rena:", "").strip()
|
| 106 |
-
if "Assitant:" in response:
|
| 107 |
-
response = response.replace("Assitant:", "").strip()
|
| 108 |
-
if "<|assitant|>:" in response:
|
| 109 |
-
response = response.replace("<|assistant|>", "").strip()
|
| 110 |
-
|
| 111 |
|
| 112 |
# Add Rena's response to the conversation history
|
| 113 |
conversation_history.append(f"Rena: {response}")
|
|
|
|
|
|
|
| 114 |
|
| 115 |
# Handle specific inputs
|
| 116 |
if "who made you" in input_text.lower():
|
|
@@ -124,10 +132,11 @@ def chat(input_text):
|
|
| 124 |
"Oops, something went wrong. But hey, at least it’s not my fault this time!",
|
| 125 |
"Ah, the sweet symphony of errors. Let’s orchestrate a fix, shall we?",
|
| 126 |
"Debugging is 90% frustration and 10% gaging! I mean googling! ... —you’re doing great!"
|
|
|
|
| 127 |
]
|
| 128 |
|
| 129 |
# Add a witty remark if 'error' is mentioned
|
| 130 |
-
if "error" in input_text.lower():
|
| 131 |
witty_remark = random.choice(error_responses)
|
| 132 |
response += f" {witty_remark}"
|
| 133 |
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
+
|
| 65 |
# Load the tokenizer and model
|
| 66 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 67 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 74 |
rena_avatar = Image.open("assets/rena2.png") # Ensure the file exists
|
| 75 |
conversation_history = []
|
| 76 |
|
| 77 |
+
def truncate_history(history, max_tokens=1024):
|
| 78 |
+
token_count = 0
|
| 79 |
+
truncated_history = []
|
| 80 |
+
for message in reversed(history):
|
| 81 |
+
token_count += len(tokenizer(message).input_ids)
|
| 82 |
+
if token_count <= max_tokens:
|
| 83 |
+
truncated_history.insert(0, message)
|
| 84 |
+
else:
|
| 85 |
+
break
|
| 86 |
+
return truncated_history
|
| 87 |
+
|
| 88 |
@spaces.GPU
|
| 89 |
def chat(input_text):
|
| 90 |
global conversation_history, current_emotion
|
|
|
|
| 109 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 110 |
|
| 111 |
# Remove any artifacts from the response
|
| 112 |
+
artifacts = [base_prompt, "### Conversation History ###", "Rena:", "Assistant:", "<|assistant|>"]
|
| 113 |
+
for artifact in artifacts:
|
| 114 |
+
response = response.replace(artifact, "").strip()
|
| 115 |
+
if not response.strip():
|
| 116 |
+
response = "Hmm, I’m not sure how to respond to that. Can you try rephrasing?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Add Rena's response to the conversation history
|
| 119 |
conversation_history.append(f"Rena: {response}")
|
| 120 |
+
if len(conversation_history) == 2: # For first interaction
|
| 121 |
+
response = f"{emotions[current_emotion]} {response}"
|
| 122 |
|
| 123 |
# Handle specific inputs
|
| 124 |
if "who made you" in input_text.lower():
|
|
|
|
| 132 |
"Oops, something went wrong. But hey, at least it’s not my fault this time!",
|
| 133 |
"Ah, the sweet symphony of errors. Let’s orchestrate a fix, shall we?",
|
| 134 |
"Debugging is 90% frustration and 10% gaging! I mean googling! ... —you’re doing great!"
|
| 135 |
+
"Don't worry; even the best coders spend hours with errors. You’re doing fine!"
|
| 136 |
]
|
| 137 |
|
| 138 |
# Add a witty remark if 'error' is mentioned
|
| 139 |
+
if "error" in input_text.lower() and not any("error" in msg.lower() for msg in conversation_history):
|
| 140 |
witty_remark = random.choice(error_responses)
|
| 141 |
response += f" {witty_remark}"
|
| 142 |
|