Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,155 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 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 |
-
font-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
color:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
border:
|
| 69 |
-
|
| 70 |
-
color:
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
border:
|
| 81 |
-
|
| 82 |
-
color:
|
| 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 |
-
st.
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
| 1 |
+
BACKEND_URL = "https://shiva9876-code-gen-app.hf.space"
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
# Page Config with Logo
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="Write any code",
|
| 9 |
+
page_icon="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Code.org_logo.svg/1200px-Code.org_logo.svg.png",
|
| 10 |
+
layout="centered",
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Custom CSS for Logo & UI
|
| 14 |
+
st.markdown("""
|
| 15 |
+
<style>
|
| 16 |
+
/* Full Center Alignment */
|
| 17 |
+
.main-container {
|
| 18 |
+
display: flex;
|
| 19 |
+
flex-direction: column;
|
| 20 |
+
justify-content: center;
|
| 21 |
+
align-items: center;
|
| 22 |
+
height: 90vh;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/* Logo Centered */
|
| 26 |
+
.logo {
|
| 27 |
+
display: flex;
|
| 28 |
+
justify-content: center;
|
| 29 |
+
align-items: center;
|
| 30 |
+
width: 100%;
|
| 31 |
+
margin-bottom: 10px;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* Title Centered */
|
| 35 |
+
.title {
|
| 36 |
+
text-align: center;
|
| 37 |
+
font-size: 28px;
|
| 38 |
+
font-weight: bold;
|
| 39 |
+
color: #03dac6;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/* Chatbox UI */
|
| 43 |
+
.chat-container {
|
| 44 |
+
border-radius: 8px;
|
| 45 |
+
padding: 10px;
|
| 46 |
+
margin-bottom: 10px;
|
| 47 |
+
background-color: #262626;
|
| 48 |
+
color: white;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
/* User & AI Message Styling */
|
| 52 |
+
.user-message { border-left: 5px solid #03dac6; padding-left: 10px; }
|
| 53 |
+
.ai-message { border-left: 5px solid #ff9800; padding-left: 10px; }
|
| 54 |
+
pre { white-space: pre-wrap; word-wrap: break-word; }
|
| 55 |
+
|
| 56 |
+
/* Search Bar Styling */
|
| 57 |
+
.search-container {
|
| 58 |
+
display: flex;
|
| 59 |
+
align-items: center;
|
| 60 |
+
width: 100%;
|
| 61 |
+
max-width: 600px;
|
| 62 |
+
margin: auto;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.search-input {
|
| 66 |
+
flex-grow: 1;
|
| 67 |
+
padding: 12px 15px;
|
| 68 |
+
border-radius: 25px;
|
| 69 |
+
border: 2px solid #03dac6;
|
| 70 |
+
background-color: #262626;
|
| 71 |
+
color: white;
|
| 72 |
+
font-size: 16px;
|
| 73 |
+
outline: none;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
/* Search Button */
|
| 77 |
+
.search-button {
|
| 78 |
+
width: 50px;
|
| 79 |
+
height: 50px;
|
| 80 |
+
border-radius: 50%;
|
| 81 |
+
border: none;
|
| 82 |
+
background-color: #00ffcc;
|
| 83 |
+
color: black;
|
| 84 |
+
font-size: 16px;
|
| 85 |
+
cursor: pointer;
|
| 86 |
+
transition: 0.3s;
|
| 87 |
+
text-align: center;
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
.search-button:hover {
|
| 91 |
+
background-color: #02c2ad;
|
| 92 |
+
}
|
| 93 |
+
</style>
|
| 94 |
+
""", unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
# Logo
|
| 97 |
+
st.markdown(
|
| 98 |
+
'<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>',
|
| 99 |
+
unsafe_allow_html=True
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Title
|
| 103 |
+
st.markdown('<div class="title">Write any code: Verbose❇️</div>', unsafe_allow_html=True)
|
| 104 |
+
|
| 105 |
+
# Chat history
|
| 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>User:</b><br>{msg["content"]}</div>',
|
| 112 |
+
unsafe_allow_html=True)
|
| 113 |
+
elif msg["role"] == "ai":
|
| 114 |
+
explanation, code = msg["content"]
|
| 115 |
+
st.markdown(f'<div class="chat-container ai-message"><b>AI:</b><br>{explanation}</div>', unsafe_allow_html=True)
|
| 116 |
+
if code:
|
| 117 |
+
# st.subheader("Generated Code:")
|
| 118 |
+
st.code(code, language="python")
|
| 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("Your Prompt:", key="user_prompt", placeholder="Enter your prompt...")
|
| 125 |
+
|
| 126 |
+
# Single send button
|
| 127 |
+
submit_button = st.form_submit_button("SEND ↗️")
|
| 128 |
+
|
| 129 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 130 |
+
|
| 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 |
+
try:
|
| 136 |
+
response = requests.post("http://127.0.0.1:8000/generate/", json={"prompt": user_input, "response_type": "both"})
|
| 137 |
+
if response.status_code == 200:
|
| 138 |
+
data = response.json()
|
| 139 |
+
raw_text = data.get("response", "").strip()
|
| 140 |
+
if not raw_text or raw_text.lower() == "none":
|
| 141 |
+
st.warning("⚠️ No valid response generated. Try a different prompt.")
|
| 142 |
+
else:
|
| 143 |
+
explanation, code = raw_text, None
|
| 144 |
+
code_match = re.search(r"```(?:python)?\n(.*?)\n```", raw_text, re.DOTALL)
|
| 145 |
+
if code_match:
|
| 146 |
+
code = code_match.group(1).strip()
|
| 147 |
+
explanation = raw_text.replace(code_match.group(0), "").strip()
|
| 148 |
+
st.session_state.messages.append({"role": "ai", "content": (explanation, code)})
|
| 149 |
+
st.rerun()
|
| 150 |
+
else:
|
| 151 |
+
st.error(f"API Error: {response.status_code} - {response.text}")
|
| 152 |
+
except requests.exceptions.ConnectionError:
|
| 153 |
+
st.error("⚠️ Cannot connect to backend! Make sure FastAPI is running.")
|
| 154 |
+
else:
|
| 155 |
+
st.warning("Write something in input cell.")
|