noumanjavaid commited on
Commit
2c836ab
Β·
verified Β·
1 Parent(s): e95c703

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from typing import List, Dict
4
+
5
+ # Set up page configuration
6
+ st.set_page_config(
7
+ page_title="Grok AI Assistant",
8
+ page_icon="πŸš€",
9
+ layout="wide",
10
+ initial_sidebar_state="expanded"
11
+ )
12
+
13
+ # Custom CSS for enhanced styling
14
+ st.markdown("""
15
+ <style>
16
+ .main-container {
17
+ background-color: #0E1117;
18
+ color: #FFFFFF;
19
+ font-family: 'Roboto', sans-serif;
20
+ }
21
+ .stTextInput>div>div>input {
22
+ background-color: #262730;
23
+ color: white;
24
+ border: 2px solid #4A4A4A;
25
+ }
26
+ .stButton>button {
27
+ background-color: #FF4B4B;
28
+ color: white;
29
+ border: none;
30
+ padding: 10px 20px;
31
+ border-radius: 5px;
32
+ transition: all 0.3s ease;
33
+ }
34
+ .stButton>button:hover {
35
+ background-color: #FF6B6B;
36
+ transform: scale(1.05);
37
+ }
38
+ .message-container {
39
+ background-color: #262730;
40
+ border-radius: 10px;
41
+ padding: 15px;
42
+ margin-bottom: 10px;
43
+ }
44
+ .user-message {
45
+ background-color: #4A4A4A;
46
+ text-align: right;
47
+ }
48
+ .ai-message {
49
+ background-color: #1E1E2E;
50
+ }
51
+ </style>
52
+ """, unsafe_allow_html=True)
53
+
54
+ # Initialize OpenAI client
55
+ openai.api_key = st.secrets.get("OPENAI_API_KEY", "xai-1HSpHLqxC3LnInrYpwAobgEVsjchUG0PP0adniSXWGQXwq6YfvcPto9MhsS6ouQtC4a4Dh2qqXmERgQQ")
56
+ openai.base_url = "https://api.x.ai/v1"
57
+
58
+ class GrokChatApp:
59
+ def __init__(self):
60
+ self.messages: List[Dict] = []
61
+ self.system_prompt = (
62
+ "You are Grok, a witty AI assistant inspired by the Hitchhiker's Guide to the Galaxy. "
63
+ "Provide creative, humorous, and insightful responses with a touch of cosmic wisdom."
64
+ )
65
+
66
+ def generate_response(self, user_input: str) -> str:
67
+ try:
68
+ # Prepare messages for API call
69
+ conversation = [
70
+ {"role": "system", "content": self.system_prompt}
71
+ ] + [
72
+ {"role": msg['role'], "content": msg['content']}
73
+ for msg in self.messages
74
+ ] + [
75
+ {"role": "user", "content": user_input}
76
+ ]
77
+
78
+ # Call Grok AI
79
+ response = openai.chat.completions.create(
80
+ model="grok-vision-beta",
81
+ messages=conversation
82
+ )
83
+
84
+ return response.choices[0].message.content
85
+ except Exception as e:
86
+ return f"🚨 Error: {str(e)}"
87
+
88
+ def add_message(self, role: str, content: str):
89
+ self.messages.append({"role": role, "content": content})
90
+
91
+ def main():
92
+ st.title("πŸš€ Grok AI: Cosmic Conversations")
93
+
94
+ # Initialize chat app
95
+ if 'chat_app' not in st.session_state:
96
+ st.session_state.chat_app = GrokChatApp()
97
+
98
+ # Chat history display
99
+ chat_container = st.container()
100
+ with chat_container:
101
+ for msg in st.session_state.chat_app.messages:
102
+ with st.chat_message(msg['role'], avatar='πŸ€–' if msg['role'] == 'assistant' else 'πŸ‘€'):
103
+ st.markdown(msg['content'])
104
+
105
+ # User input
106
+ user_input = st.chat_input("Ask Grok anything about life, universe, and everything...")
107
+
108
+ if user_input:
109
+ # Display user message
110
+ st.session_state.chat_app.add_message("user", user_input)
111
+ with st.chat_message("user", avatar='πŸ‘€'):
112
+ st.markdown(user_input)
113
+
114
+ # Generate and display AI response
115
+ with st.chat_message("assistant", avatar='πŸ€–'):
116
+ with st.spinner("Grok is pondering the mysteries of the universe..."):
117
+ response = st.session_state.chat_app.generate_response(user_input)
118
+ st.markdown(response)
119
+
120
+ st.session_state.chat_app.add_message("assistant", response)
121
+
122
+ if __name__ == "__main__":
123
+ main()