Spaces:
Sleeping
Sleeping
File size: 6,805 Bytes
7b4e6e0 e4543cc 8ea8534 12ce1ff 7b4e6e0 ca90b0a 51b2142 7b4e6e0 ca90b0a 7b4e6e0 51b2142 7b4e6e0 51b2142 8ea8534 51b2142 8ea8534 51b2142 8ea8534 51b2142 8ea8534 ca90b0a 7b4e6e0 51b2142 7b4e6e0 51b2142 8ea8534 ca90b0a 51b2142 7b4e6e0 ca90b0a 7b4e6e0 51b2142 7b4e6e0 ca90b0a 7b4e6e0 51b2142 e4543cc 51b2142 7b4e6e0 51b2142 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
import streamlit as st
import requests
import re
# Backend URL - Hugging Face Space API endpoint
BACKEND_URL = "https://shiva9876-code-gen-backend.hf.space"
# Page Config with Logo
st.set_page_config(
page_title="Write any code",
page_icon="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png",
layout="centered",
)
# Custom CSS for Logo & UI
st.markdown("""
<style>
/* Add padding to bottom of main container to prevent content hiding behind fixed search bar */
.main .block-container {
padding-bottom: 150px;
}
/* Full Center Alignment */
.main-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 90vh;
}
/* Logo Centered */
.logo {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
margin-bottom: 10px;
}
/* Title Centered */
.title {
text-align: center;
font-size: 28px;
font-weight: bold;
color: #03dac6;
}
/* Chatbox UI */
.chat-container {
border-radius: 8px;
padding: 10px;
margin-bottom: 10px;
background-color: #262626;
color: white;
}
/* User & AI Message Styling */
.user-message { border-left: 5px solid #03dac6; padding-left: 10px; }
.ai-message { border-left: 5px solid #ff9800; padding-left: 10px; }
pre { white-space: pre-wrap; word-wrap: break-word; }
/* Fixed Search Bar Container */
.fixed-search-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background-color: #0e1117;
padding: 20px;
border-top: 2px solid #262626;
z-index: 1000;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.5);
}
/* Search Bar Styling */
.search-container {
display: flex;
align-items: center;
width: 100%;
max-width: 600px;
margin: auto;
}
.search-input {
flex-grow: 1;
padding: 12px 15px;
border-radius: 25px;
border: 2px solid #03dac6;
background-color: #262626;
color: white;
font-size: 16px;
outline: none;
}
/* Search Button */
.search-button {
width: 50px;
height: 50px;
border-radius: 50%;
border: none;
background-color: #00ffcc;
color: black;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
text-align: center;
}
.search-button:hover {
background-color: #02c2ad;
}
/* Streamlit form styling to work with fixed position */
.fixed-search-bar .stForm {
border: none;
}
/* Style the text input inside the fixed bar */
.fixed-search-bar .stTextInput > div > div > input {
background-color: #262626;
color: white;
border: 2px solid #03dac6;
border-radius: 25px;
padding: 12px 15px;
}
/* Style the submit button */
.fixed-search-bar .stFormSubmitButton > button {
background-color: #00ffcc;
color: black;
border-radius: 25px;
padding: 10px 25px;
border: none;
font-weight: bold;
transition: 0.3s;
}
.fixed-search-bar .stFormSubmitButton > button:hover {
background-color: #02c2ad;
}
</style>
""", unsafe_allow_html=True)
# Logo
st.markdown(
'<div class="logo"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png" width="80"></div>',
unsafe_allow_html=True
)
# Title
st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
# Chat history
if "messages" not in st.session_state:
st.session_state.messages = []
for msg in st.session_state.messages:
if msg["role"] == "user":
st.markdown(f'<div class="chat-container user-message"><b>User:</b><br>{msg["content"]}</div>',
unsafe_allow_html=True)
elif msg["role"] == "ai":
explanation, code = msg["content"]
st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{explanation}</div>', unsafe_allow_html=True)
if code:
# st.subheader("Generated Code:")
st.code(code, language="python")
# Fixed Input Form at the bottom
st.markdown('<div class="fixed-search-bar">', unsafe_allow_html=True)
st.markdown('<div class="search-container">', unsafe_allow_html=True)
with st.form(key="input_form", clear_on_submit=True):
user_input = st.text_input("Your Prompt:", key="user_prompt", placeholder="Enter your prompt...", label_visibility="collapsed")
# Single send button
submit_button = st.form_submit_button("SEND ↗️")
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
# API Call Logic
if submit_button:
if user_input.strip():
st.session_state.messages.append({"role": "user", "content": user_input})
try:
response = requests.post(f"{BACKEND_URL}/generate/", json={"prompt": user_input, "response_type": "both"})
if response.status_code == 200:
data = response.json()
raw_text = data.get("response", "").strip()
if not raw_text or raw_text.lower() == "none":
st.warning("⚠️ No valid response generated. Try a different prompt.")
else:
explanation, code = raw_text, None
code_match = re.search(r"```(?:python)?\n(.*?)\n```", raw_text, re.DOTALL)
if code_match:
code = code_match.group(1).strip()
explanation = raw_text.replace(code_match.group(0), "").strip()
st.session_state.messages.append({"role": "ai", "content": (explanation, code)})
st.rerun()
else:
st.error(f"API Error: {response.status_code} - {response.text}")
except requests.exceptions.ConnectionError:
st.error("⚠️ Cannot connect to backend! Make sure FastAPI is running.")
else:
st.warning("Write something in input cell.") |