Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +91 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Neon Theme
|
18 |
+
def inject_custom_css():
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
.main { background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%); }
|
22 |
+
.stTextInput input { color: #4fffb0 !important; background: rgba(0,0,0,0.3) !important; }
|
23 |
+
.stButton>button { background: rgba(79,255,176,0.2) !important; border: 2px solid #4fffb0 !important; }
|
24 |
+
.code-editor { border: 2px solid #4fffb0; border-radius: 10px; padding: 1rem; background: rgba(0,0,0,0.5); }
|
25 |
+
</style>
|
26 |
+
""", unsafe_allow_html=True)
|
27 |
+
|
28 |
+
inject_custom_css()
|
29 |
+
|
30 |
+
# Initialize Groq Client
|
31 |
+
def get_groq_client():
|
32 |
+
api_key = os.getenv("GROQ_API_KEY")
|
33 |
+
if not api_key:
|
34 |
+
st.error("GROQ_API_KEY not found! Please set it in environment variables or secrets.")
|
35 |
+
return None
|
36 |
+
return Groq(api_key=api_key)
|
37 |
+
|
38 |
+
# Code Generation Functions
|
39 |
+
def generate_code(query, model, client):
|
40 |
+
try:
|
41 |
+
completion = client.chat.completions.create(
|
42 |
+
model=model,
|
43 |
+
messages=[{
|
44 |
+
"role": "user",
|
45 |
+
"content": f"Generate Python code for: {query}. Include comments and error handling."
|
46 |
+
}],
|
47 |
+
temperature=0.6,
|
48 |
+
max_tokens=4096,
|
49 |
+
top_p=0.95
|
50 |
+
)
|
51 |
+
return completion.choices[0].message.content
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"Error generating code: {str(e)}")
|
54 |
+
return None
|
55 |
+
|
56 |
+
# Main App Interface
|
57 |
+
def main():
|
58 |
+
client = get_groq_client()
|
59 |
+
if not client:
|
60 |
+
return
|
61 |
+
|
62 |
+
st.title("AI Code Generator π")
|
63 |
+
|
64 |
+
col1, col2 = st.columns([3, 1])
|
65 |
+
with col1:
|
66 |
+
query = st.text_input("Describe your coding requirement:")
|
67 |
+
|
68 |
+
if 'generated_code' not in st.session_state:
|
69 |
+
st.session_state.generated_code = None
|
70 |
+
|
71 |
+
if col2.button("π New Code"):
|
72 |
+
st.session_state.generated_code = None
|
73 |
+
|
74 |
+
if query and not st.session_state.generated_code:
|
75 |
+
with st.spinner("Generating code..."):
|
76 |
+
code = generate_code(query, PRIMARY_MODEL, client)
|
77 |
+
if not code:
|
78 |
+
st.warning("Trying backup model...")
|
79 |
+
code = generate_code(query, BACKUP_MODEL, client)
|
80 |
+
|
81 |
+
if code:
|
82 |
+
st.session_state.generated_code = code
|
83 |
+
else:
|
84 |
+
st.error("Failed to generate code. Please try again.")
|
85 |
+
|
86 |
+
if st.session_state.generated_code:
|
87 |
+
with st.expander("Generated Code", expanded=True):
|
88 |
+
st.markdown(f"```python\n{st.session_state.generated_code}\n```")
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
groq
|
3 |
+
torch
|
4 |
+
accelerate
|
5 |
+
python-dotenv
|