Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,10 @@ 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()
|
|
@@ -87,6 +88,27 @@ def search_arxiv(query):
|
|
| 87 |
else:
|
| 88 |
return "Error fetching results from ArXiv."
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# Sidebar for user information and settings
|
| 91 |
with st.sidebar:
|
| 92 |
st.title("User Info")
|
|
@@ -109,7 +131,7 @@ with st.sidebar:
|
|
| 109 |
st.write(f"{user['name']} ({user['browser']})")
|
| 110 |
|
| 111 |
# Main chat area
|
| 112 |
-
st.title("Personalized Real-Time Chat with ArXiv Search")
|
| 113 |
|
| 114 |
# Display chat messages
|
| 115 |
for message in st.session_state.messages:
|
|
@@ -129,13 +151,19 @@ if prompt := st.chat_input("Type your message or ArXiv search query:"):
|
|
| 129 |
with st.spinner("Searching ArXiv..."):
|
| 130 |
search_results = search_arxiv(query)
|
| 131 |
st.markdown(f"Search results for '{query}':\n\n{search_results}")
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
else:
|
| 134 |
-
#
|
| 135 |
-
# For this example, we'll just echo the message
|
| 136 |
with st.chat_message("assistant"):
|
| 137 |
-
st.
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
save_data()
|
| 141 |
st.rerun()
|
|
|
|
| 9 |
import extra_streamlit_components as stx
|
| 10 |
import requests
|
| 11 |
from urllib.parse import quote
|
| 12 |
+
from openai import OpenAI
|
| 13 |
|
| 14 |
# Set page config
|
| 15 |
+
st.set_page_config(page_title="Personalized Real-Time Chat with ArXiv Search and AI", page_icon="💬", layout="wide")
|
| 16 |
|
| 17 |
# Initialize cookie manager
|
| 18 |
cookie_manager = stx.CookieManager()
|
|
|
|
| 88 |
else:
|
| 89 |
return "Error fetching results from ArXiv."
|
| 90 |
|
| 91 |
+
# Initialize OpenAI client
|
| 92 |
+
client = OpenAI(api_key=st.secrets['OPENAI_API_KEY'])
|
| 93 |
+
MODEL = "gpt-4-0125-preview" # Use the appropriate model
|
| 94 |
+
|
| 95 |
+
# Function to get AI response
|
| 96 |
+
def get_ai_response(prompt, context=""):
|
| 97 |
+
try:
|
| 98 |
+
messages = [
|
| 99 |
+
{"role": "system", "content": "You are a helpful assistant in a chat room that can also search ArXiv."},
|
| 100 |
+
{"role": "user", "content": f"Context: {context}\n\nUser Query: {prompt}"}
|
| 101 |
+
]
|
| 102 |
+
response = client.chat.completions.create(
|
| 103 |
+
model=MODEL,
|
| 104 |
+
messages=messages,
|
| 105 |
+
max_tokens=150,
|
| 106 |
+
temperature=0.7
|
| 107 |
+
)
|
| 108 |
+
return response.choices[0].message.content
|
| 109 |
+
except Exception as e:
|
| 110 |
+
return f"An error occurred: {str(e)}"
|
| 111 |
+
|
| 112 |
# Sidebar for user information and settings
|
| 113 |
with st.sidebar:
|
| 114 |
st.title("User Info")
|
|
|
|
| 131 |
st.write(f"{user['name']} ({user['browser']})")
|
| 132 |
|
| 133 |
# Main chat area
|
| 134 |
+
st.title("Personalized Real-Time Chat with ArXiv Search and AI")
|
| 135 |
|
| 136 |
# Display chat messages
|
| 137 |
for message in st.session_state.messages:
|
|
|
|
| 151 |
with st.spinner("Searching ArXiv..."):
|
| 152 |
search_results = search_arxiv(query)
|
| 153 |
st.markdown(f"Search results for '{query}':\n\n{search_results}")
|
| 154 |
+
|
| 155 |
+
# Get AI commentary on the search results
|
| 156 |
+
ai_commentary = get_ai_response(f"Provide a brief analysis of these ArXiv search results: {search_results}")
|
| 157 |
+
st.markdown(f"\nAI Analysis:\n{ai_commentary}")
|
| 158 |
+
|
| 159 |
+
st.session_state.messages.append({"role": "assistant", "content": f"Search results for '{query}':\n\n{search_results}\n\nAI Analysis:\n{ai_commentary}"})
|
| 160 |
else:
|
| 161 |
+
# Get AI response for regular chat
|
|
|
|
| 162 |
with st.chat_message("assistant"):
|
| 163 |
+
with st.spinner("AI is thinking..."):
|
| 164 |
+
ai_response = get_ai_response(prompt)
|
| 165 |
+
st.markdown(ai_response)
|
| 166 |
+
st.session_state.messages.append({"role": "assistant", "content": ai_response})
|
| 167 |
|
| 168 |
save_data()
|
| 169 |
st.rerun()
|