Engr-Usman-Ali commited on
Commit
e1c545c
ยท
verified ยท
1 Parent(s): 6e48fda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -115
app.py CHANGED
@@ -29,48 +29,76 @@ if huggingface_api_key:
29
 
30
 
31
  # =======================
32
- # Sidebar (Scrollable + Features)
33
  # =======================
34
- st.sidebar.title("โš™๏ธ Settings")
35
-
36
- with st.sidebar:
37
- st.markdown(f"**Groq API Status:** {groq_status}")
38
- st.markdown(f"**HuggingFace API Status:** {hf_status}")
39
-
40
- with st.expander("๐Ÿ”ง API Settings", expanded=True):
41
- api_priority = st.radio(
42
- "Choose API Priority",
43
- ["Groq First", "HuggingFace First"],
44
- index=0
45
- )
46
- model_choice = st.selectbox(
47
- "Choose Model",
48
- ["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
49
- )
50
 
51
- st.markdown("---")
52
- st.markdown("### ๐Ÿ“‚ Chats")
53
 
54
- # Sidebar dots + chat list
55
- if "custom_chats" not in st.session_state:
56
- st.session_state.custom_chats = {"Chat 1": []}
57
-
58
- for i, (chat_name, chat_history) in enumerate(st.session_state.custom_chats.items(), start=1):
59
- st.markdown(f"โ€ข {chat_name}")
60
 
61
- if st.button("+ New Chat"):
62
- new_chat_name = f"Chat {len(st.session_state.custom_chats) + 1}"
63
- st.session_state.custom_chats[new_chat_name] = []
 
 
64
 
 
 
 
 
65
 
66
- # =======================
67
- # Main Title
68
- # =======================
69
- st.title("๐Ÿค– CodeCraft AI - Mini Copilot (Chat Edition)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
  # =======================
73
- # Helper Functions
74
  # =======================
75
  def call_groq(system_prompt, chat_history):
76
  if not groq_client:
@@ -125,38 +153,33 @@ def get_ai_response(system_prompt, chat_history):
125
  if ai_msg is None:
126
  ai_msg = call_groq(system_prompt, chat_history)
127
 
128
- if ai_msg is None:
129
- return "โŒ Both APIs failed. Please check your API keys or try again later."
130
- return ai_msg
131
 
132
 
133
  # =======================
134
- # Chat UI Function
135
  # =======================
136
- def chat_ui(tab_name, chat_history, system_prompt, input_key):
137
- st.subheader(tab_name)
 
 
138
 
139
  chat_container = st.container()
140
  with chat_container:
141
  for role, msg in chat_history:
142
- if role == "user":
143
- with st.chat_message("user"):
 
 
 
 
 
 
 
 
 
144
  st.write(msg)
145
- else:
146
- with st.chat_message("assistant"):
147
- if "```" in msg:
148
- parts = msg.split("```")
149
- for i, part in enumerate(parts):
150
- if i % 2 == 1:
151
- lang, *code_lines = part.split("\n")
152
- code = "\n".join(code_lines)
153
- st.code(code, language=lang if lang else "python")
154
- else:
155
- st.write(part)
156
- else:
157
- st.write(msg)
158
-
159
- # --- Fixed input at bottom ---
160
  user_input = st.chat_input("Type your message...", key=input_key)
161
 
162
  if user_input:
@@ -168,67 +191,23 @@ def chat_ui(tab_name, chat_history, system_prompt, input_key):
168
 
169
 
170
  # =======================
171
- # Tabs
172
  # =======================
173
- tab1, tab2, tab3 = st.tabs(["๐Ÿ’ก Generate Code", "๐Ÿ›  Debug Code", "๐Ÿ“˜ Explain Code"])
174
-
175
- with tab1:
176
- if "generate_chat" not in st.session_state:
177
- st.session_state.generate_chat = []
178
- chat_ui(
179
- "๐Ÿ’ก Generate Code",
180
- st.session_state.generate_chat,
181
- "You are a helpful coding assistant. Generate correct code first, then a short simple explanation.",
182
- input_key="generate_input"
183
- )
184
-
185
- with tab2:
186
- if "debug_chat" not in st.session_state:
187
- st.session_state.debug_chat = []
188
- chat_ui(
189
- "๐Ÿ›  Debug Code",
190
- st.session_state.debug_chat,
191
- "You are an expert code debugger. Fix errors and give corrected code, then explain what changed and why in simple terms.",
192
- input_key="debug_input"
193
- )
194
-
195
- with tab3:
196
- if "explain_chat" not in st.session_state:
197
- st.session_state.explain_chat = []
198
- chat_ui(
199
- "๐Ÿ“˜ Explain Code",
200
- st.session_state.explain_chat,
201
- "You are a teacher that explains code in simple words. The user pastes code, and you explain step by step.",
202
- input_key="explain_input"
203
- )
204
 
205
 
206
  # =======================
207
- # Footer (Fixed bottom)
208
  # =======================
209
- st.markdown(
210
- """
211
- <style>
212
- .block-container {
213
- padding-bottom: 80px; /* leave space for footer */
214
- }
215
- footer {
216
- visibility: hidden;
217
- }
218
- .custom-footer {
219
- position: fixed;
220
- bottom: 0;
221
- left: 0;
222
- width: 100%;
223
- text-align: center;
224
- background-color: white;
225
- padding: 8px;
226
- border-top: 1px solid #ddd;
227
- }
228
- </style>
229
- <div class="custom-footer">
230
- โœจ CodeCraft may make mistakes. Always check important info.
231
- </div>
232
- """,
233
- unsafe_allow_html=True
234
- )
 
29
 
30
 
31
  # =======================
32
+ # Session State
33
  # =======================
34
+ if "chats" not in st.session_state:
35
+ st.session_state.chats = {
36
+ "๐Ÿ’ก Generate Code": [],
37
+ "๐Ÿ›  Debug Code": [],
38
+ "๐Ÿ“˜ Explain Code": []
39
+ }
40
+ if "active_chat" not in st.session_state:
41
+ st.session_state.active_chat = "๐Ÿ’ก Generate Code"
 
 
 
 
 
 
 
 
42
 
 
 
43
 
44
+ # =======================
45
+ # Sidebar - Chat Manager
46
+ # =======================
47
+ st.sidebar.title("โš™๏ธ Settings")
48
+ st.sidebar.markdown(f"**Groq API Status:** {groq_status}")
49
+ st.sidebar.markdown(f"**HuggingFace API Status:** {hf_status}")
50
 
51
+ api_priority = st.sidebar.radio(
52
+ "Choose API Priority",
53
+ ["Groq First", "HuggingFace First"],
54
+ index=0
55
+ )
56
 
57
+ model_choice = st.sidebar.selectbox(
58
+ "Choose Model",
59
+ ["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
60
+ )
61
 
62
+ st.sidebar.markdown("---")
63
+ st.sidebar.subheader("๐Ÿ’ฌ My Chats")
64
+
65
+ # Scrollable sidebar with chats
66
+ with st.sidebar.container():
67
+ for chat_name in list(st.session_state.chats.keys()):
68
+ col1, col2 = st.sidebar.columns([4, 1])
69
+ if col1.button(chat_name, key=f"open_{chat_name}"):
70
+ st.session_state.active_chat = chat_name
71
+
72
+ with col2:
73
+ dots = st.button("โ‹ฎ", key=f"menu_{chat_name}")
74
+ if dots:
75
+ action = st.radio(
76
+ f"Action for {chat_name}",
77
+ ["Cancel", "Rename", "Delete"],
78
+ key=f"action_{chat_name}",
79
+ horizontal=True
80
+ )
81
+ if action == "Rename":
82
+ new_name = st.text_input("New name", key=f"rename_{chat_name}")
83
+ if new_name and new_name not in st.session_state.chats:
84
+ st.session_state.chats[new_name] = st.session_state.chats.pop(chat_name)
85
+ st.session_state.active_chat = new_name
86
+ st.rerun()
87
+ elif action == "Delete":
88
+ st.session_state.chats.pop(chat_name)
89
+ if st.session_state.active_chat == chat_name:
90
+ st.session_state.active_chat = list(st.session_state.chats.keys())[0]
91
+ st.rerun()
92
+
93
+ if st.sidebar.button("โž• Add Chat"):
94
+ new_chat_name = f"Chat {len(st.session_state.chats) + 1}"
95
+ st.session_state.chats[new_chat_name] = []
96
+ st.session_state.active_chat = new_chat_name
97
+ st.rerun()
98
 
99
 
100
  # =======================
101
+ # API Calls
102
  # =======================
103
  def call_groq(system_prompt, chat_history):
104
  if not groq_client:
 
153
  if ai_msg is None:
154
  ai_msg = call_groq(system_prompt, chat_history)
155
 
156
+ return ai_msg or "โŒ Both APIs failed. Please check your API keys."
 
 
157
 
158
 
159
  # =======================
160
+ # Chat UI
161
  # =======================
162
+ def chat_ui(chat_name, system_prompt, input_key):
163
+ st.subheader(chat_name)
164
+
165
+ chat_history = st.session_state.chats[chat_name]
166
 
167
  chat_container = st.container()
168
  with chat_container:
169
  for role, msg in chat_history:
170
+ with st.chat_message(role):
171
+ if role == "assistant" and "```" in msg:
172
+ parts = msg.split("```")
173
+ for i, part in enumerate(parts):
174
+ if i % 2 == 1:
175
+ lang, *code_lines = part.split("\n")
176
+ code = "\n".join(code_lines)
177
+ st.code(code, language=lang if lang else "python")
178
+ else:
179
+ st.write(part)
180
+ else:
181
  st.write(msg)
182
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  user_input = st.chat_input("Type your message...", key=input_key)
184
 
185
  if user_input:
 
191
 
192
 
193
  # =======================
194
+ # Main Chat Area
195
  # =======================
196
+ system_prompts = {
197
+ "๐Ÿ’ก Generate Code": "You are a helpful coding assistant...",
198
+ "๐Ÿ›  Debug Code": "You are an expert code debugger...",
199
+ "๐Ÿ“˜ Explain Code": "You are a teacher explaining code..."
200
+ }
201
+
202
+ active = st.session_state.active_chat
203
+ if active in system_prompts:
204
+ chat_ui(active, system_prompts[active], input_key=f"{active}_input")
205
+ else:
206
+ chat_ui(active, "You are a helpful assistant.", input_key=f"{active}_input")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
 
209
  # =======================
210
+ # Footer
211
  # =======================
212
+ st.markdown("---")
213
+ st.caption("โœจ CodeCraft may make mistakes. Check important info.")