Haseeb-001 commited on
Commit
53b0f27
Β·
verified Β·
1 Parent(s): d24e90d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -53
app.py CHANGED
@@ -1,67 +1,93 @@
 
1
  import os
2
  import streamlit as st
3
  from groq import Groq
4
 
5
- api = "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" # Replace with your actual API key
 
 
6
 
7
- # --- Groq API Setup ---
8
- client = Groq(api_key=api)
 
 
 
 
 
9
 
10
- # Function to get code from StarCoder model
11
- def generate_code(summary, language):
12
- try:
13
- chat_completion = client.chat.completions.create(
14
- messages=[
15
- {
16
- "role": "user",
17
- "content": f"Generate efficient {language} code for the following task without any explanations or comments: {summary}",
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
- # Page Title
33
- st.title("Generative AI Code Generator Using StarCoder")
34
 
35
- # Summary Input
36
- summary = st.text_area("Enter the Summary of the Task", "For example: Create a function to add two numbers.")
37
- language = st.selectbox("Select the Programming Language", ["Python", "Java", "JavaScript", "C++"])
 
 
 
 
 
38
 
39
- # Generate Code Button
40
- if st.button("Generate Code"):
41
- if summary:
42
- # Generate the code using Groq and StarCoder
43
- generated_code = generate_code(summary, language)
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- if generated_code:
46
- st.subheader(f"Generated {language} Code:")
47
- st.code(generated_code, language=language.lower())
 
 
48
 
49
- # Modify or remove sections of code (optional)
50
- modified_code = st.text_area("Modify the Code (Optional)", value=generated_code, height=200)
 
 
 
 
 
 
 
 
 
51
 
52
- # Download Code Button (Download as TXT)
53
- st.download_button(
54
- label="Download Code",
55
- data=modified_code or generated_code, # Use modified_code if edited, otherwise generated_code
56
- file_name="generated_code.txt",
57
- mime="text/plain"
58
- )
 
 
 
 
59
 
60
- # New Code Button
61
- if st.button("Generate New Code"):
62
- summary = "" # Clear the summary input
63
- st.rerun() # Refresh the page to clear inputs
64
 
65
- # Footer Information
66
- st.markdown("---")
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()