File size: 3,316 Bytes
dedc207
 
 
 
857eed7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b756fcc
dedc207
 
 
 
 
857eed7
 
 
460768e
857eed7
460768e
 
 
 
 
857eed7
460768e
 
 
 
 
 
dedc207
 
 
 
 
857eed7
 
dedc207
 
460768e
dedc207
 
 
 
 
 
460768e
dedc207
 
 
460768e
dedc207
 
 
 
857eed7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import streamlit as st 
import time
import google.generativeai as genai

# Animated text function with new color
def animated_text(text, speed=0.05):
    placeholder = st.empty()
    displayed_text = ""
    for letter in text:
        displayed_text += letter
        placeholder.markdown(f"""
            <h1 style="text-align:center; color: #007BFF;">✨ {displayed_text}</h1>
        """, unsafe_allow_html=True)
        time.sleep(speed)

# Display new animated text
animated_text("AI Code Doctor - Fix & Optimize!", speed=0.1)

# Configure Google Gemini API Key
genai.configure(api_key="AIzaSyBNuAnoR316s3mlaVY6zsgtmarKR4ZbajE")

# Custom Styling
st.markdown(
    """
    <style>
        body {background-color: #F0F8FF;}
        .title {text-align: center; color: #007BFF; font-size: 42px; font-weight: bold;}
        .subtitle {text-align: center; color: #333; font-size: 22px;}
        .container {
            background: white;
            padding: 20px; 
            border-radius: 12px; 
            box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.2);
        }
        .button {
            background-color: #007BFF;
            color: white; 
            font-size: 18px; 
            font-weight: bold; 
            border-radius: 10px; 
            padding: 10px;
        }
    </style>
    """,
    unsafe_allow_html=True,
)

# Streamlit App Title
st.title("πŸ€– AI-Powered Code Debugging & Optimization")

# Text Area for User Input (Buggy Code)
buggy_code = st.text_area("🐞 Paste Your Code Below:", height=200)

# Function to Debug Code Using Gemini AI
def debug_code_with_gemini(code):
    prompt = f"Debug the following code and provide the corrected version:\n\n{code}"
    model = genai.GenerativeModel("models/gemini-2.0-flash")
    response = model.generate_content(prompt)
    return response.text if response.text else "⚠️ AI Error: Unable to generate response."

# Function to Generate Code Improvement Suggestions
def get_suggestions_with_gemini(code):
    prompt = f"Suggest improvements for the following code, focusing on best practices, efficiency, and readability:\n\n{code}"
    model = genai.GenerativeModel("gemini-pro")
    response = model.generate_content(prompt)
    return response.text if response.text else "⚠️ No suggestions available."

# Analyze Button with new color scheme
if st.button("πŸš€ Fix & Improve Code", key="analyze_button"):
    if buggy_code:
        with st.spinner("πŸ› οΈ Debugging your code..."):
            fixed_code = debug_code_with_gemini(buggy_code)
        
        with st.spinner("πŸ” Generating suggestions..."):
            suggestions = get_suggestions_with_gemini(fixed_code)
        
        # Display Fixed Code
        st.subheader("βœ… Debugged Code:")
        st.code(fixed_code, language="python")
        
        # Copy Button for Fixed Code
        st.download_button(
            label="πŸ“₯ Download Fixed Code",
            data=fixed_code,
            file_name="fixed_code.py",
            mime="text/plain",
            help="Click to save the corrected code."
        )
        
        # Display AI-Generated Suggestions
        st.subheader("πŸ’‘ Code Improvement Suggestions:")
        st.write(suggestions)
    else:
        st.warning("⚠️ Please enter some code before generating fixes and suggestions!")