Spaces:
Runtime error
Runtime error
File size: 5,851 Bytes
c4cf4bc 55e69a7 c4cf4bc 267970e 55e69a7 c4cf4bc 267970e c4cf4bc 53b0f27 c4cf4bc 267970e c4cf4bc 53b0f27 c4cf4bc 53b0f27 55e69a7 c4cf4bc 53b0f27 c4cf4bc 53b0f27 267970e c4cf4bc 267970e c4cf4bc 267970e c4cf4bc 53b0f27 c4cf4bc 53b0f27 c4cf4bc 53b0f27 c4cf4bc 53b0f27 655e71d c4cf4bc 53b0f27 c4cf4bc 53b0f27 c4cf4bc 53b0f27 c4cf4bc 655e71d c4cf4bc 53b0f27 c4cf4bc 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 |
# app.py
import os
import time
import streamlit as st
from groq import Groq
from streamlit_lottie import st_lottie
import json
import base64
# --- Constants ---
PRIMARY_MODEL = "qwen-2.5-coder-32b"
SUPPORTED_LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
LOTTIE_ANIMATION = "https://assets3.lottiefiles.com/packages/lf20_hi95bvmx/ai.json"
# --- Setup ---
st.set_page_config(
page_title="CodeForge AI",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
# --- Lottie Animation Loader ---
def load_lottie(url):
with open("animation.json") as f:
return json.load(f)
# --- Custom CSS with Animations ---
def inject_css():
st.markdown(f"""
<style>
@keyframes glow {{
0% {{ box-shadow: 0 0 5px #4fffb0; }}
50% {{ box-shadow: 0 0 20px #4fffb0; }}
100% {{ box-shadow: 0 0 5px #4fffb0; }}
}}
.main {{
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #ffffff;
}}
.stTextInput input {{
animation: glow 2s infinite;
background: rgba(0,0,0,0.3) !important;
border: 2px solid #4fffb0 !important;
}}
.code-editor {{
border: 2px solid #4fffb0;
border-radius: 15px;
padding: 1rem;
background: rgba(0,0,0,0.5) !important;
animation: glow 2s infinite;
}}
.download-btn {{
background: linear-gradient(45deg, #4fffb0, #2bd2ff) !important;
color: #1a1a2e !important;
border: none !important;
border-radius: 25px;
padding: 12px 24px;
font-weight: bold;
transition: transform 0.3s !important;
}}
.download-btn:hover {{
transform: scale(1.05);
}}
</style>
""", unsafe_allow_html=True)
# --- File Download Generator ---
def get_download_link(code, filename):
b64 = base64.b64encode(code.encode()).decode()
return f'<a href="data:file/txt;base64,{b64}" download="{filename}" class="download-btn">β¬οΈ Download {filename}</a>'
# --- Model Client ---
@st.cache_resource
def init_groq():
return Groq(api_key=os.environ.get("GROQ_API_KEY"))
# --- Code Generation Core ---
def generate_code(client, query, lang):
try:
system_prompt = f"""
You are a {lang} expert. Generate:
1. Clean, production-ready code
2. With error handling
3. Proper documentation
4. Optimized for performance
"""
response = client.chat.completions.create(
model=PRIMARY_MODEL,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": query}
],
temperature=0.7,
max_tokens=4096,
top_p=0.95
)
return response.choices[0].message.content
except Exception as e:
st.error(f"π¨ Generation Error: {str(e)}")
return None
# --- UI Components ---
def main():
inject_css()
client = init_groq()
# --- Hero Section ---
col1, col2 = st.columns([1, 2])
with col1:
st_lottie(load_lottie(LOTTIE_ANIMATION), height=200)
with col2:
st.markdown("# CodeForge AI")
st.markdown("### _Transform Ideas into Production Code_ β‘")
# --- Language Selector ---
lang = st.sidebar.selectbox("π» Programming Language", SUPPORTED_LANGUAGES, index=0)
user_query = st.text_input("π― Describe your coding requirement:", placeholder="e.g., 'Create a REST API with JWT authentication'")
# --- Main Generation Flow ---
if st.button("π Generate Code", use_container_width=True):
if not user_query:
st.warning("β οΈ Please enter a coding requirement!")
return
with st.spinner(f"π§ Crafting {lang} magic..."):
start_time = time.time()
code = generate_code(client, user_query, lang)
gen_time = time.time() - start_time
if code:
st.session_state.code = code
st.balloons()
# --- Code Display ---
with st.expander(f"β¨ Generated {lang} Code", expanded=True):
st.code(code, language="python" if lang == "Python" else "javascript")
# --- Download Button ---
ext = {"Python": "py", "JavaScript": "js", "Java": "java",
"C++": "cpp", "Go": "go", "Rust": "rs"}[lang]
st.markdown(get_download_link(code, f"generated_code.{ext}"),
unsafe_allow_html=True)
# --- Performance Metrics ---
st.sidebar.markdown(f"""
### π Generation Metrics
β±οΈ **Time**: {gen_time:.2f}s
π **Tokens**: {len(code.split())}
π§ **Model**: {PRIMARY_MODEL}
""")
# --- Explanation Section ---
if st.button("π Explain Implementation"):
with st.spinner("π Analyzing code structure..."):
explanation = client.chat.completions.create(
model=PRIMARY_MODEL,
messages=[{
"role": "user",
"content": f"Explain this {lang} code:\n\n{code}"
}]
)
st.markdown(f"## Code Breakdown\n{explanation.choices[0].message.content}")
if __name__ == "__main__":
main() |