Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,93 @@
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from groq import Groq
|
4 |
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
model="starcoder", # Specify the model you want to use
|
21 |
-
stream=False,
|
22 |
-
)
|
23 |
-
generated_code = chat_completion.choices[0].message.content
|
24 |
-
return generated_code
|
25 |
-
except Exception as e:
|
26 |
-
st.error(f"Error generating code: {e}")
|
27 |
-
return ""
|
28 |
-
|
29 |
-
# --- Streamlit Interface ---
|
30 |
-
st.set_page_config(page_title="Generative AI Code Generator", page_icon="π¨βπ»", layout="wide")
|
31 |
|
32 |
-
|
33 |
-
st.title("Generative AI Code Generator Using StarCoder")
|
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 |
-
st.rerun() # Refresh the page to clear inputs
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
st.write("π» Powered by Streamlit | AI Code Generation by Groq and StarCoder")
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
import streamlit as st
|
4 |
from groq import Groq
|
5 |
|
6 |
+
# Configuration
|
7 |
+
PRIMARY_MODEL = "qwen-2.5-coder-32b"
|
8 |
+
BACKUP_MODEL = "llama3-70b-8192"
|
9 |
|
10 |
+
# Streamlit UI Config
|
11 |
+
st.set_page_config(
|
12 |
+
page_title="AI Code Generator",
|
13 |
+
page_icon="π",
|
14 |
+
layout="wide",
|
15 |
+
initial_sidebar_state="expanded"
|
16 |
+
)
|
17 |
|
18 |
+
# Custom CSS for Neon Theme
|
19 |
+
def inject_custom_css():
|
20 |
+
st.markdown("""
|
21 |
+
<style>
|
22 |
+
.main { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); }
|
23 |
+
.stTextInput input { color: #4fffb0 !important; background: rgba(0,0,0,0.3) !important; }
|
24 |
+
.stButton>button { background: rgba(79,255,176,0.2) !important; border: 2px solid #4fffb0 !important; }
|
25 |
+
.code-editor { border: 2px solid #4fffb0; border-radius: 10px; padding: 1rem; background: rgba(0,0,0,0.5); }
|
26 |
+
</style>
|
27 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
inject_custom_css()
|
|
|
30 |
|
31 |
+
# Initialize Groq Client
|
32 |
+
def get_groq_client():
|
33 |
+
GROQ_API_KEY = "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"
|
34 |
+
api_key = GROQ_API_KEY
|
35 |
+
if not api_key:
|
36 |
+
st.error("GROQ_API_KEY not found! Please set it in environment variables or secrets.")
|
37 |
+
return None
|
38 |
+
return Groq(api_key=api_key)
|
39 |
|
40 |
+
# Code Generation Functions
|
41 |
+
def generate_code(query, model, client):
|
42 |
+
try:
|
43 |
+
completion = client.chat.completions.create(
|
44 |
+
model=model,
|
45 |
+
messages=[{
|
46 |
+
"role": "user",
|
47 |
+
"content": f"Generate Python code for: {query}. Include comments and error handling."
|
48 |
+
}],
|
49 |
+
temperature=0.6,
|
50 |
+
max_tokens=4096,
|
51 |
+
top_p=0.95
|
52 |
+
)
|
53 |
+
return completion.choices[0].message.content
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Error generating code: {str(e)}")
|
56 |
+
return None
|
57 |
|
58 |
+
# Main App Interface
|
59 |
+
def main():
|
60 |
+
client = get_groq_client()
|
61 |
+
if not client:
|
62 |
+
return
|
63 |
|
64 |
+
st.title("AI Code Generator π")
|
65 |
+
|
66 |
+
col1, col2 = st.columns([3, 1])
|
67 |
+
with col1:
|
68 |
+
query = st.text_input("Describe your coding requirement:")
|
69 |
+
|
70 |
+
if 'generated_code' not in st.session_state:
|
71 |
+
st.session_state.generated_code = None
|
72 |
+
|
73 |
+
if col2.button("π New Code"):
|
74 |
+
st.session_state.generated_code = None
|
75 |
|
76 |
+
if query and not st.session_state.generated_code:
|
77 |
+
with st.spinner("Generating code..."):
|
78 |
+
code = generate_code(query, PRIMARY_MODEL, client)
|
79 |
+
if not code:
|
80 |
+
st.warning("Trying backup model...")
|
81 |
+
code = generate_code(query, BACKUP_MODEL, client)
|
82 |
+
|
83 |
+
if code:
|
84 |
+
st.session_state.generated_code = code
|
85 |
+
else:
|
86 |
+
st.error("Failed to generate code. Please try again.")
|
87 |
|
88 |
+
if st.session_state.generated_code:
|
89 |
+
with st.expander("Generated Code", expanded=True):
|
90 |
+
st.markdown(f"```python\n{st.session_state.generated_code}\n```")
|
|
|
91 |
|
92 |
+
if __name__ == "__main__":
|
93 |
+
main()
|
|