Spaces:
Runtime error
Runtime error
File size: 6,610 Bytes
c4cf4bc 55e69a7 a49311f c4cf4bc 3602504 267970e c4cf4bc 3602504 267970e c4cf4bc 53b0f27 3602504 267970e 3602504 53b0f27 c4cf4bc 3602504 53b0f27 55e69a7 9dc5d2f c4cf4bc 53b0f27 3602504 c4cf4bc 3602504 c4cf4bc 3602504 c4cf4bc 3602504 c4cf4bc 3602504 c4cf4bc 53b0f27 267970e 3602504 267970e 3602504 267970e 3602504 655e71d 3602504 53b0f27 c4cf4bc 3602504 53b0f27 3602504 267970e 53b0f27 |
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 200 201 202 203 204 205 |
# app.py
import os
from groq import Groq
import time
import json
import zipfile
import io
import streamlit as st
from streamlit_lottie import st_lottie
from streamlit_extras.stylable_container import stylable_container
# --- Constants ---
PRIMARY_MODEL = "qwen-2.5-coder-32b"
BACKUP_MODEL = "llama3-70b-8192"
LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
THEMES = ["Dracula", "Monokai", "Nord", "Solarized", "One Dark"]
# --- Load Assets ---
def load_lottie(filepath):
with open(filepath, "r") as f:
return json.load(f)
# --- Initialize Clients ---
@st.cache_resource
def init_groq():
return Groq(api_key=os.getenv("GROQ_API_KEY"))
# --- UI Config ---
st.set_page_config(
page_title="CodeForge AI",
page_icon="β‘",
layout="wide",
initial_sidebar_state="expanded"
)
# --- Custom CSS ---
def inject_css():
st.markdown(f"""
<style>
[data-testid="stAppViewContainer"] {{
background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
}}
.stTextInput input {{
color: #00ffaa !important;
background: rgba(0,0,0,0.3) !important;
border: 2px solid #00ffaa !important;
box-shadow: 0 0 10px rgba(0,255,170,0.5);
}}
.stButton>button {{
background: linear-gradient(90deg, #00ffaa 0%, #0088ff 100%) !important;
color: #111 !important;
font-weight: bold !important;
border: none !important;
box-shadow: 0 0 15px rgba(0,255,170,0.7);
transition: all 0.3s ease !important;
}}
.stButton>button:hover {{
transform: scale(1.05);
box-shadow: 0 0 20px rgba(0,255,170,0.9);
}}
.code-header {{
color: #00ffaa;
font-family: 'Fira Code', monospace;
text-shadow: 0 0 10px rgba(0,255,170,0.7);
}}
[data-testid="stSidebar"] {{
background: rgba(16, 18, 33, 0.8) !important;
backdrop-filter: blur(10px);
border-right: 1px solid rgba(0,255,170,0.3);
}}
</style>
""", unsafe_allow_html=True)
inject_css()
# --- Animation ---
def render_hero():
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("""
# <span class="code-header">β‘ CodeForge AI</span>
### _Generate β’ Optimize β’ Deploy_
""", unsafe_allow_html=True)
with col2:
st_lottie(load_lottie("https://assets9.lottiefiles.com/packages/lf20_2znxgjyt.json"),
height=150, key="hero")
# --- Main Generator ---
def generate_code(client, prompt, language, model):
messages = [{
"role": "system",
"content": f"You are an expert {language} developer. Generate clean, efficient, production-ready code."
}, {
"role": "user",
"content": prompt
}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.7,
max_tokens=4000,
top_p=0.95,
stream=True
)
return response
# --- UI Components ---
def language_selector():
with st.sidebar:
with stylable_container(
key="lang-select",
css_styles="""
{
border: 2px solid #00ffaa;
border-radius: 10px;
padding: 10px;
background: rgba(0,0,0,0.2);
}
"""
):
return st.selectbox("ποΈ Select Language", LANGUAGES, index=0)
def theme_selector():
with st.sidebar:
with stylable_container(
key="theme-select",
css_styles="""
{
border: 2px solid #0088ff;
border-radius: 10px;
padding: 10px;
background: rgba(0,0,0,0.2);
}
"""
):
return st.selectbox("π¨ Editor Theme", THEMES, index=0)
# --- Download Handler ---
def create_download_zip(code, language):
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a") as zf:
zf.writestr(f"generated_code.{language.lower()}", code)
return zip_buffer.getvalue()
# --- Main App ---
def main():
client = init_groq()
render_hero()
# Sidebar Controls
language = language_selector()
theme = theme_selector()
# Main Input
with st.form(key="input_form"):
prompt = st.text_area("π‘ Describe your coding task:", height=150,
placeholder="e.g. 'Create a REST API with FastAPI and MongoDB'")
col1, col2, col3 = st.columns([1,1,2])
with col1:
generate_btn = st.form_submit_button("β¨ Generate Code")
with col2:
explain_btn = st.form_submit_button("π Explain")
with col3:
optimize_btn = st.form_submit_button("β‘ Optimize")
# Generation Logic
if generate_btn and prompt:
with st.spinner("π Generating your code..."):
try:
response = generate_code(client, prompt, language, PRIMARY_MODEL)
code_container = st.empty()
full_response = []
for chunk in response:
if chunk.choices[0].delta.content:
full_response.append(chunk.choices[0].delta.content)
current_code = "".join(full_response)
code_container.code(current_code, language=language.lower(), line_numbers=True)
st.session_state.generated_code = "".join(full_response)
# Download Button
zip_data = create_download_zip(st.session_state.generated_code, language)
st.download_button(
label="π₯ Download Code",
data=zip_data,
file_name=f"codeforge_{int(time.time())}.zip",
mime="application/zip"
)
except Exception as e:
st.error(f"π¨ Generation failed: {str(e)}")
st.info("Attempting with backup model...")
try:
response = generate_code(client, prompt, language, BACKUP_MODEL)
# ... [backup handling logic]
except Exception as e:
st.error("All models failed. Please try again later.")
if __name__ == "__main__":
main() |