Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,11 @@ import pytz
|
|
| 7 |
import platform
|
| 8 |
import uuid
|
| 9 |
import extra_streamlit_components as stx
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Set page config
|
| 12 |
-
st.set_page_config(page_title="Personalized Real-Time Chat", page_icon="💬", layout="wide")
|
| 13 |
|
| 14 |
# Initialize cookie manager
|
| 15 |
cookie_manager = stx.CookieManager()
|
|
@@ -66,6 +68,23 @@ if 'users' not in st.session_state:
|
|
| 66 |
if 'current_user' not in st.session_state:
|
| 67 |
st.session_state.current_user = get_or_create_user()
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Sidebar for user information and settings
|
| 70 |
with st.sidebar:
|
| 71 |
st.title("User Info")
|
|
@@ -82,27 +101,39 @@ with st.sidebar:
|
|
| 82 |
save_data()
|
| 83 |
st.success(f"Name updated to {new_name}")
|
| 84 |
break
|
| 85 |
-
|
| 86 |
st.title("Active Users")
|
| 87 |
for user in st.session_state.users:
|
| 88 |
st.write(f"{user['name']} ({user['browser']})")
|
| 89 |
|
| 90 |
# Main chat area
|
| 91 |
-
st.title("Personalized Real-Time Chat")
|
| 92 |
|
| 93 |
# Display chat messages
|
| 94 |
chat_container = st.container()
|
| 95 |
|
| 96 |
# Input for new message
|
| 97 |
-
new_message = st.text_input("Type your message:")
|
| 98 |
-
if st.button("Send"):
|
| 99 |
if new_message:
|
| 100 |
timestamp = datetime.now(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %Z')
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
save_data()
|
| 107 |
st.experimental_rerun()
|
| 108 |
|
|
|
|
| 7 |
import platform
|
| 8 |
import uuid
|
| 9 |
import extra_streamlit_components as stx
|
| 10 |
+
import requests
|
| 11 |
+
from urllib.parse import quote
|
| 12 |
|
| 13 |
# Set page config
|
| 14 |
+
st.set_page_config(page_title="Personalized Real-Time Chat with ArXiv Search", page_icon="💬", layout="wide")
|
| 15 |
|
| 16 |
# Initialize cookie manager
|
| 17 |
cookie_manager = stx.CookieManager()
|
|
|
|
| 68 |
if 'current_user' not in st.session_state:
|
| 69 |
st.session_state.current_user = get_or_create_user()
|
| 70 |
|
| 71 |
+
# ArXiv search function
|
| 72 |
+
def search_arxiv(query):
|
| 73 |
+
base_url = "http://export.arxiv.org/api/query?"
|
| 74 |
+
search_query = f"search_query=all:{quote(query)}&start=0&max_results=5"
|
| 75 |
+
response = requests.get(base_url + search_query)
|
| 76 |
+
|
| 77 |
+
if response.status_code == 200:
|
| 78 |
+
results = []
|
| 79 |
+
for entry in response.text.split('<entry>')[1:]:
|
| 80 |
+
title = entry.split('<title>')[1].split('</title>')[0]
|
| 81 |
+
summary = entry.split('<summary>')[1].split('</summary>')[0]
|
| 82 |
+
link = entry.split('<id>')[1].split('</id>')[0]
|
| 83 |
+
results.append(f"Title: {title}\nSummary: {summary}\nLink: {link}\n")
|
| 84 |
+
return "\n".join(results)
|
| 85 |
+
else:
|
| 86 |
+
return "Error fetching results from ArXiv."
|
| 87 |
+
|
| 88 |
# Sidebar for user information and settings
|
| 89 |
with st.sidebar:
|
| 90 |
st.title("User Info")
|
|
|
|
| 101 |
save_data()
|
| 102 |
st.success(f"Name updated to {new_name}")
|
| 103 |
break
|
| 104 |
+
|
| 105 |
st.title("Active Users")
|
| 106 |
for user in st.session_state.users:
|
| 107 |
st.write(f"{user['name']} ({user['browser']})")
|
| 108 |
|
| 109 |
# Main chat area
|
| 110 |
+
st.title("Personalized Real-Time Chat with ArXiv Search")
|
| 111 |
|
| 112 |
# Display chat messages
|
| 113 |
chat_container = st.container()
|
| 114 |
|
| 115 |
# Input for new message
|
| 116 |
+
new_message = st.text_input("Type your message or ArXiv search query:")
|
| 117 |
+
if st.button("Send / Search"):
|
| 118 |
if new_message:
|
| 119 |
timestamp = datetime.now(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %Z')
|
| 120 |
+
|
| 121 |
+
# Check if it's an ArXiv search query
|
| 122 |
+
if new_message.lower().startswith("arxiv:"):
|
| 123 |
+
query = new_message[6:].strip()
|
| 124 |
+
search_results = search_arxiv(query)
|
| 125 |
+
st.session_state.messages.append({
|
| 126 |
+
'user': 'ArXiv Bot',
|
| 127 |
+
'message': f"Search results for '{query}':\n\n{search_results}",
|
| 128 |
+
'timestamp': timestamp
|
| 129 |
+
})
|
| 130 |
+
else:
|
| 131 |
+
st.session_state.messages.append({
|
| 132 |
+
'user': st.session_state.current_user['name'],
|
| 133 |
+
'message': new_message,
|
| 134 |
+
'timestamp': timestamp
|
| 135 |
+
})
|
| 136 |
+
|
| 137 |
save_data()
|
| 138 |
st.experimental_rerun()
|
| 139 |
|