Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -29,48 +29,76 @@ if huggingface_api_key:
|
|
29 |
|
30 |
|
31 |
# =======================
|
32 |
-
#
|
33 |
# =======================
|
34 |
-
st.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
# =======================
|
73 |
-
#
|
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 |
-
|
129 |
-
return "โ Both APIs failed. Please check your API keys or try again later."
|
130 |
-
return ai_msg
|
131 |
|
132 |
|
133 |
# =======================
|
134 |
-
# Chat UI
|
135 |
# =======================
|
136 |
-
def chat_ui(
|
137 |
-
st.subheader(
|
|
|
|
|
138 |
|
139 |
chat_container = st.container()
|
140 |
with chat_container:
|
141 |
for role, msg in chat_history:
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
st.write(msg)
|
145 |
-
|
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 |
-
#
|
172 |
# =======================
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
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
|
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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|