Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,204 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import streamlit as st
|
3 |
-
from groq import Groq
|
4 |
-
from streamlit_tags import st_tags
|
5 |
-
|
6 |
-
# Configuration
|
7 |
-
PRIMARY_MODEL = "qwen-2.5-coder-32b"
|
8 |
-
BACKUP_MODEL = "llama3-70b-8192"
|
9 |
-
LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust", "TypeScript", "Swift"]
|
10 |
-
THEMES = ["Neon", "Cyberpunk", "Solarized", "Dracula", "Monokai"]
|
11 |
-
|
12 |
-
# Streamlit UI Config
|
13 |
-
st.set_page_config(
|
14 |
-
page_title="AstraCode",
|
15 |
-
page_icon="✨",
|
16 |
-
layout="wide",
|
17 |
-
initial_sidebar_state="expanded"
|
18 |
-
)
|
19 |
-
|
20 |
-
# Custom CSS for Modern Theme
|
21 |
-
def inject_custom_css(theme="Neon"):
|
22 |
-
theme_colors = {
|
23 |
-
"Neon": {"primary": "#4fffb0", "secondary": "#ff4fd8", "bg": "#1a1a2e"},
|
24 |
-
"Cyberpunk": {"primary": "#ff2a6d", "secondary": "#05d9e8", "bg": "#1a1a2e"},
|
25 |
-
"Solarized": {"primary": "#268bd2", "secondary": "#d33682", "bg": "#fdf6e3"},
|
26 |
-
"Dracula": {"primary": "#bd93f9", "secondary": "#ff79c6", "bg": "#282a36"},
|
27 |
-
"Monokai": {"primary": "#a6e22e", "secondary": "#fd971f", "bg": "#272822"}
|
28 |
-
}
|
29 |
-
colors = theme_colors.get(theme, theme_colors["Neon"])
|
30 |
-
|
31 |
-
st.markdown(f"""
|
32 |
-
<style>
|
33 |
-
.main {{
|
34 |
-
background: linear-gradient(135deg, {colors['bg']} 0%, #16213e 100%) !important;
|
35 |
-
color: white !important;
|
36 |
-
}}
|
37 |
-
.stTextInput input, .stSelectbox select, .stTextArea textarea {{
|
38 |
-
color: {colors['primary']} !important;
|
39 |
-
background: rgba(0,0,0,0.3) !important;
|
40 |
-
border-radius: 10px !important;
|
41 |
-
}}
|
42 |
-
.stButton>button {{
|
43 |
-
background: rgba(79,255,176,0.2) !important;
|
44 |
-
border: 2px solid {colors['primary']} !important;
|
45 |
-
color: {colors['primary']} !important;
|
46 |
-
border-radius: 10px !important;
|
47 |
-
transition: all 0.3s !important;
|
48 |
-
}}
|
49 |
-
.stButton>button:hover {{
|
50 |
-
background: rgba(79,255,176,0.4) !important;
|
51 |
-
transform: scale(1.05);
|
52 |
-
}}
|
53 |
-
.code-block {{
|
54 |
-
border: 2px solid {colors['secondary']};
|
55 |
-
border-radius: 15px;
|
56 |
-
padding: 1rem;
|
57 |
-
background: rgba(0,0,0,0.5);
|
58 |
-
margin-bottom: 1rem;
|
59 |
-
}}
|
60 |
-
.sidebar .sidebar-content {{
|
61 |
-
background: rgba(0,0,0,0.3) !important;
|
62 |
-
}}
|
63 |
-
h1, h2, h3 {{
|
64 |
-
color: {colors['primary']} !important;
|
65 |
-
}}
|
66 |
-
.st-expander {{
|
67 |
-
background: rgba(0,0,0,0.3) !important;
|
68 |
-
border: 1px solid {colors['secondary']} !important;
|
69 |
-
}}
|
70 |
-
</style>
|
71 |
-
""", unsafe_allow_html=True)
|
72 |
-
|
73 |
-
# Initialize Groq Client
|
74 |
-
def get_groq_client():
|
75 |
-
api_key = os.getenv("GROQ_API_KEY")
|
76 |
-
if not api_key:
|
77 |
-
st.error("GROQ_API_KEY not found! Please set it in environment variables or secrets.")
|
78 |
-
return None
|
79 |
-
return Groq(api_key=api_key)
|
80 |
-
|
81 |
-
# Code Generation Functions
|
82 |
-
def generate_code(query, language, model, client, complexity="Medium"):
|
83 |
-
complexity_levels = {
|
84 |
-
"Basic": "Generate simple code for",
|
85 |
-
"Medium": "Generate well-structured code with comments for",
|
86 |
-
"Advanced": "Generate production-ready code with error handling, tests, and documentation for"
|
87 |
-
}
|
88 |
-
|
89 |
-
try:
|
90 |
-
completion = client.chat.completions.create(
|
91 |
-
model=model,
|
92 |
-
messages=[{
|
93 |
-
"role": "user",
|
94 |
-
"content": f"""
|
95 |
-
{complexity_levels[complexity]} {query} in {language}.
|
96 |
-
Include comments in the code itself.
|
97 |
-
IMPORTANT: Return ONLY the raw executable code with comments,
|
98 |
-
without any additional explanation before or after the code block.
|
99 |
-
"""
|
100 |
-
}],
|
101 |
-
temperature=0.7 if complexity == "Advanced" else 0.5,
|
102 |
-
max_tokens=4096,
|
103 |
-
top_p=0.95
|
104 |
-
)
|
105 |
-
# Extract just the code block if it's wrapped in markdown
|
106 |
-
raw_content = completion.choices[0].message.content
|
107 |
-
if '```' in raw_content:
|
108 |
-
# Extract content between the first ``` and last ```
|
109 |
-
code = raw_content.split('```')[1]
|
110 |
-
if code.startswith(language.lower()):
|
111 |
-
code = code[len(language.lower()):]
|
112 |
-
return code.strip()
|
113 |
-
return raw_content.strip()
|
114 |
-
except Exception as e:
|
115 |
-
st.error(f"Error generating code: {str(e)}")
|
116 |
-
return None
|
117 |
-
|
118 |
-
# Main App Interface
|
119 |
-
def main():
|
120 |
-
client = get_groq_client()
|
121 |
-
if not client:
|
122 |
-
return
|
123 |
-
|
124 |
-
# Sidebar for settings
|
125 |
-
with st.sidebar:
|
126 |
-
st.title("⚙️ Settings")
|
127 |
-
selected_theme = st.selectbox("Theme", THEMES, index=0)
|
128 |
-
selected_language = st.selectbox("Programming Language", LANGUAGES, index=0)
|
129 |
-
complexity = st.select_slider("Code Complexity", ["Basic", "Medium", "Advanced"], value="Medium")
|
130 |
-
tags = st_tags(label="Add Keywords:", text="Press enter to add more", value=[], key="tags")
|
131 |
-
|
132 |
-
inject_custom_css(selected_theme)
|
133 |
-
|
134 |
-
# Main content
|
135 |
-
st.title("AstraCode")
|
136 |
-
st.caption("Generate production-ready code in multiple languages with a single click")
|
137 |
-
|
138 |
-
col1, col2 = st.columns([4, 1])
|
139 |
-
with col1:
|
140 |
-
query = st.text_area("Describe your coding requirement:", height=100,
|
141 |
-
placeholder="e.g., A function to calculate Fibonacci sequence")
|
142 |
-
|
143 |
-
if 'generated_code' not in st.session_state:
|
144 |
-
st.session_state.generated_code = None
|
145 |
-
if 'alternative_code' not in st.session_state:
|
146 |
-
st.session_state.alternative_code = None
|
147 |
-
|
148 |
-
action_cols = st.columns([1, 1, 1, 1, 2])
|
149 |
-
with action_cols[0]:
|
150 |
-
if st.button("🚀 Generate Code", use_container_width=True):
|
151 |
-
with st.spinner(f"Generating {selected_language} code..."):
|
152 |
-
code = generate_code(query, selected_language, PRIMARY_MODEL, client, complexity)
|
153 |
-
if not code:
|
154 |
-
st.warning("Trying backup model...")
|
155 |
-
code = generate_code(query, selected_language, BACKUP_MODEL, client, complexity)
|
156 |
-
|
157 |
-
if code:
|
158 |
-
st.session_state.generated_code = code
|
159 |
-
st.session_state.alternative_code = None
|
160 |
-
else:
|
161 |
-
st.error("Failed to generate code. Please try again.")
|
162 |
-
|
163 |
-
with action_cols[1]:
|
164 |
-
if st.button("🔄 Alternative", use_container_width=True, disabled=not st.session_state.generated_code):
|
165 |
-
with st.spinner(f"Generating alternative {selected_language} solution..."):
|
166 |
-
alt_code = generate_code(f"Alternative approach for: {query}", selected_language, PRIMARY_MODEL, client, complexity)
|
167 |
-
if alt_code:
|
168 |
-
st.session_state.alternative_code = alt_code
|
169 |
-
|
170 |
-
with action_cols[2]:
|
171 |
-
if st.button("🧹 Clear", use_container_width=True):
|
172 |
-
st.session_state.generated_code = None
|
173 |
-
st.session_state.alternative_code = None
|
174 |
-
|
175 |
-
# Display results
|
176 |
-
if st.session_state.generated_code:
|
177 |
-
with st.container():
|
178 |
-
st.subheader(f"Generated {selected_language} Code")
|
179 |
-
st.code(st.session_state.generated_code, language=selected_language.lower())
|
180 |
-
|
181 |
-
# Explanation section
|
182 |
-
with st.expander("📝 Code Explanation", expanded=True):
|
183 |
-
if client:
|
184 |
-
try:
|
185 |
-
explanation = client.chat.completions.create(
|
186 |
-
model=PRIMARY_MODEL,
|
187 |
-
messages=[{
|
188 |
-
"role": "user",
|
189 |
-
"content": f"Explain this {selected_language} code in simple terms:\n\n{st.session_state.generated_code}"
|
190 |
-
}],
|
191 |
-
temperature=0.3,
|
192 |
-
max_tokens=1024
|
193 |
-
)
|
194 |
-
st.write(explanation.choices[0].message.content)
|
195 |
-
except Exception as e:
|
196 |
-
st.error(f"Error generating explanation: {str(e)}")
|
197 |
-
|
198 |
-
if st.session_state.alternative_code:
|
199 |
-
with st.container():
|
200 |
-
st.subheader(f"Alternative {selected_language} Solution")
|
201 |
-
st.code(st.session_state.alternative_code, language=selected_language.lower())
|
202 |
-
|
203 |
-
if __name__ == "__main__":
|
204 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|