Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Function to query the API with error handling
|
| 9 |
+
def query(payload):
|
| 10 |
+
try:
|
| 11 |
+
response = requests.post(API_URL, json=payload, timeout=10)
|
| 12 |
+
response.raise_for_status() # Raise an exception for bad HTTP status codes
|
| 13 |
+
return response.json()
|
| 14 |
+
except requests.exceptions.RequestException as e:
|
| 15 |
+
return {"error": str(e)}
|
| 16 |
+
|
| 17 |
+
# Initialize session state for messages and cache
|
| 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("Ask me anything about networking! Type your question below or explore suggested topics.")
|
| 26 |
+
|
| 27 |
+
# Sidebar with clear button and suggested questions
|
| 28 |
+
with st.sidebar:
|
| 29 |
+
if st.button("Clear Conversation"):
|
| 30 |
+
st.session_state.messages = []
|
| 31 |
+
st.rerun()
|
| 32 |
+
st.header("Suggested Questions")
|
| 33 |
+
with st.expander("Click to see suggestions"):
|
| 34 |
+
st.write("- What is TCP/IP?")
|
| 35 |
+
st.write("- How does DNS work?")
|
| 36 |
+
st.write("- Explain VLANs")
|
| 37 |
+
|
| 38 |
+
# Display conversation history
|
| 39 |
+
for message in st.session_state.messages:
|
| 40 |
+
with st.chat_message(message["role"]):
|
| 41 |
+
st.markdown(f"**{message['role'].capitalize()} ({message['timestamp']})**: {message['content']}")
|
| 42 |
+
|
| 43 |
+
# Capture user input via chat interface
|
| 44 |
+
if user_input := st.chat_input("Type your question here"):
|
| 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})**: {user_input}")
|
| 52 |
+
|
| 53 |
+
# Check if the answer is cached
|
| 54 |
+
if user_input in st.session_state.cache:
|
| 55 |
+
answer = st.session_state.cache[user_input]
|
| 56 |
+
else:
|
| 57 |
+
# Query the API with a loading spinner
|
| 58 |
+
with st.spinner("Waiting for response..."):
|
| 59 |
+
output = query({"question": user_input})
|
| 60 |
+
if "error" in output:
|
| 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 |
+
st.markdown(f"**Assistant ({timestamp})**: {answer}")
|