anasfsd123 commited on
Commit
045544e
Β·
verified Β·
1 Parent(s): 2df9e53

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from groq import Groq
3
+ from streamlit_ace import st_ace
4
+ from datetime import datetime
5
+ import time
6
+
7
+ # --- Constants ---
8
+ THEMES = ["monokai", "github", "twilight"]
9
+ LANGUAGES = ["python", "javascript", "java", "c", "cpp"]
10
+
11
+ # --- Secure Groq Client Setup ---
12
+ client = Groq(api_key=st.secrets["groq_api_key"])
13
+
14
+ # --- Page Config ---
15
+ st.set_page_config(
16
+ page_title="πŸ” CodeMedic AI",
17
+ page_icon="πŸ€–",
18
+ layout="centered",
19
+ initial_sidebar_state="expanded"
20
+ )
21
+
22
+ # --- Session State ---
23
+ if 'history' not in st.session_state:
24
+ st.session_state.history = []
25
+ if 'processing' not in st.session_state:
26
+ st.session_state.processing = False
27
+
28
+ # --- UI ---
29
+ def gradient_text(text):
30
+ return f"""
31
+ <h1 style="
32
+ background: linear-gradient(45deg, #6EE7B7, #3B82F6);
33
+ -webkit-background-clip: text;
34
+ background-clip: text;
35
+ color: transparent;
36
+ text-align: center;
37
+ ">{text}</h1>
38
+ """
39
+
40
+ with st.sidebar:
41
+ st.markdown(gradient_text("CodeMedic AI"), unsafe_allow_html=True)
42
+ st.markdown("---")
43
+ with st.expander("βš™οΈ Settings"):
44
+ selected_theme = st.selectbox("Editor Theme", THEMES, index=0)
45
+ selected_lang = st.selectbox("Code Language", LANGUAGES, index=0)
46
+ model_temp = st.slider("🧠 AI Creativity", 0.0, 1.0, 0.7)
47
+ st.markdown("---")
48
+ st.button("🧹 Clear History", on_click=lambda: st.session_state.history.clear())
49
+ st.markdown(f"<small>Session Start: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</small>", unsafe_allow_html=True)
50
+
51
+ st.markdown(gradient_text("Code Diagnostics Suite"), unsafe_allow_html=True)
52
+ st.markdown("<div style='text-align: center; margin-bottom: 30px;'>AI-powered code analysis with deep error detection and fix generation</div>", unsafe_allow_html=True)
53
+
54
+ with st.container(border=True):
55
+ code = st_ace(
56
+ placeholder=f"Enter {selected_lang} code...",
57
+ language=selected_lang,
58
+ theme=selected_theme,
59
+ height=400,
60
+ key=f"ace_{selected_lang}",
61
+ wrap=True
62
+ )
63
+
64
+ col1, col2 = st.columns([1, 3])
65
+ with col1:
66
+ analyze_btn = st.button("πŸš€ Analyze Code", use_container_width=True)
67
+ with col2:
68
+ if st.session_state.processing:
69
+ st.warning("AI is analyzing code...")
70
+
71
+ if analyze_btn and not st.session_state.processing:
72
+ if not code.strip():
73
+ st.error("⚠️ Please enter valid code")
74
+ else:
75
+ st.session_state.processing = True
76
+ try:
77
+ with st.spinner("πŸ” Deep code analysis in progress..."):
78
+ start_time = time.time()
79
+ response = client.chat.completions.create(
80
+ messages=[
81
+ {
82
+ "role": "system",
83
+ "content": (
84
+ "You are an expert static code analyzer. Your only job is to analyze code for:\n"
85
+ "1. Syntax validation\n2. Logical error detection\n"
86
+ "3. Security vulnerabilities\n4. Optimization suggestions\n\n"
87
+ "IMPORTANT:\n"
88
+ "- Only respond with code-related analysis.\n"
89
+ "- Do NOT respond to general questions.\n"
90
+ "- If the user input is not code or code-related, reply with:\n"
91
+ " '⚠️ Please input code to analyze. I only respond to code-related requests.'"
92
+ )
93
+ },
94
+ {"role": "user", "content": code}
95
+ ],
96
+ model="llama3-70b-8192",
97
+ temperature=model_temp
98
+ )
99
+ analysis = response.choices[0].message.content
100
+ duration = time.time() - start_time
101
+ st.session_state.history.append({
102
+ "timestamp": datetime.now().isoformat(),
103
+ "code": code,
104
+ "analysis": analysis,
105
+ "language": selected_lang,
106
+ "duration": f"{duration:.2f}s"
107
+ })
108
+
109
+ st.success("βœ… Analysis Complete")
110
+ st.markdown("### πŸ“ Analysis Report")
111
+ st.markdown(analysis)
112
+ except Exception as e:
113
+ st.error(f"❌ Analysis Failed: {str(e)}")
114
+ finally:
115
+ st.session_state.processing = False
116
+
117
+ if st.session_state.history:
118
+ st.markdown("## πŸ“œ Analysis History")
119
+ for entry in reversed(st.session_state.history):
120
+ with st.container(border=True):
121
+ cols = st.columns([1, 3, 1])
122
+ cols[0].markdown(f"**Language**: {entry['language']}")
123
+ cols[1].markdown(f"**Time**: {entry['timestamp']}")
124
+ cols[2].markdown(f"**Duration**: {entry['duration']}")
125
+ st.markdown("**Code:**")
126
+ st.code(entry['code'], language=entry['language'])
127
+ st.markdown("**Analysis:**")
128
+ st.markdown(entry['analysis'])
129
+
130
+ st.markdown("---")
131
+ st.markdown("""
132
+ <div style="text-align: center; color: #6B7280;">
133
+ <div style="display: flex; justify-content: center; gap: 15px; margin-bottom: 10px;">
134
+ <a href="https://groq.com" style="color: #3B82F6; text-decoration: none;">Powered by Groq</a>
135
+ <span>β€’</span>
136
+ <a href="#" style="color: #3B82F6; text-decoration: none;">Documentation</a>
137
+ <span>β€’</span>
138
+ <a href="#" style="color: #3B82F6; text-decoration: none;">Feedback</a>
139
+ </div>
140
+ <div>Made with ❀️ by AI Code Experts</div>
141
+ </div>
142
+ """, unsafe_allow_html=True)