Haseeb-001 commited on
Commit
9dc5d2f
Β·
verified Β·
1 Parent(s): 81da16f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -79
app.py CHANGED
@@ -4,13 +4,14 @@ import time
4
  import streamlit as st
5
  from groq import Groq
6
  from streamlit_lottie import st_lottie
 
7
  import json
8
  import base64
9
 
10
  # --- Constants ---
11
  PRIMARY_MODEL = "qwen-2.5-coder-32b"
12
  SUPPORTED_LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
13
- LOTTIE_ANIMATION = "https://assets3.lottiefiles.com/packages/lf20_hi95bvmx/ai.json"
14
 
15
  # --- Setup ---
16
  st.set_page_config(
@@ -22,28 +23,22 @@ st.set_page_config(
22
 
23
  # --- Lottie Animation Loader ---
24
  def load_lottie(url):
25
- with open("animation.json") as f:
26
- return json.load(f)
27
 
28
- # --- Custom CSS with Animations ---
29
  def inject_css():
30
  st.markdown(f"""
31
  <style>
32
- @keyframes glow {{
33
- 0% {{ box-shadow: 0 0 5px #4fffb0; }}
34
- 50% {{ box-shadow: 0 0 20px #4fffb0; }}
35
- 100% {{ box-shadow: 0 0 5px #4fffb0; }}
36
- }}
37
-
38
  .main {{
39
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
40
  color: #ffffff;
41
  }}
42
 
43
  .stTextInput input {{
44
- animation: glow 2s infinite;
45
  background: rgba(0,0,0,0.3) !important;
46
  border: 2px solid #4fffb0 !important;
 
47
  }}
48
 
49
  .code-editor {{
@@ -51,7 +46,6 @@ def inject_css():
51
  border-radius: 15px;
52
  padding: 1rem;
53
  background: rgba(0,0,0,0.5) !important;
54
- animation: glow 2s infinite;
55
  }}
56
 
57
  .download-btn {{
@@ -60,12 +54,6 @@ def inject_css():
60
  border: none !important;
61
  border-radius: 25px;
62
  padding: 12px 24px;
63
- font-weight: bold;
64
- transition: transform 0.3s !important;
65
- }}
66
-
67
- .download-btn:hover {{
68
- transform: scale(1.05);
69
  }}
70
  </style>
71
  """, unsafe_allow_html=True)
@@ -83,30 +71,21 @@ def init_groq():
83
  # --- Code Generation Core ---
84
  def generate_code(client, query, lang):
85
  try:
86
- system_prompt = f"""
87
- You are a {lang} expert. Generate:
88
- 1. Clean, production-ready code
89
- 2. With error handling
90
- 3. Proper documentation
91
- 4. Optimized for performance
92
- """
93
-
94
  response = client.chat.completions.create(
95
  model=PRIMARY_MODEL,
96
- messages=[
97
- {"role": "system", "content": system_prompt},
98
- {"role": "user", "content": query}
99
- ],
100
  temperature=0.7,
101
- max_tokens=4096,
102
- top_p=0.95
103
  )
104
  return response.choices[0].message.content
105
  except Exception as e:
106
- st.error(f"🚨 Generation Error: {str(e)}")
107
  return None
108
 
109
- # --- UI Components ---
110
  def main():
111
  inject_css()
112
  client = init_groq()
@@ -114,59 +93,32 @@ def main():
114
  # --- Hero Section ---
115
  col1, col2 = st.columns([1, 2])
116
  with col1:
117
- st_lottie(load_lottie(LOTTIE_ANIMATION), height=200)
 
 
118
  with col2:
119
  st.markdown("# CodeForge AI")
120
  st.markdown("### _Transform Ideas into Production Code_ ⚑")
121
 
122
  # --- Language Selector ---
123
- lang = st.sidebar.selectbox("πŸ’» Programming Language", SUPPORTED_LANGUAGES, index=0)
124
- user_query = st.text_input("🎯 Describe your coding requirement:", placeholder="e.g., 'Create a REST API with JWT authentication'")
125
 
126
- # --- Main Generation Flow ---
127
  if st.button("πŸš€ Generate Code", use_container_width=True):
128
- if not user_query:
129
- st.warning("⚠️ Please enter a coding requirement!")
130
- return
131
-
132
- with st.spinner(f"🧠 Crafting {lang} magic..."):
133
- start_time = time.time()
134
- code = generate_code(client, user_query, lang)
135
- gen_time = time.time() - start_time
136
-
137
- if code:
138
- st.session_state.code = code
139
- st.balloons()
140
-
141
- # --- Code Display ---
142
- with st.expander(f"✨ Generated {lang} Code", expanded=True):
143
- st.code(code, language="python" if lang == "Python" else "javascript")
144
 
145
- # --- Download Button ---
146
- ext = {"Python": "py", "JavaScript": "js", "Java": "java",
147
- "C++": "cpp", "Go": "go", "Rust": "rs"}[lang]
148
- st.markdown(get_download_link(code, f"generated_code.{ext}"),
149
- unsafe_allow_html=True)
150
-
151
- # --- Performance Metrics ---
152
- st.sidebar.markdown(f"""
153
- ### πŸ“Š Generation Metrics
154
- ⏱️ **Time**: {gen_time:.2f}s
155
- πŸ“ **Tokens**: {len(code.split())}
156
- 🧠 **Model**: {PRIMARY_MODEL}
157
- """)
158
-
159
- # --- Explanation Section ---
160
- if st.button("πŸ“– Explain Implementation"):
161
- with st.spinner("πŸ” Analyzing code structure..."):
162
- explanation = client.chat.completions.create(
163
- model=PRIMARY_MODEL,
164
- messages=[{
165
- "role": "user",
166
- "content": f"Explain this {lang} code:\n\n{code}"
167
- }]
168
- )
169
- st.markdown(f"## Code Breakdown\n{explanation.choices[0].message.content}")
170
 
171
  if __name__ == "__main__":
172
  main()
 
4
  import streamlit as st
5
  from groq import Groq
6
  from streamlit_lottie import st_lottie
7
+ import requests
8
  import json
9
  import base64
10
 
11
  # --- Constants ---
12
  PRIMARY_MODEL = "qwen-2.5-coder-32b"
13
  SUPPORTED_LANGUAGES = ["Python", "JavaScript", "Java", "C++", "Go", "Rust"]
14
+ LOTTIE_URL = "https://assets3.lottiefiles.com/packages/lf20_hi95bvmx/ai.json"
15
 
16
  # --- Setup ---
17
  st.set_page_config(
 
23
 
24
  # --- Lottie Animation Loader ---
25
  def load_lottie(url):
26
+ response = requests.get(url)
27
+ return response.json() if response.status_code == 200 else None
28
 
29
+ # --- Custom CSS ---
30
  def inject_css():
31
  st.markdown(f"""
32
  <style>
 
 
 
 
 
 
33
  .main {{
34
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
35
  color: #ffffff;
36
  }}
37
 
38
  .stTextInput input {{
 
39
  background: rgba(0,0,0,0.3) !important;
40
  border: 2px solid #4fffb0 !important;
41
+ color: #4fffb0 !important;
42
  }}
43
 
44
  .code-editor {{
 
46
  border-radius: 15px;
47
  padding: 1rem;
48
  background: rgba(0,0,0,0.5) !important;
 
49
  }}
50
 
51
  .download-btn {{
 
54
  border: none !important;
55
  border-radius: 25px;
56
  padding: 12px 24px;
 
 
 
 
 
 
57
  }}
58
  </style>
59
  """, unsafe_allow_html=True)
 
71
  # --- Code Generation Core ---
72
  def generate_code(client, query, lang):
73
  try:
 
 
 
 
 
 
 
 
74
  response = client.chat.completions.create(
75
  model=PRIMARY_MODEL,
76
+ messages=[{
77
+ "role": "user",
78
+ "content": f"Generate {lang} code for: {query}. Include comments and error handling."
79
+ }],
80
  temperature=0.7,
81
+ max_tokens=4096
 
82
  )
83
  return response.choices[0].message.content
84
  except Exception as e:
85
+ st.error(f"Error: {str(e)}")
86
  return None
87
 
88
+ # --- Main UI ---
89
  def main():
90
  inject_css()
91
  client = init_groq()
 
93
  # --- Hero Section ---
94
  col1, col2 = st.columns([1, 2])
95
  with col1:
96
+ lottie_anim = load_lottie(LOTTIE_URL)
97
+ if lottie_anim:
98
+ st_lottie(lottie_anim, height=150)
99
  with col2:
100
  st.markdown("# CodeForge AI")
101
  st.markdown("### _Transform Ideas into Production Code_ ⚑")
102
 
103
  # --- Language Selector ---
104
+ lang = st.sidebar.selectbox("πŸ’» Language", SUPPORTED_LANGUAGES)
105
+ user_query = st.text_input("🎯 Describe your requirement:", placeholder="e.g., 'Create a REST API with JWT authentication'")
106
 
107
+ # --- Generation Flow ---
108
  if st.button("πŸš€ Generate Code", use_container_width=True):
109
+ if user_query:
110
+ with st.spinner(f"🧠 Crafting {lang} code..."):
111
+ code = generate_code(client, user_query, lang)
112
+ if code:
113
+ st.session_state.code = code
114
+ st.balloons()
 
 
 
 
 
 
 
 
 
 
115
 
116
+ with st.expander(f"✨ Generated Code", expanded=True):
117
+ st.code(code)
118
+ ext = {"Python": "py", "JavaScript": "js"}.get(lang, "txt")
119
+ st.markdown(get_download_link(code, f"code.{ext}"), unsafe_allow_html=True)
120
+ else:
121
+ st.warning("Please enter a requirement!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  if __name__ == "__main__":
124
  main()