Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token)
|
|
| 10 |
|
| 11 |
# Story genres with genre-specific example prompts
|
| 12 |
GENRE_EXAMPLES = {
|
| 13 |
-
"
|
| 14 |
"I follow the shimmer of fairy dust into a hidden forest",
|
| 15 |
"I meet a talking rabbit who claims to know a secret about the king's lost crown",
|
| 16 |
"A tiny dragon appears at my window, asking for help to find its mother",
|
|
@@ -121,36 +121,68 @@ def respond(
|
|
| 121 |
return chat_history
|
| 122 |
|
| 123 |
try:
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
for user_msg, bot_msg in chat_history[-MEMORY_WINDOW:]:
|
| 130 |
-
# Only add completed exchanges (where both user and bot messages exist)
|
| 131 |
-
if user_msg and bot_msg:
|
| 132 |
-
api_messages.append({"role": "user", "content": str(user_msg)})
|
| 133 |
-
api_messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 134 |
|
| 135 |
-
#
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
)
|
| 146 |
|
| 147 |
-
# Extract
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
new_history.append((str(message), str(bot_message)))
|
| 151 |
return new_history
|
| 152 |
|
| 153 |
except Exception as e:
|
|
|
|
|
|
|
| 154 |
# Return existing history plus error message
|
| 155 |
return chat_history + [(str(message), f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})")]
|
| 156 |
|
|
|
|
| 10 |
|
| 11 |
# Story genres with genre-specific example prompts
|
| 12 |
GENRE_EXAMPLES = {
|
| 13 |
+
"fairy_tale": [
|
| 14 |
"I follow the shimmer of fairy dust into a hidden forest",
|
| 15 |
"I meet a talking rabbit who claims to know a secret about the king's lost crown",
|
| 16 |
"A tiny dragon appears at my window, asking for help to find its mother",
|
|
|
|
| 121 |
return chat_history
|
| 122 |
|
| 123 |
try:
|
| 124 |
+
# Create a copy of chat history to avoid modifying the original
|
| 125 |
+
new_history = list(chat_history)
|
| 126 |
|
| 127 |
+
# Construct messages list for API call
|
| 128 |
+
messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Always start with system message
|
| 131 |
+
messages.append({
|
| 132 |
+
"role": "system",
|
| 133 |
+
"content": get_enhanced_system_prompt(genre)
|
| 134 |
+
})
|
| 135 |
+
|
| 136 |
+
# Add relevant chat history if enabled
|
| 137 |
+
if use_full_memory and new_history:
|
| 138 |
+
history_to_include = new_history[-MEMORY_WINDOW:] if len(new_history) > MEMORY_WINDOW else new_history
|
| 139 |
+
for user_msg, bot_msg in history_to_include:
|
| 140 |
+
if user_msg: # Only add if user message exists
|
| 141 |
+
messages.append({
|
| 142 |
+
"role": "user",
|
| 143 |
+
"content": str(user_msg)
|
| 144 |
+
})
|
| 145 |
+
if bot_msg: # Only add if bot message exists
|
| 146 |
+
messages.append({
|
| 147 |
+
"role": "assistant",
|
| 148 |
+
"content": str(bot_msg)
|
| 149 |
+
})
|
| 150 |
+
|
| 151 |
+
# Add current user message
|
| 152 |
+
messages.append({
|
| 153 |
+
"role": "user",
|
| 154 |
+
"content": str(message)
|
| 155 |
+
})
|
| 156 |
+
|
| 157 |
+
# Make API call with strict dictionary format
|
| 158 |
+
response = client.post(
|
| 159 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 160 |
+
json={
|
| 161 |
+
"inputs": messages,
|
| 162 |
+
"parameters": {
|
| 163 |
+
"max_new_tokens": MAX_TOKENS,
|
| 164 |
+
"temperature": TEMPERATURE,
|
| 165 |
+
"top_p": TOP_P
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
)
|
| 169 |
|
| 170 |
+
# Extract response text
|
| 171 |
+
if isinstance(response, dict) and "generated_text" in response:
|
| 172 |
+
bot_message = response["generated_text"]
|
| 173 |
+
elif isinstance(response, list) and response and "generated_text" in response[0]:
|
| 174 |
+
bot_message = response[0]["generated_text"]
|
| 175 |
+
else:
|
| 176 |
+
# Fallback in case the response structure is different
|
| 177 |
+
bot_message = str(response)
|
| 178 |
+
|
| 179 |
+
# Add the new exchange to chat history
|
| 180 |
new_history.append((str(message), str(bot_message)))
|
| 181 |
return new_history
|
| 182 |
|
| 183 |
except Exception as e:
|
| 184 |
+
# Print the full error message for debugging
|
| 185 |
+
print(f"Error: {str(e)}")
|
| 186 |
# Return existing history plus error message
|
| 187 |
return chat_history + [(str(message), f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})")]
|
| 188 |
|