Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,106 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
st.
|
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 |
-
st.session_state.
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
reply = f"Hereβs the corrected version of your code:\n\n{user_input}\n\nβ
Fixed and explained."
|
105 |
-
elif tab_name == "Generate":
|
106 |
-
reply = f"Generated working code for:\n\n{user_input}\n\nβ
Explained simply."
|
107 |
-
else:
|
108 |
-
reply = f"Explanation:\n\n{user_input}\n\nβ
Made clear."
|
109 |
-
|
110 |
-
# Save AI reply
|
111 |
-
st.session_state.chats[tab_name].append(("ai", reply))
|
112 |
-
|
113 |
-
# Clear input
|
114 |
-
st.session_state[f"{tab_name}_input_key"] = ""
|
115 |
-
|
116 |
-
|
117 |
-
# Render all three tabs
|
118 |
-
with tabs[0]:
|
119 |
-
render_chat("Debug")
|
120 |
-
with tabs[1]:
|
121 |
-
render_chat("Generate")
|
122 |
-
with tabs[2]:
|
123 |
-
render_chat("Explain")
|
124 |
|
125 |
|
126 |
|
|
|
1 |
import streamlit as st
|
2 |
+
from groq import Groq
|
3 |
+
|
4 |
+
# Initialize Groq client
|
5 |
+
client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
6 |
+
|
7 |
+
# Sidebar model selector
|
8 |
+
st.sidebar.title("βοΈ Settings")
|
9 |
+
model_choice = st.sidebar.selectbox(
|
10 |
+
"Choose AI Model",
|
11 |
+
["llama-3.1-8b-instant", "llama-3.1-70b-versatile", "mixtral-8x7b-32768"]
|
12 |
+
)
|
13 |
+
|
14 |
+
st.title("π€ CodeCraft AI - Mini Copilot (Chat Edition)")
|
15 |
+
|
16 |
+
# Session state for chats
|
17 |
+
if "generate_chat" not in st.session_state:
|
18 |
+
st.session_state.generate_chat = []
|
19 |
+
if "debug_chat" not in st.session_state:
|
20 |
+
st.session_state.debug_chat = []
|
21 |
+
if "explain_chat" not in st.session_state:
|
22 |
+
st.session_state.explain_chat = []
|
23 |
+
|
24 |
+
|
25 |
+
def chat_ui(tab_name, chat_history, system_prompt, input_key):
|
26 |
+
"""Reusable chat UI for each tab with fixed bottom input bar"""
|
27 |
+
|
28 |
+
st.subheader(tab_name)
|
29 |
+
|
30 |
+
# --- Chat history display ---
|
31 |
+
chat_container = st.container()
|
32 |
+
with chat_container:
|
33 |
+
for role, msg in chat_history:
|
34 |
+
if role == "user":
|
35 |
+
with st.chat_message("user"):
|
36 |
+
st.write(msg)
|
37 |
+
else:
|
38 |
+
with st.chat_message("assistant"):
|
39 |
+
if "```" in msg: # detect code blocks
|
40 |
+
parts = msg.split("```")
|
41 |
+
for i, part in enumerate(parts):
|
42 |
+
if i % 2 == 1: # inside code block
|
43 |
+
lang, *code_lines = part.split("\n")
|
44 |
+
code = "\n".join(code_lines)
|
45 |
+
st.code(code, language=lang if lang else "python")
|
46 |
+
else:
|
47 |
+
st.write(part)
|
48 |
+
else:
|
49 |
+
st.write(msg)
|
50 |
+
|
51 |
+
# --- Chat input at bottom (centered) ---
|
52 |
+
input_container = st.container()
|
53 |
+
with input_container:
|
54 |
+
user_input = st.chat_input("Type your message...", key=input_key)
|
55 |
+
|
56 |
+
# Handle input
|
57 |
+
if user_input:
|
58 |
+
# Save user message
|
59 |
+
st.session_state[input_key + "_last"] = user_input
|
60 |
+
chat_history.append(("user", user_input))
|
61 |
+
|
62 |
+
# Generate AI reply
|
63 |
+
with st.spinner("Thinking..."):
|
64 |
+
response = client.chat.completions.create(
|
65 |
+
model=model_choice,
|
66 |
+
messages=[{"role": "system", "content": system_prompt}]
|
67 |
+
+ [{"role": role, "content": msg} for role, msg in chat_history],
|
68 |
+
temperature=0.4
|
69 |
+
)
|
70 |
+
ai_msg = response.choices[0].message.content
|
71 |
+
|
72 |
+
chat_history.append(("assistant", ai_msg))
|
73 |
+
|
74 |
+
# Force rerun so chat history appears ABOVE input
|
75 |
+
st.rerun()
|
76 |
+
|
77 |
+
|
78 |
+
# Tabs
|
79 |
+
tab1, tab2, tab3 = st.tabs(["π‘ Generate Code", "π Debug Code", "π Explain Code"])
|
80 |
+
|
81 |
+
with tab1:
|
82 |
+
chat_ui(
|
83 |
+
"π‘ Generate Code",
|
84 |
+
st.session_state.generate_chat,
|
85 |
+
"You are a helpful coding assistant. Generate correct code first, then a short simple explanation.",
|
86 |
+
input_key="generate_input"
|
87 |
+
)
|
88 |
+
|
89 |
+
with tab2:
|
90 |
+
chat_ui(
|
91 |
+
"π Debug Code",
|
92 |
+
st.session_state.debug_chat,
|
93 |
+
"You are an expert code debugger. Fix errors and give corrected code, then explain what changed and why in simple terms.",
|
94 |
+
input_key="debug_input"
|
95 |
+
)
|
96 |
+
|
97 |
+
with tab3:
|
98 |
+
chat_ui(
|
99 |
+
"π Explain Code",
|
100 |
+
st.session_state.explain_chat,
|
101 |
+
"You are a teacher that explains code in simple words. The user pastes code, and you explain step by step.",
|
102 |
+
input_key="explain_input"
|
103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
|
106 |
|