Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,75 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
|
|
4 |
|
5 |
# Configuration
|
6 |
PRIMARY_MODEL = "qwen-2.5-coder-32b"
|
7 |
BACKUP_MODEL = "llama3-70b-8192"
|
|
|
|
|
8 |
|
9 |
# Streamlit UI Config
|
10 |
st.set_page_config(
|
11 |
-
page_title="AI Code Generator",
|
12 |
-
page_icon="
|
13 |
layout="wide",
|
14 |
initial_sidebar_state="expanded"
|
15 |
)
|
16 |
|
17 |
-
# Custom CSS for
|
18 |
-
def inject_custom_css():
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
<style>
|
21 |
-
.main {
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
</style>
|
27 |
""", unsafe_allow_html=True)
|
28 |
|
29 |
-
inject_custom_css()
|
30 |
-
|
31 |
# Initialize Groq Client
|
32 |
def get_groq_client():
|
33 |
api_key = os.getenv("GROQ_API_KEY")
|
@@ -37,15 +79,21 @@ def get_groq_client():
|
|
37 |
return Groq(api_key=api_key)
|
38 |
|
39 |
# Code Generation Functions
|
40 |
-
def generate_code(query, language, model, client):
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
try:
|
42 |
completion = client.chat.completions.create(
|
43 |
model=model,
|
44 |
messages=[{
|
45 |
"role": "user",
|
46 |
-
"content": f"
|
47 |
}],
|
48 |
-
temperature=0.
|
49 |
max_tokens=4096,
|
50 |
top_p=0.95
|
51 |
)
|
@@ -60,45 +108,86 @@ def main():
|
|
60 |
if not client:
|
61 |
return
|
62 |
|
63 |
-
|
64 |
-
st.
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
with col1:
|
69 |
-
query = st.
|
70 |
-
|
71 |
|
72 |
if 'generated_code' not in st.session_state:
|
73 |
st.session_state.generated_code = None
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
if st.session_state.generated_code:
|
91 |
-
with st.
|
92 |
-
st.
|
93 |
-
|
94 |
-
|
95 |
-
st.write("This block will contain the explanation of the generated code.")
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
-
main()
|
|
|
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="β¨ AI Code Generator Pro",
|
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")
|
|
|
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"{complexity_levels[complexity]} {query} in {language}. Include comments."
|
95 |
}],
|
96 |
+
temperature=0.7 if complexity == "Advanced" else 0.5,
|
97 |
max_tokens=4096,
|
98 |
top_p=0.95
|
99 |
)
|
|
|
108 |
if not client:
|
109 |
return
|
110 |
|
111 |
+
# Sidebar for settings
|
112 |
+
with st.sidebar:
|
113 |
+
st.title("βοΈ Settings")
|
114 |
+
selected_theme = st.selectbox("Theme", THEMES, index=0)
|
115 |
+
selected_language = st.selectbox("Programming Language", LANGUAGES, index=0)
|
116 |
+
complexity = st.select_slider("Code Complexity", ["Basic", "Medium", "Advanced"], value="Medium")
|
117 |
+
tags = st_tags(label="Add Keywords:", text="Press enter to add more", value=[], key="tags")
|
118 |
+
|
119 |
+
inject_custom_css(selected_theme)
|
120 |
+
|
121 |
+
# Main content
|
122 |
+
st.title("β¨ AI Code Generator Pro")
|
123 |
+
st.caption("Generate production-ready code in multiple languages with a single click")
|
124 |
+
|
125 |
+
col1, col2 = st.columns([4, 1])
|
126 |
with col1:
|
127 |
+
query = st.text_area("Describe your coding requirement:", height=100,
|
128 |
+
placeholder="e.g., A function to calculate Fibonacci sequence")
|
129 |
|
130 |
if 'generated_code' not in st.session_state:
|
131 |
st.session_state.generated_code = None
|
132 |
+
if 'alternative_code' not in st.session_state:
|
133 |
+
st.session_state.alternative_code = None
|
134 |
+
|
135 |
+
action_cols = st.columns([1, 1, 1, 1, 2])
|
136 |
+
with action_cols[0]:
|
137 |
+
if st.button("π Generate Code", use_container_width=True):
|
138 |
+
with st.spinner(f"Generating {selected_language} code..."):
|
139 |
+
code = generate_code(query, selected_language, PRIMARY_MODEL, client, complexity)
|
140 |
+
if not code:
|
141 |
+
st.warning("Trying backup model...")
|
142 |
+
code = generate_code(query, selected_language, BACKUP_MODEL, client, complexity)
|
143 |
+
|
144 |
+
if code:
|
145 |
+
st.session_state.generated_code = code
|
146 |
+
st.session_state.alternative_code = None
|
147 |
+
else:
|
148 |
+
st.error("Failed to generate code. Please try again.")
|
149 |
+
|
150 |
+
with action_cols[1]:
|
151 |
+
if st.button("π Alternative", use_container_width=True, disabled=not st.session_state.generated_code):
|
152 |
+
with st.spinner(f"Generating alternative {selected_language} solution..."):
|
153 |
+
alt_code = generate_code(f"Alternative approach for: {query}", selected_language, PRIMARY_MODEL, client, complexity)
|
154 |
+
if alt_code:
|
155 |
+
st.session_state.alternative_code = alt_code
|
156 |
+
|
157 |
+
with action_cols[2]:
|
158 |
+
if st.button("π§Ή Clear", use_container_width=True):
|
159 |
+
st.session_state.generated_code = None
|
160 |
+
st.session_state.alternative_code = None
|
161 |
+
|
162 |
+
# Display results
|
163 |
if st.session_state.generated_code:
|
164 |
+
with st.container():
|
165 |
+
st.subheader(f"Generated {selected_language} Code")
|
166 |
+
st.markdown(f"""<div class="code-block"><pre><code class="language-{selected_language.lower()}">{st.session_state.generated_code}</code></pre></div>""",
|
167 |
+
unsafe_allow_html=True)
|
|
|
168 |
|
169 |
+
# Explanation section
|
170 |
+
with st.expander("π Code Explanation", expanded=True):
|
171 |
+
if client:
|
172 |
+
try:
|
173 |
+
explanation = client.chat.completions.create(
|
174 |
+
model=PRIMARY_MODEL,
|
175 |
+
messages=[{
|
176 |
+
"role": "user",
|
177 |
+
"content": f"Explain this {selected_language} code in simple terms:\n\n{st.session_state.generated_code}"
|
178 |
+
}],
|
179 |
+
temperature=0.3,
|
180 |
+
max_tokens=1024
|
181 |
+
)
|
182 |
+
st.write(explanation.choices[0].message.content)
|
183 |
+
except Exception as e:
|
184 |
+
st.error(f"Error generating explanation: {str(e)}")
|
185 |
+
|
186 |
+
if st.session_state.alternative_code:
|
187 |
+
with st.container():
|
188 |
+
st.subheader(f"Alternative {selected_language} Solution")
|
189 |
+
st.markdown(f"""<div class="code-block"><pre><code class="language-{selected_language.lower()}">{st.session_state.alternative_code}</code></pre></div>""",
|
190 |
+
unsafe_allow_html=True)
|
191 |
|
192 |
if __name__ == "__main__":
|
193 |
+
main()
|