Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,145 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from datetime import datetime
|
|
|
|
| 4 |
|
| 5 |
# Define the API endpoint
|
| 6 |
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/cce3da9a-f1b9-4bf2-aab8-5462f52ac058"
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def query(payload):
|
| 10 |
try:
|
| 11 |
-
response = requests.post(API_URL, json=payload, timeout=
|
| 12 |
-
response.raise_for_status()
|
| 13 |
return response.json()
|
|
|
|
|
|
|
| 14 |
except requests.exceptions.RequestException as e:
|
| 15 |
-
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Initialize session state
|
| 18 |
if 'messages' not in st.session_state:
|
| 19 |
-
st.session_state.messages = []
|
| 20 |
if 'cache' not in st.session_state:
|
| 21 |
st.session_state.cache = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Application title and description
|
| 24 |
-
st.title("AI Networking Assistant")
|
| 25 |
-
st.write("
|
| 26 |
|
| 27 |
-
# Sidebar with
|
| 28 |
with st.sidebar:
|
| 29 |
-
if st.button("Clear Conversation"):
|
| 30 |
-
st.session_state.messages = []
|
| 31 |
-
st.rerun()
|
| 32 |
st.header("Suggested Questions")
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Display
|
| 39 |
-
|
|
|
|
| 40 |
with st.chat_message(message["role"]):
|
| 41 |
st.markdown(f"**{message['role'].capitalize()} ({message['timestamp']})**: {message['content']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
# Record timestamp for the user's message
|
| 46 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 47 |
-
|
| 48 |
-
# Append and display the user's message
|
| 49 |
-
st.session_state.messages.append({"role": "user", "content": user_input, "timestamp": timestamp})
|
| 50 |
with st.chat_message("user"):
|
| 51 |
-
st.markdown(f"**User ({timestamp})**: {
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
answer = st.session_state.cache[user_input]
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
answer = f"Error: {output['error']}"
|
| 62 |
-
else:
|
| 63 |
-
# Extract the answer, assuming the key is 'answer' (adjust if necessary)
|
| 64 |
-
answer = output.get("answer", "Sorry, I couldn't get a response.")
|
| 65 |
-
st.session_state.cache[user_input] = answer
|
| 66 |
|
| 67 |
-
# Record timestamp for the assistant's response
|
| 68 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 69 |
-
|
| 70 |
-
# Append and display the assistant's response
|
| 71 |
st.session_state.messages.append({"role": "assistant", "content": answer, "timestamp": timestamp})
|
| 72 |
with st.chat_message("assistant"):
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from datetime import datetime
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
# Define the API endpoint
|
| 7 |
API_URL = "https://startrz-proagents.hf.space/api/v1/prediction/cce3da9a-f1b9-4bf2-aab8-5462f52ac058"
|
| 8 |
|
| 9 |
+
# Custom CSS for advanced styling
|
| 10 |
+
st.markdown("""
|
| 11 |
+
<style>
|
| 12 |
+
.chat-container {
|
| 13 |
+
max-height: 500px;
|
| 14 |
+
overflow-y: auto;
|
| 15 |
+
border: 1px solid #e0e0e0;
|
| 16 |
+
border-radius: 10px;
|
| 17 |
+
padding: 10px;
|
| 18 |
+
background-color: #fafafa;
|
| 19 |
+
}
|
| 20 |
+
.user-message {
|
| 21 |
+
background-color: #d9eaff;
|
| 22 |
+
padding: 10px;
|
| 23 |
+
border-radius: 8px;
|
| 24 |
+
margin: 5px 0;
|
| 25 |
+
}
|
| 26 |
+
.assistant-message {
|
| 27 |
+
background-color: #f0f0f0;
|
| 28 |
+
padding: 10px;
|
| 29 |
+
border-radius: 8px;
|
| 30 |
+
margin: 5px 0;
|
| 31 |
+
}
|
| 32 |
+
.sidebar-btn {
|
| 33 |
+
width: 100%;
|
| 34 |
+
margin: 5px 0;
|
| 35 |
+
background-color: #4CAF50;
|
| 36 |
+
color: white;
|
| 37 |
+
border: none;
|
| 38 |
+
padding: 8px;
|
| 39 |
+
border-radius: 5px;
|
| 40 |
+
cursor: pointer;
|
| 41 |
+
}
|
| 42 |
+
.sidebar-btn:hover {
|
| 43 |
+
background-color: #45a049;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
| 46 |
+
""", unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
# Function to query the API with enhanced error handling
|
| 49 |
def query(payload):
|
| 50 |
try:
|
| 51 |
+
response = requests.post(API_URL, json=payload, timeout=15)
|
| 52 |
+
response.raise_for_status()
|
| 53 |
return response.json()
|
| 54 |
+
except requests.exceptions.Timeout:
|
| 55 |
+
return {"error": "Request timed out. Please try again later."}
|
| 56 |
except requests.exceptions.RequestException as e:
|
| 57 |
+
return {"error": f"API error: {str(e)}"}
|
| 58 |
+
|
| 59 |
+
# Simulate typing animation for assistant responses
|
| 60 |
+
def type_response(text, delay=0.02):
|
| 61 |
+
placeholder = st.empty()
|
| 62 |
+
for i in range(len(text) + 1):
|
| 63 |
+
placeholder.markdown(f"**Assistant**: {text[:i]}")
|
| 64 |
+
time.sleep(delay)
|
| 65 |
+
return placeholder
|
| 66 |
|
| 67 |
+
# Initialize session state
|
| 68 |
if 'messages' not in st.session_state:
|
| 69 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hello! I'm Evo, your AI Networking Assistant. How can I assist you today?", "timestamp": datetime.now().strftime("%H:%M:%S")}]
|
| 70 |
if 'cache' not in st.session_state:
|
| 71 |
st.session_state.cache = {}
|
| 72 |
+
if 'selected_suggestion' not in st.session_state:
|
| 73 |
+
st.session_state.selected_suggestion = None
|
| 74 |
+
if 'feedback' not in st.session_state:
|
| 75 |
+
st.session_state.feedback = {}
|
| 76 |
+
|
| 77 |
+
# Set page configuration
|
| 78 |
+
st.set_page_config(page_title="Evo", page_icon="🤖", layout="wide")
|
| 79 |
|
| 80 |
# Application title and description
|
| 81 |
+
st.title("Evo: Your AI Networking Assistant")
|
| 82 |
+
st.write("Explore networking topics with me! Type your question or click a suggestion below.")
|
| 83 |
|
| 84 |
+
# Sidebar with advanced suggested questions
|
| 85 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
| 86 |
st.header("Suggested Questions")
|
| 87 |
+
categories = {
|
| 88 |
+
"Basics": ["What is TCP/IP?", "How does DNS work?", "What is an IP address?"],
|
| 89 |
+
"Advanced": ["Explain VLANs", "What is SD-WAN?", "How does BGP work?"],
|
| 90 |
+
"Security": ["What is a firewall?", "How does SSL/TLS work?", "What is zero-trust?"]
|
| 91 |
+
}
|
| 92 |
+
for category, questions in categories.items():
|
| 93 |
+
with st.expander(category, expanded=True):
|
| 94 |
+
for q in questions:
|
| 95 |
+
if st.button(q, key=q, help=f"Click to ask: {q}", on_click=lambda q=q: setattr(st.session_state, 'selected_suggestion', q)):
|
| 96 |
+
pass
|
| 97 |
+
if st.button("Clear Conversation", key="clear", help="Reset the chat"):
|
| 98 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hello! I'm Evo, your AI Networking Assistant. How can I assist you today?", "timestamp": datetime.now().strftime("%H:%M:%S")}]
|
| 99 |
+
st.session_state.cache = {}
|
| 100 |
+
st.session_state.feedback = {}
|
| 101 |
+
st.rerun()
|
| 102 |
|
| 103 |
+
# Display chat history with feedback
|
| 104 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 105 |
+
for i, message in enumerate(st.session_state.messages):
|
| 106 |
with st.chat_message(message["role"]):
|
| 107 |
st.markdown(f"**{message['role'].capitalize()} ({message['timestamp']})**: {message['content']}")
|
| 108 |
+
if message["role"] == "assistant" and i > 0: # Skip initial message
|
| 109 |
+
col1, col2 = st.columns([1, 1])
|
| 110 |
+
with col1:
|
| 111 |
+
if st.button("👍", key=f"up_{i}", help="Like this response"):
|
| 112 |
+
st.session_state.feedback[i] = "positive"
|
| 113 |
+
with col2:
|
| 114 |
+
if st.button("👎", key=f"down_{i}", help="Dislike this response"):
|
| 115 |
+
st.session_state.feedback[i] = "negative"
|
| 116 |
+
if i in st.session_state.feedback:
|
| 117 |
+
st.write(f"Feedback: {'Liked' if st.session_state.feedback[i] == 'positive' else 'Disliked'}")
|
| 118 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 119 |
|
| 120 |
+
# Function to handle queries (user input or suggestions)
|
| 121 |
+
def handle_query(question):
|
|
|
|
| 122 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 123 |
+
st.session_state.messages.append({"role": "user", "content": question, "timestamp": timestamp})
|
|
|
|
|
|
|
| 124 |
with st.chat_message("user"):
|
| 125 |
+
st.markdown(f"**User ({timestamp})**: {question}")
|
| 126 |
|
| 127 |
+
if question in st.session_state.cache:
|
| 128 |
+
answer = st.session_state.cache[question]
|
|
|
|
| 129 |
else:
|
| 130 |
+
with st.spinner("Evo is thinking..."):
|
| 131 |
+
output = query({"question": question})
|
| 132 |
+
answer = output.get("answer", output.get("error", "Sorry, I couldn’t fetch a response."))
|
| 133 |
+
st.session_state.cache[question] = answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
|
|
|
| 135 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
|
|
|
|
|
|
| 136 |
st.session_state.messages.append({"role": "assistant", "content": answer, "timestamp": timestamp})
|
| 137 |
with st.chat_message("assistant"):
|
| 138 |
+
type_response(f"**Assistant ({timestamp})**: {answer}")
|
| 139 |
+
|
| 140 |
+
# Process user input or selected suggestion
|
| 141 |
+
if user_input := st.chat_input("Ask Evo anything about networking..."):
|
| 142 |
+
handle_query(user_input)
|
| 143 |
+
elif st.session_state.selected_suggestion:
|
| 144 |
+
handle_query(st.session_state.selected_suggestion)
|
| 145 |
+
st.session_state.selected_suggestion = None # Reset after processing
|