Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -29,6 +29,8 @@ def get_cosmic_background() -> str:
|
|
29 |
# Advanced CSS with Cosmic Design
|
30 |
st.markdown(f"""
|
31 |
<style>
|
|
|
|
|
32 |
body {{
|
33 |
background: {get_cosmic_background()};
|
34 |
color: #ffffff;
|
@@ -72,16 +74,31 @@ st.markdown(f"""
|
|
72 |
transform: scale(1.1);
|
73 |
box-shadow: 0 0 20px rgba(255,255,255,0.3);
|
74 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
</style>
|
76 |
""", unsafe_allow_html=True)
|
77 |
|
78 |
class AdvancedGrokChatApp:
|
79 |
def __init__(self):
|
80 |
-
|
81 |
-
|
82 |
-
if not self.XAI_API_KEY:
|
83 |
-
st.error("πΈ Cosmic Connection Lost: Please set the XAI_API_KEY environment variable!")
|
84 |
-
st.stop()
|
85 |
|
86 |
try:
|
87 |
self.client = OpenAI(
|
@@ -92,11 +109,11 @@ class AdvancedGrokChatApp:
|
|
92 |
st.error(f"πΈ Failed to initialize OpenAI client: {str(e)}")
|
93 |
st.stop()
|
94 |
|
95 |
-
# Advanced Personality Prompts
|
96 |
self.personality_modes: Dict[str, str] = {
|
97 |
"Cosmic Philosopher": "You are a wise AI that speaks like a blend of Douglas Adams and Carl Sagan.",
|
98 |
"Intergalactic Comedian": "You are a witty AI that makes jokes about the universe's absurdities.",
|
99 |
-
"Scientific Oracle": "You provide deep scientific insights with poetic eloquence."
|
|
|
100 |
}
|
101 |
|
102 |
self.current_mode: str = "Cosmic Philosopher"
|
@@ -133,13 +150,18 @@ class AdvancedGrokChatApp:
|
|
133 |
"""Add a message to the conversation history."""
|
134 |
self.messages.append({"role": role, "content": content})
|
135 |
|
|
|
|
|
|
|
|
|
136 |
def initialize_session_state() -> None:
|
137 |
"""Initialize or reset the session state."""
|
138 |
if 'chat_app' not in st.session_state:
|
139 |
st.session_state.chat_app = AdvancedGrokChatApp()
|
140 |
|
141 |
def main() -> None:
|
142 |
-
|
|
|
143 |
|
144 |
initialize_session_state()
|
145 |
|
@@ -157,26 +179,30 @@ def main() -> None:
|
|
157 |
# Conversation Management
|
158 |
col1, col2 = st.columns(2)
|
159 |
with col1:
|
160 |
-
if st.button("π Reset"):
|
161 |
st.session_state.chat_app.messages = []
|
162 |
-
st.success("Conversation reset to cosmic zero!")
|
163 |
|
164 |
with col2:
|
165 |
if st.button("πΎ Save Chat"):
|
|
|
166 |
st.download_button(
|
167 |
-
label="Download
|
168 |
-
data=
|
169 |
-
file_name="
|
170 |
mime="text/plain"
|
171 |
)
|
172 |
|
173 |
# Chat Interface
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
177 |
|
178 |
# User Input
|
179 |
-
if user_input := st.chat_input("Whisper your cosmic query..."):
|
180 |
# User Message
|
181 |
st.session_state.chat_app.add_message("user", user_input)
|
182 |
with st.chat_message("user", avatar='π€'):
|
@@ -184,7 +210,7 @@ def main() -> None:
|
|
184 |
|
185 |
# AI Response
|
186 |
with st.chat_message("assistant", avatar='π'):
|
187 |
-
with st.spinner("Traversing cosmic data streams..."):
|
188 |
response = st.session_state.chat_app.generate_response(user_input)
|
189 |
st.markdown(response)
|
190 |
|
|
|
29 |
# Advanced CSS with Cosmic Design
|
30 |
st.markdown(f"""
|
31 |
<style>
|
32 |
+
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;600;700&display=swap');
|
33 |
+
|
34 |
body {{
|
35 |
background: {get_cosmic_background()};
|
36 |
color: #ffffff;
|
|
|
74 |
transform: scale(1.1);
|
75 |
box-shadow: 0 0 20px rgba(255,255,255,0.3);
|
76 |
}}
|
77 |
+
|
78 |
+
.cosmic-title {{
|
79 |
+
text-align: center;
|
80 |
+
font-size: 2.5rem;
|
81 |
+
margin-bottom: 2rem;
|
82 |
+
text-shadow: 0 0 10px rgba(255,255,255,0.5);
|
83 |
+
animation: glow 2s ease-in-out infinite alternate;
|
84 |
+
}}
|
85 |
+
|
86 |
+
@keyframes glow {{
|
87 |
+
from {{
|
88 |
+
text-shadow: 0 0 10px rgba(255,255,255,0.5);
|
89 |
+
}}
|
90 |
+
to {{
|
91 |
+
text-shadow: 0 0 20px rgba(255,255,255,0.8),
|
92 |
+
0 0 30px rgba(255,255,255,0.6);
|
93 |
+
}}
|
94 |
+
}}
|
95 |
</style>
|
96 |
""", unsafe_allow_html=True)
|
97 |
|
98 |
class AdvancedGrokChatApp:
|
99 |
def __init__(self):
|
100 |
+
"""Initialize the chat application with API configuration and personality modes."""
|
101 |
+
self.XAI_API_KEY: str = "xai-1HSpHLqxC3LnInrYpwAobgEVsjchUG0PP0adniSXWGQXwq6YfvcPto9MhsS6ouQtC4a4Dh2qqXmERgQQ"
|
|
|
|
|
|
|
102 |
|
103 |
try:
|
104 |
self.client = OpenAI(
|
|
|
109 |
st.error(f"πΈ Failed to initialize OpenAI client: {str(e)}")
|
110 |
st.stop()
|
111 |
|
|
|
112 |
self.personality_modes: Dict[str, str] = {
|
113 |
"Cosmic Philosopher": "You are a wise AI that speaks like a blend of Douglas Adams and Carl Sagan.",
|
114 |
"Intergalactic Comedian": "You are a witty AI that makes jokes about the universe's absurdities.",
|
115 |
+
"Scientific Oracle": "You provide deep scientific insights with poetic eloquence.",
|
116 |
+
"Space Explorer": "You are an adventurous AI exploring the mysteries of the cosmos."
|
117 |
}
|
118 |
|
119 |
self.current_mode: str = "Cosmic Philosopher"
|
|
|
150 |
"""Add a message to the conversation history."""
|
151 |
self.messages.append({"role": role, "content": content})
|
152 |
|
153 |
+
def save_conversation(self) -> str:
|
154 |
+
"""Save the conversation history to a string format."""
|
155 |
+
return "\n".join([f"{msg['role']}: {msg['content']}" for msg in self.messages])
|
156 |
+
|
157 |
def initialize_session_state() -> None:
|
158 |
"""Initialize or reset the session state."""
|
159 |
if 'chat_app' not in st.session_state:
|
160 |
st.session_state.chat_app = AdvancedGrokChatApp()
|
161 |
|
162 |
def main() -> None:
|
163 |
+
"""Main application function."""
|
164 |
+
st.markdown('<h1 class="cosmic-title">π Grok: Cosmic Companion</h1>', unsafe_allow_html=True)
|
165 |
|
166 |
initialize_session_state()
|
167 |
|
|
|
179 |
# Conversation Management
|
180 |
col1, col2 = st.columns(2)
|
181 |
with col1:
|
182 |
+
if st.button("π Reset Chat"):
|
183 |
st.session_state.chat_app.messages = []
|
184 |
+
st.success("β¨ Conversation reset to cosmic zero!")
|
185 |
|
186 |
with col2:
|
187 |
if st.button("πΎ Save Chat"):
|
188 |
+
conversation_text = st.session_state.chat_app.save_conversation()
|
189 |
st.download_button(
|
190 |
+
label="π₯ Download",
|
191 |
+
data=conversation_text,
|
192 |
+
file_name="cosmic_conversation.txt",
|
193 |
mime="text/plain"
|
194 |
)
|
195 |
|
196 |
# Chat Interface
|
197 |
+
chat_container = st.container()
|
198 |
+
|
199 |
+
with chat_container:
|
200 |
+
for msg in st.session_state.chat_app.messages:
|
201 |
+
with st.chat_message(msg['role'], avatar='π€' if msg['role'] == 'assistant' else 'π€'):
|
202 |
+
st.markdown(msg['content'])
|
203 |
|
204 |
# User Input
|
205 |
+
if user_input := st.chat_input("π Whisper your cosmic query..."):
|
206 |
# User Message
|
207 |
st.session_state.chat_app.add_message("user", user_input)
|
208 |
with st.chat_message("user", avatar='π€'):
|
|
|
210 |
|
211 |
# AI Response
|
212 |
with st.chat_message("assistant", avatar='π'):
|
213 |
+
with st.spinner("π Traversing cosmic data streams..."):
|
214 |
response = st.session_state.chat_app.generate_response(user_input)
|
215 |
st.markdown(response)
|
216 |
|