Engr-Usman-Ali commited on
Commit
2e0e60d
Β·
verified Β·
1 Parent(s): 6a22ef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -122
app.py CHANGED
@@ -1,126 +1,106 @@
1
  import streamlit as st
2
-
3
- st.set_page_config(page_title="CodeCraft", layout="wide")
4
-
5
- # Session state for each tab chat
6
- if "chats" not in st.session_state:
7
- st.session_state.chats = {
8
- "Debug": [],
9
- "Generate": [],
10
- "Explain": []
11
- }
12
-
13
- # --- Custom CSS ---
14
- st.markdown("""
15
- <style>
16
- .chat-container {
17
- padding-bottom: 70px; /* leave space for fixed input */
18
- }
19
- .user-msg, .ai-msg {
20
- padding: 10px 14px;
21
- margin: 6px 0;
22
- border-radius: 12px;
23
- max-width: 80%;
24
- word-wrap: break-word;
25
- }
26
- .user-msg {
27
- background: #DCF8C6;
28
- margin-left: auto;
29
- text-align: right;
30
- }
31
- .ai-msg {
32
- background: #F1F0F0;
33
- margin-right: auto;
34
- text-align: left;
35
- }
36
- .fixed-input {
37
- position: fixed;
38
- bottom: 0;
39
- left: 0;
40
- right: 0;
41
- background: #fff;
42
- padding: 8px;
43
- border-top: 1px solid #ddd;
44
- display: flex;
45
- align-items: center;
46
- z-index: 999;
47
- }
48
- .fixed-input input {
49
- flex: 1;
50
- border-radius: 20px;
51
- border: 1px solid #ccc;
52
- padding: 10px 12px;
53
- font-size: 14px;
54
- }
55
- .fixed-input button {
56
- margin-left: 8px;
57
- background: #4CAF50;
58
- color: white;
59
- border: none;
60
- border-radius: 50%;
61
- width: 42px;
62
- height: 42px;
63
- font-size: 18px;
64
- cursor: pointer;
65
- }
66
- </style>
67
- """, unsafe_allow_html=True)
68
-
69
-
70
- tabs = st.tabs(["🐞 Debug", "⚑ Generate", "πŸ“– Explain"])
71
-
72
- def render_chat(tab_name):
73
- st.markdown('<div class="chat-container">', unsafe_allow_html=True)
74
-
75
- # Show chat history
76
- for role, msg in st.session_state.chats[tab_name]:
77
- if role == "user":
78
- st.markdown(f'<div class="user-msg">{msg}</div>', unsafe_allow_html=True)
79
- else:
80
- st.markdown(f'<div class="ai-msg">{msg}</div>', unsafe_allow_html=True)
81
-
82
- st.markdown('</div>', unsafe_allow_html=True)
83
-
84
- # Fixed input bar
85
- st.markdown('<div class="fixed-input">', unsafe_allow_html=True)
86
- col1, col2 = st.columns([8,1])
87
- with col1:
88
- user_input = st.text_input(
89
- f"{tab_name}_input",
90
- "",
91
- key=f"{tab_name}_input_key",
92
- label_visibility="collapsed"
93
- )
94
- with col2:
95
- send_button = st.button("➀", key=f"{tab_name}_send")
96
- st.markdown('</div>', unsafe_allow_html=True)
97
-
98
- if send_button and user_input.strip():
99
- # Save user msg
100
- st.session_state.chats[tab_name].append(("user", user_input))
101
-
102
- # Example AI reply
103
- if tab_name == "Debug":
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