Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,13 +4,14 @@ import time
|
|
4 |
import streamlit as st
|
5 |
from groq import Groq
|
6 |
from streamlit_lottie import st_lottie
|
|
|
7 |
import json
|
8 |
import base64
|
9 |
|
10 |
# --- Constants ---
|
11 |
PRIMARY_MODEL = "qwen-2.5-coder-32b"
|
12 |
SUPPORTED_LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
|
13 |
-
|
14 |
|
15 |
# --- Setup ---
|
16 |
st.set_page_config(
|
@@ -22,28 +23,22 @@ st.set_page_config(
|
|
22 |
|
23 |
# --- Lottie Animation Loader ---
|
24 |
def load_lottie(url):
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
# --- Custom CSS
|
29 |
def inject_css():
|
30 |
st.markdown(f"""
|
31 |
<style>
|
32 |
-
@keyframes glow {{
|
33 |
-
0% {{ box-shadow: 0 0 5px #4fffb0; }}
|
34 |
-
50% {{ box-shadow: 0 0 20px #4fffb0; }}
|
35 |
-
100% {{ box-shadow: 0 0 5px #4fffb0; }}
|
36 |
-
}}
|
37 |
-
|
38 |
.main {{
|
39 |
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
40 |
color: #ffffff;
|
41 |
}}
|
42 |
|
43 |
.stTextInput input {{
|
44 |
-
animation: glow 2s infinite;
|
45 |
background: rgba(0,0,0,0.3) !important;
|
46 |
border: 2px solid #4fffb0 !important;
|
|
|
47 |
}}
|
48 |
|
49 |
.code-editor {{
|
@@ -51,7 +46,6 @@ def inject_css():
|
|
51 |
border-radius: 15px;
|
52 |
padding: 1rem;
|
53 |
background: rgba(0,0,0,0.5) !important;
|
54 |
-
animation: glow 2s infinite;
|
55 |
}}
|
56 |
|
57 |
.download-btn {{
|
@@ -60,12 +54,6 @@ def inject_css():
|
|
60 |
border: none !important;
|
61 |
border-radius: 25px;
|
62 |
padding: 12px 24px;
|
63 |
-
font-weight: bold;
|
64 |
-
transition: transform 0.3s !important;
|
65 |
-
}}
|
66 |
-
|
67 |
-
.download-btn:hover {{
|
68 |
-
transform: scale(1.05);
|
69 |
}}
|
70 |
</style>
|
71 |
""", unsafe_allow_html=True)
|
@@ -83,30 +71,21 @@ def init_groq():
|
|
83 |
# --- Code Generation Core ---
|
84 |
def generate_code(client, query, lang):
|
85 |
try:
|
86 |
-
system_prompt = f"""
|
87 |
-
You are a {lang} expert. Generate:
|
88 |
-
1. Clean, production-ready code
|
89 |
-
2. With error handling
|
90 |
-
3. Proper documentation
|
91 |
-
4. Optimized for performance
|
92 |
-
"""
|
93 |
-
|
94 |
response = client.chat.completions.create(
|
95 |
model=PRIMARY_MODEL,
|
96 |
-
messages=[
|
97 |
-
|
98 |
-
|
99 |
-
],
|
100 |
temperature=0.7,
|
101 |
-
max_tokens=4096
|
102 |
-
top_p=0.95
|
103 |
)
|
104 |
return response.choices[0].message.content
|
105 |
except Exception as e:
|
106 |
-
st.error(f"
|
107 |
return None
|
108 |
|
109 |
-
# --- UI
|
110 |
def main():
|
111 |
inject_css()
|
112 |
client = init_groq()
|
@@ -114,59 +93,32 @@ def main():
|
|
114 |
# --- Hero Section ---
|
115 |
col1, col2 = st.columns([1, 2])
|
116 |
with col1:
|
117 |
-
|
|
|
|
|
118 |
with col2:
|
119 |
st.markdown("# CodeForge AI")
|
120 |
st.markdown("### _Transform Ideas into Production Code_ β‘")
|
121 |
|
122 |
# --- Language Selector ---
|
123 |
-
lang = st.sidebar.selectbox("π»
|
124 |
-
user_query = st.text_input("π― Describe your
|
125 |
|
126 |
-
# ---
|
127 |
if st.button("π Generate Code", use_container_width=True):
|
128 |
-
if
|
129 |
-
st.
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
code = generate_code(client, user_query, lang)
|
135 |
-
gen_time = time.time() - start_time
|
136 |
-
|
137 |
-
if code:
|
138 |
-
st.session_state.code = code
|
139 |
-
st.balloons()
|
140 |
-
|
141 |
-
# --- Code Display ---
|
142 |
-
with st.expander(f"β¨ Generated {lang} Code", expanded=True):
|
143 |
-
st.code(code, language="python" if lang == "Python" else "javascript")
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
# --- Performance Metrics ---
|
152 |
-
st.sidebar.markdown(f"""
|
153 |
-
### π Generation Metrics
|
154 |
-
β±οΈ **Time**: {gen_time:.2f}s
|
155 |
-
π **Tokens**: {len(code.split())}
|
156 |
-
π§ **Model**: {PRIMARY_MODEL}
|
157 |
-
""")
|
158 |
-
|
159 |
-
# --- Explanation Section ---
|
160 |
-
if st.button("π Explain Implementation"):
|
161 |
-
with st.spinner("π Analyzing code structure..."):
|
162 |
-
explanation = client.chat.completions.create(
|
163 |
-
model=PRIMARY_MODEL,
|
164 |
-
messages=[{
|
165 |
-
"role": "user",
|
166 |
-
"content": f"Explain this {lang} code:\n\n{code}"
|
167 |
-
}]
|
168 |
-
)
|
169 |
-
st.markdown(f"## Code Breakdown\n{explanation.choices[0].message.content}")
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
main()
|
|
|
4 |
import streamlit as st
|
5 |
from groq import Groq
|
6 |
from streamlit_lottie import st_lottie
|
7 |
+
import requests
|
8 |
import json
|
9 |
import base64
|
10 |
|
11 |
# --- Constants ---
|
12 |
PRIMARY_MODEL = "qwen-2.5-coder-32b"
|
13 |
SUPPORTED_LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
|
14 |
+
LOTTIE_URL = "https://assets3.lottiefiles.com/packages/lf20_hi95bvmx/ai.json"
|
15 |
|
16 |
# --- Setup ---
|
17 |
st.set_page_config(
|
|
|
23 |
|
24 |
# --- Lottie Animation Loader ---
|
25 |
def load_lottie(url):
|
26 |
+
response = requests.get(url)
|
27 |
+
return response.json() if response.status_code == 200 else None
|
28 |
|
29 |
+
# --- Custom CSS ---
|
30 |
def inject_css():
|
31 |
st.markdown(f"""
|
32 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
.main {{
|
34 |
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
|
35 |
color: #ffffff;
|
36 |
}}
|
37 |
|
38 |
.stTextInput input {{
|
|
|
39 |
background: rgba(0,0,0,0.3) !important;
|
40 |
border: 2px solid #4fffb0 !important;
|
41 |
+
color: #4fffb0 !important;
|
42 |
}}
|
43 |
|
44 |
.code-editor {{
|
|
|
46 |
border-radius: 15px;
|
47 |
padding: 1rem;
|
48 |
background: rgba(0,0,0,0.5) !important;
|
|
|
49 |
}}
|
50 |
|
51 |
.download-btn {{
|
|
|
54 |
border: none !important;
|
55 |
border-radius: 25px;
|
56 |
padding: 12px 24px;
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}}
|
58 |
</style>
|
59 |
""", unsafe_allow_html=True)
|
|
|
71 |
# --- Code Generation Core ---
|
72 |
def generate_code(client, query, lang):
|
73 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
response = client.chat.completions.create(
|
75 |
model=PRIMARY_MODEL,
|
76 |
+
messages=[{
|
77 |
+
"role": "user",
|
78 |
+
"content": f"Generate {lang} code for: {query}. Include comments and error handling."
|
79 |
+
}],
|
80 |
temperature=0.7,
|
81 |
+
max_tokens=4096
|
|
|
82 |
)
|
83 |
return response.choices[0].message.content
|
84 |
except Exception as e:
|
85 |
+
st.error(f"Error: {str(e)}")
|
86 |
return None
|
87 |
|
88 |
+
# --- Main UI ---
|
89 |
def main():
|
90 |
inject_css()
|
91 |
client = init_groq()
|
|
|
93 |
# --- Hero Section ---
|
94 |
col1, col2 = st.columns([1, 2])
|
95 |
with col1:
|
96 |
+
lottie_anim = load_lottie(LOTTIE_URL)
|
97 |
+
if lottie_anim:
|
98 |
+
st_lottie(lottie_anim, height=150)
|
99 |
with col2:
|
100 |
st.markdown("# CodeForge AI")
|
101 |
st.markdown("### _Transform Ideas into Production Code_ β‘")
|
102 |
|
103 |
# --- Language Selector ---
|
104 |
+
lang = st.sidebar.selectbox("π» Language", SUPPORTED_LANGUAGES)
|
105 |
+
user_query = st.text_input("π― Describe your requirement:", placeholder="e.g., 'Create a REST API with JWT authentication'")
|
106 |
|
107 |
+
# --- Generation Flow ---
|
108 |
if st.button("π Generate Code", use_container_width=True):
|
109 |
+
if user_query:
|
110 |
+
with st.spinner(f"π§ Crafting {lang} code..."):
|
111 |
+
code = generate_code(client, user_query, lang)
|
112 |
+
if code:
|
113 |
+
st.session_state.code = code
|
114 |
+
st.balloons()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
with st.expander(f"β¨ Generated Code", expanded=True):
|
117 |
+
st.code(code)
|
118 |
+
ext = {"Python": "py", "JavaScript": "js"}.get(lang, "txt")
|
119 |
+
st.markdown(get_download_link(code, f"code.{ext}"), unsafe_allow_html=True)
|
120 |
+
else:
|
121 |
+
st.warning("Please enter a requirement!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
if __name__ == "__main__":
|
124 |
main()
|