Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
BACKEND_URL = "https://huggingface.co/spaces/shiva9876/code_gen_backend"
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Page Config with Logo
|
| 7 |
st.set_page_config(
|
|
@@ -90,6 +93,20 @@ st.markdown("""
|
|
| 90 |
.search-button:hover {
|
| 91 |
background-color: #02c2ad;
|
| 92 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
</style>
|
| 94 |
""", unsafe_allow_html=True)
|
| 95 |
|
|
@@ -102,26 +119,71 @@ st.markdown(
|
|
| 102 |
# Title
|
| 103 |
st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
|
| 104 |
|
| 105 |
-
#
|
| 106 |
if "messages" not in st.session_state:
|
| 107 |
st.session_state.messages = []
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
for msg in st.session_state.messages:
|
| 110 |
if msg["role"] == "user":
|
| 111 |
-
st.markdown(f'<div class="chat-container user-message"><b>
|
| 112 |
unsafe_allow_html=True)
|
| 113 |
elif msg["role"] == "ai":
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if code
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Input Form
|
| 121 |
st.markdown('<div class="search-container">', unsafe_allow_html=True)
|
| 122 |
|
| 123 |
with st.form(key="input_form"):
|
| 124 |
-
user_input = st.text_input(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Single send button
|
| 127 |
submit_button = st.form_submit_button("SEND ↗️")
|
|
@@ -131,25 +193,96 @@ st.markdown("</div>", unsafe_allow_html=True)
|
|
| 131 |
# API Call Logic
|
| 132 |
if submit_button:
|
| 133 |
if user_input.strip():
|
|
|
|
| 134 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
st.rerun()
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
else:
|
| 155 |
-
st.warning("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import re
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
# Backend URL - Update this with your actual Hugging Face Space URL
|
| 7 |
+
BACKEND_URL = "https://huggingface.co/spaces/shiva9876/code_gen_backend"
|
| 8 |
|
| 9 |
# Page Config with Logo
|
| 10 |
st.set_page_config(
|
|
|
|
| 93 |
.search-button:hover {
|
| 94 |
background-color: #02c2ad;
|
| 95 |
}
|
| 96 |
+
|
| 97 |
+
/* Clear Chat Button */
|
| 98 |
+
.stButton>button {
|
| 99 |
+
background-color: #ff4444;
|
| 100 |
+
color: white;
|
| 101 |
+
border-radius: 5px;
|
| 102 |
+
padding: 5px 15px;
|
| 103 |
+
border: none;
|
| 104 |
+
cursor: pointer;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.stButton>button:hover {
|
| 108 |
+
background-color: #cc0000;
|
| 109 |
+
}
|
| 110 |
</style>
|
| 111 |
""", unsafe_allow_html=True)
|
| 112 |
|
|
|
|
| 119 |
# Title
|
| 120 |
st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
|
| 121 |
|
| 122 |
+
# Initialize session state
|
| 123 |
if "messages" not in st.session_state:
|
| 124 |
st.session_state.messages = []
|
| 125 |
|
| 126 |
+
if "session_id" not in st.session_state:
|
| 127 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 128 |
+
|
| 129 |
+
# Display chat history
|
| 130 |
for msg in st.session_state.messages:
|
| 131 |
if msg["role"] == "user":
|
| 132 |
+
st.markdown(f'<div class="chat-container user-message"><b>You:</b><br>{msg["content"]}</div>',
|
| 133 |
unsafe_allow_html=True)
|
| 134 |
elif msg["role"] == "ai":
|
| 135 |
+
content = msg["content"]
|
| 136 |
+
|
| 137 |
+
# Check if it's a conversational response or code response
|
| 138 |
+
if isinstance(content, tuple):
|
| 139 |
+
explanation, code = content
|
| 140 |
+
if explanation:
|
| 141 |
+
st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{explanation}</div>',
|
| 142 |
+
unsafe_allow_html=True)
|
| 143 |
+
if code:
|
| 144 |
+
st.code(code, language="python")
|
| 145 |
+
else:
|
| 146 |
+
# Simple text response (for conversational messages)
|
| 147 |
+
st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{content}</div>',
|
| 148 |
+
unsafe_allow_html=True)
|
| 149 |
+
|
| 150 |
+
# Sidebar with options
|
| 151 |
+
with st.sidebar:
|
| 152 |
+
st.header("Options")
|
| 153 |
+
|
| 154 |
+
response_type = st.selectbox(
|
| 155 |
+
"Response Type:",
|
| 156 |
+
["both", "code", "explanation", "conversation"],
|
| 157 |
+
help="Choose what type of response you want"
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
if st.button("🗑️ Clear Chat History"):
|
| 161 |
+
try:
|
| 162 |
+
# Clear on backend
|
| 163 |
+
requests.post(
|
| 164 |
+
f"{BACKEND_URL}/clear_history/",
|
| 165 |
+
json={"session_id": st.session_state.session_id}
|
| 166 |
+
)
|
| 167 |
+
except:
|
| 168 |
+
pass
|
| 169 |
+
|
| 170 |
+
# Clear locally
|
| 171 |
+
st.session_state.messages = []
|
| 172 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 173 |
+
st.rerun()
|
| 174 |
+
|
| 175 |
+
st.divider()
|
| 176 |
+
st.caption("💡 Tip: Ask coding questions or request code generation!")
|
| 177 |
|
| 178 |
# Input Form
|
| 179 |
st.markdown('<div class="search-container">', unsafe_allow_html=True)
|
| 180 |
|
| 181 |
with st.form(key="input_form"):
|
| 182 |
+
user_input = st.text_input(
|
| 183 |
+
"Your Prompt:",
|
| 184 |
+
key="user_prompt",
|
| 185 |
+
placeholder="e.g., Write a function to sort a list or just say Hi!"
|
| 186 |
+
)
|
| 187 |
|
| 188 |
# Single send button
|
| 189 |
submit_button = st.form_submit_button("SEND ↗️")
|
|
|
|
| 193 |
# API Call Logic
|
| 194 |
if submit_button:
|
| 195 |
if user_input.strip():
|
| 196 |
+
# Add user message to chat
|
| 197 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 198 |
+
|
| 199 |
+
# Show loading spinner
|
| 200 |
+
with st.spinner("🤔 Thinking..."):
|
| 201 |
+
try:
|
| 202 |
+
# Make API call to backend
|
| 203 |
+
response = requests.post(
|
| 204 |
+
f"{BACKEND_URL}/generate/",
|
| 205 |
+
json={
|
| 206 |
+
"prompt": user_input,
|
| 207 |
+
"session_id": st.session_state.session_id,
|
| 208 |
+
"response_type": response_type
|
| 209 |
+
},
|
| 210 |
+
timeout=60
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
if response.status_code == 200:
|
| 214 |
+
data = response.json()
|
| 215 |
+
|
| 216 |
+
# Update session_id if returned
|
| 217 |
+
if "session_id" in data:
|
| 218 |
+
st.session_state.session_id = data["session_id"]
|
| 219 |
+
|
| 220 |
+
message_type = data.get("message_type", "code")
|
| 221 |
+
|
| 222 |
+
if message_type == "conversation":
|
| 223 |
+
# Handle conversational response
|
| 224 |
+
ai_response = data.get("response", "")
|
| 225 |
+
if ai_response and ai_response.strip().lower() != "none":
|
| 226 |
+
st.session_state.messages.append({
|
| 227 |
+
"role": "ai",
|
| 228 |
+
"content": ai_response
|
| 229 |
+
})
|
| 230 |
+
else:
|
| 231 |
+
st.warning("⚠️ No valid response generated. Try a different prompt.")
|
| 232 |
+
|
| 233 |
+
else:
|
| 234 |
+
# Handle code response
|
| 235 |
+
raw_text = data.get("response", "")
|
| 236 |
+
generated_code = data.get("generated_code")
|
| 237 |
+
explanation = data.get("explanation", "")
|
| 238 |
+
|
| 239 |
+
# Fallback: extract code from raw response if not provided
|
| 240 |
+
if not generated_code and raw_text:
|
| 241 |
+
code_match = re.search(r"```(?:\w+)?\n(.*?)\n```", raw_text, re.DOTALL)
|
| 242 |
+
if code_match:
|
| 243 |
+
generated_code = code_match.group(1).strip()
|
| 244 |
+
if not explanation:
|
| 245 |
+
explanation = raw_text.replace(code_match.group(0), "").strip()
|
| 246 |
+
|
| 247 |
+
# If we have explanation or code, add to messages
|
| 248 |
+
if explanation or generated_code:
|
| 249 |
+
st.session_state.messages.append({
|
| 250 |
+
"role": "ai",
|
| 251 |
+
"content": (explanation, generated_code)
|
| 252 |
+
})
|
| 253 |
+
elif raw_text:
|
| 254 |
+
# Fallback to raw text
|
| 255 |
+
st.session_state.messages.append({
|
| 256 |
+
"role": "ai",
|
| 257 |
+
"content": raw_text
|
| 258 |
+
})
|
| 259 |
+
else:
|
| 260 |
+
st.warning("⚠️ No valid response generated. Try a different prompt.")
|
| 261 |
+
|
| 262 |
+
# Rerun to show the new message
|
| 263 |
st.rerun()
|
| 264 |
+
|
| 265 |
+
elif response.status_code == 401:
|
| 266 |
+
st.error("🔐 Authentication Error: Invalid API key on backend.")
|
| 267 |
+
elif response.status_code == 429:
|
| 268 |
+
st.error("⏱️ Rate limit exceeded. Please wait a moment and try again.")
|
| 269 |
+
elif response.status_code == 503:
|
| 270 |
+
st.error("🔧 Service temporarily unavailable. Please try again in a moment.")
|
| 271 |
+
else:
|
| 272 |
+
st.error(f"❌ API Error: {response.status_code} - {response.text}")
|
| 273 |
+
|
| 274 |
+
except requests.exceptions.Timeout:
|
| 275 |
+
st.error("⏱️ Request timed out. The model might be taking too long to respond.")
|
| 276 |
+
except requests.exceptions.ConnectionError:
|
| 277 |
+
st.error("⚠️ Cannot connect to backend! Make sure the FastAPI server is running.")
|
| 278 |
+
except Exception as e:
|
| 279 |
+
st.error(f"❌ Unexpected error: {str(e)}")
|
| 280 |
else:
|
| 281 |
+
st.warning("⚠️ Please enter a prompt before sending.")
|
| 282 |
+
|
| 283 |
+
# Footer
|
| 284 |
+
st.markdown("---")
|
| 285 |
+
st.markdown(
|
| 286 |
+
'<div style="text-align: center; color: #888;">Powered by Groq AI | Built with ❤️ using Streamlit</div>',
|
| 287 |
+
unsafe_allow_html=True
|
| 288 |
+
)
|