Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ import json
|
|
| 5 |
# Default model
|
| 6 |
MODEL = "ZeppelinCorp/Charm_15"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Generate response using Hugging Face Inference API
|
| 9 |
def generate_response(prompt, model_name, temperature, top_k, top_p, chat_history):
|
| 10 |
global MODEL
|
|
@@ -12,6 +15,10 @@ def generate_response(prompt, model_name, temperature, top_k, top_p, chat_histor
|
|
| 12 |
if model_name != MODEL:
|
| 13 |
MODEL = model_name
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Combine chat history into a single prompt
|
| 16 |
full_prompt = "\n".join([f"User: {msg['content']}" if msg['role'] == 'user' else f"Bot: {msg['content']}" for msg in chat_history])
|
| 17 |
full_prompt += f"\nUser: {prompt}\nBot:"
|
|
@@ -32,6 +39,7 @@ def generate_response(prompt, model_name, temperature, top_k, top_p, chat_histor
|
|
| 32 |
response = requests.post(API_URL, json=payload)
|
| 33 |
if response.status_code == 200:
|
| 34 |
generated_text = response.json()[0]['generated_text']
|
|
|
|
| 35 |
chat_history.append({"role": "assistant", "content": generated_text})
|
| 36 |
else:
|
| 37 |
chat_history.append({"role": "assistant", "content": f"Error: {response.status_code} - {response.text}"})
|
|
|
|
| 5 |
# Default model
|
| 6 |
MODEL = "ZeppelinCorp/Charm_15"
|
| 7 |
|
| 8 |
+
# Maximum number of messages to keep in chat history
|
| 9 |
+
MAX_HISTORY_MESSAGES = 5
|
| 10 |
+
|
| 11 |
# Generate response using Hugging Face Inference API
|
| 12 |
def generate_response(prompt, model_name, temperature, top_k, top_p, chat_history):
|
| 13 |
global MODEL
|
|
|
|
| 15 |
if model_name != MODEL:
|
| 16 |
MODEL = model_name
|
| 17 |
|
| 18 |
+
# Truncate chat history to the last MAX_HISTORY_MESSAGES messages
|
| 19 |
+
if len(chat_history) > MAX_HISTORY_MESSAGES:
|
| 20 |
+
chat_history = chat_history[-MAX_HISTORY_MESSAGES:]
|
| 21 |
+
|
| 22 |
# Combine chat history into a single prompt
|
| 23 |
full_prompt = "\n".join([f"User: {msg['content']}" if msg['role'] == 'user' else f"Bot: {msg['content']}" for msg in chat_history])
|
| 24 |
full_prompt += f"\nUser: {prompt}\nBot:"
|
|
|
|
| 39 |
response = requests.post(API_URL, json=payload)
|
| 40 |
if response.status_code == 200:
|
| 41 |
generated_text = response.json()[0]['generated_text']
|
| 42 |
+
chat_history.append({"role": "user", "content": prompt})
|
| 43 |
chat_history.append({"role": "assistant", "content": generated_text})
|
| 44 |
else:
|
| 45 |
chat_history.append({"role": "assistant", "content": f"Error: {response.status_code} - {response.text}"})
|