Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,12 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from openai import OpenAI
|
4 |
-
from typing import List, Dict
|
5 |
import random
|
6 |
-
import
|
|
|
|
|
|
|
7 |
|
8 |
# Advanced Styling and Cosmic Theme
|
9 |
st.set_page_config(
|
@@ -13,9 +16,8 @@ st.set_page_config(
|
|
13 |
initial_sidebar_state="expanded"
|
14 |
)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
# Create a dynamic, gradient cosmic background
|
19 |
colors = [
|
20 |
"linear-gradient(135deg, #1e2ad2, #8e2de2)",
|
21 |
"linear-gradient(135deg, #ff6a00, #ee0979)",
|
@@ -27,86 +29,82 @@ def get_cosmic_background():
|
|
27 |
# Advanced CSS with Cosmic Design
|
28 |
st.markdown(f"""
|
29 |
<style>
|
30 |
-
body {{
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
}}
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
.
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
transition: all 0.3s ease;
|
74 |
-
}}
|
75 |
-
|
76 |
-
.stButton > button:hover {{
|
77 |
-
transform: scale(1.1);
|
78 |
-
box-shadow: 0 0 20px rgba(255,255,255,0.3);
|
79 |
-
}}
|
80 |
</style>
|
81 |
""", unsafe_allow_html=True)
|
82 |
|
83 |
class AdvancedGrokChatApp:
|
84 |
def __init__(self):
|
85 |
-
|
86 |
-
self.XAI_API_KEY = "X-ai = xai-1HSpHLqxC3LnInrYpwAobgEVsjchUG0PP0adniSXWGQXwq6YfvcPto9MhsS6ouQtC4a4Dh2qqXmERgQQ"
|
87 |
|
88 |
if not self.XAI_API_KEY:
|
89 |
-
st.error("πΈ Cosmic Connection Lost:
|
90 |
st.stop()
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
96 |
|
97 |
# Advanced Personality Prompts
|
98 |
-
self.personality_modes = {
|
99 |
"Cosmic Philosopher": "You are a wise AI that speaks like a blend of Douglas Adams and Carl Sagan.",
|
100 |
"Intergalactic Comedian": "You are a witty AI that makes jokes about the universe's absurdities.",
|
101 |
"Scientific Oracle": "You provide deep scientific insights with poetic eloquence."
|
102 |
}
|
103 |
|
104 |
-
self.current_mode = "Cosmic Philosopher"
|
105 |
-
self.messages: List[Dict] = []
|
106 |
|
107 |
def generate_response(self, user_input: str) -> str:
|
|
|
108 |
try:
|
109 |
-
# Enhanced Conversation Context
|
110 |
system_prompt = (
|
111 |
f"{self.personality_modes[self.current_mode]} "
|
112 |
"Respond creatively, with depth and a touch of cosmic wonder."
|
@@ -121,25 +119,30 @@ class AdvancedGrokChatApp:
|
|
121 |
response = self.client.chat.completions.create(
|
122 |
model="grok-beta",
|
123 |
messages=conversation,
|
124 |
-
temperature=0.7,
|
125 |
max_tokens=300
|
126 |
)
|
127 |
|
128 |
return response.choices[0].message.content
|
129 |
|
130 |
except Exception as e:
|
131 |
-
|
|
|
132 |
|
133 |
-
def add_message(self, role: str, content: str):
|
|
|
134 |
self.messages.append({"role": role, "content": content})
|
135 |
|
136 |
-
def
|
137 |
-
|
138 |
-
|
139 |
-
# Initialize chat app with session state
|
140 |
if 'chat_app' not in st.session_state:
|
141 |
st.session_state.chat_app = AdvancedGrokChatApp()
|
142 |
|
|
|
|
|
|
|
|
|
|
|
143 |
# Sidebar with Advanced Controls
|
144 |
with st.sidebar:
|
145 |
st.header("π Cosmic Controls")
|
@@ -159,9 +162,13 @@ def main():
|
|
159 |
st.success("Conversation reset to cosmic zero!")
|
160 |
|
161 |
with col2:
|
162 |
-
if st.button("πΎ Save
|
163 |
-
|
164 |
-
|
|
|
|
|
|
|
|
|
165 |
|
166 |
# Chat Interface
|
167 |
for msg in st.session_state.chat_app.messages:
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from openai import OpenAI
|
4 |
+
from typing import List, Dict, Optional
|
5 |
import random
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
|
11 |
# Advanced Styling and Cosmic Theme
|
12 |
st.set_page_config(
|
|
|
16 |
initial_sidebar_state="expanded"
|
17 |
)
|
18 |
|
19 |
+
def get_cosmic_background() -> str:
|
20 |
+
"""Generate a random cosmic gradient background."""
|
|
|
21 |
colors = [
|
22 |
"linear-gradient(135deg, #1e2ad2, #8e2de2)",
|
23 |
"linear-gradient(135deg, #ff6a00, #ee0979)",
|
|
|
29 |
# Advanced CSS with Cosmic Design
|
30 |
st.markdown(f"""
|
31 |
<style>
|
32 |
+
body {{
|
33 |
+
background: {get_cosmic_background()};
|
34 |
+
color: #ffffff;
|
35 |
+
font-family: 'Orbitron', sans-serif;
|
36 |
+
}}
|
37 |
+
.stApp {{
|
38 |
+
background: transparent;
|
39 |
+
}}
|
40 |
+
.main-container {{
|
41 |
+
background: rgba(0,0,0,0.7);
|
42 |
+
border-radius: 15px;
|
43 |
+
padding: 20px;
|
44 |
+
backdrop-filter: blur(10px);
|
45 |
+
}}
|
46 |
+
.chat-message {{
|
47 |
+
background: rgba(255,255,255,0.1);
|
48 |
+
border-radius: 10px;
|
49 |
+
padding: 15px;
|
50 |
+
margin-bottom: 10px;
|
51 |
+
transition: all 0.3s ease;
|
52 |
+
}}
|
53 |
+
.chat-message:hover {{
|
54 |
+
transform: scale(1.02);
|
55 |
+
box-shadow: 0 0 20px rgba(255,255,255,0.2);
|
56 |
+
}}
|
57 |
+
.stTextInput > div > div > input {{
|
58 |
+
background: rgba(255,255,255,0.1);
|
59 |
+
color: white;
|
60 |
+
border: 2px solid rgba(255,255,255,0.2);
|
61 |
+
border-radius: 10px;
|
62 |
+
}}
|
63 |
+
.stButton > button {{
|
64 |
+
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
|
65 |
+
color: white;
|
66 |
+
border: none;
|
67 |
+
padding: 10px 20px;
|
68 |
+
border-radius: 20px;
|
69 |
+
transition: all 0.3s ease;
|
70 |
+
}}
|
71 |
+
.stButton > button:hover {{
|
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 |
+
self.XAI_API_KEY: Optional[str] = os.getenv('XAI_API_KEY')
|
|
|
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(
|
88 |
+
api_key=self.XAI_API_KEY,
|
89 |
+
base_url="https://api.x.ai/v1"
|
90 |
+
)
|
91 |
+
except Exception as e:
|
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"
|
103 |
+
self.messages: List[Dict[str, str]] = []
|
104 |
|
105 |
def generate_response(self, user_input: str) -> str:
|
106 |
+
"""Generate AI response based on user input and conversation history."""
|
107 |
try:
|
|
|
108 |
system_prompt = (
|
109 |
f"{self.personality_modes[self.current_mode]} "
|
110 |
"Respond creatively, with depth and a touch of cosmic wonder."
|
|
|
119 |
response = self.client.chat.completions.create(
|
120 |
model="grok-beta",
|
121 |
messages=conversation,
|
122 |
+
temperature=0.7,
|
123 |
max_tokens=300
|
124 |
)
|
125 |
|
126 |
return response.choices[0].message.content
|
127 |
|
128 |
except Exception as e:
|
129 |
+
st.error(f"π Cosmic Disruption: {str(e)}")
|
130 |
+
return "I apologize, but I'm experiencing a cosmic disturbance. Please try again."
|
131 |
|
132 |
+
def add_message(self, role: str, content: str) -> None:
|
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 |
+
st.title("π Grok: Cosmic Companion")
|
143 |
+
|
144 |
+
initialize_session_state()
|
145 |
+
|
146 |
# Sidebar with Advanced Controls
|
147 |
with st.sidebar:
|
148 |
st.header("π Cosmic Controls")
|
|
|
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 Chat",
|
168 |
+
data=str(st.session_state.chat_app.messages),
|
169 |
+
file_name="cosmic_chat.txt",
|
170 |
+
mime="text/plain"
|
171 |
+
)
|
172 |
|
173 |
# Chat Interface
|
174 |
for msg in st.session_state.chat_app.messages:
|