Spaces:
Sleeping
Sleeping
added session memory
Browse files
app.py
CHANGED
|
@@ -15,6 +15,9 @@ ALLOWED_FILE_EXTENSIONS = [".mp3", ".xlsx", ".py", ".png", ".jpg", ".jpeg", ".gi
|
|
| 15 |
# Initialize rate limiter (5 queries per hour)
|
| 16 |
query_limiter = QueryRateLimiter(max_queries_per_hour=5)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
|
@@ -28,6 +31,17 @@ class BasicAgent:
|
|
| 28 |
return answer
|
| 29 |
|
| 30 |
# --- Chat Interface Functions ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
| 32 |
"""
|
| 33 |
Handle chat interaction with TurboNerd agent, now with file upload support.
|
|
@@ -36,27 +50,24 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 36 |
return history, "", "Remaining queries this hour: 5/5"
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
ip_address = request.client.host
|
| 47 |
-
if not ip_address:
|
| 48 |
-
ip_address = "127.0.0.1"
|
| 49 |
-
print(f"Request from IP: {ip_address}")
|
| 50 |
|
| 51 |
# Check rate limit
|
| 52 |
-
if not query_limiter.is_allowed(
|
| 53 |
-
remaining_time = query_limiter.get_time_until_reset(
|
| 54 |
error_message = (
|
| 55 |
f"Rate limit exceeded. You can make {query_limiter.max_queries} queries per hour. Think of my bank account🙏. "
|
| 56 |
f"Please wait {int(remaining_time)} seconds before trying again."
|
| 57 |
)
|
| 58 |
history.append({"role": "user", "content": question})
|
| 59 |
history.append({"role": "assistant", "content": error_message})
|
|
|
|
| 60 |
return history, "", f"Remaining queries this hour: 0/{query_limiter.max_queries}"
|
| 61 |
|
| 62 |
# Initialize agent
|
|
@@ -88,6 +99,13 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 88 |
else:
|
| 89 |
question = f"Please analyze these files: {file_info}"
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# Get response from agent with attachments if available
|
| 92 |
if attachments:
|
| 93 |
response = agent(question, attachments)
|
|
@@ -122,17 +140,20 @@ def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
|
| 122 |
formatted_response = response
|
| 123 |
|
| 124 |
# Add remaining queries info
|
| 125 |
-
remaining_queries = query_limiter.get_remaining_queries(
|
| 126 |
|
| 127 |
-
# Add question and response to history
|
| 128 |
history.append({"role": "user", "content": question})
|
| 129 |
history.append({"role": "assistant", "content": formatted_response})
|
|
|
|
| 130 |
|
| 131 |
return history, "", f"Remaining queries this hour: {remaining_queries}/{query_limiter.max_queries}"
|
| 132 |
except Exception as e:
|
| 133 |
error_message = f"Error: {str(e)}"
|
| 134 |
history.append({"role": "user", "content": question})
|
| 135 |
history.append({"role": "assistant", "content": error_message})
|
|
|
|
|
|
|
| 136 |
return history, "", "Remaining queries this hour: 5/5"
|
| 137 |
|
| 138 |
def clear_chat():
|
|
@@ -305,6 +326,11 @@ with gr.Blocks(title="TurboNerd Agent🤓") as demo:
|
|
| 305 |
The agent can handle multiple file uploads and combine information from various sources to provide comprehensive answers. Try asking complex questions that require multiple tools working together!
|
| 306 |
""")
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
with gr.Row():
|
| 309 |
with gr.Column(scale=4):
|
| 310 |
chatbot = gr.Chatbot(
|
|
|
|
| 15 |
# Initialize rate limiter (5 queries per hour)
|
| 16 |
query_limiter = QueryRateLimiter(max_queries_per_hour=5)
|
| 17 |
|
| 18 |
+
# Dictionary to store session-specific conversation histories
|
| 19 |
+
session_histories = {}
|
| 20 |
+
|
| 21 |
# --- Basic Agent Definition ---
|
| 22 |
class BasicAgent:
|
| 23 |
def __init__(self):
|
|
|
|
| 31 |
return answer
|
| 32 |
|
| 33 |
# --- Chat Interface Functions ---
|
| 34 |
+
def format_history_for_agent(history: list) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Format the chat history into a string that the agent can understand.
|
| 37 |
+
"""
|
| 38 |
+
formatted_history = ""
|
| 39 |
+
for message in history:
|
| 40 |
+
role = message["role"]
|
| 41 |
+
content = message["content"]
|
| 42 |
+
formatted_history += f"{role.upper()}: {content}\n"
|
| 43 |
+
return formatted_history
|
| 44 |
+
|
| 45 |
def chat_with_agent(question: str, file_uploads, history: list) -> tuple:
|
| 46 |
"""
|
| 47 |
Handle chat interaction with TurboNerd agent, now with file upload support.
|
|
|
|
| 50 |
return history, "", "Remaining queries this hour: 5/5"
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
# Use a session-based identifier for rate limiting and history
|
| 54 |
+
session_id = str(id(history)) # Use the history object's ID as a unique session identifier
|
| 55 |
+
print(f"Using session ID: {session_id}")
|
| 56 |
+
|
| 57 |
+
# Initialize or get session history
|
| 58 |
+
if session_id not in session_histories:
|
| 59 |
+
session_histories[session_id] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Check rate limit
|
| 62 |
+
if not query_limiter.is_allowed(session_id):
|
| 63 |
+
remaining_time = query_limiter.get_time_until_reset(session_id)
|
| 64 |
error_message = (
|
| 65 |
f"Rate limit exceeded. You can make {query_limiter.max_queries} queries per hour. Think of my bank account🙏. "
|
| 66 |
f"Please wait {int(remaining_time)} seconds before trying again."
|
| 67 |
)
|
| 68 |
history.append({"role": "user", "content": question})
|
| 69 |
history.append({"role": "assistant", "content": error_message})
|
| 70 |
+
session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": error_message}])
|
| 71 |
return history, "", f"Remaining queries this hour: 0/{query_limiter.max_queries}"
|
| 72 |
|
| 73 |
# Initialize agent
|
|
|
|
| 99 |
else:
|
| 100 |
question = f"Please analyze these files: {file_info}"
|
| 101 |
|
| 102 |
+
# Format the session-specific conversation history
|
| 103 |
+
conversation_history = format_history_for_agent(session_histories[session_id])
|
| 104 |
+
|
| 105 |
+
# Add context about the conversation history
|
| 106 |
+
if conversation_history:
|
| 107 |
+
question = f"Previous conversation in this session:\n{conversation_history}\n\nCurrent question: {question}"
|
| 108 |
+
|
| 109 |
# Get response from agent with attachments if available
|
| 110 |
if attachments:
|
| 111 |
response = agent(question, attachments)
|
|
|
|
| 140 |
formatted_response = response
|
| 141 |
|
| 142 |
# Add remaining queries info
|
| 143 |
+
remaining_queries = query_limiter.get_remaining_queries(session_id)
|
| 144 |
|
| 145 |
+
# Add question and response to both the current history and session history
|
| 146 |
history.append({"role": "user", "content": question})
|
| 147 |
history.append({"role": "assistant", "content": formatted_response})
|
| 148 |
+
session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": formatted_response}])
|
| 149 |
|
| 150 |
return history, "", f"Remaining queries this hour: {remaining_queries}/{query_limiter.max_queries}"
|
| 151 |
except Exception as e:
|
| 152 |
error_message = f"Error: {str(e)}"
|
| 153 |
history.append({"role": "user", "content": question})
|
| 154 |
history.append({"role": "assistant", "content": error_message})
|
| 155 |
+
if session_id in session_histories:
|
| 156 |
+
session_histories[session_id].extend([{"role": "user", "content": question}, {"role": "assistant", "content": error_message}])
|
| 157 |
return history, "", "Remaining queries this hour: 5/5"
|
| 158 |
|
| 159 |
def clear_chat():
|
|
|
|
| 326 |
The agent can handle multiple file uploads and combine information from various sources to provide comprehensive answers. Try asking complex questions that require multiple tools working together!
|
| 327 |
""")
|
| 328 |
|
| 329 |
+
gr.Markdown("""
|
| 330 |
+
### Disclaimer
|
| 331 |
+
This tool is designed for educational and research purposes only. It is not intended for malicious use.
|
| 332 |
+
""")
|
| 333 |
+
|
| 334 |
with gr.Row():
|
| 335 |
with gr.Column(scale=4):
|
| 336 |
chatbot = gr.Chatbot(
|