File size: 5,677 Bytes
b203e5a
2c836ab
b203e5a
2c836ab
b203e5a
 
2c836ab
b203e5a
2c836ab
b203e5a
2c836ab
 
 
 
 
b203e5a
 
 
 
 
 
 
 
 
 
 
 
 
2c836ab
b203e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c836ab
b203e5a
 
 
 
 
 
2c836ab
 
 
b203e5a
2c836ab
b203e5a
 
 
 
 
 
2c836ab
 
 
b203e5a
2c836ab
b203e5a
af552f4
b203e5a
 
 
 
 
 
 
 
2c836ab
b203e5a
 
 
 
 
 
 
 
 
 
2c836ab
 
 
b203e5a
 
 
 
 
 
2c836ab
b203e5a
 
2c836ab
 
 
b203e5a
ca60cc8
b203e5a
 
 
2c836ab
 
 
b203e5a
2c836ab
b203e5a
2c836ab
 
 
 
 
b203e5a
2c836ab
b203e5a
2c836ab
b203e5a
2c836ab
b203e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c836ab
b203e5a
 
 
 
2c836ab
b203e5a
 
 
2c836ab
 
 
 
b203e5a
 
 
2c836ab
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import os
import streamlit as st
from openai import OpenAI
from typing import List, Dict
import random
import base64

# Advanced Styling and Cosmic Theme
st.set_page_config(
    page_title="Grok Cosmic Companion",
    page_icon="πŸš€",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Cosmic Background Generator
def get_cosmic_background():
    # Create a dynamic, gradient cosmic background
    colors = [
        "linear-gradient(135deg, #1e2ad2, #8e2de2)",
        "linear-gradient(135deg, #ff6a00, #ee0979)",
        "linear-gradient(135deg, #000428, #004e92)",
        "linear-gradient(135deg, #2c3e50, #3498db)"
    ]
    return random.choice(colors)

# Advanced CSS with Cosmic Design
st.markdown(f"""
<style>
body {{
    background: {get_cosmic_background()};
    color: #ffffff;
    font-family: 'Orbitron', sans-serif;
}}

.stApp {{
    background: transparent;
}}

.main-container {{
    background: rgba(0,0,0,0.7);
    border-radius: 15px;
    padding: 20px;
    backdrop-filter: blur(10px);
}}

.chat-message {{
    background: rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 15px;
    margin-bottom: 10px;
    transition: all 0.3s ease;
}}

.chat-message:hover {{
    transform: scale(1.02);
    box-shadow: 0 0 20px rgba(255,255,255,0.2);
}}

.stTextInput > div > div > input {{
    background: rgba(255,255,255,0.1);
    color: white;
    border: 2px solid rgba(255,255,255,0.2);
    border-radius: 10px;
}}

.stButton > button {{
    background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 20px;
    transition: all 0.3s ease;
}}

.stButton > button:hover {{
    transform: scale(1.1);
    box-shadow: 0 0 20px rgba(255,255,255,0.3);
}}
</style>
""", unsafe_allow_html=True)

class AdvancedGrokChatApp:
    def __init__(self):
        # Cosmic AI Configuration
        self.XAI_API_KEY = "X-ai = xai-1HSpHLqxC3LnInrYpwAobgEVsjchUG0PP0adniSXWGQXwq6YfvcPto9MhsS6ouQtC4a4Dh2qqXmERgQQ"
        
        if not self.XAI_API_KEY:
            st.error("πŸ›Έ Cosmic Connection Lost: API Key Missing!")
            st.stop()
        
        self.client = OpenAI(
            api_key=self.XAI_API_KEY,
            base_url="https://api.x.ai/v1"
        )
        
        # Advanced Personality Prompts
        self.personality_modes = {
            "Cosmic Philosopher": "You are a wise AI that speaks like a blend of Douglas Adams and Carl Sagan.",
            "Intergalactic Comedian": "You are a witty AI that makes jokes about the universe's absurdities.",
            "Scientific Oracle": "You provide deep scientific insights with poetic eloquence."
        }
        
        self.current_mode = "Cosmic Philosopher"
        self.messages: List[Dict] = []

    def generate_response(self, user_input: str) -> str:
        try:
            # Enhanced Conversation Context
            system_prompt = (
                f"{self.personality_modes[self.current_mode]} "
                "Respond creatively, with depth and a touch of cosmic wonder."
            )
            
            conversation = [
                {"role": "system", "content": system_prompt}
            ] + self.messages + [
                {"role": "user", "content": user_input}
            ]

            response = self.client.chat.completions.create(
                model="grok-beta",
                messages=conversation,
                temperature=0.7,  # More creative responses
                max_tokens=300
            )

            return response.choices[0].message.content
        
        except Exception as e:
            return f"πŸŒ‹ Cosmic Disruption: {str(e)}"

    def add_message(self, role: str, content: str):
        self.messages.append({"role": role, "content": content})

def main():
    st.title("πŸš€ Grok: Cosmic Companion")
    
    # Initialize chat app with session state
    if 'chat_app' not in st.session_state:
        st.session_state.chat_app = AdvancedGrokChatApp()

    # Sidebar with Advanced Controls
    with st.sidebar:
        st.header("🌌 Cosmic Controls")
        
        # Personality Mode Selector
        mode = st.selectbox(
            "Choose Grok's Personality", 
            list(st.session_state.chat_app.personality_modes.keys())
        )
        st.session_state.chat_app.current_mode = mode
        
        # Conversation Management
        col1, col2 = st.columns(2)
        with col1:
            if st.button("πŸ”„ Reset"):
                st.session_state.chat_app.messages = []
                st.success("Conversation reset to cosmic zero!")
        
        with col2:
            if st.button("πŸ’Ύ Save Conversation"):
                # Placeholder for conversation saving logic
                st.info("Conversation saved to cosmic archives!")

    # Chat Interface
    for msg in st.session_state.chat_app.messages:
        with st.chat_message(msg['role'], avatar='πŸ€–' if msg['role'] == 'assistant' else 'πŸ‘€'):
            st.markdown(msg['content'])

    # User Input
    if user_input := st.chat_input("Whisper your cosmic query..."):
        # User Message
        st.session_state.chat_app.add_message("user", user_input)
        with st.chat_message("user", avatar='πŸ‘€'):
            st.markdown(user_input)

        # AI Response
        with st.chat_message("assistant", avatar='πŸš€'):
            with st.spinner("Traversing cosmic data streams..."):
                response = st.session_state.chat_app.generate_response(user_input)
                st.markdown(response)
        
        st.session_state.chat_app.add_message("assistant", response)

if __name__ == "__main__":
    main()